Skip to content

Commit 01180c2

Browse files
committed
update doc, and refactor color utility functions
1 parent 90b3c2a commit 01180c2

File tree

5 files changed

+45
-333
lines changed

5 files changed

+45
-333
lines changed

text/256_COLOR_ANALYSIS.md

Lines changed: 0 additions & 296 deletions
This file was deleted.

text/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Used heavily in the other packages in this repo ([list](../list),
1515
- Foreground colors (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White)
1616
- Background colors (matching foreground set)
1717
- Hi-intensity variants for both foreground and background
18+
- **256-color palette support** - Extended color support for terminals
19+
- Standard 16 colors (0-15)
20+
- RGB cube colors (16-231) - 216 colors organized in a 6x6x6 cube
21+
- Grayscale colors (232-255) - 24 shades of gray
22+
- Helper functions: `Fg256Color(index)`, `Bg256Color(index)`, `Fg256RGB(r, g, b)`, `Bg256RGB(r, g, b)`
1823
- Text attributes (Bold, Faint, Italic, Underline, Blink, Reverse, Concealed, CrossedOut)
1924
- Automatic color detection based on environment variables (`NO_COLOR`, `FORCE_COLOR`, `TERM`)
2025
- Global enable/disable functions for colors
@@ -76,6 +81,7 @@ Used heavily in the other packages in this repo ([list](../list),
7681
- `EscSeqParser` - Parser for advanced escape sequence parsing and tracking
7782
- Supports both CSI (Control Sequence Introducer) and OSI (Operating System Command) formats
7883
- Tracks active formatting codes and can generate consolidated escape sequences
84+
- Full support for 256-color escape sequences (`\x1b[38;5;n`m` and `\x1b[48;5;n`m`)
7985

8086
### Cursor Control
8187

text/ansi_unix.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
package text
55

6+
import "os"
7+
68
func areANSICodesSupported() bool {
7-
return true
9+
// On Unix systems, ANSI codes are generally supported unless TERM is "dumb"
10+
// This is a basic check; 256-color sequences are ANSI sequences and will
11+
// be handled by terminals that support them (or ignored by those that don't)
12+
term := os.Getenv("TERM")
13+
return term != "dumb"
814
}

text/color.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010
)
1111

12+
// colorsEnabled is true if colors are enabled and supported by the terminal.
1213
var colorsEnabled = areColorsOnInTheEnv() && areANSICodesSupported()
1314

1415
// DisableColors (forcefully) disables color coding globally.
@@ -21,17 +22,24 @@ func EnableColors() {
2122
colorsEnabled = true
2223
}
2324

24-
// areColorsOnInTheEnv returns true is colors are not disable using
25+
// areColorsOnInTheEnv returns true if colors are not disabled using
2526
// well known environment variables.
2627
func areColorsOnInTheEnv() bool {
27-
if os.Getenv("FORCE_COLOR") == "1" {
28+
// FORCE_COLOR takes precedence - if set to a truthy value, enable colors
29+
forceColor := os.Getenv("FORCE_COLOR")
30+
if forceColor != "" && forceColor != "0" && forceColor != "false" {
2831
return true
2932
}
30-
if os.Getenv("NO_COLOR") == "" || os.Getenv("NO_COLOR") == "0" {
31-
return os.Getenv("TERM") != "dumb"
33+
34+
// NO_COLOR: if set to any non-empty value (except "0"), disable colors
35+
// Note: "0" is treated as "not set" to allow explicit enabling via NO_COLOR=0
36+
noColor := os.Getenv("NO_COLOR")
37+
if noColor != "" && noColor != "0" {
38+
return false
3239
}
3340

34-
return false
41+
// Default: check TERM - if not "dumb", assume colors are supported
42+
return os.Getenv("TERM") != "dumb"
3543
}
3644

3745
// The logic here is inspired from github.com/fatih/color; the following is

0 commit comments

Comments
 (0)