aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/MichaelMure/go-term-text/len.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-11-03 13:09:55 +0000
committerGitHub <noreply@github.com>2019-11-03 13:09:55 +0000
commit8c7c9880b1adcf876ad63ea39e46e62bd7ebde5d (patch)
treef9c6369a6593e3c00cfb3c0b45009dfb6d055d7b /vendor/github.com/MichaelMure/go-term-text/len.go
parentf5193cc76dd1a1c1bb55194a64ef90bae2278115 (diff)
parent912b5ca320891f2a4f1a88f1a137ce8ee46a1a03 (diff)
downloadgit-bug-8c7c9880b1adcf876ad63ea39e46e62bd7ebde5d.tar.gz
Merge pull request #228 from ludovicm67/patch-cli-label-colors
Display label colors in termui
Diffstat (limited to 'vendor/github.com/MichaelMure/go-term-text/len.go')
-rw-r--r--vendor/github.com/MichaelMure/go-term-text/len.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/vendor/github.com/MichaelMure/go-term-text/len.go b/vendor/github.com/MichaelMure/go-term-text/len.go
new file mode 100644
index 00000000..c6bcaeac
--- /dev/null
+++ b/vendor/github.com/MichaelMure/go-term-text/len.go
@@ -0,0 +1,45 @@
+package text
+
+import (
+ "strings"
+
+ "github.com/mattn/go-runewidth"
+)
+
+// Len return the length of a string in a terminal, while ignoring the terminal
+// escape sequences.
+func Len(text string) int {
+ length := 0
+ escape := false
+
+ for _, char := range text {
+ if char == '\x1b' {
+ escape = true
+ }
+ if !escape {
+ length += runewidth.RuneWidth(char)
+ }
+ if char == 'm' {
+ escape = false
+ }
+ }
+
+ return length
+}
+
+// MaxLineLen return the length in a terminal of the longest line, while
+// ignoring the terminal escape sequences.
+func MaxLineLen(text string) int {
+ lines := strings.Split(text, "\n")
+
+ max := 0
+
+ for _, line := range lines {
+ length := Len(line)
+ if length > max {
+ max = length
+ }
+ }
+
+ return max
+}