terminal

package
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 14 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

Package terminal runs the person's shell in a pseudo-terminal for the window's terminal tab (spec_062_terminal): one Manager a workspace, each Terminal keeping its recent output as scrollback that a watcher replays from an offset, so a window that reloads picks a terminal up as it was.

A terminal is the person's, not the agent's: it runs outside the agent's sandbox and nothing of its text reaches a model or the logs (TRM-02).

Index

Constants

View Source
const (
	// DefaultMax is how many terminals a workspace keeps at once.
	DefaultMax = 10
	// DefaultScrollback is how many bytes of output a terminal keeps.
	DefaultScrollback = 2 << 20
	// MaxWrite is the most input one write takes.
	MaxWrite = 64 << 10
)

Limits (TRM-05).

Variables

View Source
var (
	// ErrUnknown is a terminal that isn't there (or was closed).
	ErrUnknown = errors.New("no such terminal")
	// ErrTooMany is a terminal past the workspace's limit.
	ErrTooMany = errors.New("too many terminals")
	// ErrUnsupported is a platform without pseudo-terminals.
	ErrUnsupported = errors.New("terminals aren't supported on this system yet")
	// ErrClosed is a manager whose workspace has closed.
	ErrClosed = errors.New("the workspace's terminals are closed")
	// ErrNotReading is input the shell hasn't taken for WriteTimeout: what
	// runs in it isn't reading (a hung program, an ssh whose network went),
	// so its input is full (TRM-13a).
	ErrNotReading = errors.New("the terminal isn't taking input: what runs in it isn't reading")
	// ErrExited is input for a shell that has exited.
	ErrExited = errors.New("the terminal's shell has exited")
)

Errors a caller tells apart.

View Source
var WriteTimeout = 5 * time.Second

WriteTimeout is how long input waits for room in the shell's input before the write fails with ErrNotReading (TRM-13a); a variable for tests.

Functions

func Environment

func Environment(env []string, version string) []string

Environment is env (the service's when nil) with what a terminal adds (TRM-01): its type and colours, Blitz as its program, and a UTF-8 locale when none is set.

func LoginShell

func LoginShell() string

LoginShell is the person's shell: $SHELL, else zsh on macOS and bash elsewhere (TRM-01).

Types

type Chunk

type Chunk struct {
	Data     []byte
	Offset   int64
	Skipped  int64
	Exited   bool
	ExitCode int
}

Chunk is output a watcher is sent: Data ends at Offset (counted from the terminal's start); Skipped is output before Data that was no longer in the scrollback (older than it, or come and gone while the watcher was behind); Exited ends the stream with the shell's exit code.

type Info

type Info struct {
	ID       string
	Dir      string
	Shell    string
	Started  time.Time
	Exited   bool
	ExitCode int
}

Info is a terminal as the window lists it.

type Manager

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

Manager keeps a workspace's terminals.

func NewManager

func NewManager(max, scrollback int) *Manager

NewManager is a manager keeping at most max terminals, each with scrollback bytes of output (0 for each's default).

func (*Manager) Close

func (m *Manager) Close(id string) error

Close ends terminal id (TRM-03).

func (*Manager) CloseAll

func (m *Manager) CloseAll()

CloseAll ends every terminal; none opens after it (the workspace closed).

func (*Manager) Get

func (m *Manager) Get(id string) (*Terminal, error)

Get is terminal id.

func (*Manager) List

func (m *Manager) List() []Info

List is the terminals, oldest first.

func (*Manager) Open

func (m *Manager) Open(o Options) (*Terminal, error)

Open starts a terminal (TRM-01, TRM-10).

func (*Manager) Running added in v1.2.0

func (m *Manager) Running() int

Running is how many terminals are open.

type Options

type Options struct {
	// Dir is the folder it starts in (absolute).
	Dir string
	// Shell is the program to run; "" for the person's login shell.
	Shell string
	// Args are the shell's arguments; nil for a login shell's ("-l").
	Args []string
	// Env is its environment; nil for the service's (TRM-01's additions
	// are made either way).
	Env []string
	// Cols and Rows are its size; 0 for 80×24.
	Cols, Rows uint16
	// Version is Blitz's, as TERM_PROGRAM_VERSION.
	Version string
}

Options are how a terminal starts.

type Terminal

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

Terminal is one shell in a pseudo-terminal.

func (*Terminal) Done

func (t *Terminal) Done() <-chan struct{}

Done is closed when the shell has exited and its output is read.

func (*Terminal) Info

func (t *Terminal) Info() Info

Info is the terminal as the window lists it.

func (*Terminal) Resize

func (t *Terminal) Resize(cols, rows uint16) error

Resize sets the terminal's size (TRM-11).

func (*Terminal) Watch

func (t *Terminal) Watch(ctx context.Context, from int64, send func(Chunk) error) error

Watch sends the output after from, then each new output as it comes, until the shell exits (a last Chunk with Exited) or ctx ends (TRM-12).

func (*Terminal) Write

func (t *Terminal) Write(p []byte) error

Write writes to the shell's input (TRM-13), through the terminal's writer: a PTY whose input is full blocks its writer, never the caller, who gets ErrNotReading once the queue has had no room for WriteTimeout (TRM-13a).

Jump to

Keyboard shortcuts

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