terminal

package
v0.1.28 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 16 Imported by: 0 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 interactive shells on pseudo-terminals. It knows nothing of HTTP, jobs, or runs.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeeperClosed is returned by Start and Attach after Close.
	ErrKeeperClosed = errors.New("terminal keeper is closed")
	// ErrDetached is returned when using an Attachment that has ended.
	ErrDetached = errors.New("terminal attachment has ended")
	// ErrAttachedElsewhere is returned by AttachIfFree when another attachment
	// has the shell.
	ErrAttachedElsewhere = errors.New("terminal is attached elsewhere")
	// ErrNoShell is returned for a shell that is not running, which may have ended.
	ErrNoShell = errors.New("no such shell")
	// ErrTooManyShells is returned by Start at the limit on all shells.
	ErrTooManyShells = errors.New("too many shells are running")
	// ErrTooManyInGroup is returned by Start at the limit on a group's shells.
	ErrTooManyInGroup = errors.New("too many shells are running in this group")
)

Functions

This section is empty.

Types

type Attachment

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

Attachment is one caller's view of the shell.

func (*Attachment) ClearOutput

func (attachment *Attachment) ClearOutput() error

ClearOutput forgets the output kept to replay, so what has been cleared from the screen does not come back with the next Attach. What already waits for this reader still arrives.

func (*Attachment) Detach

func (attachment *Attachment) Detach()

Detach ends the attachment and leaves the shell running; the idle limit starts if this was the last attachment to any shell. It does nothing to an attachment that has already ended.

func (*Attachment) Next

func (attachment *Attachment) Next() ([]byte, bool)

Next returns the output so far, waiting until there is some. It returns false once the attachment has ended and its last output has been taken; Reason and Result are valid after that.

func (*Attachment) Reason

func (attachment *Attachment) Reason() DetachReason

Reason says why the attachment ended; it is valid once Next has returned false.

func (*Attachment) Resize

func (attachment *Attachment) Resize(rows, cols uint16) error

Resize changes the terminal's size.

func (*Attachment) Result

func (attachment *Attachment) Result() Result

Result is how the shell ended, when Reason is ShellExited.

func (*Attachment) Write

func (attachment *Attachment) Write(input []byte) (int, error)

Write sends input to the shell. A tab that lost the shell cannot type into it.

type DetachReason

type DetachReason int

DetachReason says why an Attachment ended.

const (
	// DetachedByCaller means Detach was called.
	DetachedByCaller DetachReason = iota
	// TakenOver means a newer Attach replaced this one.
	TakenOver
	// ShellExited means the shell ended; Result says how.
	ShellExited
	// TooSlow means the reader took nothing for stallLimit. Attaching again
	// replays recent output.
	TooSlow
	// KeeperClosed means the Keeper was closed.
	KeeperClosed
)

type Keeper

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

Keeper holds shells, which outlive the attachments that show them: callers come and go, and each new one sees recent output. A shell with nothing attached sends its output only to its ring, so a program never waits. An attached reader that falls behind makes the program wait, so it misses nothing, unless it stalls.

A person watches one shell at a time, so a shell with nothing attached says nothing about whether anyone is there. The idle limit therefore applies to them together: it runs only while nothing is attached to any shell, restarts on KeepAlive, and then ends them all.

func NewKeeper

func NewKeeper(options KeeperOptions) (*Keeper, error)

NewKeeper returns a Keeper with no shells yet.

func (*Keeper) Attach

func (keeper *Keeper) Attach(id string, rows, cols uint16) (*Attachment, error)

Attach shows the shell at the given size, and ends any earlier Attachment to it with TakenOver. Next begins with the replay of recent output. A program already running is made to redraw itself.

func (*Keeper) AttachIfFree

func (keeper *Keeper) AttachIfFree(id string, rows, cols uint16) (*Attachment, error)

AttachIfFree is Attach that leaves another attachment alone and returns ErrAttachedElsewhere instead. It is for reconnecting by itself, which must not take the shell from whoever is using it.

func (*Keeper) Close

func (keeper *Keeper) Close()

Close ends the shells and refuses further ones.

func (*Keeper) DefaultDir

func (keeper *Keeper) DefaultDir() string

DefaultDir is where a shell starts unless told otherwise.

func (*Keeper) Dir added in v0.1.28

func (keeper *Keeper) Dir(id string) (string, error)

Dir is a shell's working directory now. It attaches nothing, so it neither takes the shell from an attachment nor counts as showing its output. It returns ErrNoShell for a shell that is not running, and another error when the directory cannot be found.

func (*Keeper) End

func (keeper *Keeper) End(id string) error

End ends a shell and returns once it has.

func (*Keeper) EndAll

func (keeper *Keeper) EndAll()

EndAll ends every shell and returns once they have. Each may take a moment to hang up, so they end together rather than in turn.

func (*Keeper) Foreground added in v0.1.28

func (keeper *Keeper) Foreground(id string) (string, error)

Foreground is the command line of the program a shell runs in the foreground, or "" while the shell is at its prompt. As Dir, it attaches nothing, and returns ErrNoShell for a shell that is not running.

func (*Keeper) KeepAlive

func (keeper *Keeper) KeepAlive()

KeepAlive says someone is still there, attached or not, and restarts the idle limit.

func (*Keeper) Limits

func (keeper *Keeper) Limits() (shells, inGroup int)

Limits are how many shells may run at once, and how many of one group.

func (*Keeper) Rename

func (keeper *Keeper) Rename(id, name string) (ShellInfo, error)

Rename sets a shell's name, which means nothing to the Keeper.

func (*Keeper) Reorder

func (keeper *Keeper) Reorder(ids []string)

Reorder arranges the group of the first running shell it is given in the order given. Shells of that group it does not name follow, as they were, and it ignores shells that have ended or are of another group, since a page's list can be a moment behind.

func (*Keeper) Shells

func (keeper *Keeper) Shells() []ShellInfo

Shells lists the running shells, by group, and within one in the order arranged.

func (*Keeper) Start

func (keeper *Keeper) Start(start StartOptions) (ShellInfo, error)

Start starts a shell at the given size. A shell nobody attaches to falls under the idle limit, which starting one restarts.

type KeeperOptions

type KeeperOptions struct {
	// Shell starts each shell; its Rows and Cols are ignored, since Start gives
	// the size.
	Shell Options
	// IdleLimit is how long shells run with nothing attached to any of them.
	IdleLimit time.Duration
	// RingSize is how many bytes of recent output, per shell, a new Attach replays.
	RingSize int
	// ShellLimit is how many shells may run at once.
	ShellLimit int
	// GroupLimit is how many shells of one group may run at once.
	GroupLimit int
	// Log, if set, is told when a shell starts and ends, and never what it shows.
	Log func(message string)
}

KeeperOptions says how a Keeper runs its shells.

type Options

type Options struct {
	// Shell is the program to run, as a login shell.
	Shell string
	// Dir is the working directory.
	Dir string
	// Env is the shell's whole environment; nothing is inherited, so a caller
	// cannot pass on a secret by leaving it out. TERM is always set.
	Env []string
	// Rows and Cols are the terminal's first size, both above zero.
	Rows, Cols uint16
}

Options says how to start a shell.

type Result

type Result struct {
	// ExitCode is the shell's exit status, or -1 when a signal ended it.
	ExitCode int
	// Signal is the signal that ended the shell, or 0.
	Signal syscall.Signal
}

Result says how a shell ended.

func (Result) String

func (result Result) String() string

String describes how the shell ended.

type Session

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

Session is a started shell and its terminal. Read and Write may run at the same time as each other and as Resize and Close.

func Start

func Start(options Options) (*Session, error)

Start starts a shell in a session of its own, on a new terminal.

func (*Session) Close

func (session *Session) Close()

Close closes the terminal, which sends the shell and its foreground program SIGHUP, and returns once the shell has exited. A shell still running after closeGrace is killed with its process group. What has left the shell's session or ignores SIGHUP, such as a nohup command or a rotari supervisor, keeps running, as after closing any terminal.

func (*Session) Dir added in v0.1.28

func (session *Session) Dir() (string, error)

Dir is the shell's working directory now, which programs it runs do not change.

func (*Session) Done

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

Done is closed when the shell has exited.

func (*Session) Foreground added in v0.1.28

func (session *Session) Foreground() (string, error)

Foreground is the command line of the program the shell runs in the foreground, or "" while the shell itself is, at its prompt. A pipeline gives its first program.

func (*Session) Read

func (session *Session) Read(buffer []byte) (int, error)

Read reads what the shell and its programs wrote. It returns io.EOF once the terminal has closed or nothing still has it open; Linux reports the latter as EIO, which is not an error here.

func (*Session) Resize

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

Resize changes the terminal's size, which tells the running program.

func (*Session) Result

func (session *Session) Result() Result

Result is how the shell ended; it is valid once Done is closed.

func (*Session) Write

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

Write sends input to the shell, as typing would.

type ShellInfo

type ShellInfo struct {
	// ID names this shell for as long as the Keeper lasts, and is never reused, so
	// that a stale reference cannot reach a later shell.
	ID string
	// Number is what a person calls it within its group: the lowest not in use when
	// it started.
	Number int
	// Name is what someone has called it, if anyone has.
	Name  string
	Group string
	// Dir is where it started.
	Dir string
	// UnseenOutput says it has printed something that no attachment has shown since one
	// last did. A shell nothing has ever shown does not count: it is only starting.
	UnseenOutput bool
	// CommandFinished says a command the shell ran ended, back at its prompt, while no
	// attachment showed it, and none has since.
	CommandFinished bool
	// Running says a command holds the shell's foreground as Shells looks, rather than
	// its prompt; false when that cannot be told.
	Running bool
}

ShellInfo describes a running shell.

type StartOptions

type StartOptions struct {
	Rows, Cols uint16
	// Group is what the shell belongs to, which means nothing to the Keeper beyond
	// its numbers and limit.
	Group string
	// Dir is where it starts, instead of the Keeper's Shell.Dir.
	Dir string
	// Banner is shown before the shell's own output, and replayed with it.
	Banner string
	// Env is added to the Keeper's Shell.Env, overriding what is there.
	Env []string
}

StartOptions say how to start one shell.

Jump to

Keyboard shortcuts

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