term

package
v3.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CHAR_UNSPECIFIED    = 0
	CHAR_COLOR_SEQUENCE = 1
	CHAR_CONTROL        = 2
)

Variables

This section is empty.

Functions

func Fprintf

func Fprintf(w io.Writer, format string, a ...any) (int, error)

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

func GetSize(fd int) (width, height int, err error)

GetSize returns the dimensions of the terminal for the given file descriptor. Returns width, height in characters, and any error encountered.

func IsCygwinTerminal

func IsCygwinTerminal(fd uintptr) bool

func IsNativeTerminal

func IsNativeTerminal(fd uintptr) bool

IsNativeTerminal returns true if the given file descriptor is a native terminal (not a Cygwin/MSYS2 pseudo-terminal).

func IsTerminal

func IsTerminal(fd uintptr) bool

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

func SanitizeANSI(content string, allowColor bool) string

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

func SanitizedF(format string, a ...any) (int, error)

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
const (
	LevelNone Level = iota
	Level256
	Level16M
)
var (
	// StderrLevel is the detected color support level for stderr.
	StderrLevel Level
	// StdoutLevel is the detected color support level for stdout.
	StdoutLevel Level
)

func (Level) Blue added in v3.2.0

func (v Level) Blue(s string) string

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

func (v Level) Green(s string) string

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

func (v Level) Purple(s string) string

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

func (v Level) Red(s string) string

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

func (level Level) SupportColor() bool

SupportColor returns true if the terminal supports any color output.

func (Level) Yellow added in v3.2.0

func (v Level) Yellow(s string) string

Yellow returns the string s wrapped in yellow ANSI color codes. The color format depends on the Level:

  • Level16M: Uses RGB #fee240 (truecolor)
  • Level256: Uses standard ANSI yellow
  • LevelNone: Returns s unchanged

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL