Documentation
¶
Index ¶
- Constants
- Variables
- func DebugFlags()
- func PrintTerminal(t Terminal)
- func PrintTerminalTo(w io.Writer, t Terminal)
- type Backend
- type BatchFrontend
- type ChangeReason
- type ClipboardSink
- type Color
- type ColorComponent
- type EmptyFrontend
- func (d *EmptyFrontend) Bell(v LockedView)
- func (d *EmptyFrontend) CursorMoved(v LockedView, x, y int)
- func (d *EmptyFrontend) RegionChanged(v LockedView, r Region, c ChangeReason)
- func (d *EmptyFrontend) ScrollLines(v LockedView, delta int)
- func (d *EmptyFrontend) StyleChanged(v LockedView, s Style)
- func (d *EmptyFrontend) ViewFlagChanged(v LockedView, vs ViewFlag, value bool)
- func (d *EmptyFrontend) ViewIntChanged(v LockedView, vs ViewInt, value int)
- func (d *EmptyFrontend) ViewStringChanged(v LockedView, vs ViewString, value string)
- type Frontend
- type GraphemeReader
- func (r *GraphemeReader) Buffered() int
- func (r *GraphemeReader) ReadByte() (byte, error)
- func (r *GraphemeReader) ReadPrintableBytes(maxWidth int) (string, int, bool, error)
- func (r *GraphemeReader) ReadPrintableTokens(maxWidth int) ([]GraphemeToken, error)
- func (r *GraphemeReader) ReadPrintableTokensInto(maxWidth int, dst []GraphemeToken) ([]GraphemeToken, error)
- func (r *GraphemeReader) SnapshotState() GraphemeState
- type GraphemeState
- type GraphemeToken
- type Handoff
- type KeyCode
- type KeyEvent
- type KeyEventType
- type KeyMod
- type KeyboardEnhancement
- type Line
- type LockedScrollback
- func (s LockedScrollback) Len() int
- func (s LockedScrollback) LogicalLine(id ScrollID) []Row
- func (s LockedScrollback) MovePos(pos ScrollPos, delta int) ScrollPos
- func (s LockedScrollback) Range() (base, next ScrollID)
- func (s LockedScrollback) Row(id ScrollID) (Row, bool)
- func (s LockedScrollback) Snapshot() ScrollbackSnapshot
- type LockedView
- func (v LockedView) ANSILine(y int) string
- func (v LockedView) CursorColor() Color
- func (v LockedView) CursorPos() Pos
- func (v LockedView) DefaultBackground() Color
- func (v LockedView) DefaultForeground() Color
- func (v LockedView) GetViewFlag(f ViewFlag) bool
- func (v LockedView) GetViewInt(i ViewInt) int
- func (v LockedView) GetViewString(s ViewString) string
- func (v LockedView) Line(y int) string
- func (v LockedView) PaletteColor(idx int) Color
- func (v LockedView) Scrollback() LockedScrollback
- func (v LockedView) Size() (w, h int)
- func (v LockedView) Snapshot() Snapshot
- func (v LockedView) StyledLine(x, w, y int) Line
- func (v LockedView) StyledLines(r Region) []Line
- type Mode
- type MouseBtn
- type MouseFlag
- type NoPTYBackend
- type NotificationSink
- type PTYBackend
- func (p *PTYBackend) Adopt(master, slave *os.File) error
- func (p *PTYBackend) AdoptMaster(master *os.File) error
- func (p *PTYBackend) Attach(c *exec.Cmd) error
- func (p *PTYBackend) Close() error
- func (p *PTYBackend) Master() *os.File
- func (p *PTYBackend) Open() (*os.File, error)
- func (p *PTYBackend) PID() int
- func (p *PTYBackend) Read(b []byte) (int, error)
- func (p *PTYBackend) SetReadDeadline(t time.Time) error
- func (p *PTYBackend) SetSize(w, h int) error
- func (p *PTYBackend) Slave() *os.File
- func (p *PTYBackend) Wait() error
- func (p *PTYBackend) Wake()
- func (p *PTYBackend) Write(b []byte) (int, error)
- type Pos
- type PromptMark
- type PromptMarkKind
- type ReaderState
- type Region
- type Row
- type ScreenState
- type ScrollID
- type ScrollPos
- type Scrollback
- func (s *Scrollback) Clear()
- func (s *Scrollback) Len() int
- func (s *Scrollback) LogicalLine(id ScrollID) []Row
- func (s *Scrollback) MovePos(pos ScrollPos, delta int) ScrollPos
- func (s *Scrollback) Range() (base, next ScrollID)
- func (s *Scrollback) Restore(snap ScrollbackSnapshot) error
- func (s *Scrollback) Row(id ScrollID) (Row, bool)
- func (s *Scrollback) Snapshot() ScrollbackSnapshot
- type ScrollbackListener
- type ScrollbackSnapshot
- type ShellIntegration
- type Snapshot
- type Span
- type Style
- func (s Style) ANSIEscape() []byte
- func (s Style) ANSIEscapeFrom(prev Style) []byte
- func (s *Style) GetColor(component ColorComponent) (Color, error)
- func (s *Style) GobDecode(data []byte) error
- func (s Style) GobEncode() ([]byte, error)
- func (s Style) MarshalJSON() ([]byte, error)
- func (s *Style) Modes() []Mode
- func (s *Style) Reset(modes ...Mode)
- func (s *Style) SetColor(component ColorComponent, color Color) error
- func (s *Style) SetMode(modes ...Mode)
- func (s *Style) TestMode(mode Mode) bool
- func (s *Style) UnmarshalJSON(data []byte) error
- type TTYFrontend
- func (t *TTYFrontend) Attach(r Region)
- func (t *TTYFrontend) Bell(v LockedView)
- func (t *TTYFrontend) Blur()
- func (t *TTYFrontend) CursorMoved(v LockedView, x, y int)
- func (t *TTYFrontend) Detach()
- func (t *TTYFrontend) Focus()
- func (t *TTYFrontend) PrepareBatch(view LockedView)
- func (t *TTYFrontend) RegionChanged(view LockedView, r Region, reason ChangeReason)
- func (t *TTYFrontend) ScrollLines(v LockedView, y int)
- func (t *TTYFrontend) SetFocus(focused bool)
- func (t *TTYFrontend) SetTerminal(term Terminal)
- func (t *TTYFrontend) StyleChanged(v LockedView, s Style)
- func (t *TTYFrontend) ViewFlagChanged(v LockedView, flag ViewFlag, value bool)
- func (t *TTYFrontend) ViewIntChanged(v LockedView, flag ViewInt, value int)
- func (t *TTYFrontend) ViewStringChanged(v LockedView, flag ViewString, value string)
- func (t *TTYFrontend) WriteBatch()
- type TeeBackend
- func (t *TeeBackend) Close() error
- func (t *TeeBackend) Read(p []byte) (int, error)
- func (t *TeeBackend) SetReadDeadline(d time.Time) error
- func (t *TeeBackend) SetSize(w, h int) error
- func (t *TeeBackend) SetTee(w io.Writer)
- func (t *TeeBackend) Wake()
- func (t *TeeBackend) Write(p []byte) (int, error)
- type Terminal
- type TerminalOption
- type TextReadMode
- type ViewFlag
- type ViewInt
- type ViewString
Constants ¶
const ( MMNone int = iota MMPress MMPressRelease MMPressReleaseMove MMPressReleaseMoveAll )
Mouse modes for VI_MouseMode
const ( CursorShapeDefault int = 0 // terminal default (blinking block) CursorShapeBlinkBlock int = 1 CursorShapeSteadyBlock int = 2 CursorShapeBlinkUnderline int = 3 CursorShapeSteadyUnderline int = 4 CursorShapeBlinkBar int = 5 CursorShapeSteadyBar int = 6 )
Cursor shapes for VICursorShape (DECSCUSR). The value is the DECSCUSR Ps parameter, so it can be forwarded to a terminal frontend unchanged.
const ( MEX10 int = iota MEUTF8 MESGR MEURXVT )
Mouse encodings for VI_MouseEncoding
const ( ColBlack = 0 ColRed = 1 ColGreen = 2 ColYellow = 3 ColBlue = 4 ColMagenta = 5 ColCyan = 6 ColWhite = 7 )
Public color constants for 256-color mode
const ( ColorType256 = 0 ColorTypeRGB = 1 )
Color constants for the Color type
const ( // low 2 bits MBtn1 MouseBtn = 0 MBtn2 MouseBtn = 1 MBtn3 MouseBtn = 2 MRelease MouseBtn = 3 // flags MShift MouseFlag = 4 MMeta MouseFlag = 8 MControl MouseFlag = 16 MMotion MouseFlag = 32 MWheel MouseFlag = 64 )
const DefaultScrollbackRows = 1000
DefaultScrollbackRows is the default capacity of the scrollback ring buffer.
const ESC byte = 27
const Version = "0.1"
Version is the emulator version reported by XTVERSION (CSI > q).
Variables ¶
var ErrHandoffInvalid = errors.New("termemu: invalid handoff")
ErrHandoffInvalid is returned by NewFromHandoff when the handoff is missing required state (a backend, a positive size, or matching snapshot dimensions).
var ErrPauseUnsupported = errors.New("termemu: Pause requires a backend that can interrupt a blocked read")
ErrPauseUnsupported is returned by Pause when the backend cannot interrupt a blocked read (it implements neither SetReadDeadline nor Wake). Waiting for the next byte would make Pause unbounded, so it fails instead.
var ErrScrollbackSnapshotInvalid = errors.New("termemu: invalid scrollback snapshot")
ErrScrollbackSnapshotInvalid is returned by Scrollback.Restore when the snapshot's row IDs are non-contiguous, its Next/Base range does not match the row count, or it otherwise cannot describe a valid ring buffer.
var ErrSnapshotSizeMismatch = errors.New("termemu: snapshot dimensions do not match terminal size")
ErrSnapshotSizeMismatch is returned by Terminal.Restore when the snapshot dimensions do not match the current terminal size. Restore never resizes the pty or screens implicitly.
Functions ¶
func DebugFlags ¶
func DebugFlags()
func PrintTerminal ¶
func PrintTerminal(t Terminal)
PrintTerminal prints the terminal screen contents to stdout for debugging. It uses only the public Terminal interface, with proper locking.
func PrintTerminalTo ¶
PrintTerminalTo prints the terminal screen contents to w for debugging. It uses only the public Terminal interface, with proper locking.
Types ¶
type Backend ¶
Backend provides the IO connection to the terminal emulator. Implementations should be initialized before constructing a Terminal.
type BatchFrontend ¶
type BatchFrontend interface {
// PrepareBatch is called with the terminal lock held and receives a
// LockedView, which is valid only for the duration of the call. It must
// render everything accumulated since the previous frame into
// frontend-owned memory and must not perform I/O: blocking here blocks
// every terminal reader.
//
// PrepareBatch is not called concurrently with itself; the terminal lock
// serializes the calls.
PrepareBatch(v LockedView)
// WriteBatch publishes the memory rendered by the most recent
// PrepareBatch. It is called after the terminal lock is released, so it
// may do I/O (write to a terminal, upload a texture, update a window).
//
// The terminal does not serialize WriteBatch calls: they can arrive from
// the read loop and from Resize/Restore concurrently, and in the general
// case from different goroutines. The frontend must serialize them itself
// (for example with its own mutex) and preserve frame order, so that the
// memory of a newer frame is never published before an older one.
//
// WriteBatch may also be called when nothing was rendered: the terminal
// calls the pair at every batch boundary, and a boundary with no
// accumulated damage still produces a PrepareBatch that has nothing to do.
WriteBatch()
}
BatchFrontend is an optional Frontend extension for damage-batched rendering. A frontend that implements it still receives every callback per change, but may accumulate the damage and render only when the terminal calls the batch methods.
The terminal calls the pair at these boundaries: when the read loop is about to block waiting for more input (so a lone write is still painted immediately), before the loop parks for Pause, after out-of-band screen updates such as Resize and Restore, and whenever the loop otherwise stops. After either method returns, no damage may be left unrendered.
The two calls have different locking and I/O rules, and the terminal may call WriteBatch from more than one goroutine: implementing the pair usually means rendering into a frontend-owned back buffer in PrepareBatch and publishing it, under a mutex the frontend owns, in WriteBatch. See the method comments.
type ChangeReason ¶
type ChangeReason int
ChangeReason says what kind of change caused the region to change, for optimization etc.
const ( // CRText means text is being printed normally. CRText ChangeReason = iota // CRClear means some area has been cleared CRClear // CRScroll means an area has been scrolled CRScroll // CRScreenSwitch means the screen has been switched between main and alt CRScreenSwitch // CRResize means the screen was resized. Content is preserved where // possible (not cleared), so frontends must repaint the full region // at the new dimensions. A dedicated reason (rather than reusing // CRClear) lets frontends skip clear-specific work such as resetting // scroll state. CRResize // CRRestore means a snapshot was restored with Terminal.Restore. // Frontends must repaint the full screen from the restored state. CRRestore // CRPalette means the color palette or a default color changed (OSC // 4/10/11/12/104/110/111/112), so every cell rendered with a palette or // default color may look different. Frontends that resolve colors // themselves must repaint the full screen. CRPalette )
type ClipboardSink ¶
type ClipboardSink interface {
Clipboard(v LockedView, selection string, data []byte)
}
ClipboardSink is an optional Frontend extension for the OSC 52 clipboard write path. If the Frontend implements it, the base64-decoded payload of an OSC 52 sequence is forwarded via Clipboard with the requested selection (usually "c") and the raw bytes.
Clipboard is called with the terminal lock held and receives a LockedView, so implementations should read terminal state through the view and must not call back into the terminal with locking methods. Clipboard queries (data == "?") are never answered or forwarded.
type Color ¶
type Color uint32
Color represents a terminal color value. Use the constructor functions to create values: DefaultColor, Color256, BrightColor, RGB.
func BrightColor ¶
BrightColor returns a bright variant (0–7) of the standard ANSI color.
func (Color) Index ¶
Index returns the 256-color or bright-color index. For RGB colors the lower 24 bits are returned.
type ColorComponent ¶
type ColorComponent uint8
ColorComponent specifies which color component of a Style to modify
const ( ComponentFG ColorComponent = iota ComponentBG ComponentUnderline )
type EmptyFrontend ¶
type EmptyFrontend struct{}
EmptyFrontend is a simple frontend that does nothing
func (*EmptyFrontend) Bell ¶
func (d *EmptyFrontend) Bell(v LockedView)
func (*EmptyFrontend) CursorMoved ¶
func (d *EmptyFrontend) CursorMoved(v LockedView, x, y int)
func (*EmptyFrontend) RegionChanged ¶
func (d *EmptyFrontend) RegionChanged(v LockedView, r Region, c ChangeReason)
func (*EmptyFrontend) ScrollLines ¶
func (d *EmptyFrontend) ScrollLines(v LockedView, delta int)
func (*EmptyFrontend) StyleChanged ¶
func (d *EmptyFrontend) StyleChanged(v LockedView, s Style)
func (*EmptyFrontend) ViewFlagChanged ¶
func (d *EmptyFrontend) ViewFlagChanged(v LockedView, vs ViewFlag, value bool)
func (*EmptyFrontend) ViewIntChanged ¶
func (d *EmptyFrontend) ViewIntChanged(v LockedView, vs ViewInt, value int)
func (*EmptyFrontend) ViewStringChanged ¶
func (d *EmptyFrontend) ViewStringChanged(v LockedView, vs ViewString, value string)
type Frontend ¶
type Frontend interface {
Bell(v LockedView)
RegionChanged(v LockedView, r Region, c ChangeReason)
// ScrollLines is called for every scroll of the main (not alternate)
// screen with the signed delta. Negative means content moved up (rows
// left the top); positive means content moved down. Scrollback capture
// and ScrollbackRowsAppended are separate.
ScrollLines(v LockedView, delta int)
CursorMoved(v LockedView, x, y int)
StyleChanged(v LockedView, s Style)
ViewFlagChanged(v LockedView, vs ViewFlag, value bool)
ViewIntChanged(v LockedView, vs ViewInt, value int)
ViewStringChanged(v LockedView, vs ViewString, value string)
}
Frontend is a type that can display data from a Terminal.
All Frontend methods are called with the terminal lock held and receive a LockedView as their first argument. The view is read-only and valid only for the duration of the callback: use it to read terminal state (Size, Line, Scrollback, ...) without locking. Do not retain it, and do not call Terminal write methods or read methods that take the lock themselves (plain Size/Line/Snapshot/... or the Terminal.Scrollback reader) from a callback, as that would deadlock. Use v.Snapshot() and v.Scrollback().Snapshot() to snapshot screen or scrollback state from a callback; Terminal.Snapshot and *Scrollback.Snapshot are for ordinary code.
type GraphemeReader ¶
type GraphemeReader struct {
// contains filtered or unexported fields
}
GraphemeReader buffers bytes and emits grapheme tokens for printable runs.
func NewGraphemeReader ¶
func NewGraphemeReader(src io.Reader) *GraphemeReader
func NewGraphemeReaderFromState ¶
func NewGraphemeReaderFromState(src io.Reader, st GraphemeState) *GraphemeReader
NewGraphemeReaderFromState creates a reader that resumes from st, including any buffered bytes and tokenizer state. The src is the new underlying stream.
func NewGraphemeReaderWithMode ¶
func NewGraphemeReaderWithMode(src io.Reader, mode TextReadMode) *GraphemeReader
func (*GraphemeReader) Buffered ¶
func (r *GraphemeReader) Buffered() int
func (*GraphemeReader) ReadByte ¶
func (r *GraphemeReader) ReadByte() (byte, error)
func (*GraphemeReader) ReadPrintableBytes ¶
ReadPrintableBytes reads a contiguous run of printable bytes from the stream. It stops before control bytes and leaves them for ReadByte. maxWidth limits the total cell width returned; pass <=0 for unlimited. merge is true when the returned text should be merged into the previous cell.
func (*GraphemeReader) ReadPrintableTokens ¶
func (r *GraphemeReader) ReadPrintableTokens(maxWidth int) ([]GraphemeToken, error)
ReadPrintableTokens reads a contiguous run of printable bytes from the stream. It stops before control bytes and leaves them for ReadByte. maxWidth limits the total cell width returned; pass <=0 for unlimited.
func (*GraphemeReader) ReadPrintableTokensInto ¶
func (r *GraphemeReader) ReadPrintableTokensInto(maxWidth int, dst []GraphemeToken) ([]GraphemeToken, error)
ReadPrintableTokensInto is like ReadPrintableTokens but reuses dst when provided. TODO: allow a token sink for direct screen writes without slice allocation.
func (*GraphemeReader) SnapshotState ¶
func (r *GraphemeReader) SnapshotState() GraphemeState
SnapshotState returns a deep copy of the reader's buffered bytes and tokenizer state. The receiver is not modified.
type GraphemeState ¶
type GraphemeState struct {
Pending []byte
State int
// Initial is true when State has not been set yet; the reader then
// starts from the tokenizer's initial state (-1) instead of state 0,
// which would incorrectly continue a previous grapheme.
Initial bool
ForceMergeNext bool
LastWasRI bool
RunHasBase bool
LastWasHanging bool
LastWasSynthetic bool
Mode TextReadMode
}
GraphemeState is the user-space tokenizer/parser state needed to resume a GraphemeReader in another process. Pending holds the bytes already read from the source but not yet consumed (data[start:end]).
type GraphemeToken ¶
GraphemeToken represents a grapheme cluster or a merge fragment. Bytes is a copy of the input bytes to keep tokens safe across buffer refills. When Merge is true, the token should be merged into the previous cell.
type Handoff ¶
type Handoff struct {
Snapshot Snapshot
Scrollback ScrollbackSnapshot
Reader ReaderState
Size Pos
TextMode TextReadMode
}
Handoff is everything needed to resume a terminal in a new process. Backend fds are transferred separately (see PTYBackend.Master/Slave).
type KeyCode ¶
type KeyCode int
const ( KeyRune KeyCode = iota KeyUp KeyDown KeyRight KeyLeft KeyHome KeyEnd KeyInsert KeyDelete KeyPageUp KeyPageDown KeyBackspace KeyTab KeyEnter KeyEscape KeyF1 KeyF2 KeyF3 KeyF4 KeyF5 KeyF6 KeyF7 KeyF8 KeyF9 KeyF10 KeyF11 KeyF12 KeyF13 KeyF14 KeyF15 KeyF16 KeyF17 KeyF18 KeyF19 KeyF20 KeyF21 KeyF22 KeyF23 KeyF24 KeyF25 KeyF26 KeyF27 KeyF28 KeyF29 KeyF30 KeyF31 KeyF32 KeyF33 KeyF34 KeyF35 KeyCapsLock KeyScrollLock KeyNumLock KeyPrintScreen KeyPause KeyMenu KeyKP0 KeyKP1 KeyKP2 KeyKP3 KeyKP4 KeyKP5 KeyKP6 KeyKP7 KeyKP8 KeyKP9 KeyKPDecimal KeyKPDivide KeyKPMultiply KeyKPSubtract KeyKPAdd KeyKPEnter KeyKPEqual KeyKPSeparator KeyKPLeft KeyKPRight KeyKPUp KeyKPDown KeyKPPageUp KeyKPPageDown KeyKPHome KeyKPEnd KeyKPInsert KeyKPDelete KeyKPBegin KeyMediaPlay KeyMediaPause KeyMediaPlayPause KeyMediaReverse KeyMediaStop KeyMediaFastForward KeyMediaRewind KeyMediaTrackNext KeyMediaTrackPrev KeyMediaRecord KeyVolumeDown KeyVolumeUp KeyVolumeMute KeyLeftShift KeyLeftControl KeyLeftAlt KeyLeftSuper KeyLeftHyper KeyLeftMeta KeyRightShift KeyRightControl KeyRightAlt KeyRightSuper KeyRightHyper KeyRightMeta KeyISOLevel3Shift KeyISOLevel5Shift )
type KeyEvent ¶
type KeyEvent struct {
// Code is required. Use KeyRune for text-producing keys.
Code KeyCode
// Rune is required when Code == KeyRune. Otherwise it is ignored.
Rune rune
// Mod is a bitmask of active modifiers. For modifier key events, include
// the modifier state resulting from the event.
Mod KeyMod
// Event defaults to KeyPress when zero. Repeat and release require the
// report-events enhancement.
Event KeyEventType
// Shifted and BaseLayout are optional alternate key codes used when the
// report-alternates enhancement is enabled. Shifted should only be set if
// shift is pressed.
Shifted rune
BaseLayout rune
// Text is optional associated text codepoints used when report-text is
// enabled with report-all-keys. If empty, Rune is used for KeyRune.
Text []rune
}
type KeyEventType ¶
type KeyEventType uint8
KeyEventType is press/repeat/release. Zero is treated as press.
const ( KeyPress KeyEventType = iota + 1 KeyRepeat KeyRelease )
type KeyMod ¶
type KeyMod uint8
KeyMod is a bitmask of active modifier keys. It follows the Kitty keyboard protocol bit assignments.
type KeyboardEnhancement ¶
type KeyboardEnhancement int
KeyboardEnhancement flags correspond to the Kitty progressive enhancement bits.
const ( KbdDisambiguate KeyboardEnhancement = 1 << iota KbdReportEvents KbdReportAlternates KbdReportAllKeys KbdReportText )
type Line ¶
Line holds a list of spans
func (Line) Equivalent ¶
Equivalent reports whether l and other cover the same cells with the same content, style, and OSC 8 link. It is a semantic comparison: span packing is ignored, so a run of blanks stored as Span{Width: n} (empty Text) is equivalent to the same blanks stored as a Text span of spaces. Wide characters occupy their full cell width, and zero-width content (combining marks, for example) is compared in order at the same cell position rather than being dropped.
Equivalent does not compare the structural representation of the lines; use reflect.DeepEqual or cmp for that.
func (Line) PlainTextString ¶
type LockedScrollback ¶
type LockedScrollback struct {
// contains filtered or unexported fields
}
LockedScrollback is the no-lock counterpart of *Scrollback, obtained from LockedView.Scrollback. It is valid only for the duration of the callback or View that produced the view. Its methods do not lock, so they are safe to call from a Frontend callback (unlike *Scrollback methods). Its Snapshot is the callback-safe snapshot entry point; *Scrollback.Snapshot is for ordinary code.
func (LockedScrollback) Len ¶
func (s LockedScrollback) Len() int
Len returns the number of stored scrollback rows.
func (LockedScrollback) LogicalLine ¶
func (s LockedScrollback) LogicalLine(id ScrollID) []Row
LogicalLine returns all physical rows that form the logical line containing the given row ID. See Scrollback.LogicalLine for details on how reflown rows report IDs (one stored ID per row, can repeat).
func (LockedScrollback) MovePos ¶
func (s LockedScrollback) MovePos(pos ScrollPos, delta int) ScrollPos
MovePos returns a new ScrollPos offset by delta physical rows from pos.
func (LockedScrollback) Range ¶
func (s LockedScrollback) Range() (base, next ScrollID)
Range returns the IDs of the oldest and next-to-be-assigned rows.
func (LockedScrollback) Row ¶
func (s LockedScrollback) Row(id ScrollID) (Row, bool)
Row returns the physical row with the given ID, or false if it has been evicted.
func (LockedScrollback) Snapshot ¶
func (s LockedScrollback) Snapshot() ScrollbackSnapshot
Snapshot returns a deep copy of every stored row without taking the terminal lock. It is the callback-safe counterpart of *Scrollback.Snapshot. A LockedScrollback with no terminal returns an empty snapshot.
type LockedView ¶
type LockedView struct {
// contains filtered or unexported fields
}
LockedView is a read-only handle to terminal state. It is valid only for the duration of the call that received it (a Frontend callback or Terminal.View); do not retain it. Its methods do not lock, which makes Snapshot and Scrollback (and LockedScrollback.Snapshot) the callback-safe snapshot entry points. Terminal.Snapshot and *Scrollback.Snapshot are for ordinary code and would deadlock here.
func (LockedView) ANSILine ¶
func (v LockedView) ANSILine(y int) string
ANSILine returns the ANSI-escaped content of line y.
func (LockedView) CursorColor ¶
func (v LockedView) CursorColor() Color
CursorColor returns the cursor color (OSC 12).
func (LockedView) CursorPos ¶
func (v LockedView) CursorPos() Pos
CursorPos returns the cursor position.
func (LockedView) DefaultBackground ¶
func (v LockedView) DefaultBackground() Color
DefaultBackground returns the default background color (OSC 11).
func (LockedView) DefaultForeground ¶
func (v LockedView) DefaultForeground() Color
DefaultForeground returns the default foreground color (OSC 10).
func (LockedView) GetViewFlag ¶
func (v LockedView) GetViewFlag(f ViewFlag) bool
GetViewFlag returns the value of a boolean terminal flag.
func (LockedView) GetViewInt ¶
func (v LockedView) GetViewInt(i ViewInt) int
GetViewInt returns the value of an integer terminal setting.
func (LockedView) GetViewString ¶
func (v LockedView) GetViewString(s ViewString) string
GetViewString returns the value of a string terminal setting.
func (LockedView) Line ¶
func (v LockedView) Line(y int) string
Line returns the plain text content of line y.
func (LockedView) PaletteColor ¶
func (v LockedView) PaletteColor(idx int) Color
PaletteColor returns the RGB color mapped to palette index idx. Indexes outside 0-255 return the default color.
func (LockedView) Scrollback ¶
func (v LockedView) Scrollback() LockedScrollback
Scrollback returns a no-lock scrollback reader for use inside callbacks.
func (LockedView) Size ¶
func (v LockedView) Size() (w, h int)
Size returns the terminal width and height.
func (LockedView) Snapshot ¶
func (v LockedView) Snapshot() Snapshot
Snapshot returns a deep copy of the terminal's screen state without taking the terminal lock. It is the callback-safe counterpart of Terminal.Snapshot. Like Terminal.Snapshot, it excludes the scrollback buffer and backend/process state. A zero LockedView returns an empty Snapshot.
func (LockedView) StyledLine ¶
func (v LockedView) StyledLine(x, w, y int) Line
StyledLine returns the styled content of a line region.
func (LockedView) StyledLines ¶
func (v LockedView) StyledLines(r Region) []Line
StyledLines returns the styled content of multiple lines in a region.
type NoPTYBackend ¶
type NoPTYBackend struct {
// contains filtered or unexported fields
}
NoPTYBackend implements Backend using provided IO streams.
func NewNoPTYBackend ¶
func NewNoPTYBackend(r io.Reader, w io.Writer) *NoPTYBackend
NewNoPTYBackend returns a backend using the provided reader and writer.
func (*NoPTYBackend) Close ¶
func (b *NoPTYBackend) Close() error
func (*NoPTYBackend) SetReadDeadline ¶
func (b *NoPTYBackend) SetReadDeadline(t time.Time) error
SetReadDeadline forwards to the underlying reader when it supports deadlines (for example *os.File or an os.Pipe). Otherwise it returns errors.ErrUnsupported. Terminal.Pause then cannot interrupt a blocked read until the next byte arrives.
func (*NoPTYBackend) SetSize ¶
func (b *NoPTYBackend) SetSize(w, h int) error
type NotificationSink ¶
type NotificationSink interface {
Notify(v LockedView, title, body string)
}
NotificationSink is an optional Frontend extension for OSC 9 and OSC 777 desktop notifications. OSC 9 carries a body only (title is empty); OSC 777 carries "notify;title;body". It is called with the terminal lock held.
type PTYBackend ¶
type PTYBackend struct {
// AutoEnv lists the environment variables Attach injects into commands,
// in "KEY=VALUE" form. nil applies the defaults (TERM and COLORTERM);
// set it to an empty non-nil slice to inject nothing, or to a custom
// list to override the defaults and add your own variables. Injected
// values replace existing entries with the same key in cmd.Env.
AutoEnv []string
// contains filtered or unexported fields
}
PTYBackend implements Backend using github.com/creack/pty. A single pty pair is shared by every command attached to the backend, so commands can be run back-to-back through the same terminal state.
func (*PTYBackend) Adopt ¶
func (p *PTYBackend) Adopt(master, slave *os.File) error
Adopt adopts an already-open pty pair. master is required; slave may be nil when only the master is available. The backend takes ownership of the files (Close closes them). Call before constructing the Terminal or opening the pty backend. With slave == nil, Attach is unavailable and Read does not wait for reuse on EOF/EIO.
Use os.NewFile(fd, "pts") to wrap raw pty file descriptors before calling Adopt.
func (*PTYBackend) AdoptMaster ¶
func (p *PTYBackend) AdoptMaster(master *os.File) error
AdoptMaster is Adopt(master, nil). With no slave, Open and Attach are unavailable and Read surfaces EOF/EIO instead of waiting for reuse.
func (*PTYBackend) Attach ¶
func (p *PTYBackend) Attach(c *exec.Cmd) error
Attach wires an *exec.Cmd to run inside the PTY. The PTY pair is opened lazily on first call and reused on subsequent calls. Caller starts the command themselves via cmd.Start().
func (*PTYBackend) Close ¶
func (p *PTYBackend) Close() error
Close shuts down the pty pair so a blocked Read returns promptly, and prevents any later lazy reopen: subsequent Read/Write/Attach calls fail with io.ErrClosedPipe. It is idempotent.
func (*PTYBackend) Master ¶
func (p *PTYBackend) Master() *os.File
Master returns the pty master file. The backend still owns it; do not close it while handing it off. It returns nil if the pty cannot be opened (for example after Close).
func (*PTYBackend) Open ¶
func (p *PTYBackend) Open() (*os.File, error)
Open creates a new pty pair and returns the slave for external use. The pair is created lazily, so it is safe to call this before or after constructing a Terminal. It returns an error when the backend was created by Adopt with a nil slave, because there is nothing to return or attach.
func (*PTYBackend) PID ¶
func (p *PTYBackend) PID() int
PID returns the process ID of the most recently attached command, or 0 if no command has been attached or it has not been started yet. It is safe to call from any goroutine.
func (*PTYBackend) Read ¶
func (p *PTYBackend) Read(b []byte) (int, error)
Read reads from the pty master. When the current child has exited the master returns syscall.EIO on Linux and io.EOF on some platforms (Darwin); because the pty stays valid for the next attached command, Read waits for one instead of surfacing that as EOF. That reuse behavior requires a slave: for an adopted master-only pty, Read performs a plain master read and surfaces EOF/EIO so the read loop can exit. After Close, Read returns io.ErrClosedPipe instead of retrying. NoPTYBackend keeps EOF terminal.
func (*PTYBackend) SetReadDeadline ¶
func (p *PTYBackend) SetReadDeadline(t time.Time) error
SetReadDeadline sets a read deadline on the pty master. It is used by Terminal.Pause to interrupt a blocked read.
func (*PTYBackend) SetSize ¶
func (p *PTYBackend) SetSize(w, h int) error
func (*PTYBackend) Slave ¶
func (p *PTYBackend) Slave() *os.File
Slave returns the pty slave file, or nil when the backend was adopted with only a master (see AdoptMaster). The backend still owns it; do not close it while handing it off.
func (*PTYBackend) Wait ¶
func (p *PTYBackend) Wait() error
Wait waits for the most recently attached command to exit and returns the error from (*exec.Cmd).Wait. It errors if no command has been attached.
The caller must not also call cmd.Wait() on the attached command: like os/exec, only one Wait may consume the process result. A second call to PTYBackend.Wait returns an "exec: Wait was already called" error. Wait may be called after Close (the child still needs reaping) and after a later Attach, in which case it waits for that newer command.
func (*PTYBackend) Wake ¶
func (p *PTYBackend) Wake()
Wake interrupts a Read that is waiting for the next attached command, so a Pause does not have to wait out the reuse backoff. It is safe to call from any goroutine.
type PromptMark ¶
type PromptMark struct {
Kind PromptMarkKind
// ExitCode is the status reported by OSC 133;D, or -1 when absent (it is
// always -1 for the other kinds).
ExitCode int
// Raw is the full parameter string that followed the kind letter, for
// marks that carry extension data this package does not decode.
Raw string
}
PromptMark is a decoded OSC 133 shell-integration mark.
type PromptMarkKind ¶
type PromptMarkKind int
PromptMarkKind identifies one of the OSC 133 semantic prompt marks that shells and prompt frameworks emit for shell integration.
const ( // PromptMarkStart is OSC 133;A: the prompt is about to be drawn. PromptMarkStart PromptMarkKind = iota // PromptMarkInput is OSC 133;B: the prompt ended and user input starts. PromptMarkInput // PromptMarkCommand is OSC 133;C: the command was accepted and its // output starts. PromptMarkCommand // PromptMarkDone is OSC 133;D: the command finished. ExitCode carries // the reported status, or -1 when the sequence did not include one. PromptMarkDone )
type ReaderState ¶
type ReaderState struct {
Pending []byte // GraphemeReader buffered bytes (data[start:end])
PendingCommand []byte // partial ESC-initiated command (ESC already consumed)
State int // GraphemeReader UTF-8/grapheme state
// Initial marks a reader that has not processed any bytes yet; State is
// then ignored and the reader starts from the tokenizer's initial state.
Initial bool
ForceMergeNext bool
LastWasRI bool
RunHasBase bool
LastWasHanging bool
LastWasSynthetic bool
Mode TextReadMode
}
ReaderState is the read-loop state that lives in user space: bytes already read from the backend but not applied, plus any partial escape command.
type Region ¶
type Region struct {
X, Y, X2, Y2 int
}
Region is an non-inclusive rectangle on the screen. X2 and Y2 are not included in the region.
type Row ¶
Row represents a single physical row from the scrollback buffer. Spans is a copy owned by the caller: mutating it does not affect the stored history.
func (Row) Equivalent ¶
Equivalent reports whether r and other cover the same cells with the same content, style, and OSC 8 link. Wrapped and ID are ignored; see Line.Equivalent for the cell comparison semantics.
func (Row) PlainTextString ¶
PlainTextString returns the row's text with styling dropped.
type ScreenState ¶
type ScreenState struct {
Width, Height int
Lines []Line // full-width rows; Spans are copies
Wrapped []bool // auto-wrap continuation flags, len == Height
Cursor Pos
PendingWrap bool // next printable write wraps before writing
SavedCursor Pos
SavedPendingWrap bool // pending-wrap state saved by DECSC
SavedStyle Style
// SavedCharsets is the G0-G3/GL state saved by DECSC; Charsets is the
// current character set state.
SavedCharsets charsetState
Charsets charsetState
// InsertMode is IRM; OriginMode/SavedOriginMode are DECOM and its DECSC
// slot. LastChar is the last graphic code point replayed by REP (0 means
// none). Tabs is the horizontal tab stop map (nil means the default stops
// every 8 columns).
InsertMode bool
OriginMode bool
SavedOriginMode bool
LastChar rune
Tabs []bool
Style Style
TopMargin int
BottomMargin int
AutoWrap bool
}
ScreenState is a deep copy of one screen buffer.
type ScrollID ¶
type ScrollID uint64
ScrollID is a monotonically increasing identifier for a physical row in the scrollback buffer. IDs are globally unique per terminal and stable even as new lines are added or old lines are evicted. Use Range to discover the set of valid IDs.
type ScrollPos ¶
type ScrollPos struct {
ID ScrollID
}
ScrollPos identifies a position within the scrollback by its physical row ID.
type Scrollback ¶
type Scrollback struct {
// contains filtered or unexported fields
}
Scrollback provides read access to the terminal's scrollback buffer. All methods handle their own locking and are safe to call from any goroutine, except from within a Frontend callback: callbacks run with the terminal lock already held, so calling any Scrollback method there deadlocks. Collect what you need before/after instead.
Rows handed out by Row and LogicalLine own their Spans slices (copies), so callers may freely mutate the returned Rows.
func (*Scrollback) Clear ¶
func (s *Scrollback) Clear()
Clear drops all stored scrollback rows. ScrollIDs stay monotonic: after Clear, Range reports base == next == the current counter, and subsequently captured rows continue from that counter (no ID is ever reused).
func (*Scrollback) Len ¶
func (s *Scrollback) Len() int
Len returns the number of stored scrollback rows.
func (*Scrollback) LogicalLine ¶
func (s *Scrollback) LogicalLine(id ScrollID) []Row
LogicalLine returns all physical rows that form the logical line containing the given row ID. The rows are reflown at the terminal's current width, so the number and sizes of returned rows may differ from the captured storage. Returns nil if the ID has been evicted. The returned Rows own their span slices; mutating them does not affect the stored history.
Every returned Row carries the ScrollID of the stored physical row that contains its first cell. When reflow splits a stored row across several reflown rows they share that stored row's ID, and because reflown rows may straddle a boundary between two stored rows, the reported IDs are non-decreasing and may repeat but are always non-zero. Row(id) still works for each reported ID; it returns the stored (captured) row, not the reflown one.
func (*Scrollback) MovePos ¶
func (s *Scrollback) MovePos(pos ScrollPos, delta int) ScrollPos
MovePos returns a new ScrollPos offset by delta physical rows from pos. Negative delta moves toward older rows; positive moves toward newer ones. If delta would go out of bounds, the result is clamped to the valid range. Note: MovePos operates on the stored physical row count, not on reflown rows. Use LogicalLine to discover how a stored row expands at the current width.
func (*Scrollback) Range ¶
func (s *Scrollback) Range() (base, next ScrollID)
Range returns the IDs of the oldest and next-to-be-assigned rows. Valid IDs satisfy base <= id < next.
func (*Scrollback) Restore ¶
func (s *Scrollback) Restore(snap ScrollbackSnapshot) error
Restore replaces the scrollback contents with snap. The snapshot's Base and Next IDs are preserved, so IDs continue monotonically from where the snapshot left off. If snap has more rows than the current capacity, only the newest rows are kept and Base is advanced accordingly. Restore returns ErrScrollbackSnapshotInvalid without changing the buffer when the snapshot is malformed (non-contiguous IDs, Next/Base inconsistent with the row count, or a zero row ID).
func (*Scrollback) Row ¶
func (s *Scrollback) Row(id ScrollID) (Row, bool)
Row returns the physical row with the given ID, or false if it has been evicted. The returned Row owns a copy of the stored spans.
func (*Scrollback) Snapshot ¶
func (s *Scrollback) Snapshot() ScrollbackSnapshot
Snapshot returns a deep copy of every stored row, oldest to newest, along with the ID range. Mutating the snapshot does not affect the buffer. Scrollback snapshots are independent of Terminal.Snapshot, which excludes the scrollback buffer; use both when restoring a full terminal session.
type ScrollbackListener ¶
type ScrollbackListener interface {
ScrollbackRowsAppended(v LockedView, first, next ScrollID)
}
ScrollbackListener is an optional Frontend extension. When rows are appended to the main-screen scrollback, ScrollbackRowsAppended is called once per capture batch with the terminal lock held and the range [first, next) of newly assigned ScrollIDs. Use the LockedView (including v.Scrollback()) to read the rows; do not retain the view. Alternate-screen output never produces rows. Scrollback.Clear() does not call this.
type ScrollbackSnapshot ¶
type ScrollbackSnapshot struct {
Rows []Row // oldest -> newest, deep copies
Base ScrollID // ID of Rows[0]
Next ScrollID // next ID to assign
}
ScrollbackSnapshot is a deep copy of a scrollback buffer. Rows are ordered oldest to newest; Base is the ID of Rows[0] and Next is the next ID that would be assigned. It is separate from Terminal.Snapshot/Restore, which deliberately exclude the scrollback buffer.
type ShellIntegration ¶
type ShellIntegration interface {
PromptMark(v LockedView, mark PromptMark)
}
ShellIntegration is an optional Frontend extension for OSC 133 semantic prompt marks (shell integration). It is called with the terminal lock held; read state through the view and do not call locking methods on the terminal. Unknown OSC 133 marks (for example 133;P properties) are not forwarded.
type Snapshot ¶
type Snapshot struct {
Main, Alt ScreenState
OnAltScreen bool
ViewFlags []bool
ViewInts []int
ViewStrings []string
KeyboardMain keyboardMode
KeyboardAlt keyboardMode
Link string // active OSC 8 hyperlink, if any
NewlineMode bool // LNM (CSI 20 h/l)
// Palette and default colors are resolved by frontends rather than stored
// in cells, so they travel with the snapshot.
Palette [paletteSize]Color
DefaultFG Color
DefaultBG Color
DefaultCursor Color
}
Snapshot is a deep copy of terminal state. It excludes the scrollback buffer and backend/process state.
type Span ¶
type Span struct {
Style Style // Complete text styling (colors and modes)
Text string
Width int
// Link is the OSC 8 hyperlink URI active when the span was written.
// Empty means no link. Blank/erased spans never carry a link.
Link string
}
Span represents a run of text with consistent styling. If Text is not empty, it contains the text content. If Text is empty, the span represents Width spaces; a Width of zero (or less) is empty.
type Style ¶
type Style struct {
// contains filtered or unexported fields
}
Style represents complete text styling including colors and text modes. Modes are distributed across the high bytes (bits 24-30) of fg, bg, and underlineColor. Color data uses bits 0-23, and bit 31 is the color type flag (256-color vs RGB).
func NewStyle ¶
func NewStyle() Style
NewStyle creates a default style with no colors and no modes set
func (Style) ANSIEscape ¶
ANSIEscape generates a complete ANSI escape sequence for this style
func (Style) ANSIEscapeFrom ¶
ANSIEscapeFrom generates a minimal ANSI escape sequence that changes from prev to this style
func (*Style) GetColor ¶
func (s *Style) GetColor(component ColorComponent) (Color, error)
GetColor returns the Color for a component.
func (Style) MarshalJSON ¶
MarshalJSON encodes the style as its packed internal representation. The numbers are not palette indexes: they are Style's raw uint32 fields (color data in bits 0-23, modes in bits 24-30, RGB flag in bit 31).
func (*Style) Reset ¶
Reset clears specific text modes, or resets everything when no modes are given.
style.Reset() // reset all colors and modes style.Reset(ModeBold) // clear only bold
func (*Style) SetColor ¶
func (s *Style) SetColor(component ColorComponent, color Color) error
SetColor sets a color component to the given Color value.
func (*Style) UnmarshalJSON ¶
UnmarshalJSON decodes a style previously produced by MarshalJSON. A JSON null leaves the style unchanged.
type TTYFrontend ¶
type TTYFrontend struct {
// contains filtered or unexported fields
}
TTYFrontend renders changed regions to a terminal (tty) output. It can be attached to a region of the screen and detached to stop updates.
Rendering is damage-batched: RegionChanged unions the changed area into a pending frame instead of repainting immediately. The terminal calls PrepareBatch/WriteBatch (see BatchFrontend) at its batch boundaries, so a burst of escape-dense output costs one render pass and one write instead of one per callback. Rows whose rendered bytes match the previous frame are skipped, which makes an unchanged full-screen repaint nearly free.
Lock ordering: the terminal lock always ranks above the frontend mutex. Callback methods (invoked with the terminal lock held) may take t.mu, but public methods must never hold t.mu while taking the terminal lock. Attach snapshots state under t.mu, releases it, then renders under the terminal lock. writeMu is a leaf lock serializing output writes; it is never taken while the terminal lock is held.
func NewTTYFrontend ¶
func NewTTYFrontend(out io.Writer) *TTYFrontend
NewTTYFrontend returns a frontend that writes to out (defaults to stdout). The terminal is set automatically when the Terminal is created with NewWithMode.
func (*TTYFrontend) Attach ¶
func (t *TTYFrontend) Attach(r Region)
Attach starts updating the provided region.
Locking: this takes t.mu only to update fields and snapshot state, then releases it before taking the terminal lock to render. It must never hold t.mu while taking the terminal lock: RegionChanged runs with the terminal lock held and takes t.mu, so the opposite order would deadlock.
func (*TTYFrontend) Bell ¶
func (t *TTYFrontend) Bell(v LockedView)
func (*TTYFrontend) Blur ¶
func (t *TTYFrontend) Blur()
Blur disables cursor updates and restores cursor visibility.
func (*TTYFrontend) CursorMoved ¶
func (t *TTYFrontend) CursorMoved(v LockedView, x, y int)
func (*TTYFrontend) Detach ¶
func (t *TTYFrontend) Detach()
Detach stops updating the attached region.
func (*TTYFrontend) Focus ¶
func (t *TTYFrontend) Focus()
Focus enables cursor updates and visibility for this frontend.
func (*TTYFrontend) PrepareBatch ¶
func (t *TTYFrontend) PrepareBatch(view LockedView)
PrepareBatch renders all damage accumulated since the last frame into the pending output buffer. It is called with the terminal lock held and does no I/O. Implements BatchFrontend.
func (*TTYFrontend) RegionChanged ¶
func (t *TTYFrontend) RegionChanged(view LockedView, r Region, reason ChangeReason)
func (*TTYFrontend) ScrollLines ¶
func (t *TTYFrontend) ScrollLines(v LockedView, y int)
func (*TTYFrontend) SetFocus ¶
func (t *TTYFrontend) SetFocus(focused bool)
SetFocus sets focus state. When unfocused, cursor updates are suppressed.
func (*TTYFrontend) SetTerminal ¶
func (t *TTYFrontend) SetTerminal(term Terminal)
SetTerminal updates the terminal used for rendering.
func (*TTYFrontend) StyleChanged ¶
func (t *TTYFrontend) StyleChanged(v LockedView, s Style)
func (*TTYFrontend) ViewFlagChanged ¶
func (t *TTYFrontend) ViewFlagChanged(v LockedView, flag ViewFlag, value bool)
func (*TTYFrontend) ViewIntChanged ¶
func (t *TTYFrontend) ViewIntChanged(v LockedView, flag ViewInt, value int)
func (*TTYFrontend) ViewStringChanged ¶
func (t *TTYFrontend) ViewStringChanged(v LockedView, flag ViewString, value string)
func (*TTYFrontend) WriteBatch ¶
func (t *TTYFrontend) WriteBatch()
WriteBatch writes the output prepared by PrepareBatch. It is called without the terminal lock; concurrent calls are serialized by writeMu and preserve frame order. Implements BatchFrontend.
type TeeBackend ¶
type TeeBackend struct {
// contains filtered or unexported fields
}
TeeBackend duplicates all reads into tee.
func NewTeeBackend ¶
func NewTeeBackend(backend Backend) *TeeBackend
NewTeeBackend returns a TeeBackend wrapping the provided backend.
func (*TeeBackend) Close ¶
func (t *TeeBackend) Close() error
Close forwards to the wrapped backend when it implements io.Closer.
func (*TeeBackend) SetReadDeadline ¶
func (t *TeeBackend) SetReadDeadline(d time.Time) error
func (*TeeBackend) SetSize ¶
func (t *TeeBackend) SetSize(w, h int) error
func (*TeeBackend) Wake ¶
func (t *TeeBackend) Wake()
SetReadDeadline forwards to the wrapped backend when it supports deadlines. Wake forwards to the wrapped backend when it implements Wake.
type Terminal ¶
type Terminal interface {
SetFrontend(f Frontend)
// View runs fn with a read-only view of terminal state while holding
// the terminal lock. Use it for atomic multi-reads. The view is only
// valid for the duration of fn; do not retain it. Do not call the
// locking read methods (Size, Line, Scrollback, ...) from fn.
View(func(LockedView))
Write(b []byte) (int, error)
SendKey(KeyEvent) (int, error)
SendMouseRaw(btn MouseBtn, press bool, mods MouseFlag, x, y int) error
Resize(int, int) error
// Size returns the terminal width and height. It takes the terminal
// lock itself, so it is safe from any goroutine but must not be called
// from inside View or a Frontend callback (use the view there).
Size() (int, int)
// CursorPos returns the cursor position. It locks internally; use the
// view from callbacks.
CursorPos() Pos
// Line returns the plain text content of line y. It locks internally;
// use the view from callbacks.
Line(int) string
// ANSILine returns the ANSI-escaped content of line y. It locks
// internally; use the view from callbacks.
ANSILine(y int) string
// StyledLine returns the styled content of a line region. It locks
// internally; use the view from callbacks.
StyledLine(x, w, y int) Line
// StyledLines returns the styled content of multiple lines in a region.
// It locks internally; use the view from callbacks.
StyledLines(r Region) []Line
// GetViewFlag, GetViewInt and GetViewString return terminal view
// settings. They lock internally; use the view from callbacks.
GetViewFlag(ViewFlag) bool
GetViewInt(ViewInt) int
GetViewString(ViewString) string
// PaletteColor returns the RGB color mapped to a palette index (OSC 4),
// and the default color accessors report the OSC 10/11/12 colors. They
// lock internally; use the LockedView counterparts in callbacks.
PaletteColor(idx int) Color
DefaultForeground() Color
DefaultBackground() Color
CursorColor() Color
// Scrollback returns the terminal's scrollback buffer for ordinary
// code. Its methods take the terminal lock themselves, so they must
// not be called from inside View or a Frontend callback. Use
// LockedView.Scrollback there instead.
Scrollback() *Scrollback
// Snapshot returns a deep copy of the terminal's screen state. It
// locks internally, so it must not be called from inside View or a
// Frontend callback (use LockedView.Snapshot there). The scrollback
// buffer and backend/process state are not included. Mutating the
// returned Snapshot does not affect the terminal, and vice versa.
Snapshot() Snapshot
// Restore replaces the terminal's screen state with a previously
// captured Snapshot and emits a full repaint. It locks internally. A
// snapshot of a different size is resized to the snapshot dimensions
// and the backend is resized too. The scrollback buffer is untouched.
Restore(Snapshot) error
// Close shuts down the terminal: it closes the backend (if it
// implements io.Closer) so a blocked Read returns and the read loop
// exits. It is idempotent and never blocks waiting for the loop.
Close() error
// Pause stops the read loop, captures the user-space reader/parser
// state plus a snapshot and scrollback snapshot, and leaves the
// terminal paused. Resume continues a same-process paused loop.
// For a cross-process handoff, Pause then transfer the backend fds
// (see PTYBackend.Master/Slave) and close this terminal.
Pause(context.Context) (Handoff, error)
Resume() error
// Done is closed when the read loop exits.
Done() <-chan struct{}
// Err reports how the read loop terminated: nil for a clean EOF,
// otherwise the error that stopped the loop. It is only meaningful
// after Done is closed.
Err() error
}
func New ¶
New makes a new terminal using the provided Frontend, Backend, and default text read mode. The default mode is TextReadModeGrapheme, which is the correct terminal behavior for emoji, combining marks and other grapheme clusters. Use NewWithMode to select TextReadModeRune explicitly.
func NewFromHandoff ¶
func NewFromHandoff(f Frontend, backend Backend, h Handoff, opts ...TerminalOption) (Terminal, error)
NewFromHandoff creates a terminal from a Handoff and starts its read loop with the captured reader state. The terminal is created paused (no reads happen) until the snapshot and scrollback have been restored. Handoff.Size is the target size; if the snapshot was captured at a different size its content is resized to fit. The caller owns backend and must arrange any cross-process fd transfer beforehand (PTYBackend.Adopt/AdoptMaster). On a restore error the backend is closed.
func NewWithMode ¶
func NewWithMode(f Frontend, backend Backend, mode TextReadMode, opts ...TerminalOption) Terminal
NewWithMode makes a new terminal using the provided Frontend, Backend, and text read mode. If the frontend implements SetTerminal (e.g. *TTYFrontend), it is wired automatically. Optional TerminalOptions configure scrollback size and other settings.
type TerminalOption ¶
type TerminalOption func(*terminal)
TerminalOption configures a Terminal at construction time.
func WithScrollback ¶
func WithScrollback(rows int) TerminalOption
WithScrollback sets the scrollback row capacity. Rows <= 0 uses DefaultScrollbackRows.
type TextReadMode ¶
type TextReadMode int
TextReadMode controls how printable text is tokenized from the input stream.
const ( // TextReadModeRune emits one token per rune. Zero-width runes (combining // marks, VS16, ZWJ, emoji modifiers) merge into the previous cell, but // rune mode does not cluster whole ZWJ sequences: a ZWJ family such as // "👨\u200d👩\u200d👧\u200d👦" occupies 8 cells (each person is width 2) // while the same sequence is 2 cells in grapheme mode. TextReadModeRune TextReadMode = iota // TextReadModeGrapheme emits one token per grapheme cluster. TextReadModeGrapheme )
type ViewFlag ¶
type ViewFlag int
ViewFlag is an enum of boolean flags on a terminal
const ( VFBlinkCursor ViewFlag = iota VFShowCursor VFReportFocus VFBracketedPaste VFAppCursorKeys VFAppKeypad // VFAltScroll tracks DECSET/DECRST 1007 (alternate scroll mode): on the // alternate screen, with no mouse mode active, wheel events are sent to // the application as cursor up/down keys instead of mouse reports. VFAltScroll // VFMousePixel tracks DECSET/DECRST 1016 (SGR-Pixels mouse reporting). // While set, the frontend should pass pixel coordinates to SendMouseRaw; // the coordinates are encoded verbatim (SGR-Pixels pairs with mode 1006). VFMousePixel // VFAltScreenAllowed tracks DEC private mode 1046. While clear, requests // to switch to the alternate screen (?47/?1047/?1049) are ignored. VFAltScreenAllowed // VFSynchronizedOutput tracks DECSET/DECRST 2026 (synchronized output). // While set, the application is emitting a batch of updates and // frontends may defer repaints until it is cleared. Damage batching // itself is opt-in through BatchFrontend. VFSynchronizedOutput )
type ViewString ¶
type ViewString int
ViewString is an enum of string settings on a terminal
const ( VSWindowTitle ViewString = iota VSCurrentDirectory VSCurrentFile VSWindowIcon )
ViewString identifiers are stored positionally in Snapshot.ViewStrings, so new values must be appended at the end of this block. Do not renumber or insert constants in the middle.