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
- Variables
- func CleanText(s string) string
- func ConsoleVT(int) (ok bool, restore func())
- func DataTable(headers []string, rows [][]string) string
- func DisplayWidth(s string) int
- func DropEarlyInput(fd int) bool
- func HumanBytes(n int64) string
- func Interrupted() *out.Error
- func JoinAnd(names []string) string
- func LayoutTable(headers []string, rows [][]string) string
- func NoneMatch(things, filter string, note ...string) string
- func NoneYet(things, add string, note ...string) string
- func Plural(n int) string
- func Raise(s os.Signal)
- func ReadAnswer(in io.Reader, errw io.Writer, interrupted *bool, label string, hidden bool) (string, error)
- func SignalContext(parent context.Context, onSecond func(os.Signal)) (context.Context, context.CancelFunc)
- func StripPasteMarks(s string) string
- func TimeCell(t time.Time) string
- func TruncateWidth(s string, w int) string
- func TypedAhead(fd int) int
- func YesCell(b bool) string
- type Key
- type KeyKind
- type Screen
- type ScreenOptions
Constants ¶
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.
const FieldMax = 4096
FieldMax is the most bytes one field or one paste takes: the read buffer size of today's questions.
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 ¶
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") )
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.
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 ¶
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 DataTable ¶
DataTable is LayoutTable with upper-case headers and "-" for empty cells, like the other komando lists.
func DisplayWidth ¶
DisplayWidth is how many terminal columns s takes.
func DropEarlyInput ¶
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 Interrupted ¶
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 LayoutTable ¶
LayoutTable aligns rows under headers, two spaces apart, as they are (no upper-casing, no placeholders for empty cells).
func NoneYet ¶
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 Raise ¶
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 ¶
StripPasteMarks removes the bracketed-paste marks a terminal puts around pasted text when a program before komando left that mode on.
func TimeCell ¶
TimeCell is a moment: this computer's time with its zone (2026-11-07 14:30 WIB), "-" for none.
func TruncateWidth ¶
TruncateWidth cuts s to at most w columns, ending in "…" (one column) when cut.
func TypedAhead ¶
TypedAhead is how many bytes wait unread on the terminal fd.
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 ¶
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 ¶
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.
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.