vt

package
v3.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Copyright 2025 The TCell Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use file except in compliance with the License. You may obtain a copy of the license at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use file except in compliance with the License. You may obtain a copy of the license at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package vt provides common definitions for VT derived terminals and applications. This includes the venerable VT100, XTerm, and newer emulators such as Kitty and the Windows Terminal.

Package vt provides common definitions for VT derived terminals and applications. This includes the venerable VT100, XTerm, and newer emulators such as Kitty and the Windows Terminal.

This package is still under development and direct access to any of the interfaces here is not guaranteed to be stable yet. Caveat emptor.

Index

Constants

View Source
const (
	Plain         = Attr(0)      // basic, plain style
	Bold          = Attr(1 << 0) // maybe double strike, or just brighter
	Blink         = Attr(1 << 1) // NB: many terminals do not support it
	Reverse       = Attr(1 << 2) // foreground and background reversed
	Dim           = Attr(1 << 3) // fainter, may also be lower alpha
	Italic        = Attr(1 << 4) // italicized
	StrikeThrough = Attr(1 << 5) // crossed-out
	Underline     = Attr(1 << 6) // any underline style
	Overline      = Attr(1 << 7) // rarely supported

	// Underline styles, always mixed with underline, only one can be selected
	PlainUnderline  = Underline
	DoubleUnderline = Underline | 1<<13
	CurlyUnderline  = Underline | 2<<13
	DottedUnderline = Underline | 3<<13
	DashedUnderline = Underline | 4<<13
	UnderlineMask   = Underline | 7<<13 // bits 13, 14, 15
)
View Source
const (
	ModNone  = Modifier(0)
	ModShift = Modifier(1 << iota)
	ModCtrl
	ModAlt
	ModMeta
	ModHyper
	ModCapsLock
	ModNumLock
)
View Source
const (
	// definitions in ASCII space
	KcBackspace = KeyCode(0x08)
	KcTab       = KeyCode(0x09)
	KcReturn    = KeyCode(0x0d)
	KcEsc       = KeyCode(0x1B)
	KcSpace     = KeyCode(0x20)
	KcDelete    = KeyCode(0x7F) // not KcDel, but legacy Unix DELETE

	// misc function keys
	KcCancel = KeyCode(0xf000) + iota
	KcPause
	KcMenu
	KcSelect
	KcPrint
	KcExecute
	KcPrtScr
	KcHelp
	KcSleep
	KcApp // application key

	// navigation keys
	KcUp
	KcDown
	KcLeft
	KcRight
	KcCenter // center key in cursor keypad
	KcUpLeft
	KcUpRight
	KcDownLeft
	KcDownRight
	KcPgUp
	KcPgDn
	KcHome
	KcEnd
	KcIns
	KcDel

	// keypad keys
	KcPad0
	KcPad1
	KcPad2
	KcPad3
	KcPad4
	KcPad5
	KcPad6
	KcPad7
	KcPad8
	KcPad9
	KcPadDec // decimal (.)
	KcPadDiv // divide (/)
	KcPadMul // multiply (*)
	KcPadAdd // add (+)
	KcPadSub // subtract (-)
	KcPadEq  // equals (=)
	KcPadSep // separator (comma)
	KcPadEnter
	KcPadUp
	KcPadDown
	KcPadLeft
	KcPadRight
	KcPadPgUp
	KcPadPgDn
	KcPadHome
	KcPadEnd
	KcPadBegin
	KcPadIns
	KcPadDel

	// media keys
	KcMediaPlay
	KcMediaStop
	KcMediaPause // usually play is just modal
	KcMediaRewind
	KcMediaForward
	KcMediaNext
	KcMediaPrev
	KcMediaVolUp
	KcMediaVolDn
	KcMediaMute
	KcMediaRecord

	// browser keys (rarely supported)
	KcBrowserBack
	KcBrowserForward
	KcBrowserStop
	KcBrowserRefresh
	KcBrowserSearch
	KcBrowserFavorite
	KcBrowserHome

	// application keys
	KcApp1
	KcApp2
	KcApp3
	KcApp4
	KcApp5
	KcApp6
	KcApp7
	KcApp8

	// modifier keys (can be presented separately)
	KcRShift
	KcLShift
	KcLCtrl
	KcRCtrl
	KcLSuper
	KcRSuper
	KcLMeta
	KcRMeta
	KcLHyper
	KcRHyper
	KcIsoL3 // ISO level 3 shift - usually AltGr
	KcIsoL5 // ISO level 5 shift - custom usually

	// lock keys
	KcCapsLock
	KcScrLock
	KcNumLock

	// IME related
	KcKana
	KcHangul
	KcJunja
	KcKanji
	KcImeOn
	KcImeOff
	KcConvert    // IME convert
	KcNonConvert // IME non-convert
	KcAccept     // IME accept

	// virtual unicode - use to inject raw UTF-8
	KcUTF

	// up to 256 F-keys
	KcF1 = KeyCode(0xf100) + iota
	KcF2
	KcF3
	KcF4
	KcF5
	KcF6
	KcF7
	KcF8
	KcF9
	KcF10
	KcF11
	KcF12
	KcF13
	KcF14
	KcF15
	KcF16
	KcF17
	KcF18
	KcF19
	KcF20
	KcF21
	KcF22
	KcF23
	KcF24
	KcF25
	KcF26
	KcF27
	KcF28
	KcF29
	KcF30
	KcF31
	KcF32
	KcF33
	KcF34
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr added in v3.0.6

type Attr uint16

Attr is a synthetic combination of display attributes for cells, apart from color which is handled separately.

type Backend added in v3.0.6

type Backend interface {

	// GetPrivateMode returns the status of a given private mode.
	GetPrivateMode(PrivateMode) ModeStatus

	// SetPrivateMode sets a private mode to the given status.
	// If either value is invalid, this should simply ignore the operation.
	SetPrivateMode(PrivateMode, ModeStatus) error

	// GetSize returns the size of the terminal in characters.
	// The X and Y are counts, so the bottom right cell should be at coordinate (X-1, Y-1).
	GetSize() Coord

	// Put a single character at a specific position with the given attributes.
	// The cursor should be moved to this location.  It may advance.  If the value
	// of the rune is zero, then the cell is being erased, and no content should be
	// displayed.
	PutAbs(Coord, rune, Attr)

	// GetPosition returns the cursor position.
	GetPosition() Coord

	// SetPosition sets the cursor position. If the position is out of bounds,
	// it should be clipped to the window size.
	SetPosition(Coord)
}

Backend describes the backend of a terminal. This can be used to create a real emulator, while allowing the processor front end to handle the common details of parsing escape sequences, the state machine, and so forth. Backends support a limited set of common functionality, including a cursor. They only need to support writing at the cursor.

type Beeper added in v3.0.6

type Beeper interface {
	Beep()
}

Beeper can be implemented by a backend to indicate it can ring the bell or beep. This is typically done in response to a 0x07 bell.

type Col

type Col int

Col indicates a column number (x position). We use zero based indices.

type Colorer added in v3.0.6

type Colorer interface {
	// Colors returns the number of colors this terminal can support.  For direct color,
	// return 1<<24. The XTerm palette is assumed. Monochrome terminals should return 0.
	Colors() int

	// SetFgColor sets the foreground color.
	SetFgColor(color.Color)

	// SetBgColor sets the background color.
	SetBgColor(color.Color)
}

Colorer can select the colors used. This interface is stateful, so that an implementation needs to remember the values and use them.

type Coord

type Coord struct {
	X Col // Column number, or X position.
	Y Row // Row number, or Y position.
}

Coord indicates a coordinate. This can also be used for window sizes.

type Emulator added in v3.0.6

type Emulator interface {
	// SetId sets our identity.
	SetId(name string, version string)

	// SendRaw sends raw data to the consumer.  This bypasses the normal encoding,
	// so it should be used with caution.
	SendRaw([]byte)

	// KeyEvent injects a keyboard event into the emulator, which will ultimately
	// result in data being sent via SendRaw.
	KeyEvent(ev KbdEvent)

	// ResizeEvent is called by a backend when the terminal has resized
	// This will send inband resize notifications if the client has requested them.
	ResizeEvent()

	// Drain waits until any queued but not processed input has finished processing.
	// It also wakes the reader.
	Drain() error

	// Start starts processing.
	Start() error

	// Stop stops processing.
	Stop() error

	// Reader reads data from the emulator.  These are bytes that would be transmitted
	// to a remote party.
	io.Reader

	// Writer writes data to the emulator.  These are commands that the emulator should process.
	io.Writer
}

Emulator is a terminal emulator API. It implements the state machinery (escape parsing and so forth) associated with being a terminal emulator. The backend handles rendering the content, and some low level details.

NOTE: This is not a committed interface yet, its entirely a work in progress.

func NewEmulator added in v3.0.6

func NewEmulator(be Backend) Emulator

NewEmulator creates an emulator instance on top of the given backend. The input is relative to the emulator, so it receives data from the host, whereas the emulator sends data to the application through the output.

type GraphemePositioner added in v3.0.6

type GraphemePositioner interface {
	Positioner

	// Put the grapheme cluster at the cursor position. This will occupy a single
	// narrow or wide terminal cell.
	PutGrapheme([]rune)
}

GraphemePositioner adds the ability to write a grapheme cluster to Positioner. If a terminal does not implement this, then it will not support grapheme clusters.

type KbdEvent added in v3.0.6

type KbdEvent struct {
	Down   bool     // true if event is for key down event
	Repeat int      // if > 1, a repeat count
	Code   KeyCode  // logical key code (X11 keysm, e.g. 'A')
	Base   KeyCode  // base key code (physical key, e.g 'a'), may be zero if same as code
	Mod    Modifier // modifiers
	Utf    string   // if non-empty, the unicode content for this
}

type KeyCode added in v3.0.6

type KeyCode rune

KeyCode represents either a physical key, or logical key. When it is the "code", it will represent the logical key such as 'A'. But (on English keyboards), 'A' becomes 'a' (with the ModShift) modifier, as a base. (Sometimes we don't care about the logical keyboard, but only about a physical key, and using the base lets us deal with that.)

Normally KeyCodes are numerically the value of the key they represent. E.g 65 for 'A' or 97 for 'a'.

Following the Kitty protocol, we use the Private Use Area 57344 - 63743 for keys that do not correspond to a Unicode value (such as function keys). However, we do not use the same values as we need to represent more keys here than kitty does.

This should be sufficient to support all known keyboard protocols.

Note that Windows defines key codes for mice and gamepads. Those should be treated as different events entirely.

type ModeStatus

type ModeStatus int

ModeStatus represents the status of the mode.

const (
	ModeNA        ModeStatus = 0 // Mode is not supported (or unknown)
	ModeOn        ModeStatus = 1 // Mode is on (e.g. via CSI-h)
	ModeOff       ModeStatus = 2 // Mode is off (e.g. via CSI-l)
	ModeOnLocked  ModeStatus = 3 // Mode is hardwired on
	ModeOffLocked ModeStatus = 4 // Mode is hardwired off
)

func (ModeStatus) Changeable

func (ms ModeStatus) Changeable() bool

Changeable indicates that the mode may be changed.

type Modifier added in v3.0.6

type Modifier int

type Positioner added in v3.0.6

type Positioner interface {

	// Put the rune at the cursor position with the given attributes. The cursor is expected
	// to advance by the rune width.
	PutChar(rune, Attr)
}

Positioner tracks the cursor. Backends that implement this should automatically advance the position after writing content.

type PrivateMode

type PrivateMode int

PrivateMode describes a DEC Private Mode.

const (
	PmAppCursor      PrivateMode = 1 // application cursor keys
	PmAutoMargin     PrivateMode = 7
	PmAltScreen      PrivateMode = 1049 // 47 and 1047 are alternates, but we use 1049
	PmBlinkCursor    PrivateMode = 12
	PmShowCursor     PrivateMode = 25
	PmMouseButton    PrivateMode = 1000
	PmMouseDrag      PrivateMode = 1002
	PmMouseMotion    PrivateMode = 1003
	PmFocusReports   PrivateMode = 1004
	PmMouseSgr       PrivateMode = 1006
	PmMouseSgrPixel  PrivateMode = 1016
	PmBracketedPaste PrivateMode = 2004
	PmSyncOutput     PrivateMode = 2026
	PmResizeReports  PrivateMode = 2048 // send in-band resize reports
)

func (PrivateMode) Disable

func (pm PrivateMode) Disable() string

Disable returns the string used to disable this private mode.

func (PrivateMode) Enable

func (pm PrivateMode) Enable() string

Enable returns the string used to enable this private mode.

func (PrivateMode) Query

func (pm PrivateMode) Query() string

Query returns the string used to query the state of this private mode.

func (PrivateMode) Reply

func (pm PrivateMode) Reply(status ModeStatus) string

Reply returns a string representing a query reply for the given mode and status.

type Resizer added in v3.0.6

type Resizer interface {
	// NotifyResize registers a channel to be posted to if the window size changes.
	NotifyResize(chan<- bool)
}

Resizer adds notifications when the window size changes.

type Row

type Row int

Row indicates a row number (y position). We use zero based indices, although the VT standard mostly communicates using 1-based offsets.

type Titler added in v3.0.6

type Titler interface {
	// SetWindowTitle only changes the window title.
	SetWindowTitle(string)
}

Titler adds support for setting the window title. (Typically this is OSC2.) Note that for security reasons we only support setting this. We don't bother with icon titles, since few terminal emulators support it, and it would be hard for us to do this in any portable fashion.

type UnderlineColorer added in v3.0.6

type UnderlineColorer interface {
	Colorer

	// SetUlColor sets the underline color.
	// If color.None is chosen, then default foreground color is used.
	SetUlColor(color.Color)
}

UnderlineColorer adds underline color management to the Colorer.

Jump to

Keyboard shortcuts

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