-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
len.go
45 lines (36 loc) · 733 Bytes
/
len.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
}