Documentation
¶
Overview ¶
Package terminal runs interactive shells on pseudo-terminals. It knows nothing of HTTP, jobs, or runs.
Index ¶
- Variables
- func WriteIntegration() (dir string, lasting bool, note string, err error)
- type Attachment
- func (attachment *Attachment) ClearOutput() error
- func (attachment *Attachment) Detach()
- func (attachment *Attachment) Next() ([]byte, bool)
- func (attachment *Attachment) Reason() DetachReason
- func (attachment *Attachment) Resize(rows, cols uint16) error
- func (attachment *Attachment) Result() Result
- func (attachment *Attachment) Write(input []byte) (int, error)
- type CommandEntry
- type DetachReason
- type DirEntry
- type Keeper
- func (keeper *Keeper) Attach(id string, rows, cols uint16) (*Attachment, error)
- func (keeper *Keeper) AttachIfFree(id string, rows, cols uint16) (*Attachment, error)
- func (keeper *Keeper) Close()
- func (keeper *Keeper) Commands(group string) []CommandEntry
- func (keeper *Keeper) DefaultDir() string
- func (keeper *Keeper) Dir(id string) (string, error)
- func (keeper *Keeper) End(id string) error
- func (keeper *Keeper) EndAll()
- func (keeper *Keeper) Foreground(id string) (string, error)
- func (keeper *Keeper) History(group string) []DirEntry
- func (keeper *Keeper) KeepAlive()
- func (keeper *Keeper) Limits() (shells, inGroup int)
- func (keeper *Keeper) Move(id, group string, set, unset []string) (ShellInfo, error)
- func (keeper *Keeper) Processes(id string) ([]Process, error)
- func (keeper *Keeper) Rename(id, name string) (ShellInfo, error)
- func (keeper *Keeper) Reorder(ids []string)
- func (keeper *Keeper) Shells() []ShellInfo
- func (keeper *Keeper) Start(start StartOptions) (ShellInfo, error)
- type KeeperOptions
- type LastCommand
- type Options
- type Process
- type Result
- type Session
- func (session *Session) Close()
- func (session *Session) Descendants() ([]Process, error)
- func (session *Session) Dir() (string, error)
- func (session *Session) Done() <-chan struct{}
- func (session *Session) Foreground() (string, error)
- func (session *Session) Read(buffer []byte) (int, error)
- func (session *Session) Resize(rows, cols uint16) error
- func (session *Session) Result() Result
- func (session *Session) Write(input []byte) (int, error)
- type ShellActivity
- type ShellInfo
- type StartOptions
Constants ¶
This section is empty.
Variables ¶
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 and Move at the limit on a group's shells. ErrTooManyInGroup = errors.New("too many shells are running in this group") // ErrCannotMove is returned by Move for a shell that would not take its new place's // variables: one started without rotari's integration, or a shell other than bash and zsh. ErrCannotMove = errors.New("this shell was not started with rotari's shell integration, so it cannot take another project's variables") )
Functions ¶
func WriteIntegration ¶ added in v0.1.30
WriteIntegration writes the shell integration's files where the shells can read them for as long as any of them, or anything started in them, lives: a directory under the user's cache named by the files' content, written once and kept, so that a tmux or an editor started in a shell and outliving the server still finds them. When that cannot be, it writes a private temporary directory instead, for the caller to remove, and says why in note.
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.
type CommandEntry ¶ added in v0.1.32
type CommandEntry struct {
Command string
// Exit is its exit status, or nil when the shell gave none.
Exit *int
StartedAt, EndedAt time.Time
}
CommandEntry is a command that a shell of a group ran and that ended.
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 DirEntry ¶ added in v0.1.30
type DirEntry struct {
Dir string
// EnteredAt is when one of them last went there.
EnteredAt time.Time
}
DirEntry is a directory that shells of a group have been in.
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) Commands ¶ added in v0.1.32
func (keeper *Keeper) Commands(group string) []CommandEntry
Commands is the commands a group's shells ran and that ended, newest first, up to 200, as the shells' marks said them; a shell that does not mark its commands adds none. EndAll forgets every group's. Nothing of it is kept outside memory.
func (*Keeper) DefaultDir ¶
DefaultDir is where a shell starts unless told otherwise.
func (*Keeper) Dir ¶ added in v0.1.28
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) EndAll ¶
func (keeper *Keeper) EndAll()
EndAll ends every shell and returns once they have, and forgets every group's history. Each may take a moment to hang up, so they end together rather than in turn.
func (*Keeper) Foreground ¶ added in v0.1.28
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) History ¶ added in v0.1.30
History is the directories a group's shells have been in, newest first, each once, up to 20. A shell's directory counts when it is back at its prompt after Enter, and where it started counts too; one changed by a program in front of the shell, such as a nested shell or ssh, does not. A shell ending leaves the history, and EndAll forgets every group's. Nothing of it is kept outside memory.
func (*Keeper) KeepAlive ¶
func (keeper *Keeper) KeepAlive()
KeepAlive says someone is still there, attached or not, and restarts the idle limit.
func (*Keeper) Move ¶ added in v0.1.30
Move puts a running shell in another group, after that group's shells, with the lowest number free there and its name kept, and gives the shell the variables set (NAME=VALUE) and unset (NAME) of its new place, all ROTARI_ variables, which rotari's prompt hook takes at the shell's next prompt; what is already running keeps its own. Moving to the shell's own group changes nothing. Directories the shell goes to from then on are its new group's; those it went to stay its old group's. It returns ErrNoShell, ErrCannotMove for a shell that does not follow moves, ErrTooManyInGroup at the new group's limit, and an error for a variable that is not a ROTARI_ one or a value holding a line break or NUL.
func (*Keeper) Processes ¶ added in v0.1.29
Processes are the processes below a shell, as Session.Descendants lists them. As Dir, it attaches nothing, and returns ErrNoShell for a shell that is not running.
func (*Keeper) Reorder ¶
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.
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; 0 means
// they run until ended, however long nothing is attached.
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)
// ShellIntegration starts bash and zsh so that they mark their prompts and commands
// (see WriteIntegration), from files the Keeper writes now: in the user's cache, kept,
// or else in a temporary directory, removed on Close.
ShellIntegration bool
}
KeeperOptions says how a Keeper runs its shells.
type LastCommand ¶ added in v0.1.30
type LastCommand struct {
// Command is its command line, as the shell said it in C, or "" when it said none.
Command string
// Exit is its exit status, or nil when the shell gave none (no D, or one without an
// integer).
Exit *int
// StartedAt is when its C passed, and EndedAt when its D, or the next prompt, did.
StartedAt, EndedAt time.Time
// Sequence counts the shell's ended commands, this one included, so that a reader can
// tell a new one.
Sequence int
}
LastCommand is how a shell's last marked command ended.
type Options ¶
type Options struct {
// Shell is the program to run, as a login shell.
Shell string
// Integration, if set, is a directory WriteIntegration filled: bash and zsh then
// start with its files, which read the user's and mark prompts and commands.
Integration 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 Process ¶ added in v0.1.29
type Process struct {
PID, PPID int
// Depth is how far below the shell it is: 1 for the shell's own children.
Depth int
Command string
// contains filtered or unexported fields
}
Process is a process a shell started, directly or through others.
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.
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 (*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) Descendants ¶ added in v0.1.29
Descendants are the processes below the shell, its children first, each followed by its own. A process that left the shell's tree, as a daemon does, is not among them.
func (*Session) Dir ¶ added in v0.1.28
Dir is the current shell's working directory now, which programs it runs do not change: the foreground group's leader if that is an interactive shell, else the nearest one above it, or else the session's shell.
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
Foreground is the command line of the program the current shell runs in the foreground, or "" while it is at its prompt. A pipeline gives its first program.
func (*Session) Read ¶
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.
type ShellActivity ¶ added in v0.1.30
ShellActivity is what runs in one of a server's shells, seen from outside the server: Foreground is the command holding the shell's terminal, empty while the shell is at its prompt, and Background the shell's other processes.
func ServerShells ¶ added in v0.1.30
func ServerShells(serverPID int) ([]ShellActivity, error)
ServerShells describes the shells of the server with the given PID from the process table, for a process that cannot ask the server, such as rotari web --shutdown. A shell is a child of the server that leads its own session on a terminal, as a Keeper starts it; other children, such as launches, are not shells. A failure to read the process table is an error, never "no shells".
func (ShellActivity) Busy ¶ added in v0.1.30
func (activity ShellActivity) Busy() bool
Busy says something runs in the shell.
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
// StartedAt is when the shell started, and ActiveAt when it last printed something or
// was typed into, or started; the time since ActiveAt is how long it has been idle.
StartedAt, ActiveAt time.Time
// LastCommand is how its last command ended, read from the marks it prints, or nil
// before one has.
LastCommand *LastCommand
// FollowsMoves says Move can move it: it runs rotari's prompt hook, which takes the
// new place's variables.
FollowsMoves 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
// Unset names variables of the Keeper's Shell.Env that this shell does not get.
Unset []string
}
StartOptions say how to start one shell.