term

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 20 Imported by: 1 module, 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

Terminal emulator interpreting xterm control sequences into a cell grid with scrollback, driving a program on a pty

Index

Constants

View Source
const (
	MarkPrompt uint8 = 1 << iota
	MarkInput
	MarkOutput
	MarkEnd
)

Shell integration marks a row may carry, set through OSC 133

Variables

This section is empty.

Functions

func Environ

func Environ(base []string, version string) []string

Environment a program on the terminal starts with, the terminal variables set over the given base

Types

type Command

type Command struct {
	Path string
	Args []string
	Dir  string
	Env  []string
}

Program to run on the terminal

func DefaultShell

func DefaultShell() Command

Program a terminal starts when none is configured, the login shell of the user or the plain shell of the system

type Cursor

type Cursor struct {
	X, Y    int
	Visible bool
	Shape   CursorShape
	Blink   bool
	Color   color.Color
}

Where the cursor is, whether it shows, its shape and blink, and the color the program gave it

type CursorShape

type CursorShape int

Shape the program asked the cursor to take through DECSCUSR

const (
	CursorBlock CursorShape = iota
	CursorUnderline
	CursorBar
)

type Emulator

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

Terminal emulator holding a main grid with scrollback and an alternate grid, safe for one writer and many readers

func New

func New(w, h int, o Options) *Emulator

Builds an emulator of w columns by h rows

func (*Emulator) AltScreen

func (e *Emulator) AltScreen() bool

Reports whether the alternate screen is shown

func (*Emulator) AltScroll

func (e *Emulator) AltScroll() bool

Reports whether a wheel turn should reach the program as arrow keys, the alternate scroll of xterm

func (*Emulator) Base

func (e *Emulator) Base() uint64

Absolute number of the top row of the screen, growing by one for every row pushed above it

func (*Emulator) BracketedPaste

func (e *Emulator) BracketedPaste() bool

Reports whether the program asked for bracketed paste

func (*Emulator) ClearScrollback

func (e *Emulator) ClearScrollback()

Forgets every row above the screen

func (*Emulator) Cursor

func (e *Emulator) Cursor() Cursor

Cursor as the program left it

func (*Emulator) Cwd

func (e *Emulator) Cwd() string

Working directory the program reported through OSC 7, empty when it reported none or one on another host

func (*Emulator) Draw

func (e *Emulator) Draw(buf *uv.Buffer, v View)

Paints the rows in view into the buffer, the scrollback above the screen when scrolled

func (*Emulator) Events

func (e *Emulator) Events() []Event

Takes every event queued since the last call

func (*Emulator) Find

func (e *Emulator) Find(query string) []Match

Every place the text occurs, scrollback first, letters matched without regard to case when the query has no capitals

func (*Emulator) Focus

func (e *Emulator) Focus(in bool) []byte

Bytes the program receives when the terminal gains or loses focus, none unless it asked

func (*Emulator) FocusEvents

func (e *Emulator) FocusEvents() bool

Reports whether the program asked to hear about focus changes

func (*Emulator) Generation

func (e *Emulator) Generation() uint64

Counter that moves whenever output arrived, so a host knows when to draw again

func (*Emulator) Key

func (e *Emulator) Key(k Key) []byte

Bytes the program receives for the key, none when the key sends nothing

func (*Emulator) KittyFlags

func (e *Emulator) KittyFlags() int

Kitty keyboard flags in force on the shown screen

func (*Emulator) LineAt

func (e *Emulator) LineAt(p Pos) (Pos, Pos)

First and last position of the logical row holding p, soft wraps included

func (*Emulator) Mouse

func (e *Emulator) Mouse(m Mouse) ([]byte, bool)

Bytes the program receives for the pointer event, false when it does not track it and the host keeps the event

func (*Emulator) MouseTracking

func (e *Emulator) MouseTracking() bool

Reports whether the program tracks the mouse in any mode

func (*Emulator) Paste

func (e *Emulator) Paste(text string) []byte

Bytes the program receives for pasted text, bracketed when it asked for that

func (*Emulator) Progress

func (e *Emulator) Progress() (state, value int)

Progress the program reported through OSC 9;4, state zero when none is shown

func (*Emulator) Prompt

func (e *Emulator) Prompt(from uint64, d int) (uint64, bool)

Absolute row of the nearest prompt mark above or below the row, false when there is none

func (*Emulator) Reset

func (e *Emulator) Reset()

Returns everything to its starting state, keeping the scrollback

func (*Emulator) Resize

func (e *Emulator) Resize(w, h int)

Changes the grid to w by h, rewrapping the main screen and its scrollback to the new width

func (*Emulator) Row

func (e *Emulator) Row(abs uint64) (Line, bool)

Copy of the row at an absolute number, false when it is gone or past the bottom

func (*Emulator) ScrollbackLen

func (e *Emulator) ScrollbackLen() int

Rows above the screen

func (*Emulator) Scrolled

func (e *Emulator) Scrolled() uint64

Total rows that ever scrolled above the screen, the dropped ones included

func (*Emulator) SetDefaults

func (e *Emulator) SetDefaults(fg, bg, cursor color.Color)

Replaces the colors answered for the default text, background and cursor

func (*Emulator) SetScrollback

func (e *Emulator) SetScrollback(n int)

Replaces the rows the scrollback keeps

func (*Emulator) Size

func (e *Emulator) Size() (int, int)

Columns and rows of the grid

func (*Emulator) Synchronized

func (e *Emulator) Synchronized() bool

Reports whether a synchronized update holds the picture, which it may for a second at most

func (*Emulator) Text

func (e *Emulator) Text(s Selection) string

Text the selection covers, soft wrapped rows joined and trailing blanks of each row dropped

func (*Emulator) Title

func (e *Emulator) Title() string

Title the program set, empty when it set none

func (*Emulator) WordAt

func (e *Emulator) WordAt(p Pos) (Pos, Pos)

Start and end of the word under the position, crossing soft wraps, the position alone on a blank

func (*Emulator) Write

func (e *Emulator) Write(p []byte) (int, error)

Feeds program output through the parser

func (*Emulator) WriteString

func (e *Emulator) WriteString(s string) (int, error)

Feeds program output given as a string

type Event

type Event struct {
	Kind EventKind
	Text string
	Body string
}

One queued host event with the text it carries

type EventKind

type EventKind int

Something the program did that the host shows or acts on outside the grid

const (
	EventBell EventKind = iota
	EventTitle
	EventClipboard
	EventNotify
	EventCwd
	EventProgress
)

type Exit

type Exit struct {
	Code   int
	Signal string
	Err    error
}

How a program ended

type Key

type Key struct {
	Code    rune
	Shifted rune
	Base    rune
	Text    string
	Mod     uv.KeyMod
	Repeat  bool
	Release bool
}

Key event from the host, codes as ultraviolet names them

type Line

type Line struct {
	Cells   []uv.Cell
	Wrapped bool
	Marks   uint8
	// Exit code of the command that ended on this row when MarkEnd is set
	Exit int
	// contains filtered or unexported fields
}

Row of cells with whether it continues on the row below and the shell marks it carries

type Match

type Match struct {
	Row      uint64
	From, To int
}

Columns a match covers on one row

type Mouse

type Mouse struct {
	X, Y   int
	Button uv.MouseButton
	Mod    uv.KeyMod
	Kind   MouseKind
}

Pointer event in cell coordinates inside the grid

type MouseKind

type MouseKind int

What the pointer did

const (
	MousePress MouseKind = iota
	MouseRelease
	MouseMotion
	MouseWheel
)

type Options

type Options struct {
	// Rows kept above the screen, none when zero
	Scrollback int
	// Receives every answer the program asked for, such as cursor position reports
	Reply io.Writer
	// Colors reported for the default text, background and cursor
	Fg, Bg, Cursor color.Color
	// Name and version answered to XTVERSION
	Version string
}

Settings an emulator starts with

type Pos

type Pos struct {
	Row uint64
	Col int
}

Cell position by absolute row and column

type Process

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

Program running on a pseudo terminal

func (*Process) Close

func (p *Process) Close()

Hangs up the terminal, then kills the program and everything under it when it lingers

func (*Process) Cwd

func (p *Process) Cwd() string

Working directory of the program as the kernel knows it, empty where that cannot be read

func (*Process) Pid

func (p *Process) Pid() int

Process id of the program

func (*Process) Read

func (p *Process) Read(b []byte) (int, error)

Output of the program

func (*Process) Resize

func (p *Process) Resize(cols, rows int) error

Tells the program the terminal is cols by rows

func (*Process) Write

func (p *Process) Write(b []byte) (int, error)

Input to the program

type SelMode

type SelMode int

How a selection grows from its anchor

const (
	SelChars SelMode = iota
	SelWords
	SelLines
	SelBlock
)

type Selection

type Selection struct {
	Anchor Pos
	Head   Pos
	Mode   SelMode
}

Stretch of cells between an anchor and the head the pointer moves, both inclusive

type Session

type Session struct {
	Emu *Emulator
	// contains filtered or unexported fields
}

Program on a pseudo terminal feeding an emulator, with a wake channel the host waits on for output

func Open

func Open(c Command, cols, rows int, o Options) (*Session, error)

Starts the command on a terminal of cols by rows that feeds a fresh emulator built with the options

func (*Session) Bytes

func (s *Session) Bytes() int64

Bytes the program has written so far

func (*Session) Close

func (s *Session) Close()

Ends the program and the goroutines feeding it

func (*Session) Command

func (s *Session) Command() Command

Command the session runs

func (*Session) Cwd

func (s *Session) Cwd() string

Working directory of the program, what it announced over OSC 7, else what the kernel reports

func (*Session) Done

func (s *Session) Done() <-chan struct{}

Channel closed once the program has ended

func (*Session) Exit

func (s *Session) Exit() Exit

How the program ended, meaningful once Done is closed

func (*Session) Exited

func (s *Session) Exited() bool

Reports whether the program has ended

func (*Session) Input

func (s *Session) Input(b []byte)

Queues bytes for the program without waiting, dropping them once it has ended

func (*Session) Pid

func (s *Session) Pid() int

Process id of the program

func (*Session) Resize

func (s *Session) Resize(cols, rows int)

Tells the program and the emulator the terminal is cols by rows

func (*Session) Wake

func (s *Session) Wake() <-chan struct{}

Channel that gets a token whenever output arrived since the host last took one

func (*Session) Write

func (s *Session) Write(b []byte) (int, error)

Queues an answer the emulator owes the program

type Span

type Span struct {
	From, To int
}

Columns from through to inclusive on one row

type View

type View struct {
	// Rows scrolled back into the scrollback, zero for the live screen
	Scroll int
	// Theme colors for the sixteen ANSI colors, a nil entry leaving the terminal default
	Colors [16]color.Color
	// Colors cells take when the program swaps them through reverse video or a selection covers them
	Fg, Bg color.Color
	// Highlight colors of selected cells
	SelFg, SelBg color.Color
	Selection    *Selection
	// Spans lit as search matches, by absolute row
	Matches map[uint64][]Span
	MatchFg color.Color
	MatchBg color.Color
	// Span lit as the current match
	Current *Match
	CurFg   color.Color
	CurBg   color.Color
}

What a host paints the grid with

Jump to

Keyboard shortcuts

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