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
- func EncodeFocus(m Modes, focused bool) []byte
- func EncodeMouse(m Modes, ev MouseEvent) []byte
- func RuneWidth(r rune) int
- type Attr
- type Cell
- type Color
- type Comb
- type CursorStyle
- type Line
- type Match
- type Modes
- type MouseAction
- type MouseBtn
- type MouseEvent
- type MouseMode
- type OutputEncoding
- type Pos
- type Snapshot
- type Terminal
- func (t *Terminal) Cursor(offset int) (x, y int)
- func (t *Terminal) CursorLine() string
- func (t *Terminal) Cwd() string
- func (t *Terminal) CwdHost() string
- func (t *Terminal) Find(query string, caseSensitive bool) []Match
- func (t *Terminal) Generation() uint64
- func (t *Terminal) LinkAt(p Pos) (string, Pos, Pos)
- func (t *Terminal) Modes() Modes
- func (t *Terminal) OffsetFor(n, current int) int
- func (t *Terminal) OutputEncoding() OutputEncoding
- func (t *Terminal) ResetInputModes(leaveAlt bool)
- func (t *Terminal) ResetOutputStream()
- func (t *Terminal) Resize(cols, rows int)
- func (t *Terminal) ScreenText() string
- func (t *Terminal) SetLineHook(hook func(text string, wrapped bool))
- func (t *Terminal) SetOSC52Max(n int)
- func (t *Terminal) SetOutputEncoding(selected OutputEncoding) error
- func (t *Terminal) SetScrollback(n int)
- func (t *Terminal) Size() (cols, rows int)
- func (t *Terminal) Snapshot(offset int) Snapshot
- func (t *Terminal) SnapshotInto(s *Snapshot, offset int)
- func (t *Terminal) Text(a, b Pos) string
- func (t *Terminal) Title() string
- func (t *Terminal) WordAt(p Pos) (Pos, Pos)
- func (t *Terminal) Write(p []byte) (int, error)
- func (t *Terminal) WriteString(s string)
Constants ¶
const DefaultScrollback = 10000
DefaultScrollback is how many lines scroll back by default.
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 ¶
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.
Types ¶
type Cell ¶
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.
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.
type Comb ¶ added in v0.0.12
type Comb uint32
Comb is what joins a cell's rune, interned: 0 is nothing.
type CursorStyle ¶
type CursorStyle int
CursorStyle is the cursor's shape, as DECSCUSR sets it.
const ( CursorBlock CursorStyle = iota CursorUnderline CursorBar )
type Line ¶
Line is a row of cells. Wrapped is set when the text continued on the next line because it reached the right margin.
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 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 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.
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 (*Terminal) Cursor ¶ added in v0.0.12
Cursor is where Snapshot(offset) has the cursor, without copying the screen.
func (*Terminal) CursorLine ¶ added in v0.0.22
CursorLine is the text of the line the cursor is on, the end of what SetLineHook told; "" on the alternate screen.
func (*Terminal) Cwd ¶
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 ¶
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 ¶
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 ¶
Generation increases every time the terminal changes.
func (*Terminal) LinkAt ¶ added in v0.0.13
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) OffsetFor ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
SetScrollback sets how many lines scroll back.
func (*Terminal) 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
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 ¶
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) WriteString ¶
WriteString is Write for a string.