vterm

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const MaxScrollback = 10000

Variables

This section is empty.

Functions

func StyleToANSI

func StyleToANSI(s Style) string

StyleToANSI converts a Style to ANSI escape codes. Optimized to avoid allocations using strings.Builder.

func StyleToDeltaANSI

func StyleToDeltaANSI(prev, next Style) string

StyleToDeltaANSI returns the minimal SGR escape sequence to transition from prev to next style. This avoids the overhead of always emitting a full reset. Optimized to avoid allocations using strings.Builder.

Types

type Cell

type Cell struct {
	Rune  rune
	Style Style
	Width int // 1 normal, 2 wide, 0 continuation
}

Cell represents a single character cell

func CopyLine

func CopyLine(src []Cell) []Cell

CopyLine deep copies a line

func DefaultCell

func DefaultCell() Cell

DefaultCell returns a blank cell

func MakeBlankLine

func MakeBlankLine(width int) []Cell

MakeBlankLine creates a blank line

type Color

type Color struct {
	Type  ColorType
	Value uint32 // Indexed: 0-255, RGB: 0xRRGGBB
}

Color represents a terminal color

type ColorType

type ColorType uint8
const (
	ColorDefault ColorType = iota
	ColorIndexed
	ColorRGB
)

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser handles ANSI escape sequence parsing

func NewParser

func NewParser(vt *VTerm) *Parser

NewParser creates a new parser for the given VTerm

func (*Parser) Parse

func (p *Parser) Parse(data []byte)

Parse processes bytes from PTY output

type ResponseWriter

type ResponseWriter func([]byte)

ResponseWriter is called when the terminal needs to send a response back to the PTY

type Style

type Style struct {
	Fg        Color
	Bg        Color
	Bold      bool
	Dim       bool
	Italic    bool
	Underline bool
	Blink     bool
	Reverse   bool
	Hidden    bool
	Strike    bool
}

Style holds text styling attributes

type VTerm

type VTerm struct {
	// Screen buffer (visible area)
	Screen [][]Cell

	// Scrollback buffer (oldest at index 0)
	Scrollback [][]Cell

	// Cursor position (0-indexed)
	CursorX, CursorY int

	// Dimensions
	Width, Height int

	// Scroll viewing position (0 = live, >0 = lines scrolled up)
	ViewOffset int

	// Alt screen mode (full-screen TUI applications).
	AltScreen bool
	// AllowAltScreenScrollback keeps scrollback active even in alt screen.
	// Useful for tmux-backed sessions where scrollback should remain available.
	AllowAltScreenScrollback bool

	// Scrolling region (for DECSTBM)
	ScrollTop    int
	ScrollBottom int
	// Origin mode (DECOM) - cursor positions are relative to scroll region.
	OriginMode bool

	// Current style for new characters
	CurrentStyle Style

	// Saved cursor state (for DECSC/DECRC)
	SavedCursorX int
	SavedCursorY int
	SavedStyle   Style

	// Cursor visibility (controlled externally when pane is focused)
	ShowCursor bool

	// CursorHidden tracks if terminal app hid cursor via DECTCEM (mode 25)
	CursorHidden bool
	// contains filtered or unexported fields
}

VTerm is a virtual terminal emulator with scrollback support.

Synchronization contract: VTerm has no internal mutex. All callers must provide external synchronization. In practice, every call site (WriteToTerminal, SidebarPTYFlush, and TerminalLayer snapshot creation) holds TerminalState.mu for the duration of the operation. Snapshot-based rendering (TerminalLayer) copies data under the lock and then renders the immutable snapshot without locks.

func New

func New(width, height int) *VTerm

New creates a new VTerm with the given dimensions

func (*VTerm) AbsoluteLineToScreenY added in v0.0.4

func (v *VTerm) AbsoluteLineToScreenY(absLine int) int

AbsoluteLineToScreenY converts an absolute line number to a screen Y coordinate. Returns -1 if the line is not currently visible.

func (*VTerm) ClearDirty

func (v *VTerm) ClearDirty()

ClearDirty resets dirty tracking state after a render.

func (*VTerm) ClearDirtyWithCursor

func (v *VTerm) ClearDirtyWithCursor(showCursor bool)

ClearDirtyWithCursor resets dirty tracking state and updates cursor tracking. This should be called after snapshotting to track cursor position changes.

func (*VTerm) ClearSelection

func (v *VTerm) ClearSelection()

ClearSelection clears the current selection

func (*VTerm) DirtyLines

func (v *VTerm) DirtyLines() ([]bool, bool)

DirtyLines returns the dirty line flags and whether all lines are dirty. This is used by VTermLayer for optimized rendering.

func (*VTerm) GetAllLines

func (v *VTerm) GetAllLines() []string

GetAllLines returns all content (scrollback + screen) as lines for search

func (*VTerm) GetScrollInfo

func (v *VTerm) GetScrollInfo() (int, int)

GetScrollInfo returns (current offset, max offset)

func (*VTerm) GetSelectedText

func (v *VTerm) GetSelectedText(startX, startLine, endX, endLine int) string

GetSelectedText extracts text from the current selection. Returns empty string if no selection is active.

func (*VTerm) GetTextRange

func (v *VTerm) GetTextRange(startX, startLine, endX, endLine int) string

GetTextRange extracts text from a range in the combined scrollback+screen buffer. Coordinates are absolute line indices (0-based) and columns.

func (*VTerm) HasSelection added in v0.0.4

func (v *VTerm) HasSelection() bool

HasSelection returns true if there is an active selection.

func (*VTerm) IsInSelection

func (v *VTerm) IsInSelection(x, screenY int) bool

IsInSelection checks if coordinate (x, screenY) is within the selection. screenY is a screen-relative coordinate (0 to Height-1).

func (*VTerm) IsScrolled

func (v *VTerm) IsScrolled() bool

IsScrolled returns true if viewing scrollback

func (*VTerm) LastCursorHidden

func (v *VTerm) LastCursorHidden() bool

LastCursorHidden returns the DECTCEM cursor hidden state from the previous render frame.

func (*VTerm) LastCursorX

func (v *VTerm) LastCursorX() int

LastCursorX returns the cursor X position from the previous render frame. Used to detect cursor movement and mark dirty lines.

func (*VTerm) LastCursorY

func (v *VTerm) LastCursorY() int

LastCursorY returns the cursor Y position from the previous render frame. Used to detect cursor movement and mark old cursor line dirty.

func (*VTerm) LastShowCursor

func (v *VTerm) LastShowCursor() bool

LastShowCursor returns the cursor visibility from the previous render frame.

func (*VTerm) LineCells

func (v *VTerm) LineCells(line int) []Cell

LineCells returns the cell slice for an absolute line index in scrollback+screen.

func (*VTerm) MaxViewOffset

func (v *VTerm) MaxViewOffset() int

MaxViewOffset returns the maximum scrollback offset for the current buffers.

func (*VTerm) PrependScrollback added in v0.0.9

func (v *VTerm) PrependScrollback(data []byte)

PrependScrollback parses captured scrollback content (with ANSI escapes) and prepends the resulting lines to the scrollback buffer. This is used to populate scrollback history when attaching to an existing tmux session. It is a no-op if data is empty.

func (*VTerm) Render

func (v *VTerm) Render() string

Render returns the terminal content as a string with ANSI codes

func (*VTerm) RenderBuffers

func (v *VTerm) RenderBuffers() ([][]Cell, int)

RenderBuffers returns the current screen buffer and scrollback length. During synchronized output, it returns the frozen snapshot.

func (*VTerm) Resize

func (v *VTerm) Resize(width, height int)

Resize handles terminal resize

func (*VTerm) ScreenYToAbsoluteLine added in v0.0.4

func (v *VTerm) ScreenYToAbsoluteLine(screenY int) int

ScreenYToAbsoluteLine converts a screen Y coordinate (0 to Height-1) to an absolute line number. Absolute line 0 is the first line in scrollback.

func (*VTerm) ScrollToLine

func (v *VTerm) ScrollToLine(lineIdx int)

ScrollToLine scrolls view to show the given line index (in combined buffer)

func (*VTerm) ScrollView

func (v *VTerm) ScrollView(delta int)

ScrollView scrolls the view by delta lines (positive = up into history)

func (*VTerm) ScrollViewTo

func (v *VTerm) ScrollViewTo(offset int)

ScrollViewTo sets absolute scroll position

func (*VTerm) ScrollViewToBottom

func (v *VTerm) ScrollViewToBottom()

ScrollViewToBottom returns to live view

func (*VTerm) ScrollViewToTop

func (v *VTerm) ScrollViewToTop()

ScrollViewToTop scrolls to oldest content

func (*VTerm) Search

func (v *VTerm) Search(query string) []int

Search finds all line indices matching query

func (*VTerm) SelActive

func (v *VTerm) SelActive() bool

SelActive reports whether a selection is active.

func (*VTerm) SelEndLine added in v0.0.4

func (v *VTerm) SelEndLine() int

SelEndLine returns the selection end line (absolute line number).

func (*VTerm) SelEndX

func (v *VTerm) SelEndX() int

SelEndX returns the selection end X.

func (*VTerm) SelEndY

func (v *VTerm) SelEndY() int

SelEndY returns the selection end Y in screen coordinates. Returns -1 if the end line is not visible.

func (*VTerm) SelStartLine added in v0.0.4

func (v *VTerm) SelStartLine() int

SelStartLine returns the selection start line (absolute line number).

func (*VTerm) SelStartX

func (v *VTerm) SelStartX() int

SelStartX returns the selection start X.

func (*VTerm) SelStartY

func (v *VTerm) SelStartY() int

SelStartY returns the selection start Y in screen coordinates. Returns -1 if the start line is not visible.

func (*VTerm) SetResponseWriter

func (v *VTerm) SetResponseWriter(w ResponseWriter)

SetResponseWriter sets the callback for terminal query responses

func (*VTerm) SetSelection

func (v *VTerm) SetSelection(startX, startLine, endX, endLine int, active bool, rect bool)

SetSelection stores selection coordinates for rendering with highlight. startLine and endLine are absolute line numbers (0 = first scrollback line).

func (*VTerm) SyncActive

func (v *VTerm) SyncActive() bool

SyncActive reports whether synchronized output is currently active.

func (*VTerm) TotalLines

func (v *VTerm) TotalLines() int

TotalLines returns the total number of lines in scrollback+screen.

func (*VTerm) Version

func (v *VTerm) Version() uint64

Version returns the current version counter. This increments whenever visible content changes.

func (*VTerm) VisibleLineRange

func (v *VTerm) VisibleLineRange() (start, end, total int)

VisibleLineRange returns the [start, end) line indices in the combined scrollback+screen buffer that are currently visible, along with total lines.

func (*VTerm) VisibleScreen

func (v *VTerm) VisibleScreen() [][]Cell

VisibleScreen returns the currently visible screen buffer as a copy.

func (*VTerm) VisibleScreenWithSelection

func (v *VTerm) VisibleScreenWithSelection() [][]Cell

VisibleScreenWithSelection returns the visible screen with selection highlighting applied.

func (*VTerm) Write

func (v *VTerm) Write(data []byte)

Write processes input bytes from PTY

Jump to

Keyboard shortcuts

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