termcore

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	Depth16   = 16
	Depth256  = 256
	DepthTrue = 1 << 24
)

Color depth tiers reported by ColorDepth.

View Source
const DefaultTerminalWidth = 80

DefaultTerminalWidth is used when detection fails (pipes, files, dumb terminals): the classic 80 columns.

Variables

View Source
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".

View Source
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

func CellPixels(f *os.File) (int, int)

CellPixels returns the terminal cell size in device pixels for f, or zeros when the terminal does not report it.

func Color

func Color(f *os.File, noColor bool) bool

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

func ColorFor(w io.Writer, noColor bool) bool

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

func EscapeLen(s string, i int) int

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

func ExitCodeOf(err error) int

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

func IsTerminal(f *os.File) bool

IsTerminal reports whether f is an interactive terminal. Pipes, files, and CI log collectors are not terminals.

func ReadPassword

func ReadPassword(fd int) ([]byte, error)

ReadPassword reads a line from the terminal without echo. Callers keep the prompt and cancellation policy; this only owns the platform read.

func RuneWidth

func RuneWidth(r rune) int

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 SetDebug

func SetDebug(debug bool)

SetDebug enables debug logging when debug is true.

func ShellSplit

func ShellSplit(s string) ([]string, error)

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

func SignalContext(parent context.Context) (context.Context, context.CancelFunc)

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 Stderr

func Stderr() io.Writer

Stderr returns a writer for standard error with the same guarantee as Stdout.

func Stdout

func Stdout() io.Writer

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

func TerminalHeight(f *os.File) int

TerminalHeight returns console rows for f, or -1 when unknown (pipes, files, and dumb terminals). Callers treat -1 as "draw unbounded".

func TerminalQuery

func TerminalQuery(f *os.File, query []byte, timeout time.Duration, terminator byte) []byte

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

func TerminalWidth(f *os.File) int

TerminalWidth returns console columns for f, or DefaultTerminalWidth when unknown. Never returns zero or negative.

func Truncate

func Truncate(s string, max int) string

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

func VisibleWidth(s string) int

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.

func Width

func Width(s string) int

Width returns the terminal column width of s. East-Asian wide runes count as 2, so CJK text never breaks tables.

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) Error

func (e ExitCodeError) Error() string

Error returns the cause message.

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.

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

Jump to

Keyboard shortcuts

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