term

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: AGPL-3.0 Imports: 17 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 term holds what every command prints to a terminal: how wide text is in columns, text tables and the line an empty list says. It needs no cobra and no komando state, so commands and their tests share it.

Index

Constants

View Source
const EscWait = 50 * time.Millisecond

EscWait is how long a lone ESC waits for the rest of a sequence before it counts as the Esc key. A terminal sends a whole sequence at once, so this is far longer than one takes, and short enough not to be felt.

View Source
const FieldMax = 4096

FieldMax is the most bytes one field or one paste takes: the read buffer size of today's questions.

View Source
const PasteWait = time.Second

PasteWait is how long a paste that has not ended waits for more of it (a long paste over a slow link comes in parts); after that it ends.

Variables

View Source
var (
	// ErrNoScreen: this terminal cannot carry the screen; ask in lines.
	ErrNoScreen = errors.New("this terminal cannot show the interactive screen")
	// ErrTooSmall: the window is smaller than the screen needs; ask in lines.
	ErrTooSmall = errors.New("the window is too small for the interactive screen")
	// ErrIdle: nobody pressed a key for ScreenOptions.Idle.
	ErrIdle = errors.New("no key was pressed for a long time")
)
View Source
var ErrMoreLines = errors.New("more than one line")

ErrMoreLines: more input followed a hidden answer, as a paste of several lines leaves it. ReadAnswer threw it away.

View Source
var VTOK = true

VTOK: the terminal on stderr reads escape sequences (colour, clearing a line). Only an old Windows console does not.

Functions

func CleanText

func CleanText(s string) string

CleanText drops control and bidi characters, so a name or address cannot move the cursor, retitle the window or reorder what the line says.

func ConsoleVT

func ConsoleVT(int) (ok bool, restore func())

ConsoleVT: a Unix terminal reads escape sequences as they are.

func DataTable

func DataTable(headers []string, rows [][]string) string

DataTable is LayoutTable with upper-case headers and "-" for empty cells, like the other komando lists.

func DisplayWidth

func DisplayWidth(s string) int

DisplayWidth is how many terminal columns s takes.

func DropEarlyInput

func DropEarlyInput(fd int) bool

DropEarlyInput throws away what was typed before the prompt, and says whether there was any. A terminal in line mode counts only finished lines as waiting, so it looks with line mode (and echo) off for a moment.

func HumanBytes

func HumanBytes(n int64) string

func Interrupted

func Interrupted() *out.Error

Interrupted is the CANCELLED stop ("interrupted") of a command or a question that Ctrl-C or a signal ended. Each call builds a new error, so a caller may change the one it gets.

func JoinAnd

func JoinAnd(names []string) string

JoinAnd lists names for a sentence: "a", "a and b", "a, b and c".

func LayoutTable

func LayoutTable(headers []string, rows [][]string) string

LayoutTable aligns rows under headers, two spaces apart, as they are (no upper-casing, no placeholders for empty cells).

func NoneMatch

func NoneMatch(things, filter string, note ...string) string

NoneMatch is the line for a list a filter left empty.

func NoneYet

func NoneYet(things, add string, note ...string) string

NoneYet is the line for an empty list: add is the command that adds one ("" for a list komando does not fill), note an aside in brackets.

func Plural

func Plural(n int) string

func Raise

func Raise(s os.Signal)

Raise sends s to this process. The caller resets its handler for s first (signal.Reset), so the process ends of it, as it would have without a handler.

func ReadAnswer

func ReadAnswer(in io.Reader, errw io.Writer, interrupted *bool, label string, hidden bool) (string, error)

ReadAnswer prints label on errw and reads one line from in, without echo when hidden is set.

komando catches Ctrl-C (SIGINT) for the whole command, so a read that does not watch for it would just go on waiting. ReadAnswer listens for SIGINT while it reads; on Ctrl-C it puts the terminal back as it was (echo on again after a hidden read), ends the line and returns Interrupted. Ctrl-D (end of input) where the answer shows is an empty answer; the line is ended there too, as the terminal does not. (x/term's ReadPassword takes no notice of Ctrl-D.)

func SignalContext

func SignalContext(parent context.Context, onSecond func(os.Signal)) (context.Context, context.CancelFunc)

SignalContext is cancelled by the first Ctrl-C (or SIGTERM, or SIGHUP: the terminal closed), so the command stops cleanly. A second one calls onSecond with that signal, which ends the process at once, as with kubectl and gh, when the clean stop hangs. A nil onSecond still ends it: the process dies of the signal (exit 130 where a signal cannot end it). A SIGHUP that was ignored when the process started (nohup, a background holder) stays ignored.

func StripPasteMarks

func StripPasteMarks(s string) string

StripPasteMarks removes the bracketed-paste marks a terminal puts around pasted text when a program before komando left that mode on.

func TimeCell

func TimeCell(t time.Time) string

TimeCell is a moment: this computer's time with its zone (2026-11-07 14:30 WIB), "-" for none.

func TruncateWidth

func TruncateWidth(s string, w int) string

TruncateWidth cuts s to at most w columns, ending in "…" (one column) when cut.

func TypedAhead

func TypedAhead(fd int) int

TypedAhead is how many bytes wait unread on the terminal fd.

func YesCell

func YesCell(b bool) string

YesCell is a boolean: "yes", or "-" so the few that say yes stand out.

Types

type Key

type Key struct {
	Kind KeyKind
	Rune rune   // for KeyRune
	Text string // for KeyPaste: printable text; line breaks kept as \n
	// TooLong: the paste was longer than FieldMax; Text is empty and
	// nothing of it is used
	TooLong bool
}

Key is one key a person pressed, or one paste.

type KeyKind

type KeyKind uint8

KeyKind is what a key does.

const (
	KeyNone      KeyKind = iota // nothing to act on (input that was dropped)
	KeyRune                     // a typed character, in Key.Rune
	KeyEnter                    // Enter (CR or LF)
	KeyEsc                      // Esc on its own
	KeyUp                       // arrow up
	KeyDown                     // arrow down
	KeyLeft                     // arrow left
	KeyRight                    // arrow right
	KeyHome                     // Home
	KeyEnd                      // End
	KeyPageUp                   // Page Up
	KeyPageDown                 // Page Down
	KeyTab                      // Tab
	KeyBackTab                  // Shift-Tab
	KeyBackspace                // Backspace
	KeyDelete                   // Delete
	KeyClear                    // Ctrl-U: clear the field
	KeyInterrupt                // Ctrl-C: stop, nothing runs
	KeyEOF                      // Ctrl-D
	KeySuspend                  // Ctrl-Z
	KeyPaste                    // a bracketed paste, in Key.Text
	KeyResize                   // the window changed size and the screen still fits
)

type Screen

type Screen struct {
	// contains filtered or unexported fields
}

Screen is a terminal in raw mode for the interactive screen.

func OpenScreen

func OpenScreen(in *os.File, out io.Writer, opt ScreenOptions) (*Screen, error)

OpenScreen puts in into raw mode for the screen and turns bracketed paste on, drawing on out. ErrNoScreen says this terminal cannot carry it, ErrTooSmall that the window is smaller than opt asks; both leave the terminal as it was.

func (*Screen) ReadKey

func (s *Screen) ReadKey() (Key, error)

ReadKey waits for the next key. A window that changed size and still fits gives KeyResize. Any error restores the terminal first: ErrIdle, ErrTooSmall, io.EOF (the terminal closed) or a read error.

func (*Screen) Restore

func (s *Screen) Restore()

Restore puts the terminal back as it was before OpenScreen: bracketed paste off, colours reset, cursor shown, the saved mode back, the signal handlers gone. It does nothing the second time.

func (*Screen) Size

func (s *Screen) Size() (w, h int)

Size is the window's width and height; 0, 0 when it cannot be read.

func (*Screen) Suspend

func (s *Screen) Suspend() error

Suspend is Ctrl-Z: the terminal goes back to how it was, the process stops as a shell expects (SIGTSTP), and when it goes on (fg) raw mode and bracketed paste come back. The caller draws the screen again.

func (*Screen) Write

func (s *Screen) Write(p []byte) (int, error)

Write draws on the screen's output.

type ScreenOptions

type ScreenOptions struct {
	MinWidth, MinHeight int           // a smaller window ends the screen (ErrTooSmall)
	Idle                time.Duration // no key for this long ends it (ErrIdle); 0 means never
}

ScreenOptions are the limits of a screen.

Jump to

Keyboard shortcuts

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