aboutsummaryrefslogtreecommitdiff
path: root/internal/color
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-06-29 04:41:40 -0400
committerhistoria <[not public]>2026-06-29 04:41:40 -0400
commit71ce47ca37566be8dc390434df4cd01422298094 (patch)
treec688dc5ff84af8c03a5c825dd6a3926281a8cae3 /internal/color
parent0fe67cfd5def4ac3e919fd611fe5acc55ca2d253 (diff)
downloadthehouseoficarus-71ce47ca37566be8dc390434df4cd01422298094.tar.gz
fix: long fields truncated better in tables
Diffstat (limited to 'internal/color')
-rw-r--r--internal/color/color.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/internal/color/color.go b/internal/color/color.go
index 92535d1..8e301e4 100644
--- a/internal/color/color.go
+++ b/internal/color/color.go
@@ -180,12 +180,23 @@ func VisibleLen(s string) int {
return utf8.RuneCountInString(ansiRe.ReplaceAllString(s, ""))
}
+// Ellipsis returns a Unicode ellipsis character when unicode is true,
+// or the ASCII "..." otherwise.
+func Ellipsis(unicode bool) string {
+ if unicode {
+ return "\u2026"
+ }
+ return "..."
+}
+
// TruncateVisible truncates s to at most maxVisible visible characters,
// preserving ANSI SGR codes. Assumes cells follow the common pattern
-// "\x1b[...mTEXT\x1b[0m" or are plain text. Appends "..." when truncated.
-func TruncateVisible(s string, maxVisible int) string {
- if maxVisible < 3 {
- maxVisible = 3
+// "\x1b[...mTEXT\x1b[0m" or are plain text. Appends an ellipsis when truncated.
+func TruncateVisible(s string, maxVisible int, unicode bool) string {
+ ellipsis := Ellipsis(unicode)
+ ellipLen := utf8.RuneCountInString(ellipsis)
+ if maxVisible < ellipLen {
+ maxVisible = ellipLen
}
if VisibleLen(s) <= maxVisible {
return s
@@ -201,12 +212,12 @@ func TruncateVisible(s string, maxVisible int) string {
rest = rest[:len(rest)-len(Reset)]
}
runes := []rune(rest)
- budget := maxVisible - 3
+ budget := maxVisible - ellipLen
if budget < 0 {
budget = 0
}
if len(runes) > budget {
- rest = string(runes[:budget]) + "..."
+ rest = string(runes[:budget]) + ellipsis
}
return pre + rest + suf
}