MCPcopy Index your code
hub / github.com/box-cli-maker/box-cli-maker / longestLine

Function longestLine

util.go:47–77  ·  view source on GitHub ↗

longestLine expands tabs in lines and determines longest visible return longest length and array of expanded lines

(lines []string)

Source from the content-addressed store, hash-verified

45// longestLine expands tabs in lines and determines longest visible
46// return longest length and array of expanded lines
47func longestLine(lines []string) (int, []expandedLine) {
48 longest := 0
49 var expandedLines []expandedLine
50 var tmpLine strings.Builder
51 var lineLen int
52
53 for _, line := range lines {
54 tmpLine.Reset()
55 for _, c := range line {
56 lineLen = runewidth.StringWidth(tmpLine.String())
57
58 if c == '\t' {
59 tmpLine.WriteString(strings.Repeat(" ", 8-(lineLen&7)))
60 } else {
61 tmpLine.WriteRune(c)
62 }
63 }
64 lineLen = runewidth.StringWidth(tmpLine.String())
65 expandedLines = append(expandedLines, expandedLine{tmpLine.String(), lineLen})
66
67 // Check if each line has ANSI Color Code then decrease the length accordingly
68 if runewidth.StringWidth(ansi.Strip(tmpLine.String())) < runewidth.StringWidth(tmpLine.String()) {
69 lineLen = runewidth.StringWidth(ansi.Strip(tmpLine.String()))
70 }
71
72 if lineLen > longest {
73 longest = lineLen
74 }
75 }
76 return longest, expandedLines
77}
78
79// charWidth returns the visible width of a string, treating zero-width
80// results as width 1 so that box calculations always make progress.

Callers 2

RenderMethod · 0.85

Calls

no outgoing calls

Tested by 1