This commit is contained in:
Josip Tišljar Mataušić
2026-05-20 16:02:46 +02:00
commit c5fb1169b0
5 changed files with 266 additions and 0 deletions
+40
View File
@@ -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
}