aboutsummaryrefslogtreecommitdiff
path: root/internal/color/color.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/color/color.go')
-rw-r--r--internal/color/color.go21
1 files changed, 19 insertions, 2 deletions
diff --git a/internal/color/color.go b/internal/color/color.go
index ff47a47..8376dde 100644
--- a/internal/color/color.go
+++ b/internal/color/color.go
@@ -36,7 +36,18 @@ var ansi16RGB = [16][3]int{
var cubeValues = [6]int{0, 95, 135, 175, 215, 255}
+// Plain reports whether a color mode renders no color. The empty mode and
+// "none" are equivalent (Render treats both as plain), so callers should use
+// this instead of open-coding the pair.
+func Plain(mode string) bool { return mode == "" || mode == "none" }
+
func Xterm256ToRGB(index int) (int, int, int) {
+ if index < 0 {
+ index = 0
+ }
+ if index > 255 {
+ index = 255
+ }
if index < 16 {
return ansi16RGB[index][0], ansi16RGB[index][1], ansi16RGB[index][2]
}
@@ -205,8 +216,14 @@ func TruncateVisible(s string, maxVisible int, unicode bool) string {
}
var pre, suf string
rest := s
- if idx := strings.IndexByte(rest, 'm'); idx >= 0 && strings.HasPrefix(rest, "\x1b[") {
- pre = rest[:idx+1]
+ // Render emits style and foreground as separate SGR sequences; peel every
+ // leading one, not just the first, or the second is counted as visible text.
+ for strings.HasPrefix(rest, "\x1b[") {
+ idx := strings.IndexByte(rest, 'm')
+ if idx < 0 {
+ break
+ }
+ pre += rest[:idx+1]
rest = rest[idx+1:]
}
if strings.HasSuffix(rest, Reset) {