Documentation
¶
Overview ¶
Package termcore holds process-wide primitives shared by every terminull app:
- signals: a context cancelled on Ctrl+C (SIGINT) and SIGTERM
- exit codes: ExitCodeError maps errors to process exit codes
- version: binary version and commit, with `go install` fallback
- log level: one shared slog.LevelVar behind --verbose/--debug
- terminal facts: width, height, tty/color/console detection
- terminal I/O: raw-mode capability queries, cell pixels, no-echo password reads
Core is the only library that imports x/sys and x/term, and it behaves the same way across Linux, macOS, and Windows.
Index ¶
- Constants
- Variables
- func CellPixels(f *os.File) (int, int)
- func Color(f *os.File, noColor bool) bool
- func ColorDepth() int
- func ColorFor(w io.Writer, noColor bool) bool
- func Emoji() bool
- func EnsureConsole() error
- func EscapeLen(s string, i int) int
- func ExitCodeOf(err error) int
- func FullVersion() string
- func IsTerminal(f *os.File) bool
- func ReadPassword(fd int) ([]byte, error)
- func RuneWidth(r rune) int
- func SetDebug(debug bool)
- func ShellSplit(s string) ([]string, error)
- func SignalContext(parent context.Context) (context.Context, context.CancelFunc)
- func Stderr() io.Writer
- func Stdout() io.Writer
- func TerminalHeight(f *os.File) int
- func TerminalQuery(f *os.File, query []byte, timeout time.Duration, terminator byte) []byte
- func TerminalWidth(f *os.File) int
- func Truncate(s string, max int) string
- func VisibleWidth(s string) int
- func Width(s string) int
- type ExitCodeError
Constants ¶
const ( Depth16 = 16 Depth256 = 256 DepthTrue = 1 << 24 )
Color depth tiers reported by ColorDepth.
const DefaultTerminalWidth = 80
DefaultTerminalWidth is used when detection fails (pipes, files, dumb terminals): the classic 80 columns.
Variables ¶
var ( Version = "unknown" Commit = "unknown" )
Version and Commit are set at link time via -ldflags for releases:
go build -ldflags "-X github.com/CHE3MZ/terminull/v2/termcore.Version=1.2.3 -X github.com/CHE3MZ/terminull/v2/termcore.Commit=abc123"
When left as "unknown" (plain `go build` / `go install`), Version falls back to the module version from the build info, else stays "unknown".
var Level = &slog.LevelVar{}
Level is the single shared slog level for the process. --verbose/--debug flip it to slog.LevelDebug via SetDebug. Apps wire it into their handler, e.g. slog.NewTextHandler(out, &slog.HandlerOptions{Level: termcore.Level}).
Functions ¶
func CellPixels ¶
CellPixels returns the terminal cell size in device pixels for f, or zeros when the terminal does not report it.
func Color ¶
Color reports whether ANSI color codes may be written to f. Precedence: noColor flag, NO_COLOR env, TERM=dumb, not-a-tty, CI.
Pass the --no-color flag value as noColor. Never read os.Stdout directly for this decision; always pass the actual destination file.
func ColorDepth ¶
func ColorDepth() int
ColorDepth reports what ANSI color tiers the terminal can paint: COLORTERM truecolor/24bit wins (case-insensitive), a real Windows console paints 24-bit once VT is enabled, then 256-color terms, else the 16-color base set. One detector feeds text dimming and image backends alike.
func ColorFor ¶
ColorFor reports whether ANSI may be written to w: false for --no-color, non-file writers (buffers, pipes without files), dumb terminals, and CI; true only for capable terminal files.
func Emoji ¶
func Emoji() bool
Emoji reports whether emoji glyphs may be used. False on dumb terminals, in CI, and on legacy Windows consoles (conhost without Windows Terminal, detected via missing WT_SESSION), where raster fonts cannot render them and they break column layout.
func EnsureConsole ¶
func EnsureConsole() error
EnsureConsole prepares the console for ANSI output. No-op on Unix.
func EscapeLen ¶
EscapeLen returns the byte length of the ANSI escape sequence beginning at index i, or 0 when no escape starts there (i out of range or not an ESC byte). It covers CSI, OSC (BEL- or ST-terminated), and two-byte escapes, so walkers can skip sequences while measuring or slicing styled text.
func ExitCodeOf ¶
ExitCodeOf maps any error to a process exit code: the carried code for ExitCodeError (even when wrapped), 130 for a cancelled context (Ctrl+C), 1 for anything else, 0 for nil.
func FullVersion ¶
func FullVersion() string
FullVersion returns "Version-Commit" for --version output.
func IsTerminal ¶
IsTerminal reports whether f is an interactive terminal. Pipes, files, and CI log collectors are not terminals.
func ReadPassword ¶
ReadPassword reads a line from the terminal without echo. Callers keep the prompt and cancellation policy; this only owns the platform read.
func RuneWidth ¶
RuneWidth returns the terminal column width of one rune: 2 for CJK wide runes, 0 for combining marks, 1 otherwise. Wrappers use it to never split a wide rune across lines.
func ShellSplit ¶
ShellSplit splits a command line into arguments using POSIX shell rules: quoting, escapes, and comments. It is the one shell-splitting implementation in the tree, shared by pager parsing and command output filtering.
func SignalContext ¶
SignalContext returns a context cancelled on SIGINT (Ctrl+C) or SIGTERM. Callers must defer the returned cancel func. Works on Linux, macOS, and Windows (Go maps console Ctrl+C to SIGINT on all three).
func Stdout ¶
Stdout returns a writer for standard output that translates ANSI codes on legacy Windows consoles and passes through everywhere else. Always write through this, never os.Stdout directly.
func TerminalHeight ¶
TerminalHeight returns console rows for f, or -1 when unknown (pipes, files, and dumb terminals). Callers treat -1 as "draw unbounded".
func TerminalQuery ¶
TerminalQuery writes query to f, switches f to raw mode, and returns the reply up to terminator, timeout, or error. Detection callers use it for capability probes so no other package touches terminal modes. Returns nil when f is not a terminal or the probe fails.
func TerminalWidth ¶
TerminalWidth returns console columns for f, or DefaultTerminalWidth when unknown. Never returns zero or negative.
func Truncate ¶
Truncate shortens s to at most max columns, appending "..." when cut. A max below 4 still returns a valid (shorter) string, never panics.
func VisibleWidth ¶
VisibleWidth returns the terminal column width of s ignoring ANSI escape sequences (CSI colors, OSC hyperlinks): styled text measures by its glyphs, so it embeds in boxes and tables without skewing alignment.
Types ¶
type ExitCodeError ¶
type ExitCodeError struct {
// contains filtered or unexported fields
}
ExitCodeError carries a process exit code alongside the cause. Main functions map any returned error via ExitCodeOf and os.Exit.
func NewExitCodeError ¶
func NewExitCodeError(code int, err error) ExitCodeError
NewExitCodeError builds an ExitCodeError with the given code and cause.
func (ExitCodeError) ExitCode ¶
func (e ExitCodeError) ExitCode() int
ExitCode returns the process exit code.
func (ExitCodeError) Unwrap ¶
func (e ExitCodeError) Unwrap() error
Unwrap returns the cause so errors.Is/As see through it.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
isatty
Package isatty implements interface to isatty
|
Package isatty implements interface to isatty |
|
shlex
Package shlex implements a simple lexer which splits input in to tokens using shell-style rules for quoting and commenting.
|
Package shlex implements a simple lexer which splits input in to tokens using shell-style rules for quoting and commenting. |
|
uax29/graphemes
Package graphemes implements Unicode grapheme cluster boundaries: https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries
|
Package graphemes implements Unicode grapheme cluster boundaries: https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries |