From c0ce41540e775ec0a86770d10d5c3eb75bce0939 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 28 Dec 2018 23:09:20 +0800 Subject: Add back removed text functions --- util/text/text.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'util/text/text.go') 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 +} -- cgit