Created
October 22, 2011 18:32
-
-
Save henrikh/1306333 to your computer and use it in GitHub Desktop.
Very simple line-breaker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
var text = "I am a text that needs to be formatted. Please get started, I really can't wait to have so much fun with this. I also have some text that is supposed to break everything, but that won't really happen, 'cus I'm a badass programmer." | |
const WIDTH = 50 | |
func main(){ | |
slicedText := strings.Split(text, " ") | |
widthLeft := WIDTH | |
text := "" | |
for i := range slicedText { | |
currentWord := slicedText[i] | |
if len(currentWord) + 1 >= widthLeft { | |
widthLeft = WIDTH | |
text += "\n" | |
} | |
text += slicedText[i] + " " | |
widthLeft -= len(currentWord) + 1 | |
} | |
fmt.Println(text) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment