diff options
| author | historia <[not public]> | 2026-06-25 04:46:40 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-25 04:46:40 -0400 |
| commit | 2725e2927a1595c7b100d942d1f14146252adeb7 (patch) | |
| tree | e8bc2292634a2d686c751981ddb3097517cccc6a /internal/color/color.go | |
| parent | 94a06fbbe682701ca4d4bd2a70cd74d8df29c932 (diff) | |
| download | thehouseoficarus-2725e2927a1595c7b100d942d1f14146252adeb7.tar.gz | |
feat: who, global chat, removed redundant YAML IDs
Diffstat (limited to 'internal/color/color.go')
| -rw-r--r-- | internal/color/color.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/color/color.go b/internal/color/color.go index dcbb280..22fa143 100644 --- a/internal/color/color.go +++ b/internal/color/color.go @@ -180,6 +180,48 @@ func VisibleLen(s string) int { return utf8.RuneCountInString(ansiRe.ReplaceAllString(s, "")) } +// WrapANSI wraps text to the given visible width, ignoring ANSI color codes +// when measuring. Each input line (split on "\n") is handled on its own: a line +// that already fits is returned untouched, so indentation and aligned UI are +// preserved. Only over-long lines are word-wrapped. The result is joined with +// "\r\n". A width <= 0 disables wrapping. +func WrapANSI(s string, width int) string { + if width <= 0 { + return s + } + var out []string + for _, line := range strings.Split(s, "\n") { + line = strings.TrimRight(line, "\r") + out = append(out, wrapLine(line, width)...) + } + return strings.Join(out, "\r\n") +} + +func wrapLine(line string, width int) []string { + if VisibleLen(line) <= width { + return []string{line} + } + words := strings.Fields(line) + if len(words) == 0 { + return []string{line} + } + cur := words[0] + curWidth := VisibleLen(words[0]) + var lines []string + for _, w := range words[1:] { + ww := VisibleLen(w) + if curWidth+1+ww <= width { + cur += " " + w + curWidth += 1 + ww + } else { + lines = append(lines, cur) + cur = w + curWidth = ww + } + } + return append(lines, cur) +} + func ContrastFg(mode string, bgIndex int) string { r, g, b := Xterm256ToRGB(bgIndex) if r+g+b > 384 { |
