aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/text.go
diff options
context:
space:
mode:
authorYang Zhang <yang_zhang@iapcm.ac.cn>2018-12-28 23:09:20 +0800
committerYang Zhang <yang_zhang@iapcm.ac.cn>2018-12-30 08:38:31 +0800
commitc0ce41540e775ec0a86770d10d5c3eb75bce0939 (patch)
tree9d89d8b2889be165160225d9f9c457530cd2e173 /util/text/text.go
parent52419165350a2b36706a6fb67acda862d80d6730 (diff)
downloadgit-bug-c0ce41540e775ec0a86770d10d5c3eb75bce0939.tar.gz
Add back removed text functions
Diffstat (limited to 'util/text/text.go')
-rw-r--r--util/text/text.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/util/text/text.go b/util/text/text.go
index ee9d278c..ad920bd8 100644
--- a/util/text/text.go
+++ b/util/text/text.go
@@ -127,3 +127,51 @@ func wordLen(word string) int {
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
+}