aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/text.go
blob: 0447bde1dcf4b8ee355c9c9da044a1798a9af1bb (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package text

import (
	"github.com/mattn/go-runewidth"
	"strings"
	"unicode/utf8"
)

// 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) {
	pad := strings.Repeat(" ", leftPad)
	var lines []string
	nbLine := 0

	// tabs are formatted as 4 spaces
	text = strings.Replace(text, "\t", "    ", -1)
	for _, line := range strings.Split(text, "\n") {
		if line == "" || strings.TrimSpace(line) == "" {
			lines = append(lines, "")
			nbLine++
		} else {
			wrapped := softwrapLine(line, lineWidth-leftPad)
			firstLine := true
			for _, seg := range strings.Split(wrapped, "\n") {
				if firstLine {
					lines = append(lines, pad+strings.TrimRight(seg, " "))
					firstLine = false
				} else {
					lines = append(lines, pad+strings.TrimSpace(seg))
				}
				nbLine++
			}
		}
	}
	return strings.Join(lines, "\n"), nbLine
}

type EscapeItem struct {
	item string
	pos  int
}

func recordTermEscape(s string) (string, []EscapeItem) {
	var result []EscapeItem
	var newStr string

	pos := 0
	item := ""
	occupiedRuneCount := 0
	inEscape := false
	for i, r := range []rune(s) {
		if r == '\x1b' {
			pos = i
			item = string(r)
			inEscape = true
			continue
		}
		if inEscape {
			item += string(r)
			if r == 'm' {
				result = append(result, EscapeItem{item: item, pos: pos - occupiedRuneCount})
				occupiedRuneCount += utf8.RuneCountInString(item)
				inEscape = false
			}
			continue
		}
		newStr += string(r)
	}

	return newStr, result
}

func replayTermEscape(s string, sequence []EscapeItem) string {
	if len(sequence) == 0 {
		return string(s)
	}
	// Assume the original string contains no new line and the wrapped only insert
	// new lines. So that we can recover the position where we insert the term
	// escapes.
	var out string = ""

	currPos := 0
	currItem := 0
	for _, r := range []rune(s) {
		if currItem < len(sequence) && currPos == sequence[currItem].pos {
			if r == '\n' {
				out += "\n" + sequence[currItem].item
			} else {
				out += sequence[currItem].item + string(r)
				currPos++
			}
			currItem++
		} else {
			if r != '\n' {
				currPos++
			}
			out += string(r)
		}
	}

	return out
}

// Break a line into several lines so that each line consumes at most 'w' cells.
// Lines break at group of white spaces and multibyte chars. Nothing is removed
// from the line so that it behaves like a softwrap.
//
// Required: The line shall not contain '\n' (so it is a single line).
//
// WRAPPING ALGORITHM: The line is broken into non-breakable groups, then line
// breaks ("\n") is inserted between these groups so that the total length
// between breaks does not exceed the required width. Words that are longer than
// the width is broken into several words as `M+M+...+N`.
func softwrapLine(s string, w int) string {
	newStr, termSeqs := recordTermEscape(s)

	const (
		WIDE_CHAR     = iota
		INVISIBLE     = iota
		SHORT_UNICODE = iota
		SPACE         = iota
		VISIBLE_ASCII = iota
		NONE          = iota
	)

	// In order to simplify the terminal color sequence handling, we first strip
	// them out of the text and record their position, then do the wrap. After
	// that, we insert back these sequences.
	runeType := func(r rune) int {
		rw := runewidth.RuneWidth(r)
		if rw > 1 {
			return WIDE_CHAR
		} else if rw == 0 {
			return INVISIBLE
		} else if r > 127 {
			return SHORT_UNICODE
		} else if r == ' ' {
			return SPACE
		} else {
			return VISIBLE_ASCII
		}
	}

	var chunks []string
	var word string
	wordType := NONE
	for _, r := range []rune(newStr) {
		// A WIDE_CHAR itself constitutes a group.
		thisType := runeType(r)
		if thisType == WIDE_CHAR {
			chunks = append(chunks, string(r))
			continue
		}
		// Other type of groups starts with a char of that type, and ends with a
		// char with different type or end of string.
		if thisType != wordType {
			if wordType != NONE {
				chunks = append(chunks, word)
			}
			word = string(r)
			wordType = thisType
		} else {
			word += string(r)
		}
	}
	if word != "" {
		chunks = append(chunks, word)
	}

	var line string = ""
	var width int = 0
	// Reverse the chunk array so we can use it as a stack.
	for i, j := 0, len(chunks)-1; i < j; i, j = i+1, j-1 {
		chunks[i], chunks[j] = chunks[j], chunks[i]
	}
	for len(chunks) > 0 {
		thisWord := chunks[len(chunks)-1]
		wl := wordLen(thisWord)
		if width+wl <= w {
			line += chunks[len(chunks)-1]
			chunks = chunks[:len(chunks)-1]
			width += wl
			if width == w && len(chunks) > 0{
				line += "\n"
				width = 0
			}
		} else if wl > w {
			left, right := splitWord(chunks[len(chunks)-1], w)
			line += left + "\n"
			chunks[len(chunks)-1] = right
			width = 0
		} else {
			line += "\n"
			width = 0
		}
	}

	line = replayTermEscape(line, termSeqs)
	return line
}

// wordLen return the length of a word, while ignoring the terminal escape
// sequences
func wordLen(word string) int {
	length := 0
	escape := false

	for _, char := range word {
		if char == '\x1b' {
			escape = true
		}
		if !escape {
			length += runewidth.RuneWidth(rune(char))
		}
		if char == 'm' {
			escape = false
		}
	}

	return length
}

// splitWord split a word at the given length, while ignoring the terminal escape sequences
func splitWord(word string, length int) (string, string) {
	runes := []rune(word)
	var result []rune
	added := 0
	escape := false

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

	for _, r := range runes {
		if r == '\x1b' {
			escape = true
		}

		width := runewidth.RuneWidth(r)
		if width+added > length {
			// wide character made the length overflow
			break
		}

		result = append(result, r)

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

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

	leftover := runes[len(result):]

	return string(result), string(leftover)
}

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