termfmt

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 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 termfmt writes text for a terminal: whether to color it, the styles and links, and the width of what it prints. Width is measured on plain text; styles are applied after, so no escape reaches a measure.

Index

Constants

View Source
const Ellipsis = "…"

Variables

This section is empty.

Functions

func Cells

func Cells(s string) int

Cells is the width of s on a terminal.

func Clean

func Clean(s string) string

Clean keeps text to what a terminal prints in place. Control characters become spaces. Bidi controls, line and paragraph separators and invisible format characters, which a title could use to reorder or fake the line, are removed; the zero-width joiner and non-joiner stay, as emoji and scripts such as Persian need them.

func Cut

func Cut(s string, w int) string

Cut cleans s and cuts it to w cells, marking a cut with an ellipsis.

func Fit

func Fit(s string, w int) string

Fit cuts s to w cells and pads it to w.

func FitRight

func FitRight(s string, w int) string

FitRight is Fit aligned to the right.

func Joiner

func Joiner(r rune) bool

Joiner says whether r is the zero-width joiner or non-joiner.

func OneLine

func OneLine(s string, max int) string

OneLine makes text from an agent or a CI fit one line of a row or a notification: control characters become spaces, bidi and zero-width characters go, whitespace collapses, and it keeps at most max runes.

func Plain

func Plain(s string) string

Plain is s without the styles and links of this package.

func Prefix

func Prefix(s string, w int) string

Prefix is the longest start of s, in whole clusters, within w cells.

func Suffix

func Suffix(s string, w int) string

Suffix is the longest end of s, in whole clusters, within w cells.

func UseWidths

func UseWidths(c *runewidth.Condition) (restore func())

UseWidths measures with c until the returned function restores the locale's measure; tests use it to measure as a CJK terminal does.

func Wrap

func Wrap(s string, first, rest int) []string

Wrap breaks s at spaces into lines of at most first cells, then rest cells, for a caller that hangs the later lines under the first; a word wider than a line is cut across lines.

Types

type Cell

type Cell struct {
	Text  string
	Style func(string) string
}

Cell is one value of a row and the style it is printed in.

type Column

type Column struct {
	Right, Flex bool
	Max         int
}

Column lays out one column: Right aligns it to the right, Max caps its width, and the Flex column takes what the others leave of the width.

type Mode

type Mode int

Mode is the --color choice.

const (
	Auto Mode = iota
	Always
	Never
)

func (*Mode) Set

func (m *Mode) Set(s string) error

Set parses auto, always or never, so a Mode is a flag.Value.

func (Mode) String

func (m Mode) String() string

type Term

type Term struct {
	TTY   bool
	Color bool
	Width int
}

Term is how to write to one output: TTY lays it out for a person, Color styles it, Width is its columns.

func Detect

func Detect(w io.Writer, getenv func(string) string, mode Mode) Term

Detect decides for w: a terminal gets the layout and color; GH_FORCE_TTY and --color always force both; NO_COLOR, TERM=dumb and CLICOLOR=0 take the color away and CLICOLOR_FORCE gives it back, never the layout, unless NO_COLOR is set; mode overrides the color.

func (Term) Bold

func (t Term) Bold(s string) string

func (Term) Columns

func (t Term) Columns(width, gap int, cols []Column, rows [][]Cell) []string

Columns lays rows out gap spaces apart within width cells: each column as wide as its widest value, the flexible one cut to the room left. Cells are measured and cut plain, then styled.

func (Term) Cyan

func (t Term) Cyan(s string) string

func (Term) Dim

func (t Term) Dim(s string) string

func (Term) Green

func (t Term) Green(s string) string
func (t Term) Link(url, text string) string

Link makes text an OSC 8 hyperlink to url; a terminal that does not know it prints the text.

func (Term) Red

func (t Term) Red(s string) string

func (Term) Reverse

func (t Term) Reverse(s string) string

Reverse swaps the foreground and background, as a selected line is drawn.

func (Term) Yellow

func (t Term) Yellow(s string) string

Jump to

Keyboard shortcuts

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