aboutsummaryrefslogtreecommitdiffstats
path: root/util/text.go
blob: cc652cc64515c9d0365d4bb9184c21f3270ccec1 (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
package util

import (
	"bytes"
	"regexp"
	"strings"
)

func WordWrap(text string, lineWidth int) (string, int) {
	words := strings.Fields(strings.TrimSpace(text))
	if len(words) == 0 {
		return "", 1
	}
	lines := 1
	wrapped := words[0]
	spaceLeft := lineWidth - len(wrapped)
	for _, word := range words[1:] {
		if len(word)+1 > spaceLeft {
			wrapped += "\n" + word
			spaceLeft = lineWidth - len(word)
			lines++
		} else {
			wrapped += " " + word
			spaceLeft -= 1 + len(word)
		}
	}

	return wrapped, lines
}

func TextWrap(text string, lineWidth int) (string, int) {
	return TextWrapPadded(text, lineWidth, 0)
}

func TextWrapPadded(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)

	re := regexp.MustCompile(`(\x1b\[\d+m)?([^\x1b]*)(\x1b\[\d+m)?`)

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

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

		firstWord := true

		for _, word := range strings.Split(line, " ") {
			prefix := ""
			suffix := ""

			matches := re.FindStringSubmatch(word)
			if matches != nil && (matches[1] != "" || matches[3] != "") {
				// we have a color escape sequence
				prefix = matches[1]
				word = matches[2]
				suffix = matches[3]
			}

			if spaceLeft > len(word) {
				if !firstWord {
					lineBuffer.WriteString(" ")
					spaceLeft -= 1
				}
				lineBuffer.WriteString(prefix + word + suffix)
				spaceLeft -= len(word)
				firstWord = false
			} else {
				if len(word) > lineWidth {
					for len(word) > 0 {
						l := minInt(spaceLeft, len(word))
						part := prefix + word[:l]
						prefix = ""
						word = word[l:]

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

						if len(word) > 0 {
							textBuffer.WriteString("\n")
							nbLine++
						}

						spaceLeft = lineWidth - leftPad
					}
				} else {
					textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
					textBuffer.WriteString("\n")
					lineBuffer.Reset()
					lineBuffer.WriteString(prefix + word + suffix)
					firstWord = false
					spaceLeft = lineWidth - len(word)
					nbLine++
				}
			}
		}
		textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
		lineBuffer.Reset()
		firstLine = false
	}

	return textBuffer.String(), nbLine
}

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