1package text
2
3import (
4 "strings"
5
6 "github.com/mattn/go-runewidth"
7)
8
9// Len return the length of a string in a terminal, while ignoring the terminal
10// escape sequences.
11func Len(text string) int {
12 length := 0
13 escape := false
14
15 for _, char := range text {
16 if char == '\x1b' {
17 escape = true
18 }
19 if !escape {
20 length += runewidth.RuneWidth(char)
21 }
22 if char == 'm' {
23 escape = false
24 }
25 }
26
27 return length
28}
29
30// MaxLineLen return the length in a terminal of the longest line, while
31// ignoring the terminal escape sequences.
32func MaxLineLen(text string) int {
33 lines := strings.Split(text, "\n")
34
35 max := 0
36
37 for _, line := range lines {
38 length := Len(line)
39 if length > max {
40 max = length
41 }
42 }
43
44 return max
45}