Documentation
¶
Index ¶
- Constants
- func Fprintf(w io.Writer, format string, a ...any) (int, error)
- func GetSize(fd int) (width, height int, err error)
- func IsCygwinTerminal(fd uintptr) bool
- func IsNativeTerminal(fd uintptr) bool
- func IsTerminal(fd uintptr) bool
- func SanitizeANSI(content string, allowColor bool) string
- func SanitizedF(format string, a ...any) (int, error)
- type Level
Constants ¶
const ( CHAR_UNSPECIFIED = 0 CHAR_COLOR_SEQUENCE = 1 CHAR_CONTROL = 2 )
Variables ¶
This section is empty.
Functions ¶
func Fprintf ¶
Fprintf formats according to a format specifier and writes to w. It respects the global StderrLevel and StdoutLevel settings:
- If w is os.Stdout and StdoutLevel is LevelNone, ANSI codes are stripped
- If w is os.Stderr and StderrLevel is LevelNone, ANSI codes are stripped
- Otherwise, output is passed through unchanged
This allows TUI applications to automatically disable colors when the output is redirected to a file or pipe.
func GetSize ¶
GetSize returns the dimensions of the terminal for the given file descriptor. Returns width, height in characters, and any error encountered.
func IsCygwinTerminal ¶
func IsNativeTerminal ¶
IsNativeTerminal returns true if the given file descriptor is a native terminal (not a Cygwin/MSYS2 pseudo-terminal).
func IsTerminal ¶
IsTerminal returns true if the given file descriptor is connected to a terminal. This works for both native terminals and Cygwin/MSYS2 pseudo-terminals.
func SanitizeANSI ¶
SanitizeANSI sanitizes ANSI sequences in content for safe terminal output.
Behavior:
- If allowColor is true: ANSI color sequences are preserved
- If allowColor is false: All ANSI sequences are removed
- Control characters (except tab and newline) are converted to caret notation (^G, etc.)
This is useful for displaying untrusted or external output safely in a TUI.
func SanitizedF ¶
SanitizedF formats according to a format specifier, sanitizes the result, and writes it to stderr. Color sequences are preserved based on StderrLevel.
This is a convenience function for safely printing formatted output to stderr in TUI applications, ensuring control characters are converted to caret notation.
Types ¶
type Level ¶
type Level int
Level represents the color support level of a terminal.
The levels are:
- LevelNone: No color support, ANSI codes are stripped
- Level256: 256-color palette support (standard ANSI colors)
- Level16M: 16 million colors (24-bit truecolor/RGB) support
func (Level) Blue ¶ added in v3.2.0
Blue returns the string s wrapped in blue ANSI color codes. The color format depends on the Level:
- Level16M: Uses RGB #00c8ff (truecolor)
- Level256: Uses standard ANSI blue
- LevelNone: Returns s unchanged
func (Level) Green ¶ added in v3.2.0
Green returns the string s wrapped in green ANSI color codes. The color format depends on the Level:
- Level16M: Uses RGB #43e97a (truecolor)
- Level256: Uses standard ANSI green
- LevelNone: Returns s unchanged
func (Level) Purple ¶ added in v3.2.0
Purple returns the string s wrapped in purple ANSI color codes. The color format depends on the Level:
- Level16M: Uses RGB #7028e4 (truecolor)
- Level256: Uses standard ANSI purple
- LevelNone: Returns s unchanged
func (Level) Red ¶ added in v3.2.0
Red returns the string s wrapped in red ANSI color codes. The color format depends on the Level:
- Level16M: Uses RGB #f43b47 (truecolor)
- Level256: Uses standard ANSI red
- LevelNone: Returns s unchanged
func (Level) SupportColor ¶
SupportColor returns true if the terminal supports any color output.