term

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 29 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 Descendants

func Descendants(pid int) []int

Every live descendant of pid, parents before their children, zombies left for their parent to reap

func Editor

func Editor() string

Editor a person set in EDITOR or VISUAL, otherwise the first of the usual ones found on this machine, empty when there is none. It looks through PATH, so the app calls it off the UI loop

func EditorCommand

func EditorCommand(ctx context.Context, editor, path string) *exec.Cmd

Command that runs the editor on the file with the words it was set with. The editor is the user's own, run in their terminal, so it starts with the environment of the terminal icebath was started in, which carries no profile's variables

func EditorWords

func EditorWords(editor string) []string

Words of the editor a person set, its program first, so one set with flags, such as code -w, runs with them: as a shell splits them outside Windows, so a quoted path with spaces stays one word, as git and other programs read EDITOR, and at spaces on Windows, where a backslash separates folders

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

func OpenPty

func OpenPty() (*os.File, *os.File, error)

Opens a pseudo terminal pair through the multiplexer: its master and slave ends

func Processes

func Processes() map[int]int

Live processes of this machine as pid to parent pid, zombies left out, from /proc or else ps. None when neither can be read

func StripANSI

func StripANSI(text string) string

Text without its ANSI escape sequences

Types

type Changes

type Changes struct {
	// Rows that ever scrolled above the screen, the dropped ones included
	Scrolled uint64
	// Rows the scrollback holds
	Scrollback int
	// Whether the alternate screen shows
	Alt bool
	// Progress the program reported through OSC 9;4, state zero when none is shown
	Progress, Percent int
	// Events queued since the last take
	Events []Event
}

What a host reads once output arrived, taken under one hold of the lock

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. The first call looks for it on the disk, so the app asks for it off the UI loop

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) 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) 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) 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) 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) Refind

func (e *Emulator) Refind(query string, old []Match, since uint64) ([]Match, int, uint64)

Matches of the query brought up to date with the output since they were found, the rows above since taken as unchanged as they had scrolled out by then: the old matches on rows still kept above the logical row holding since stay, the rows from there on are searched again, and past the most kept the oldest go. The old matches are reused for the new ones. Reports the matches, how many of the old ones went from the front and the top row of the screen, the since of the next call. A since of zero searches every row

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) Running

func (e *Emulator) Running() bool

Running reports whether the shell marked a command as started and not yet ended. Only shells that send OSC 133 marks set it, so false proves nothing.

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) Take

func (e *Emulator) Take() Changes

Takes the events queued since the last take with where the grid stands as they are taken

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 a slice at a time, letting the lock go between slices for a host waiting to draw

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) Busy

func (s *Session) Busy() bool

Busy reports whether the shell on the terminal is doing anything but waiting at its prompt. A child program, the alternate screen, or a command marked as running makes it busy. It reads the process table, so it is called off the UI loop.

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, otherwise 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, dropped while more than the most already waits for a program that does not read

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
	// Matches lit as search matches, in the order of their rows
	Matches []Match
	MatchFg color.Color
	MatchBg color.Color
	// Match lit as the current one
	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