vt

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: AGPL-3.0 Imports: 19 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 vt is SSHYard's terminal emulator: a parser of the escape sequences a VT100/xterm-compatible terminal understands and the grid of cells they draw on, with scrollback, the alternate screen, 256 colors and truecolor. It has no UI; package app draws its cells natively with MyGo's Painter.

Index

Constants

View Source
const DefaultScrollback = 10000

DefaultScrollback is how many lines scroll back by default.

View Source
const (

	// OSC52MaxDefault is the decoded size of an OSC 52 paste when no
	// limit was set: 1 MiB.
	OSC52MaxDefault = 1 << 20
)

Variables

This section is empty.

Functions

func EncodeFocus

func EncodeFocus(m Modes, focused bool) []byte

EncodeFocus is what a program that asked for focus events (1004) reads when the terminal gains or loses the focus, or nil.

func EncodeMouse

func EncodeMouse(m Modes, ev MouseEvent) []byte

EncodeMouse is what the program reads for ev under modes m, or nil when the modes do not ask for it. The default encoding cannot say a column or row past 223: such events are dropped, as xterm does; SGR (1006) has no limit.

func RuneWidth

func RuneWidth(r rune) int

RuneWidth is the number of cells r takes: 0, 1 or 2.

Types

type Attr

type Attr uint16

Attr is a set of cell attributes.

const (
	Bold Attr = 1 << iota
	Faint
	Italic
	Underline
	Blink
	Inverse
	Hidden
	Strike
)

type Cell

type Cell struct {
	Rune  rune
	Comb  Comb
	FG    Color
	BG    Color
	Attr  Attr
	Width uint8
}

Cell is one cell of the grid. A wide character (CJK, most emoji) takes two cells: the first has Width 2, the second Width 0 and no rune. Comb holds what joins Rune in its cell: combining marks, variation selectors, emoji modifiers, and ZWJ with what it joins.

A cell holds no pointer, so the garbage collector does not scan the cells of the scrollback, megabytes of them.

func (Cell) Text

func (c Cell) Text() string

Text is the cell's text: its rune and what joins it.

type Color

type Color uint32

Color is a cell's color: the default, an entry of the 256-color palette, or a 24-bit RGB value.

const DefaultColor Color = colorDefault

DefaultColor is the terminal's default foreground or background.

func Indexed

func Indexed(i uint8) Color

Indexed returns entry i of the 256-color palette.

func RGB

func RGB(r, g, b uint8) Color

RGB returns a truecolor color.

func (Color) Index

func (c Color) Index() (uint8, bool)

Index returns the palette index of an indexed color.

func (Color) IsDefault

func (c Color) IsDefault() bool

IsDefault reports whether c is the default color.

func (Color) RGB

func (c Color) RGB() (r, g, b uint8, ok bool)

RGB returns the components of a truecolor color.

type Comb added in v0.0.12

type Comb uint32

Comb is what joins a cell's rune, interned: 0 is nothing.

func CombOf added in v0.0.12

func CombOf(s string) Comb

CombOf interns s; it is 0 for "", or when the table is full.

func (Comb) String added in v0.0.12

func (c Comb) String() string

String is the sequence.

type CursorStyle

type CursorStyle int

CursorStyle is the cursor's shape, as DECSCUSR sets it.

const (
	CursorBlock CursorStyle = iota
	CursorUnderline
	CursorBar
)

type Line

type Line struct {
	Cells   []Cell
	Wrapped bool
	// contains filtered or unexported fields
}

Line is a row of cells. Wrapped is set when the text continued on the next line because it reached the right margin.

func (Line) String

func (l Line) String() string

String returns the line's text, without trailing blanks.

type Match

type Match struct {
	Pos
	Len int
}

Match is a search result: Len cells from Pos.

type Modes

type Modes struct {
	AppCursorKeys  bool // DECCKM: arrows send ESC O A
	AppKeypad      bool
	BracketedPaste bool
	CursorVisible  bool
	AltScreen      bool
	AutoWrap       bool
	Origin         bool
	Insert         bool
	MouseTracking  bool // a mouse mode is on (9/1000/1002/1003)
	Mouse          MouseMode
	MouseSGR       bool // 1006: SGR encoding of mouse reports
	FocusReport    bool // 1004
}

Modes are the terminal modes the UI needs to know about.

type MouseAction

type MouseAction uint8

MouseAction is what happened.

const (
	MousePress MouseAction = iota
	MouseRelease
	MouseMotion
)

type MouseBtn

type MouseBtn uint8

MouseBtn is the button of a mouse event.

const (
	MouseLeft MouseBtn = iota
	MouseMiddle
	MouseRight
	MouseNone // a move with no button held
	MouseWheelUp
	MouseWheelDown
)

type MouseEvent

type MouseEvent struct {
	Button           MouseBtn
	Action           MouseAction
	Col, Row         int
	Shift, Alt, Ctrl bool
}

MouseEvent is a mouse event to report to the program, at a cell of the screen (0-based).

type MouseMode

type MouseMode uint8

MouseMode is which mouse events a program asked to receive.

const (
	MouseOff    MouseMode = iota
	MouseX10              // DECSET 9: presses only
	MouseNormal           // 1000: presses and releases
	MouseButton           // 1002: and moves while a button is held
	MouseAny              // 1003: and every move
)

type OutputEncoding added in v0.0.14

type OutputEncoding string

OutputEncoding selects how printable program output is decoded.

const (
	EncodingUTF8        OutputEncoding = "UTF-8"
	EncodingGBK         OutputEncoding = "GBK"
	EncodingGB2312      OutputEncoding = "GB2312"
	EncodingGB18030     OutputEncoding = "GB18030"
	EncodingBig5        OutputEncoding = "Big5"
	EncodingShiftJIS    OutputEncoding = "Shift_JIS"
	EncodingEUCJP       OutputEncoding = "EUC-JP"
	EncodingEUCKR       OutputEncoding = "EUC-KR"
	EncodingWindows1252 OutputEncoding = "Windows-1252"
	EncodingWindows1250 OutputEncoding = "Windows-1250"
	EncodingISO8859_1   OutputEncoding = "ISO-8859-1"
	EncodingISO8859_2   OutputEncoding = "ISO-8859-2"
	EncodingISO8859_15  OutputEncoding = "ISO-8859-15"
	EncodingWindows1251 OutputEncoding = "Windows-1251"
	EncodingKOI8R       OutputEncoding = "KOI8-R"
	EncodingKOI8U       OutputEncoding = "KOI8-U"
	EncodingISO8859_5   OutputEncoding = "ISO-8859-5"
	EncodingCP437       OutputEncoding = "CP437"
	EncodingCP850       OutputEncoding = "CP850"
	EncodingCP866       OutputEncoding = "CP866"
)

func OutputEncodings added in v0.0.18

func OutputEncodings() []OutputEncoding

OutputEncodings returns a fresh, ordered list of supported output encodings.

func (OutputEncoding) Decode added in v0.0.26

func (e OutputEncoding) Decode(p []byte) string

Decode is p, output in encoding e, as UTF-8 text; what does not decode becomes U+FFFD. An encoding not supported is taken as UTF-8.

type Pos

type Pos struct{ Line, Col int }

Pos is a position in the terminal's history: Line is absolute, counting every line that ever scrolled into the scrollback, so it stays put as output arrives; Col is a cell column.

func (Pos) Less

func (p Pos) Less(q Pos) bool

Less orders positions.

type Snapshot

type Snapshot struct {
	Cols, Rows int
	// Lines are Rows lines from line First of the history.
	Lines []Line
	First int
	// Cursor is where the cursor is on the screen, in Lines' coordinates;
	// CursorVisible is false when the program hid it or it is scrolled
	// out of view.
	CursorX, CursorY int
	CursorVisible    bool
	CursorStyle      CursorStyle
	CursorStyleSet   bool // the program selected CursorStyle with DECSCUSR
	CursorBlink      bool
	// Scrollback is how many lines are above the screen; Offset how many
	// of them the view is scrolled up.
	Scrollback, Offset int
	Modes              Modes
}

Snapshot is a copy of what the terminal shows, for drawing.

type Terminal

type Terminal struct {

	// Reply, when set, receives what the terminal answers the program
	// (device status and attributes reports). It is called with the lock
	// held, so it must not call back into the terminal.
	Reply func([]byte)
	// OnClipboard reports an OSC 52. sel is the selection parameter
	// (c, p, s or 0–7); each of those is the system clipboard. data is
	// the text. query is true when the program asked to read the
	// clipboard (Pd is "?"); data is then empty and nothing is answered.
	// Called with the lock held, like Reply.
	OnClipboard func(sel, data string, query bool)
	// OnClipboardDrop reports an OSC 52 bigger than the size limit. The
	// whole sequence is discarded. Called with the lock held.
	OnClipboardDrop func()
	// Bell, when set, is called on BEL.
	Bell func()
	// contains filtered or unexported fields
}

Terminal is a terminal emulator. Write feeds it the output of a program; the UI reads it with Snapshot. It is safe for concurrent use.

func New

func New(cols, rows int) *Terminal

New returns a terminal of cols×rows cells.

func (*Terminal) Cursor added in v0.0.12

func (t *Terminal) Cursor(offset int) (x, y int)

Cursor is where Snapshot(offset) has the cursor, without copying the screen.

func (*Terminal) CursorLine added in v0.0.22

func (t *Terminal) CursorLine() string

CursorLine is the text of the line the cursor is on, the end of what SetLineHook told; "" on the alternate screen.

func (*Terminal) Cwd

func (t *Terminal) Cwd() string

Cwd is the working directory the shell last reported (OSC 7 file://host/path or OSC 1337 CurrentDir=), or "" if it never did.

func (*Terminal) CwdHost

func (t *Terminal) CwdHost() string

CwdHost is the host the last directory report named: OSC 7's file://host/, or for OSC 1337 CurrentDir the host of the last OSC 1337 RemoteHost=user@host; "" when it named none.

func (*Terminal) Find

func (t *Terminal) Find(query string, caseSensitive bool) []Match

Find returns every match of query in the history, oldest first. It ignores case unless caseSensitive. A match does not span lines.

func (*Terminal) Generation

func (t *Terminal) Generation() uint64

Generation increases every time the terminal changes.

func (*Terminal) LinkAt added in v0.0.13

func (t *Terminal) LinkAt(p Pos) (string, Pos, Pos)

LinkAt returns the web address shown at p, an http or https URL, across lines that wrapped, with its first and last cells; "" when there is none. A space or text that is not ASCII ends it; trailing punctuation and a closing bracket it did not open are left out.

func (*Terminal) Modes

func (t *Terminal) Modes() Modes

Modes returns the terminal's modes.

func (*Terminal) OffsetFor

func (t *Terminal) OffsetFor(n, current int) int

OffsetFor returns the scroll offset that brings absolute line n into view, keeping the view still when it already is.

func (*Terminal) OutputEncoding added in v0.0.14

func (t *Terminal) OutputEncoding() OutputEncoding

func (*Terminal) ResetInputModes

func (t *Terminal) ResetInputModes(leaveAlt bool)

ResetInputModes turns off what a program asked of the terminal's input (mouse and focus reports, bracketed paste, application keys), for when it went away without undoing them: its shell ended. With leaveAlt it leaves the alternate screen as well, for a new shell.

func (*Terminal) ResetOutputStream added in v0.0.14

func (t *Terminal) ResetOutputStream()

ResetOutputStream starts a new program stream, retaining the screen and encoding selection. Unfinished characters become visible replacements; unfinished control sequences are discarded.

func (*Terminal) Resize

func (t *Terminal) Resize(cols, rows int)

Resize changes the terminal's size. On the primary screen lines are reflowed: a line that wrapped is joined with the lines it continued on and wrapped again at the new width, so narrowing and widening again loses nothing. When the screen gets too short, the blank lines below the cursor go first, then the lines at the top go to the scrollback; when it gets taller, they come back from it. The alternate screen, which its program redraws, is only cut or padded.

func (*Terminal) ScreenText

func (t *Terminal) ScreenText() string

ScreenText returns the text of the screen, one line per row, without trailing blank lines: for tests and for copying all.

func (*Terminal) SetLineHook added in v0.0.22

func (t *Terminal) SetLineHook(hook func(text string, wrapped bool))

SetLineHook has hook told each line of the primary screen the cursor leaves, by a line feed or by wrapping, as the line reads then: a line edited in place (a progress bar, a command line) as it ended up. wrapped says the text goes on in the next line. A full-screen program's alternate screen is left out. hook is called with the lock held, so it must not call back into the terminal, nor block; nil stops.

func (*Terminal) SetOSC52Max

func (t *Terminal) SetOSC52Max(n int)

SetOSC52Max sets the largest decoded OSC 52 paste to keep. Zero uses the default (1 MiB). Anything above 8 MiB is capped there.

func (*Terminal) SetOutputEncoding added in v0.0.14

func (t *Terminal) SetOutputEncoding(selected OutputEncoding) error

SetOutputEncoding affects future output only; an unfinished character is displayed as a replacement character without clearing existing text.

func (*Terminal) SetScrollback

func (t *Terminal) SetScrollback(n int)

SetScrollback sets how many lines scroll back.

func (*Terminal) Size

func (t *Terminal) Size() (cols, rows int)

Size returns the terminal's size in cells.

func (*Terminal) Snapshot

func (t *Terminal) Snapshot(offset int) Snapshot

Snapshot returns the screen, scrolled up offset lines into the scrollback (0 is the live screen).

func (*Terminal) SnapshotInto added in v0.0.12

func (t *Terminal) SnapshotInto(s *Snapshot, offset int)

SnapshotInto is Snapshot into s, reusing the cells of its lines: for a view that takes one every frame. Every line has Cols cells, the scrollback's short ones filled with blanks.

func (*Terminal) Text

func (t *Terminal) Text(a, b Pos) string

Text returns the text from a to b, inclusive of b's cell. Lines that wrapped are joined; others end with a newline.

func (*Terminal) Title

func (t *Terminal) Title() string

Title is the window title the program set (OSC 0 or 2).

func (*Terminal) WordAt

func (t *Terminal) WordAt(p Pos) (Pos, Pos)

WordAt returns the word around p, for double-click selection.

func (*Terminal) Write

func (t *Terminal) Write(p []byte) (int, error)

Write feeds the terminal a program's output.

func (*Terminal) WriteString

func (t *Terminal) WriteString(s string)

WriteString is Write for a string.

Jump to

Keyboard shortcuts

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