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 ImageMode
- type ImageProtocol
- 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 ImageMode ¶ added in v0.25.0
type ImageMode int
ImageMode represents a tri-state preference for inline image rendering, typically wired to a CLI flag like --image=auto|on|off.
const ( // ImageModeAuto honours the detected terminal capability: render images // only when DetectImageProtocol reports a supported protocol. ImageModeAuto ImageMode = iota // ImageModeOn forces image rendering even when detection returned // ImageNone (useful for terminals not on our whitelist, or for tmux // passthrough setups). When detection has no protocol, iTerm2 is used as // the most widely compatible default. ImageModeOn // ImageModeOff disables image rendering unconditionally. ImageModeOff )
func ParseImageMode ¶ added in v0.25.0
ParseImageMode parses a CLI/config string into an ImageMode. Recognised values are "auto", "on" (synonyms: "yes", "true", "1"), and "off" (synonyms: "no", "false", "0"). Unknown values fall back to ImageModeAuto so that misconfiguration never silently disables a feature the user wanted.
type ImageProtocol ¶ added in v0.25.0
type ImageProtocol int
ImageProtocol represents an inline-image protocol supported by the hosting terminal.
const ( // ImageNone means the terminal is not known to support any inline image // protocol; callers should fall back to a textual representation // (e.g. a hex dump). ImageNone ImageProtocol = iota // ImageITerm2 is the iTerm2 / WezTerm OSC 1337 inline image protocol. ImageITerm2 // ImageKitty is the Kitty graphics protocol, also implemented by WezTerm, // Ghostty and recent Konsole builds. ImageKitty )
func DetectImageProtocol ¶ added in v0.25.0
func DetectImageProtocol() ImageProtocol
DetectImageProtocol inspects environment variables to determine which inline-image protocol (if any) is supported by the current terminal.
This is intentionally an on-demand check rather than a package-init side effect: image rendering is the exception rather than the rule, so the environment probe should only run once the caller has decided it actually has an image to display.
Detection order:
- Kitty graphics: TERM=xterm-kitty, KITTY_WINDOW_ID set, KONSOLE_VERSION >= 22.04, or TERM_PROGRAM in {ghostty, WezTerm}.
- iTerm2 OSC 1337: TERM_PROGRAM=iTerm.app, or LC_TERMINAL=iTerm2.
WezTerm also speaks iTerm2's protocol and Sixel, but Kitty graphics is strictly richer (24-bit colour, no quantisation, chunked transfers), so it wins the tie.
Anything else returns ImageNone. Terminals with unreliable support (Warp, VSCode) are intentionally excluded from the auto whitelist; users can force rendering via --image=on.
func ResolveImage ¶ added in v0.25.0
func ResolveImage(mode string) ImageProtocol
ResolveImage parses a user-supplied mode string and returns the inline image protocol the caller should use.
- off: always returns ImageNone (environment probe is skipped).
- on: returns the detected protocol, or ImageITerm2 when nothing was detected (most widely compatible default for tmux passthrough and terminals not on our whitelist).
- auto (default): returns the detected protocol as-is.
func (ImageProtocol) String ¶ added in v0.25.0
func (p ImageProtocol) String() string
String returns a short, lower-case name for the protocol, suitable for logging and configuration values.
func (ImageProtocol) Supported ¶ added in v0.25.0
func (p ImageProtocol) Supported() bool
Supported reports whether the protocol is a real inline image protocol.
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 v0.19.1
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 v0.19.1
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 v0.19.1
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 v0.19.1
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.