Documentation
¶
Overview ¶
Package tuidrive runs an agent's interactive terminal UI (TUI) in a pseudo-terminal of magpie's own, for a helper that drives it as a person at a keyboard does: what the agent draws is read onto a screen (Grid) and what is typed is written to it. On macOS and Linux the terminal is a pseudo-terminal (term_unix.go), on Windows a pseudo console (term_windows.go); no tmux or terminal app is involved.
What is the same for every agent lives here: starting and ending the program in its terminal, reading what it draws and answering what it asks of the terminal, the screen as drawn since it was last cleared and as it stands, and finding a dialog at the foot of the screen (dialog.go). What an agent's screens say, how a message is pasted into its prompt and where its answers are read from belong to the agent's own driver: internal/claudetui for Claude Code.
Index ¶
- Constants
- Variables
- func Deadline(v string) (time.Time, bool)
- func LastAt(re *lazyre.RE, p string) int
- func LastMatchNear(re *lazyre.RE, words string, n int) bool
- func OldConsole() bool
- func Program(args string) string
- func TailAfter(words string, end int) int
- type Dialog
- type Grid
- type Reply
- type Running
- type Term
- func (t *Term) Attach(rw io.ReadWriter)
- func (t *Term) BytesRead() int64
- func (t *Term) Changed() <-chan struct{}
- func (t *Term) ClearScreen()
- func (t *Term) Ended(err error, open bool)
- func (t *Term) Exit() error
- func (t *Term) Exited() <-chan struct{}
- func (t *Term) Feed(d []byte)
- func (t *Term) LastScreen() string
- func (t *Term) Locked(f func())
- func (t *Term) Now() string
- func (t *Term) NowLocked() string
- func (t *Term) Plain() string
- func (t *Term) Rows() []string
- func (t *Term) Screen() []string
- func (t *Term) Seen() string
- func (t *Term) Start(path string, args, env []string) error
- func (t *Term) Stop()
- func (t *Term) Waiting() (cmds []Running, rssKiB int64, ok bool)
- func (t *Term) Write(p []byte) (int, error)
Constants ¶
const ( Cols = 200 Rows = 50 )
Cols and Rows are the size of the agent's terminal, the pseudo-terminal (term_unix.go) and Windows' pseudo console (term_windows.go) made alike, and the grid it is read on. A grid of another size misreads the screen: an 80 by 24 one, on macOS and Linux while their pseudo-terminal was 200 by 50, drew every row under the 24th over the 24th, and Claude Code's /usage panel, at the foot of the screen, was told as one window or as a figure since drawn again (#4988, shinnqy on X).
Variables ¶
var ANSI = lazyre.New(`\x1b\[[0-9;?<>=]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[P^_][^\x1b]*\x1b\\|\x1b.`)
ANSI matches a terminal escape: CSI, OSC, DCS/PM/APC strings, and the two-byte ones.
EndSignals are what a driver ends on, its agent with it.
Functions ¶
func Deadline ¶
Deadline reads a deadline given as Unix milliseconds: when the driver's caller stops waiting.
func LastMatchNear ¶
LastMatchNear is whether re matches in words with at most n characters after its last match.
func OldConsole ¶
func OldConsole() bool
OldConsole is whether the terminal is Windows 10's own pseudo console, which can lose a paste's marks and blocks of a long write: never here.
Types ¶
type Dialog ¶
type Dialog struct {
// Foot is the most the dialog draws under its option rows: its footer
// ("Enter to confirm · Esc to cancel"), in characters.
Foot int
// Body is the most it draws under its heading: its options, and a
// preview of the choice.
Body int
}
Dialog is how far up the screen's words a dialog is looked for: a dialog an agent asks is at the foot of its screen, and the same words further up are history (a resumed conversation that talks of the dialog), not the dialog.
func (Dialog) Heading ¶
Heading is whether words end on a screen drawn under heading ask (an onboarding screen, an offer, sign-in): the heading is in the bottom of the screen, not further up in history.
func (Dialog) Options ¶
Options is whether words end on a question: each of its option rows (rows, matched by their words, as the unselected one has no mark), with only the dialog's footer under them, and its words ask at most Body before the rows or in them. The question's words alone are not it: a resumed conversation's history can hold them (@alisenray: Claude Code's "Bypass Permissions mode" in the history failed every --resume at 45s with the prompt box showing). Nor do the words come first: Claude Code (2.1.327, 2.1.329) heads its trust question "Quick safety check", and "trust this folder" is in its Yes row alone, under the No row. A check that wanted the words above both rows never saw the question, and every run of a saved account in a caller's folder waited out its 45s on it (@ahz2ygnhjmIJ0E0 on X).
type Grid ¶
type Grid struct {
// contains filtered or unexported fields
}
Grid is an agent's screen as a terminal shows it: what it draws is put in its cells, where the cursor is moved to. Read as a stream, with its escapes taken out (Term.Plain), a screen drawn again only where it changed is misread: Windows' pseudo console sends only the cells that changed, a cell left as it was skipped by a move of the cursor ("Settin\x1b[1Cs" for "Settings", whose g was there already), and a number redrawn in place is a digit or two after the old one, which the stream still holds. The grid holds the screen as it is.
type Reply ¶
type Reply struct{ Ask, Answer string }
Reply is an answer the terminal gives: Answer, each time the agent draws Ask.
type Term ¶
type Term struct {
// OnDraw, when set, is called with the grid after each piece the agent
// draws, and OnClear as the screen is cleared, both under the lock
// Locked holds: what a driver keeps of the screen changes with it.
OnDraw func(g *Grid)
OnClear func()
// Undrawn, when set, says why an agent that drew nothing hasn't
// (LastScreen), "" when it can't say.
Undrawn func() string
// Replies answers more of what the agent asks of the terminal than
// where the cursor is and what terminal it is (read), for an agent
// that waits for the answer before it draws: agy asks for the
// secondary device attributes and draws nothing until it has them.
// Set before Start or Attach.
Replies []Reply
// contains filtered or unexported fields
}
Term is an agent's terminal: the program running in it, what it has drawn, and the way to type to it (Write).
func New ¶
func New() *Term
New is a terminal with no program in it yet: Start runs one, or Attach reads one already started.
func (*Term) Attach ¶
func (t *Term) Attach(rw io.ReadWriter)
Attach reads what is drawn on rw, and writes what is typed to it, as a terminal a program already runs in.
func (*Term) Changed ¶
func (t *Term) Changed() <-chan struct{}
Changed ticks when the agent has drawn something since it was last read.
func (*Term) ClearScreen ¶
func (t *Term) ClearScreen()
ClearScreen forgets what was drawn so far (Plain): what is read after it is what the agent draws next. The grid keeps the screen as it stands.
func (*Term) Ended ¶
Ended records how the agent ended, err, once read has what it drew before it ended: its exit can be seen before the bytes it wrote just before are read, and the error said then carried an empty screen, not the reason it gave (#5283, a loaded Linux CI runner). A terminal that ends with its last writer (Unix) is read to its end; a pseudo console that stays open (Windows, open) is read until it has gone quiet after its first bytes. Two seconds at most, for a terminal a child of the agent keeps open.
func (*Term) Exited ¶
func (t *Term) Exited() <-chan struct{}
Exited closes when the agent has ended (Exit).
func (*Term) Feed ¶
Feed takes d as drawn by the agent: kept as drawn since the screen was last cleared, and put on the grid. The read loop feeds what it reads; a test feeds a recorded screen.
func (*Term) LastScreen ¶
LastScreen is the end of what the agent drew, for an error to say: with nothing drawn since the screen was cleared, the screen as it stands, and with nothing on it, that nothing was drawn (and Undrawn's why). An error that ended on an empty screen said nothing of what the agent showed (sowhynot). The lines and blocks its boxes are drawn with are left out (words): Claude Code's prompt box is ruled the screen's width over and under, and a tail of 600 bytes was all rule, one long line, not the words above it (FremontDango, Windows). A screen of boxes alone is said as drawn.
func (*Term) Locked ¶
func (t *Term) Locked(f func())
Locked runs f under the lock the screen is kept under, which OnDraw and OnClear run under too.
func (*Term) Plain ¶
Plain is what the agent drew since the screen was last cleared (ClearScreen), its escapes taken out.
func (*Term) Rows ¶
Rows is the screen as it stands, row by row, for a log to keep whole (LastScreen keeps its end): each row's words (words), the rows with none left out. None before the agent draws.
func (*Term) Screen ¶
Screen is the screen as it stands, row by row as drawn, for a person to look at while it runs (magpie claude-code screen, #8075): the boxes and the spaces between words kept, the blank rows under the last drawn left out. None before the agent draws.
func (*Term) Seen ¶
Seen is what to read the screen by: what the agent drew since it was cleared, or, nothing drawn since for redrawWait, the screen as it stands. A dialog that turns away a key sent just after it opens (Claude Code 2.1.319's Claude in Chrome offer) draws nothing again, and a run that read only what was drawn since the key waited out its caller with nothing to say (sowhynot).
func (*Term) Start ¶
Start runs the program at path with args and the environment env (its whole environment, TERM included) in a terminal of its own, and reads what it draws.