terminal

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 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 a command in a pseudo-terminal, keeps its output in a ring buffer and lets readers attach (platform-topology.md §8a.4). It is the earlier bridge's PTY, LaunchSpec, ring and Session, ported; the bridge's message bus, typing into a PTY and layout are not.

Index

Constants

View Source
const (
	StateStarting = "starting"
	StateRunning  = "running"
	StateIdle     = "idle"
	StateComplete = "complete"
	StateFailed   = "failed"
)

Session states as presented in terminal tiles.

View Source
const DefaultScrollback = 256 * 1024

DefaultScrollback is the ring's size when none is given: host.toml [terminals] scrollback_kib defaults to 256 (§8a.4, the bridge's value).

Variables

View Source
var ErrSessionClosed = errors.New("session is no longer running")

ErrSessionClosed is returned when input is written to a finished session.

Functions

func LookupCommand

func LookupCommand(command string) (string, bool)

LookupCommand reports whether a launch command is present on this machine, so a profile can be shown as unavailable instead of failing at start.

Types

type LaunchSpec

type LaunchSpec struct {
	Command string
	Args    []string
	Workdir string
	Env     []string
	Columns uint16
	Rows    uint16
	// InheritAll passes the whole environment of the process that starts the
	// terminal to the child (then Env), instead of only the allowlist below.
	// A personal forge runs the user's own shells and agents, which need the
	// user's keys and settings; a server that runs other identities leaves it
	// false.
	InheritAll bool
	// Unset names variables removed from an inherited environment.
	Unset []string
	// ExtraFiles are passed to the child as descriptors 3, 4, … (Unix only).
	ExtraFiles []*os.File
}

LaunchSpec describes a process to run inside a pseudo-terminal.

type PTY

type PTY interface {
	io.ReadWriteCloser
	// Resize informs the child process of a new window size.
	Resize(columns, rows uint16) error
	// Wait blocks until the child exits and reports its exit code.
	Wait() (int, error)
	// Signal asks the child to terminate, gracefully when the platform allows.
	Signal(force bool) error
	// PID is the child process identifier, or 0 when unknown.
	PID() int
}

PTY is the platform-neutral pseudo-terminal contract. Unix uses a PTY pair, Windows uses ConPTY; nothing above this interface branches on the OS.

func StartPTY

func StartPTY(spec LaunchSpec) (PTY, error)

StartPTY launches a process attached to a new pseudo-terminal.

type Session

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

Session is one live terminal: a pseudo-terminal, the ring of its output, and the readers attached to it.

func Start

func Start(id string, spec LaunchSpec, scrollbackBytes int, onExit func(*Session)) (*Session, error)

Start runs spec in a new pseudo-terminal as session id, keeping the last scrollbackBytes of output (DefaultScrollback when 0 or less), and pumps its output until the process exits; onExit, when not nil, is called once after the exit is recorded. A command that is not found is refused before anything starts.

func (*Session) Done

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

Done is closed when the session's process has exited and its exit is recorded.

func (*Session) ID

func (session *Session) ID() string

ID is the session's id.

func (*Session) Launch

func (session *Session) Launch() (string, []string)

Launch returns the command and arguments this session was started with, kept apart so a later run can start the same process again.

func (*Session) Resize

func (session *Session) Resize(columns, rows uint16) error

Resize propagates a new window size to the child process.

func (*Session) Stop

func (session *Session) Stop(force bool) error

Stop asks the child to exit; force escalates to an immediate kill.

func (*Session) View

func (session *Session) View() SessionView

View renders the session, settling the activity-derived idle transition at read time rather than on a timer.

func (*Session) Wait

func (session *Session) Wait(timeout time.Duration) bool

Wait blocks until the session's process has exited, or timeout passes.

func (*Session) Watch

func (session *Session) Watch() ([]byte, <-chan []byte, func())

Watch returns the retained output plus a live feed of everything after it (WatchFrom from the oldest byte the ring holds). The feed closes when the session ends, or earlier if the reader falls a full feed behind; a reader that must tell those apart, and rejoin where it stopped, uses WatchFrom.

func (*Session) WatchFrom

func (session *Session) WatchFrom(offset int64) Watching

WatchFrom joins the session's output at offset (F-0023-T3e): the bytes the ring still holds from offset on, then a live feed of everything after them, taken under the one lock output writes under, so no byte falls between them and none is in both. A reader keeps its position, From + len(Output) + the bytes its feed delivered; when its feed closes it calls WatchFrom with that position, and gets the bytes it missed (or, if the ring has wrapped past them, Dropped says exactly how many are gone), until a join reports Ended. It need not read View().State, which could change between the feed closing and the read: a reader let go just before the session ended took the end for its own and lost the bytes after its feed.

func (*Session) Write

func (session *Session) Write(input []byte) error

Write sends input to the terminal.

type SessionView

type SessionView struct {
	ID           string     `json:"id"`
	Command      string     `json:"command"`
	State        string     `json:"state"`
	ExitCode     *int       `json:"exitCode,omitempty"`
	LastError    string     `json:"lastError,omitempty"`
	Stopped      bool       `json:"stopped"`
	StartedAt    time.Time  `json:"startedAt"`
	LastOutputAt time.Time  `json:"lastOutputAt"`
	EndedAt      *time.Time `json:"endedAt,omitempty"`
	Columns      uint16     `json:"columns"`
	Rows         uint16     `json:"rows"`
	BytesOut     int64      `json:"bytesOut"`
	PID          int        `json:"pid,omitempty"`
}

SessionView is a snapshot of a session: a reader never holds a reference to live process state.

type Watching

type Watching struct {
	// From is the offset of Output[0].
	From int64
	// Dropped is how many bytes between the offset asked for and From the
	// ring no longer holds: 0 when the reader joins exactly where it asked,
	// else the exact gap, so a caller can tell a gap from a joined stream.
	Dropped int64
	// Output is the retained output from From on.
	Output []byte
	// Feed carries everything after Output, each byte once. It closes when
	// the session ends, or earlier when the reader falls a full feed (512
	// chunks) behind and is let go.
	Feed <-chan []byte
	// Ended is set when the session had already ended: Output is the rest of
	// its output and Feed is closed.
	Ended bool
	// Cancel stops the feed; calling it after the feed closed is harmless.
	Cancel func()
}

Watching is a reader's join to a session's output at an offset, an index into everything the session has produced since it started.

Jump to

Keyboard shortcuts

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