aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/text.go
blob: 10b70b01f7cd5ff71545d813c07d50d02c351bc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package text

import (
	"bytes"
	"strings"
)

// Wrap a text for an exact line size
// Handle properly terminal color escape code
func Wrap(text string, lineWidth int) (string, int) {
	return WrapLeftPadded(text, lineWidth, 0)
}

// Wrap a text for an exact line size with a left padding
// Handle properly terminal color escape code
func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
	var textBuffer bytes.Buffer
	var lineBuffer bytes.Buffer
	nbLine := 1
	firstLine := true
	pad := strings.Repeat(" ", leftPad)

	// tabs are formatted as 4 spaces
	text = strings.Replace(text, "\t", "    ", 4)

	for _, line := range strings.Split(text, "\n") {
		spaceLeft := lineWidth - leftPad

		if !firstLine {
			textBuffer.WriteString("\n")
			nbLine++
		}

		firstWord := true

		for _, word := range strings.Split(line, " ") {
			wordLength := wordLen(word)

			if !firstWord {
				lineBuffer.WriteString(" ")
				spaceLeft -= 1

				if spaceLeft <= 0 {
					textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
					textBuffer.WriteString("\n")
					lineBuffer.Reset()
					spaceLeft = lineWidth - leftPad
					nbLine++
					firstLine = false
				}
			}

			// Word fit in the current line
			if spaceLeft >= wordLength {
				lineBuffer.WriteString(word)
				spaceLeft -= wordLength
				firstWord = false
			} else {
				// Break a word longer than a line
				if wordLength > lineWidth {
					for wordLength > 0 && len(word) > 0 {
						l := minInt(spaceLeft, wordLength)
						part, leftover := splitWord(word, l)
						word = leftover
						wordLength = wordLen(word)

						lineBuffer.WriteString(part)
						textBuffer.WriteString(pad)
						textBuffer.Write(lineBuffer.Bytes())
						lineBuffer.Reset()

						spaceLeft -= l

						if spaceLeft <= 0 {
							textBuffer.WriteString("\n")
							nbLine++
							spaceLeft = lineWidth - leftPad
						}
					}
				} else {
					// Normal break
					textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
					textBuffer.WriteString("\n")
					lineBuffer.Reset()
					lineBuffer.WriteString(word)
					firstWord = false
					spaceLeft = lineWidth - wordLength
					nbLine++
				}
			}
		}

		textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
		lineBuffer.Reset()
		firstLine = false
	}

	return textBuffer.String(), nbLine
}

func wordLen(word string) int {
	length := 0
	escape := false

	for _, char := range word {
		if char == '\x1b' {
			escape = true
		}

		if !escape {
			length++
		}

		if char == 'm' {
			escape = false
		}
	}

	return length
}

func splitWord(word string, length int) (string, string) {
	result := ""
	added := 0
	escape := false

	if length == 0 {
		return "", word
	}

	for _, char := range word {
		if char == '\x1b' {
			escape = true
		}

		result += string(char)

		if !escape {
			added++
			if added == length {
				break
			}
		}

		if char == 'm' {
			escape = false
		}
	}

	leftover := word[len(result):]

	return result, leftover
}

func minInt(a, b int) int {
	if a > b {
		return b
	}
	return a
}