vt

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: AGPL-3.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 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  string
	Width uint8
	FG    Color
	BG    Color
	Attr  Attr
}

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.

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 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
}

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 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
	// 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) 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) 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) 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) Resize

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

Resize changes the terminal's size. Lines are cut or padded, not reflowed; when the screen shrinks, the lines above the cursor go to the scrollback, and when it grows they come back from it.

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) 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) 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) 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