diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-09 14:06:55 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-09 14:06:55 +0200 |
commit | ca31258cd24d7f7951f14644fe592724579a53b8 (patch) | |
tree | 5169bf7e118761bd2958f9a3f023c815d5cd2f51 /util/text.go | |
parent | bf0a855f0cbdc2ea1e4a250b7152272982ffb611 (diff) | |
download | git-bug-ca31258cd24d7f7951f14644fe592724579a53b8.tar.gz |
TextWrap: support text with color escape sequence
Diffstat (limited to 'util/text.go')
-rw-r--r-- | util/text.go | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/util/text.go b/util/text.go index 83a101d3..8f762c08 100644 --- a/util/text.go +++ b/util/text.go @@ -2,6 +2,7 @@ package util import ( "bytes" + "regexp" "strings" ) @@ -36,6 +37,8 @@ func TextWrap(text string, lineWidth int) (string, int) { // 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 @@ -47,19 +50,31 @@ func TextWrap(text string, lineWidth int) (string, int) { 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(word) + 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 := word[:l] + part := prefix + word[:l] + prefix = "" word = word[l:] lineBuffer.WriteString(part) @@ -77,7 +92,7 @@ func TextWrap(text string, lineWidth int) (string, int) { textBuffer.WriteString(strings.TrimRight(lineBuffer.String(), " ")) textBuffer.WriteString("\n") lineBuffer.Reset() - lineBuffer.WriteString(word) + lineBuffer.WriteString(prefix + word + suffix) firstWord = false spaceLeft = lineWidth - len(word) nbLine++ |