Code
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// wrapLines returns a slice with every element representing a new line
|
||||
func WrapLines(input string, maxLineLength int, breakupDelimiter string) []string {
|
||||
breakupDelimiterLength := len([]rune(breakupDelimiter))
|
||||
output := []string{}
|
||||
|
||||
previousExistingLineIndex := -1
|
||||
for existingLineIndex, existingLine := range strings.Split(input, "\n") {
|
||||
words := strings.Split(existingLine, breakupDelimiter)
|
||||
length := 0
|
||||
for _, word := range words {
|
||||
wordLength := len([]rune(word))
|
||||
length += breakupDelimiterLength + wordLength
|
||||
if length > maxLineLength || len(output) == 0 || previousExistingLineIndex != existingLineIndex {
|
||||
output = append(output, word)
|
||||
length = wordLength
|
||||
} else {
|
||||
output[len(output) - 1] += breakupDelimiter + word
|
||||
}
|
||||
previousExistingLineIndex = existingLineIndex
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func JoinGophermapLines(typeCharacter string, lines []string, lineEnding string) string {
|
||||
out := ""
|
||||
for index, line := range lines {
|
||||
out += typeCharacter + line + lineEnding
|
||||
if index < len(lines) - 1 { out += "\n" }
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user