term

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 11 Imported by: 3 modules, 3 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 term answers the questions every command-line tool asks about its terminal: whether a stream is a terminal at all, how large it is, whether it may be painted in color, and how wide a piece of text is once it is drawn there.

Color is decided once per process. SetColorMode records what the user asked for (usually through a --color flag), and Colored combines that with the environment (NO_COLOR, TERM=dumb) and the stream itself. Every package in this module asks Colored before it writes an escape sequence, so output headed for a pipe, a file or a test buffer stays plain.

Index

Constants

View Source
const Ellipsis = "…"

Ellipsis marks where Truncate and TruncateMiddle cut text.

Variables

View Source
var ColorModeNames = []string{"auto", "always", "never"}

ColorModeNames are the spellings ParseColorMode accepts, in the order a help text or shell completion lists them.

View Source
var ErrNoTTY = errors.New("term: no controlling terminal")

ErrNoTTY is returned by OpenTTY where no controlling terminal can be opened apart from the standard streams.

Functions

func CanAnimate

func CanAnimate(w io.Writer) bool

CanAnimate reports whether w may be redrawn in place: it is a terminal, the environment permits color, and the user did not ask for --color=never.

func ColorAllowed

func ColorAllowed() bool

ColorAllowed reports whether the environment permits color: NO_COLOR is unset (any value, even empty, turns color off, see https://no-color.org) and TERM is not "dumb".

func Colored

func Colored(w io.Writer) bool

Colored reports whether output written to w is painted under the mode SetColorMode recorded.

func Columns

func Columns(v any, fallback int) int

Columns returns the width of the terminal v is, or wraps, or fallback when it is no terminal or its size cannot be read.

func EscapeLen

func EscapeLen(s string) int

EscapeLen returns the length in bytes of the escape sequence s starts with, or 0 when s does not start with one. It recognizes CSI sequences (ESC [ … final byte), OSC, DCS, APC and PM strings (ended by BEL or ESC \), and two-byte escapes. An unterminated sequence runs to the end of s, so a cut-off sequence is never counted as visible text. Code that walks styled text uses it to copy escape sequences through whole.

func Hyperlink(url, text string) string

Hyperlink wraps text in an OSC 8 hyperlink to url. Terminals that support OSC 8 make text clickable; all others print text alone.

func Interactive

func Interactive(in, out *os.File) bool

Interactive reports whether both in and out are terminals, which is what a prompt needs before it can ask anything.

func IsTerminal

func IsTerminal(v any) bool

IsTerminal reports whether v is, or wraps, a file that is a terminal. v is usually an io.Writer or an io.Reader; a writer wraps another by having an Unwrap() io.Writer method.

func Linkify

func Linkify(s string) string

Linkify wraps every http and https URL in s in an OSC 8 hyperlink.

func Links(w io.Writer) io.Writer

Links returns a writer that hyperlinks the URLs written through it when w is a terminal that may be painted, and w itself otherwise. A URL split across two writes is not recognized, so write whole lines.

func OpenTTY

func OpenTTY() (*os.File, error)

OpenTTY opens the controlling terminal for reading and writing. It works even when stdin and stdout are redirected, which lets a command that reads its input from a pipe still ask the user a question. The caller closes the file.

func PadLeft

func PadLeft(s string, width int) string

PadLeft pads s with leading spaces to width visible cells, right-aligning it. Text already that wide or wider is returned unchanged.

func PadRight

func PadRight(s string, width int) string

PadRight pads s with spaces to width visible cells. Text already that wide or wider is returned unchanged.

func ScreenRows

func ScreenRows(lines []string, width int) int

ScreenRows returns how many terminal rows lines take up on a terminal width cells wide. It follows the cursor the way a terminal does: a line wider than the terminal wraps onto further rows, and a wide character that would straddle the right edge starts the next row, leaving the last cell empty. A line exactly as wide as the terminal takes one row. A width of 0 or less counts one row per line. Redrawing a block in place depends on this count being exact.

func SetColorMode

func SetColorMode(m ColorMode)

SetColorMode records the color choice for the whole process. Call it once, after parsing flags and before writing output. A value other than the three defined modes records ColorAuto.

func ShouldColor

func ShouldColor(m ColorMode, w io.Writer) bool

ShouldColor reports whether output written to w is painted under mode m: always under ColorAlways, never under ColorNever, and under ColorAuto only when w is a terminal and ColorAllowed holds.

func Size

func Size(v any) (width, height int)

Size returns the width and height of the terminal v is, or wraps, in cells. It returns 0, 0 when v is no terminal or its size cannot be read.

func StripANSI

func StripANSI(s string) string

StripANSI removes terminal escape sequences from s: CSI sequences such as colors and cursor movement (ESC [ … final byte), OSC sequences such as hyperlinks (ESC ] … BEL or ESC \), and two-byte escapes. Everything else, multi-byte runes included, is left untouched.

func Truncate

func Truncate(s string, width int) string

Truncate cuts s down to width visible cells, ending it with Ellipsis when anything was cut. Escape sequences are kept, and a reset is appended after a cut so a color never bleeds into what follows. A width of 0 or less, what Size reports for anything but a terminal, leaves s whole.

func TruncateMiddle

func TruncateMiddle(s string, width int) string

TruncateMiddle cuts s down to width visible cells by taking text out of its middle, so both ends stay readable: "feature/very-long-branch-name" cut to 21 cells becomes "feature/ve…ranch-name". Escape sequences are dropped. A width of 0 or less leaves s whole.

func VisibleWidth

func VisibleWidth(s string) int

VisibleWidth returns how many terminal cells s takes up once drawn, escape sequences left out. Wide runes (CJK, most emoji) count two cells, combining marks none. For text spanning several lines it measures the widest line.

Types

type ColorMode

type ColorMode int

ColorMode is what the user asked for about color, usually through a --color flag.

const (
	// ColorAuto paints output written to a terminal and leaves everything
	// else plain. It honors NO_COLOR and TERM=dumb. It is the default.
	ColorAuto ColorMode = iota
	// ColorAlways paints output wherever it goes, even into a pipe.
	ColorAlways
	// ColorNever never paints output.
	ColorNever
)

func CurrentColorMode

func CurrentColorMode() ColorMode

CurrentColorMode returns the mode SetColorMode last recorded.

func ParseColorMode

func ParseColorMode(value string) (ColorMode, error)

ParseColorMode reads a --color value. Besides auto, always and never it takes the spellings other tools use for the same three choices: an empty value or "true" means always (a bare --color), "false" and "none" mean never, and "tty" and "if-tty" mean auto.

func (ColorMode) String

func (m ColorMode) String() string

String returns the flag spelling of m.

Jump to

Keyboard shortcuts

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