41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
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
|
|
}
|
|
|