terminal

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: BSD-3-Clause Imports: 22 Imported by: 3 modules, 19 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.

README

terminal

Direct ANSI terminal control for Go with zero-allocation rendering. Built for sustained 60fps full-screen redraws in cell-based applications (games, dashboards, TUIs). Depends on github.com/lixenwraith/color (RGB, palettes, blending), golang.org/x/sys and golang.org/x/term.

The package bypasses terminfo/termcap entirely and emits ANSI sequences directly. Target environments: xterm-compatible terminals on Linux and BSDs, Windows consoles with VT processing, and browsers via xterm.js (WASM builds).

Features

  • True color (24-bit), 256-color, ANSI 16-color and colorless output with automatic capability detection, and the terminal's own default colors
  • Double-buffered output with cell-level diffing — only changed cells emit sequences
  • Raw stdin parsing: keys, modifiers, UTF-8 runes, SGR mouse, resize, bracketed paste
  • Draws on the controlling terminal when stdin or stdout is redirected (Unix)
  • Perceptual (Redmean) RGB → 256-palette mapping via O(1) LUT, from color
  • Panic-safe terminal restoration (Fini, EmergencyReset)
  • Unix, Windows and WASM backends behind a common interface

Architecture

Terminal (interface)
  └── termImpl
        ├── outputBuffer   diffing, ANSI generation
        ├── writeLoop      the one goroutine that writes output
        ├── inputReader    escape sequence parser, event channel
        └── Backend (interface)
              ├── unixBackend     //go:build unix — termios, unix.Poll, SIGWINCH, /dev/tty
              ├── windowsBackend  //go:build windows — console modes, VT processing
              └── wasmBackend     //go:build wasm — syscall/js, xterm.js bridge

Shared code carries no build tags: cell diffing, ANSI generation, escape parsing. Platform specifics are isolated in the Backend implementations.

NewWithBackend(b, mode) builds a Terminal over a Backend of the caller's own: a pipe, an in-process emulator or a test standing in for the tty. It behaves as New's Terminal does, the writer goroutine included; New is NewWithBackend over the platform's backend and the detected color mode.

Rendering pipeline

The application owns a flat []Cell buffer (row-major, cells[y*width+x]) and passes it to Flush. The output buffer diffs against the previously flushed frame:

  • Rows are scanned with early termination (trailing unchanged cells skipped).
  • Cursor moves are emitted only when the write position is non-contiguous.
  • SGR state (fg, bg, attributes) is coalesced across cells; redundant sequences are suppressed.
  • If the backend size changed between buffer preparation and Flush, the frame is dropped to prevent resize-race corruption. The next frame (built at the new size) renders normally.
  • A cell is one glyph in one column, on a terminal that draws ambiguous-width runes (box drawing, U+FFFD) narrow: a control or format character in Rune (a pasted escape, a bidi override, a line separator) is drawn as a space, and a rune a terminal draws in no column, in two or in its neighbour's cluster (a combining or spacing mark, a wide or emoji rune) as U+FFFD.

Sync() clears the screen and invalidates the front buffer, forcing a full redraw — required after any external process writes to the terminal.

Output is written on a goroutine of its own, so no call waits on the terminal. A terminal that stops reading (a hidden tab, a multiplexer client that went away, flow control) holds that goroutine in its write; meanwhile Flush keeps only the newest frame, drawn once the write returns, and control sequences (cursor, mouse, paste, Clear, Sync) queue behind it in call order. Blocked() reports how long the write in flight has waited, so a caller can stop composing frames nobody will see. Fini waits up to two seconds for the restoring sequences, then restores the termios without them.

Auto-wrap and, where the terminal follows ECMA-48 mode 8 (VTE), its own bidi reordering are off during the session: the bottom-right cell is writable without scroll side effects, and right-to-left text stays in its cells.

Quick start

package main

import (
    "github.com/lixenwraith/color"
    "github.com/lixenwraith/terminal"
)

func main() {
    term := terminal.New() // color mode auto-detected
    if err := term.Init(); err != nil {
        panic(err)
    }
    defer term.Fini()

    w, h := term.Size()
    cells := make([]terminal.Cell, w*h)

    for {
        // Build frame
        for i := range cells {
            cells[i] = terminal.Cell{Rune: ' ', Bg: color.Gunmetal}
        }
        msg := "hello"
        for i, ch := range msg {
            // len(msg) to utf8.RuneCountIdString(msg) for non-ASCII
            cells[(h/2)*w+(w-len(msg))/2+i] = terminal.Cell{
                Rune: ch, Fg: color.Amber, Bg: color.Gunmetal,
                Attrs: terminal.AttrBold,
            }
        }
        term.Flush(cells, w, h)

        // Handle input
        ev := term.PollEvent()
        switch ev.Type {
        case terminal.EventKey:
            if ev.Key == terminal.KeyEscape || ev.Rune == 'q' {
                return
            }
        case terminal.EventResize:
            w, h = ev.Width, ev.Height
            cells = make([]terminal.Cell, w*h)
        }
    }
}

Cells and attributes

type Cell struct {
    Rune  rune
    Fg    color.RGB
    Bg    color.RGB
    Attrs Attr
}

Attr is a bitmask: AttrBold, AttrDim, AttrItalic, AttrUnderline, AttrBlink, AttrReverse.

Attr is 16 bits wide; the bits above the styles say how to read a cell's colors:

  • With AttrFg256 / AttrBg256 set, Fg.R / Bg.R holds an xterm-256 palette index directly and G/B are ignored. This allows exact palette output on true color terminals and skips RGB → palette conversion. Indices 0-15 are the ANSI 16, which every terminal shows in its own theme's shades.
  • With AttrFgDefault / AttrBgDefault set, the color is the terminal's own (SGR 39/49), whatever Fg / Bg hold: an application keeps a light or dark terminal's background without knowing which it is.

Color system

Modes

ColorModeTrueColor emits 38;2;R;G;B sequences; ColorMode256 emits 38;5;N after mapping; ColorMode16 emits 30-37/90-97 and 40-47/100-107, an RGB or palette color mapped to the nearest ANSI index; ColorModeNone emits attributes only, so a highlight drawn as a background color alone (a list's CursorBg) does not show; the tui form controls mark every selection with a glyph.

DetectColorMode() reports what the terminal can show from the environment (COLORTERM, TERM): 16 colors on a text console (TERM of linux, cons25*, vt and a digit, ansi or *-16color), whatever COLORTERM says. In 16 colors every color change restates the style from a reset, since a console's bright colors (90-97) also set bold. New() also honours NO_COLOR, choosing ColorModeNone; a mode passed to New overrides both, as a --color=always or never would. Init refuses TERM=dumb, which cannot address the cursor.

Init and Sync clear the screen to the terminal's own background. EmergencyReset(os.Stdout) writes to the controlling terminal when stdout is redirected, so the reset never lands in the program's output.

RGB → 256 mapping

RGBTo256 maps any RGB to the nearest xterm-256 index using perceptually weighted Redmean distance. The full mapping is pre-computed at init into a 6-bit-quantized LUT (256KB, L2-resident), making per-cell conversion a single array load. Applications targeting 256-color terminals can render in RGB throughout; degradation is automatic.

Palette helpers: Cube256(r,g,b) / CubeRGB256(idx) for 6×6×6 cube math, Gray256(step) for the grayscale ramp, plus named constants (P256Amber, P256SteelBlue, ...) and named true color values (Amber, Gunmetal, Obsidian, ...), all in github.com/lixenwraith/color.

Blending

github.com/lixenwraith/color provides compositing primitives operating on RGB. All take destination first and are branch-free in the hot path or LUT-backed; suitable for per-cell use at frame rate.

  • Blend(dst, src, alpha): linear interpolation, standard transparency
  • Add(dst, src, alpha): saturating add, bright accumulation, clips
  • Screen(dst, src, alpha): 1-(1-d)(1-s), lightens, never clips
  • Overlay(dst, src, alpha): multiply/screen split at 0.5, contrast, keeps dst structure
  • SoftLight(dst, src, intensity): Perez soft light, gentle tint/glow
  • Max(dst, src, alpha): per-channel max, non-additive highlight
  • Scale(c, factor): channel multiply, dim/brighten
  • Grayscale(c): Rec. 601 luma, desaturation
  • c.Lerp(other, t): method on RGB, gradients, animation

alpha/intensity/t are [0,1]; out-of-range values clamp. alpha of 0 or 1 short-circuits without float math. All float→channel conversions round half-up, so gradients from Blend, Scale, Lerp, and SoftLight are bit-consistent.

bg := color.Gunmetal
glow := color.RGB{R: 255, G: 160, B: 40}

cell.Bg = color.Screen(bg, color.Scale(glow, pulse), 1.0) // pulsing glow
cell.Bg = color.Blend(cell.Bg, color.Black, 0.6)          // dim overlay backdrop
cell.Fg = color.SoftLight(cell.Fg, tint, 0.4)                // subtle recolor
bar := cold.Lerp(hot, load)                                     // value-mapped gradient

Integer paths (Add, Screen, Overlay) use a (x + (x>>8) + 1) >> 8 division approximation; SoftLight uses init-time LUTs replacing math.Sqrt.

Input

PollEvent() blocks on a unified channel. Event.Type values:

  • EventKey — Key for named keys (KeyEnter, KeyUp, KeyCtrlC, ...), Key == KeyRune with Rune set for printable input, Modifiers bitmask (ModShift, ModAlt, ModCtrl), Action (KeyPress, and KeyRepeat and KeyRelease while releases are reported; see below)
  • EventMouse — 0-indexed MouseX/Y, MouseBtn (buttons, wheel), MouseAction (press/release/move/drag), modifiers. Enable via SetMouseMode(MouseModeClick | MouseModeDrag); SGR protocol only.
  • EventResize — new Width/Height
  • EventPaste — Text holds a whole paste, as sent, control characters included. Enable via SetPasteMode(true) (bracketed paste); off, a paste arrives as keys, its newlines pressing Enter.
  • EventKeyReport — the outcome of a SetKeyReport ask: Report and Source
  • EventFocus — Focused, while releases come through the terminal's protocol
  • EventError, EventClosed

A standalone ESC press is disambiguated from escape sequences by a short input-idle timeout (one ~10ms poll cycle). ESC ESC is Alt+Escape alone, Escape before a paste, and Alt with the key that follows otherwise (rxvt's Alt+arrows). Partial UTF-8 and escape sequences at read boundaries are reassembled in a persistent buffer. With paste mode off, a paste marker is not a paste. A paste whose end marker never arrives ends once input pauses for a second, so what follows a paste that stalls that long arrives as keys.

On Unix, a redirected stdin or stdout is replaced by /dev/tty, so app > out.toml and cmd | app keep both as data while the application draws and reads keys on the terminal. PostEvent injects synthetic events (used for clean shutdown of blocked PollEvent).

Key releases

A terminal reports presses: a key held down arrives as more presses at the keyboard's repeat rate, and nothing says when it comes up. A game that needs holds asks for releases:

term.SetKeyReport(terminal.KeyReportReleases, terminal.KeySourceConsole|terminal.KeySourceProtocol)

The ask returns at once. An EventKeyReport follows with what the terminal reports now (Report) and where releases come from (Source), so a game learns the outcome of every ask and can fall back, to taps for one, where there are none. SetKeyReport(terminal.KeyReportPresses, 0) gives the source back. Until a game asks for releases, nothing is sent to the terminal or switched, and every key event is a press, as before.

While releases are reported:

  • Each key event carries its Action: KeyPress, KeyRepeat while held, and KeyRelease. A release carries what its press carried, paired by physical key, so letting go of Shift first still releases 'A', not 'a'.
  • A key event the terminal names no physical key for (text from an input method, a key its protocol leaves out) can't be held: its release follows at once.
  • Every hold ends. Focus leaving, the console switching away, a change of source and Fini release the keys held with Lost set, since their own releases won't arrive. A press of a key already held means its release was lost (a terminal's own mode can swallow one): the old hold ends, Lost, before the new press.

Releases come from the first of these sources a game allows that the terminal has:

  • KeySourceConsole: the kernel console's own keyboard, on the console the game runs on: a Linux VT (/dev/ttyN) or FreeBSD's vt(4) (/dev/ttyvN). While releases are reported the package switches the keyboard to raw keycodes, which the kernel allows without root (Linux's K_MEDIUMRAW on the game's controlling tty, FreeBSD's K_CODE on a console the game has open), and switches it back when the game goes back to presses, at Fini and in EmergencyReset. Each key types what the console types for it cooked, by the console's own keymap read at the switch. Switching to another console ends the holds. The console's mouse reports pause while the keyboard is raw, and a mouse paste then reads as keys. Never over ssh, where the keyboard is the far terminal's.

    On Linux that is loadkeys layouts, dead keys and compose, the locks and their lights, Alt+Fn and Alt+arrows switching consoles, and cursor keys with modifiers from Linux 7.1. Left out while raw: codes typed with Alt on the keypad, braille, Ctrl+Alt+Del, and Scroll Lock, which only lets stopped output go on.

    On FreeBSD it is kbdmap layouts and their accents, the locks and their lights, Alt+Fn and Print Screen switching consoles, codes typed with left Alt on the keypad, and Alt as ESC before a character (right Alt too with kern.vt.enable_altgr=0). Left out while raw: the machine's keys (Ctrl+Alt+Del, the debugger, suspend), Shift+Insert's paste, and Scroll Lock. With Scroll Lock on, the console's history mode takes every key, so the keyboard stays as it is and the ask reports presses; an ask after Scroll Lock goes off tries again. syscons (kern.vty=sc) has no console source.

    Before the first switch the package runs the game's binary again as console-guard, which switches the keyboard back, restores the tty and shows the cursor if the game dies where nothing of its own runs: kill -9, the OOM killer, a Go fatal error. A game leading its console's session (on Linux, run with exec from the login shell or by an autologin) takes the console with it as it dies; the guard then opens it again to restore it. The package's init turns the guard's run aside before main; the inits of packages initialized before it run in the guard too, so keep side effects out of them. On FreeBSD the guard goes by the game's name, so pkill -9 by that name ends it too: kill the game by its PID. Should the guard be killed as well, switch the keyboard back from elsewhere: on Linux sudo kbd_mode -u -C /dev/ttyN; FreeBSD has no command for it, so build one:

    printf '#include <sys/ioctl.h>\n#include <sys/kbio.h>\nint main(void) { return ioctl(0, KDSKBMODE, K_XLATE); }\n' |
        cc -x c -o /tmp/kbdxlate - && /tmp/kbdxlate < /dev/ttyvN
    
  • KeySourceProtocol: the terminal's key event protocol, CSI u with event types. The package asks the terminal whether it has the protocol (CSI ? u, fenced by a DA1 request every terminal answers), pushes its flags and confirms what they came to before reporting releases; it goes by the terminal's answers, never by TERM or the terminal's name. Alacritty 0.13 and later is the reference (Enter, Tab and Backspace release from 0.15). Other terminals that answer the same way (foot, Ghostty, WezTerm with its opt-in setting, recent Konsole) should work but are untested. Works over ssh. tmux, screen and mosh don't pass the protocol through, and VTE terminals and xterm don't have it; there the answer is presses after one round trip. The flags are popped when the game goes back to presses, at Fini, and in EmergencyReset.

    With the protocol on, every key arrives as an escape code, and each press reads as the key a legacy terminal sends for it reads, so a game can keep the protocol on for its whole run: Shift goes into the character, Ctrl with a character becomes its control key, and Alt stays a modifier. Ctrl with a character that has no control key (Ctrl+1) keeps ModCtrl, which a legacy terminal can't send. Escape no longer waits out the ESC timeout. Lone modifier keys, media keys and F13 and up report nothing. Alacritty reports no keys while its own Vi mode, search or hints run; a key held then ends on its next press, or as focus leaves.

KeyHolds is the pairing on its own, for a host that reads its platform's key events and builds Events itself (KeySourceHost): it gives each release its press's key and turns the contradictions above into Lost releases.

A Terminal over a caller's backend (NewWithBackend) reports presses.

WASM

WASM builds bridge to xterm.js via JS globals:

goTerminalWrite(Uint8Array)    // Go → JS terminal output
goTerminalInput(Uint8Array)    // JS → Go keyboard input
goTerminalResize(cols, rows)   // JS → Go resize
xterm.cols, xterm.rows         // initial size query

Sub-packages

  • tui — immediate-mode widget toolkit (regions, layout, widgets, scroll/editor state) built on the cell buffer model.

Documentation

Overview

Package terminal provides direct ANSI terminal control with zero-alloc rendering.

Features

  • True color (24-bit), 256-color, ANSI 16-color and colorless output, and the terminal's own default colors
  • Double-buffered output with cell-level diffing
  • Raw input parsing with escape sequence handling and bracketed paste
  • Key releases on request, from the Linux or FreeBSD console's keyboard or the terminal's key event protocol (Alacritty's), and presses where there are neither
  • Drawing on /dev/tty when stdin or stdout is redirected (Unix)
  • Resize detection (SIGWINCH on Unix, callback on WASM)
  • Clean terminal restoration on exit/panic

Platform Support

The package uses build tags to separate platform-specific code:

  • unix: Native terminal via termios, unix.Poll, SIGWINCH
  • wasm: Browser terminal via xterm.js JavaScript bridge

Architecture

The Backend interface abstracts platform-specific operations:

Backend (interface)
├── unixBackend  (//go:build unix)  - termios, raw I/O, signals
└── wasmBackend  (//go:build wasm)  - syscall/js, callbacks

Shared code (no build tags): Terminal interface, cell diffing, ANSI generation, escape sequence parsing.

WASM Integration

WASM builds require JavaScript glue exposing these globals:

goTerminalWrite(Uint8Array)     // Go → JS: terminal output
goTerminalInput(Uint8Array)     // JS → Go: keyboard input
goTerminalResize(cols, rows)    // JS → Go: terminal resize
xterm.cols, xterm.rows          // Initial size query

Performance

Output uses cell-level diffing. Only changed cells generate ANSI sequences. Style attributes are coalesced to minimize SGR calls. One goroutine writes what is rendered, so a terminal that stops reading never holds the caller: Flush keeps the newest frame until the write in flight returns, and Blocked reports how long it has waited. Input parsing is zero-allocation for common cases.

This package bypasses terminfo/termcap entirely, emitting direct ANSI sequences. Target environments: Linux, macOS, BSDs with xterm-compatible terminals, and modern browsers with xterm.js.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmergencyReset

func EmergencyReset(w io.Writer)

EmergencyReset attempts to restore terminal to sane state Call this from panic recovery if Fini() cannot be called normally. Given a redirected stdout, it writes to the controlling terminal instead.

func KeyName

func KeyName(k Key) string

KeyName returns the canonical string name for a Key constant Returns empty string for KeyNone and KeyRune

func RGBTo256

func RGBTo256(c color.RGB) uint8

RGBTo256 delegates to the color package's perceptual quantizer. Exists to preserve terminal API backwards-compatibility.

func WarmPalette256

func WarmPalette256()

WarmPalette256 delegates to the color package's lazily evaluated 256-color LUT builder. Exists to preserve terminal API backwards-compatibility.

func WindowSize

func WindowSize(f *os.File) (w, h int, ok bool)

WindowSize queries terminal dimensions for f without raw mode or Terminal lifecycle. ok=false when f is not a terminal.

Types

type Attr

type Attr uint16

Attr represents text attributes and how to read a cell's colors (bitmask)

const (
	AttrNone      Attr = 0
	AttrBold      Attr = 1 << 0
	AttrDim       Attr = 1 << 1
	AttrItalic    Attr = 1 << 2
	AttrUnderline Attr = 1 << 3
	AttrBlink     Attr = 1 << 4
	AttrReverse   Attr = 1 << 5
	AttrFg256     Attr = 1 << 6 // Fg.R is 256-color palette index
	AttrBg256     Attr = 1 << 7 // Bg.R is 256-color palette index
	AttrFgDefault Attr = 1 << 8 // Fg is the terminal's own (SGR 39); Fg is ignored
	AttrBgDefault Attr = 1 << 9 // Bg is the terminal's own (SGR 49); Bg is ignored
)
const (
	AttrFgColor Attr = AttrFg256 | AttrFgDefault
	AttrBgColor Attr = AttrBg256 | AttrBgDefault
)

AttrFgColor and AttrBgColor mask the bits that say how to read Fg and Bg

AttrStyle masks only the style bits (excludes color mode flags)

type Backend

type Backend interface {
	// Lifecycle
	Init() error
	Fini()

	// Capabilities
	Size() (width, height int)

	// I/O
	// Write writes raw bytes to the terminal output.
	Write(p []byte) error

	// Read blocks until input is available, the stop channel is closed, or an error occurs.
	Read(stopCh <-chan struct{}) ([]byte, error)

	// Callbacks
	// SetResizeHandler registers a callback for terminal resize events.
	SetResizeHandler(handler func(width, height int))
}

Backend abstracts platform-specific terminal operations. This interface allows the terminal package to support both native Unix environments and WASM/Browser environments (via xterm.js).

type Cell

type Cell struct {
	Rune  rune
	Fg    color.RGB
	Bg    color.RGB
	Attrs Attr
}

Cell represents a single terminal cell

type ColorMode

type ColorMode uint8

ColorMode indicates terminal color capability

const (
	ColorMode256       ColorMode = iota // xterm-256 palette
	ColorModeTrueColor                  // 24-bit RGB
	ColorMode16                         // ANSI 16: SGR 30-37/90-97, 40-47/100-107
	ColorModeNone                       // no color; attributes only
)

func DetectColorMode

func DetectColorMode() ColorMode

DetectColorMode determines terminal color capability from environment: 16 colors on text consoles, true color where announced, else 256. It reports what the terminal can show; New also honours NO_COLOR.

type Event

type Event struct {
	Type      EventType
	Key       Key
	Rune      rune
	Modifiers Modifier
	Action    KeyAction // For EventKey: KeyPress unless the terminal reports releases
	Lost      bool      // For a KeyRelease the package made for a hold whose own release can't arrive
	Width     int       // For EventResize
	Height    int       // For EventResize
	Err       error     // For EventError
	Text      string    // For EventPaste: the pasted bytes as sent, controls included
	Focused   bool      // For EventFocus: whether the terminal now has focus
	Report    KeyReport // For EventKeyReport: what the terminal now reports of each key
	Source    KeySource // For EventKeyReport: the source releases come from, zero for presses

	// Mouse event fields
	MouseX      int
	MouseY      int
	MouseBtn    MouseButton
	MouseAction MouseAction
	Touch       bool // A touch, from a host that reads one; terminals report none
}

Event represents a terminal input event

type EventType

type EventType uint8

EventType distinguishes input event categories

const (
	EventKey EventType = iota
	EventResize
	EventPaste     // Bracketed paste, Text holds it
	EventMouse     // SGR mouse reporting
	EventError     // Read error
	EventClosed    // Input closed
	EventFocus     // The terminal gained or lost focus, while it reports releases through its protocol
	EventKeyReport // What a SetKeyReport ask came to: Report through Source
)

type Key

type Key uint16

Key represents a parsed input key

const (
	KeyNone Key = iota
	KeyRune     // Printable character (check Event.Rune)

	// Control keys
	KeyEscape
	KeyEnter
	KeyTab
	KeyBacktab  // Shift+Tab
	KeyShiftTab // Same as KeyBacktab,for clarity
	KeyBackspace
	KeyDelete
	KeySpace

	// Navigation
	KeyUp
	KeyDown
	KeyLeft
	KeyRight
	KeyHome
	KeyEnd
	KeyPageUp
	KeyPageDown
	KeyInsert

	// Function keys
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12

	// Ctrl+letter (Ctrl+A = 0x01, Ctrl+Z = 0x1A)
	KeyCtrlA
	KeyCtrlB
	KeyCtrlC
	KeyCtrlD
	KeyCtrlE
	KeyCtrlF
	KeyCtrlG
	KeyCtrlH // Often same as Backspace
	KeyCtrlI // Often same as Tab
	KeyCtrlJ // Often same as Enter
	KeyCtrlK
	KeyCtrlL
	KeyCtrlM // Often same as Enter
	KeyCtrlN
	KeyCtrlO
	KeyCtrlP
	KeyCtrlQ
	KeyCtrlR
	KeyCtrlS
	KeyCtrlT
	KeyCtrlU
	KeyCtrlV
	KeyCtrlW
	KeyCtrlX
	KeyCtrlY
	KeyCtrlZ

	// Ctrl+special
	KeyCtrlSpace // Ctrl+@ / Ctrl+Space (Ctrl+@ produces NUL byte 0x00)
	KeyCtrlBackslash
	KeyCtrlBracketLeft
	KeyCtrlBracketRight
	KeyCtrlCaret
	KeyCtrlUnderscore
)

Key constants - designed for expansion

func ControlKey added in v0.4.1

func ControlKey(r rune) (Key, bool)

ControlKey is the key a terminal reads for Ctrl with a printable character: the control byte that pair sends, parsed as input parsing reads it. Letters of either case, space and '@', '[' '\' ']' '^' '_', '-' and '/' as '_', and the digits 2 to 7 have one; ok is false for any other character.

func KeyByName

func KeyByName(name string) (Key, bool)

KeyByName resolves a canonical name to a Key constant Returns KeyNone and false if name is unknown

type KeyAction added in v0.5.0

type KeyAction uint8

KeyAction is what happened to a key. Only a terminal reporting releases (see SetKeyReport) tells them apart; elsewhere every key event is a press, and a key held down arrives as more presses at the keyboard's repeat rate.

const (
	KeyPress   KeyAction = iota // the key went down
	KeyRepeat                   // the keyboard repeated a key held down
	KeyRelease                  // the key came up
)

func (KeyAction) String added in v0.5.0

func (a KeyAction) String() string

String returns the action's name

type KeyHolds added in v0.5.0

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

KeyHolds pairs key releases with their presses. A source names each physical key by a code of its own (a console keycode, a protocol key number, a host's keycode) and reports presses, repeats and releases of it; KeyHolds hands each on as an Event, a release carrying what its press carried, so letting go of Shift first still releases 'A', not 'a'. The zero value is ready to use. It is not safe for concurrent use.

func (*KeyHolds) Held added in v0.5.0

func (h *KeyHolds) Held(code uint32) bool

Held reports whether the key code names is down

func (*KeyHolds) ReleaseAll added in v0.5.0

func (h *KeyHolds) ReleaseAll(send func(Event))

ReleaseAll ends every hold, sending a Lost release for each, oldest first: focus left, the console switched away, or releases stopped

func (*KeyHolds) Report added in v0.5.0

func (h *KeyHolds) Report(code uint32, ev Event, send func(Event))

Report hands on one report of the key code names through send: ev is the key as the source reads it, its Action set. A press starts a hold and a release ends it, carrying the press's Key, Rune and Modifiers; a repeat passes as it is. A report the holds contradict means the source lost one, as a terminal's own mode swallows keys: a press of a key held first ends that hold with a Lost release, a repeat of a key not held is a press, and a release of a key not held is dropped. A source that repeats a held key as more presses, as the consoles do, asks Held to tell its repeats apart.

type KeyReport added in v0.5.0

type KeyReport uint8

KeyReport is how much a terminal reports of each key

const (
	KeyReportPresses  KeyReport = iota // presses; a key held down repeats as more presses
	KeyReportReleases                  // presses, repeats and releases, each with its KeyAction
)

func (KeyReport) String added in v0.5.0

func (r KeyReport) String() string

String returns the report's name

type KeySource added in v0.5.0

type KeySource uint8

KeySource is where key releases come from (bitmask). SetKeyReport tries the sources a game allows in the order below; a terminal with none of them reports presses, as every terminal does until a game asks.

const (
	// KeySourceConsole is the kernel console's own keyboard, switched to raw
	// keycodes while releases are asked for: the Linux console on the VT the
	// game runs on, and FreeBSD's vt(4). Never over ssh, where the keyboard is
	// the far terminal's.
	KeySourceConsole KeySource = 1 << iota

	// KeySourceProtocol is the terminal's key event protocol (CSI u with event
	// types), found by asking the terminal whether it has one: Alacritty 0.13
	// and later, and any terminal that answers the same. Over ssh too.
	KeySourceProtocol

	// KeySourceHost is a host that reads its platform's own key events, such as
	// a window or a browser, and builds Events itself; no Terminal reports it.
	KeySourceHost
)

func (KeySource) String added in v0.5.0

func (s KeySource) String() string

String returns the sources' names, joined by |

type Modifier

type Modifier uint8

Modifier flags

const (
	ModNone  Modifier = 0
	ModShift Modifier = 1 << 0
	ModAlt   Modifier = 1 << 1
	ModCtrl  Modifier = 1 << 2
)

type MouseAction

type MouseAction uint8

MouseAction represents the type of mouse event

const (
	MouseActionNone MouseAction = iota
	MouseActionPress
	MouseActionRelease
	MouseActionMove
	MouseActionDrag
)

func (MouseAction) String

func (a MouseAction) String() string

String returns human-readable action name

type MouseButton

type MouseButton uint8

MouseButton represents mouse button identity

const (
	MouseBtnNone MouseButton = iota
	MouseBtnLeft
	MouseBtnMiddle
	MouseBtnRight
	MouseBtnWheelUp
	MouseBtnWheelDown
	MouseBtnBack    // Button 4 (if supported)
	MouseBtnForward // Button 5 (if supported)
)

func (MouseButton) String

func (b MouseButton) String() string

String returns human-readable button name

type MouseMode

type MouseMode uint8

MouseMode controls which mouse events are reported (bitmask)

const (
	MouseModeNone   MouseMode = 0
	MouseModeClick  MouseMode = 1 << 0 // Press/release events
	MouseModeDrag   MouseMode = 1 << 1 // Drag events (button held + motion)
	MouseModeMotion MouseMode = 1 << 2 // All motion events
)

type ResizeEvent

type ResizeEvent struct {
	Width  int
	Height int
}

ResizeEvent represents a terminal resize

type Terminal

type Terminal interface {
	// Init enters raw mode, alternate screen buffer, hides cursor
	Init() error

	// Fini restores terminal state. Safe to call multiple times
	Fini()

	// Size returns current terminal dimensions
	Size() (width, height int)

	// ResizeChan returns channel that receives resize events
	ResizeChan() <-chan ResizeEvent

	// ColorMode returns detected color capability
	ColorMode() ColorMode

	// Flush hands a frame to the terminal; cells are row-major: cells[y*width + x].
	// It never waits on the terminal: while the last write is still out, the frame
	// is held and drawn once that write returns, a newer one replacing it
	Flush(cells []Cell, width, height int)

	// Blocked reports how long the terminal has held the write in flight, zero
	// while output drains. A terminal that stops reading (a hidden tab, a client
	// that went away, flow control) shows here instead of as a caller that waits
	Blocked() time.Duration

	// Clear fills screen with specified background color
	Clear(bg color.RGB)

	// SetCursorVisible shows/hides cursor
	SetCursorVisible(visible bool)

	// MoveCursor positions cursor (0-indexed)
	MoveCursor(x, y int)

	// Sync forces full redraw
	Sync()

	// PollEvent blocks until next input event
	PollEvent() Event

	// PostEvent injects a synthetic event
	PostEvent(Event)

	// SetMouseMode enables/disables mouse event reporting
	// Modes can be combined: MouseModeClick | MouseModeDrag
	SetMouseMode(mode MouseMode) error

	// SetPasteMode turns bracketed paste on or off: on, a paste arrives as
	// one EventPaste rather than as keys, so its newlines don't press Enter
	SetPasteMode(on bool) error

	// SetKeyReport asks for keys to be reported at level report. Releases come
	// through the first source in allow the terminal has, the console before
	// the protocol; KeyReportPresses gives up the source in use. It returns at
	// once, and an EventKeyReport follows with what the terminal reports now,
	// so a game learns the outcome of every ask; asks the reader has not yet
	// acted on are answered together, by the latest. A change of source ends
	// the holds it leaves with Lost releases. Until an ask for releases, nothing
	// is sent to the terminal or switched.
	SetKeyReport(report KeyReport, allow KeySource) error
}

Terminal provides low-level terminal access

func New

func New(colorMode ...ColorMode) Terminal

New creates a new Terminal instance. Without a mode it uses DetectColorMode's, or ColorModeNone when NO_COLOR is set; an explicit mode overrides both, as a --color=always or never would

func NewWithBackend added in v0.4.0

func NewWithBackend(b Backend, c ColorMode) Terminal

NewWithBackend creates a Terminal over b in color mode c, for a pipe, an in-process emulator or a test standing in for the tty. b must not be nil; the Terminal calls its Init on Init and its Fini on Fini, and writes to it on its own goroutine as New's does

Directories

Path Synopsis
cmd
example-inline command
example-keys command
keys — shows each key event the terminal reports, for trying key releases where a game runs: a Linux or FreeBSD console, Alacritty, ssh, tmux.
keys — shows each key event the terminal reports, for trying key releases where a game runs: a Linux or FreeBSD console, Alacritty, ssh, tmux.
example-tui command
fv — file voyager: an lf / midnight-commander / xxd hybrid.
fv — file voyager: an lf / midnight-commander / xxd hybrid.
Package inline renders styled text and in-place progress in the normal terminal scrollback: no raw mode, no alternate screen, no input handling, no cursor hiding.
Package inline renders styled text and in-place progress in the normal terminal scrollback: no raw mode, no alternate screen, no input handling, no cursor hiding.
internal
env
Package env reads what the environment says about a terminal's colors, for terminal and inline alike
Package env reads what the environment says about a terminal's colors, for terminal and inline alike
window module

Jump to

Keyboard shortcuts

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