2018-09-11 23:04:16 +03:00
|
|
|
package text
|
2018-08-03 18:29:11 +03:00
|
|
|
|
|
|
|
import (
|
2018-12-25 23:17:11 +03:00
|
|
|
"github.com/mattn/go-runewidth"
|
2018-08-03 18:29:11 +03:00
|
|
|
"strings"
|
2018-12-30 20:57:10 +03:00
|
|
|
"unicode/utf8"
|
2018-08-03 18:29:11 +03:00
|
|
|
)
|
|
|
|
|
2019-01-01 16:52:05 +03:00
|
|
|
// Force runewidth not to treat ambiguous runes as wide chars, so that things
|
|
|
|
// like unicode ellipsis/up/down/left/right glyphs can have correct runewidth
|
|
|
|
// and can be displayed correctly in terminals.
|
|
|
|
func init() {
|
|
|
|
runewidth.DefaultCondition.EastAsianWidth = false
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
// Wrap a text for an exact line size
|
|
|
|
// Handle properly terminal color escape code
|
2018-09-11 23:04:16 +03:00
|
|
|
func Wrap(text string, lineWidth int) (string, int) {
|
|
|
|
return WrapLeftPadded(text, lineWidth, 0)
|
2018-08-09 15:35:55 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 23:27:45 +03:00
|
|
|
// Wrap a text for an exact line size with a left padding
|
|
|
|
// Handle properly terminal color escape code
|
2018-09-11 23:04:16 +03:00
|
|
|
func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
|
2018-12-30 20:57:10 +03:00
|
|
|
var lines []string
|
|
|
|
nbLine := 0
|
2018-12-31 06:00:05 +03:00
|
|
|
pad := strings.Repeat(" ", leftPad)
|
2018-08-03 18:29:11 +03:00
|
|
|
|
|
|
|
// tabs are formatted as 4 spaces
|
2018-12-30 20:57:10 +03:00
|
|
|
text = strings.Replace(text, "\t", " ", -1)
|
2018-12-31 06:00:05 +03:00
|
|
|
// NOTE: text is first segmented into lines so that softwrapLine can handle.
|
2018-12-30 20:57:10 +03:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 17:49:25 +03:00
|
|
|
}
|
2018-12-30 20:57:10 +03:00
|
|
|
return strings.Join(lines, "\n"), nbLine
|
2018-12-26 17:49:25 +03:00
|
|
|
}
|
2018-08-03 18:29:11 +03:00
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
// Break a line into several lines so that each line consumes at most
|
|
|
|
// 'textWidth' cells. Lines break at groups of white spaces and multibyte
|
|
|
|
// chars. Nothing is removed from the original text so that it behaves like a
|
|
|
|
// softwrap.
|
|
|
|
//
|
|
|
|
// Required: The line shall not contain '\n'
|
|
|
|
//
|
|
|
|
// WRAPPING ALGORITHM: The line is broken into non-breakable chunks, then line
|
|
|
|
// breaks ("\n") are inserted between these groups so that the total length
|
|
|
|
// between breaks does not exceed the required width. Words that are longer than
|
|
|
|
// the textWidth are broen into pieces no longer than textWidth.
|
|
|
|
//
|
|
|
|
func softwrapLine(line string, textWidth int) string {
|
|
|
|
// NOTE: terminal escapes are stripped out of the line so the algorithm is
|
|
|
|
// simpler. Do not try to mix them in the wrapping algorithm, as it can get
|
|
|
|
// complicated quickly.
|
|
|
|
line1, termEscapes := extractTermEscapes(line)
|
|
|
|
|
|
|
|
chunks := segmentLine(line1)
|
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
var line2 string = ""
|
|
|
|
var width int = 0
|
|
|
|
for len(chunks) > 0 {
|
|
|
|
thisWord := chunks[len(chunks)-1]
|
|
|
|
wl := wordLen(thisWord)
|
|
|
|
if width+wl <= textWidth {
|
|
|
|
line2 += chunks[len(chunks)-1]
|
|
|
|
chunks = chunks[:len(chunks)-1]
|
|
|
|
width += wl
|
|
|
|
if width == textWidth && len(chunks) > 0 {
|
|
|
|
// NOTE: new line begins when current line is full and there are more
|
|
|
|
// chunks to come.
|
|
|
|
line2 += "\n"
|
|
|
|
width = 0
|
|
|
|
}
|
|
|
|
} else if wl > textWidth {
|
2019-01-08 04:55:32 +03:00
|
|
|
// NOTE: By default, long words are splited to fill the remaining space.
|
|
|
|
// But if the long words is the first non-space word in the middle of the
|
|
|
|
// line, preceeding spaces shall not be counted in word spliting.
|
|
|
|
splitWidth := textWidth - width
|
|
|
|
if strings.HasSuffix(line2, "\n"+strings.Repeat(" ", width)) {
|
|
|
|
splitWidth += width
|
|
|
|
}
|
|
|
|
left, right := splitWord(chunks[len(chunks)-1], splitWidth)
|
2018-12-31 06:00:05 +03:00
|
|
|
chunks[len(chunks)-1] = right
|
2019-01-08 04:55:32 +03:00
|
|
|
line2 += left + "\n"
|
2018-12-31 06:00:05 +03:00
|
|
|
width = 0
|
|
|
|
} else {
|
|
|
|
line2 += "\n"
|
|
|
|
width = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
line3 := applyTermEscapes(line2, termEscapes)
|
|
|
|
return line3
|
|
|
|
}
|
|
|
|
|
|
|
|
// EscapeItem: Storage of terminal escapes in a line. 'item' is the actural
|
|
|
|
// escape command, and 'pos' is the index in the rune array where the 'item'
|
|
|
|
// shall be inserted back. For example, the escape item in "F\x1b33mox" is
|
|
|
|
// {"\x1b33m", 1}.
|
2019-01-07 16:30:46 +03:00
|
|
|
type escapeItem struct {
|
2018-12-30 20:57:10 +03:00
|
|
|
item string
|
|
|
|
pos int
|
|
|
|
}
|
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
// Extract terminal escapes out of a line, returns a new line without terminal
|
|
|
|
// escapes and a slice of escape items. The terminal escapes can be inserted
|
|
|
|
// back into the new line at rune index 'item.pos' to recover the original line.
|
|
|
|
//
|
|
|
|
// Required: The line shall not contain "\n"
|
|
|
|
//
|
2019-01-07 16:30:46 +03:00
|
|
|
func extractTermEscapes(line string) (string, []escapeItem) {
|
|
|
|
var termEscapes []escapeItem
|
2018-12-31 06:00:05 +03:00
|
|
|
var line1 string
|
2018-12-30 20:57:10 +03:00
|
|
|
|
|
|
|
pos := 0
|
|
|
|
item := ""
|
|
|
|
occupiedRuneCount := 0
|
|
|
|
inEscape := false
|
2018-12-31 06:00:05 +03:00
|
|
|
for i, r := range []rune(line) {
|
2018-12-30 20:57:10 +03:00
|
|
|
if r == '\x1b' {
|
|
|
|
pos = i
|
|
|
|
item = string(r)
|
|
|
|
inEscape = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if inEscape {
|
|
|
|
item += string(r)
|
|
|
|
if r == 'm' {
|
2019-01-07 16:30:46 +03:00
|
|
|
termEscapes = append(termEscapes, escapeItem{item, pos - occupiedRuneCount})
|
2018-12-30 20:57:10 +03:00
|
|
|
occupiedRuneCount += utf8.RuneCountInString(item)
|
|
|
|
inEscape = false
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2018-12-31 06:00:05 +03:00
|
|
|
line1 += string(r)
|
2018-12-30 20:57:10 +03:00
|
|
|
}
|
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
return line1, termEscapes
|
2018-12-30 20:57:10 +03:00
|
|
|
}
|
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
// Apply the extracted terminal escapes to the edited line. The only edit
|
|
|
|
// allowed is to insert "\n" like that in softwrapLine. Callers shall ensure
|
|
|
|
// this since this function is not able to check it.
|
2019-01-07 16:30:46 +03:00
|
|
|
func applyTermEscapes(line string, escapes []escapeItem) string {
|
2018-12-31 06:00:05 +03:00
|
|
|
if len(escapes) == 0 {
|
|
|
|
return line
|
2018-12-30 20:57:10 +03:00
|
|
|
}
|
2018-12-31 06:00:05 +03:00
|
|
|
|
2018-12-30 20:57:10 +03:00
|
|
|
var out string = ""
|
|
|
|
|
|
|
|
currPos := 0
|
|
|
|
currItem := 0
|
2018-12-31 06:00:05 +03:00
|
|
|
for _, r := range line {
|
|
|
|
if currItem < len(escapes) && currPos == escapes[currItem].pos {
|
|
|
|
// NOTE: We avoid terminal escapes at the end of a line by move them one
|
|
|
|
// pass the end of line, so that algorithms who trim right spaces are
|
|
|
|
// happy. But algorithms who trim left spaces are still unhappy.
|
2018-12-30 20:57:10 +03:00
|
|
|
if r == '\n' {
|
2018-12-31 06:00:05 +03:00
|
|
|
out += "\n" + escapes[currItem].item
|
2018-12-26 17:49:25 +03:00
|
|
|
} else {
|
2018-12-31 06:00:05 +03:00
|
|
|
out += escapes[currItem].item + string(r)
|
2018-12-30 20:57:10 +03:00
|
|
|
currPos++
|
2018-08-11 23:27:45 +03:00
|
|
|
}
|
2018-12-30 20:57:10 +03:00
|
|
|
currItem++
|
2018-12-26 17:49:25 +03:00
|
|
|
} else {
|
2018-12-30 20:57:10 +03:00
|
|
|
if r != '\n' {
|
|
|
|
currPos++
|
2018-08-03 18:29:11 +03:00
|
|
|
}
|
2018-12-30 20:57:10 +03:00
|
|
|
out += string(r)
|
2018-08-03 18:29:11 +03:00
|
|
|
}
|
2018-12-26 17:49:25 +03:00
|
|
|
}
|
2018-08-11 23:27:45 +03:00
|
|
|
|
2018-12-30 20:57:10 +03:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
// Segment a line into chunks, where each chunk consists of chars with the same
|
|
|
|
// type and is not breakable.
|
|
|
|
func segmentLine(s string) []string {
|
2018-12-30 20:57:10 +03:00
|
|
|
var chunks []string
|
2018-12-31 06:00:05 +03:00
|
|
|
|
2018-12-30 20:57:10 +03:00
|
|
|
var word string
|
2019-01-07 16:30:46 +03:00
|
|
|
wordType := none
|
2018-12-31 03:53:11 +03:00
|
|
|
flushWord := func() {
|
|
|
|
chunks = append(chunks, word)
|
|
|
|
word = ""
|
2019-01-07 16:30:46 +03:00
|
|
|
wordType = none
|
2018-12-31 03:53:11 +03:00
|
|
|
}
|
2018-12-31 06:00:05 +03:00
|
|
|
|
|
|
|
for _, r := range s {
|
|
|
|
// A WIDE_CHAR itself constitutes a chunk.
|
2018-12-30 20:57:10 +03:00
|
|
|
thisType := runeType(r)
|
2019-01-07 16:30:46 +03:00
|
|
|
if thisType == wideChar {
|
|
|
|
if wordType != none {
|
2018-12-31 03:53:11 +03:00
|
|
|
flushWord()
|
|
|
|
}
|
2018-12-30 20:57:10 +03:00
|
|
|
chunks = append(chunks, string(r))
|
2018-12-26 17:49:25 +03:00
|
|
|
continue
|
|
|
|
}
|
2018-12-31 06:00:05 +03:00
|
|
|
// Other type of chunks starts with a char of that type, and ends with a
|
2018-12-30 20:57:10 +03:00
|
|
|
// char with different type or end of string.
|
|
|
|
if thisType != wordType {
|
2019-01-07 16:30:46 +03:00
|
|
|
if wordType != none {
|
2018-12-31 03:53:11 +03:00
|
|
|
flushWord()
|
2018-12-30 20:57:10 +03:00
|
|
|
}
|
2018-12-26 17:49:25 +03:00
|
|
|
word = string(r)
|
2018-12-30 20:57:10 +03:00
|
|
|
wordType = thisType
|
2018-12-26 17:49:25 +03:00
|
|
|
} else {
|
|
|
|
word += string(r)
|
2018-12-23 22:47:18 +03:00
|
|
|
}
|
2018-08-03 18:29:11 +03:00
|
|
|
}
|
2018-12-30 20:57:10 +03:00
|
|
|
if word != "" {
|
2018-12-31 03:53:11 +03:00
|
|
|
flushWord()
|
2018-12-30 20:57:10 +03:00
|
|
|
}
|
2018-08-03 18:29:11 +03:00
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
return chunks
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rune categories
|
|
|
|
//
|
|
|
|
// These categories are so defined that each category forms a non-breakable
|
|
|
|
// chunk. It IS NOT the same as unicode code point categories.
|
|
|
|
//
|
|
|
|
const (
|
2019-01-07 16:30:46 +03:00
|
|
|
none int = iota
|
|
|
|
wideChar
|
|
|
|
invisible
|
|
|
|
shortUnicode
|
|
|
|
space
|
|
|
|
visibleAscii
|
2018-12-31 06:00:05 +03:00
|
|
|
)
|
2018-12-30 20:57:10 +03:00
|
|
|
|
2018-12-31 06:00:05 +03:00
|
|
|
// Determine the category of a rune.
|
|
|
|
func runeType(r rune) int {
|
|
|
|
rw := runewidth.RuneWidth(r)
|
|
|
|
if rw > 1 {
|
2019-01-07 16:30:46 +03:00
|
|
|
return wideChar
|
2018-12-31 06:00:05 +03:00
|
|
|
} else if rw == 0 {
|
2019-01-07 16:30:46 +03:00
|
|
|
return invisible
|
2018-12-31 06:00:05 +03:00
|
|
|
} else if r > 127 {
|
2019-01-07 16:30:46 +03:00
|
|
|
return shortUnicode
|
2018-12-31 06:00:05 +03:00
|
|
|
} else if r == ' ' {
|
2019-01-07 16:30:46 +03:00
|
|
|
return space
|
2018-12-31 06:00:05 +03:00
|
|
|
} else {
|
2019-01-07 16:30:46 +03:00
|
|
|
return visibleAscii
|
2018-12-31 06:00:05 +03:00
|
|
|
}
|
2018-08-03 18:29:11 +03:00
|
|
|
}
|
|
|
|
|
2018-12-26 17:49:25 +03:00
|
|
|
// wordLen return the length of a word, while ignoring the terminal escape
|
|
|
|
// sequences
|
2018-08-11 23:27:45 +03:00
|
|
|
func wordLen(word string) int {
|
|
|
|
length := 0
|
|
|
|
escape := false
|
|
|
|
|
|
|
|
for _, char := range word {
|
|
|
|
if char == '\x1b' {
|
|
|
|
escape = true
|
|
|
|
}
|
|
|
|
if !escape {
|
2018-12-25 23:17:11 +03:00
|
|
|
length += runewidth.RuneWidth(rune(char))
|
2018-08-11 23:27:45 +03:00
|
|
|
}
|
|
|
|
if char == 'm' {
|
|
|
|
escape = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return length
|
|
|
|
}
|
2018-12-28 18:09:20 +03:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|