tuidrive

package
v0.1.5049 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 19 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 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

View Source
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

View Source
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

func Deadline(v string) (time.Time, bool)

Deadline reads a deadline given as Unix milliseconds: when the driver's caller stops waiting.

func LastAt

func LastAt(re *lazyre.RE, p string) int

LastAt is where re's last match in p starts, -1 with none.

func LastMatchNear

func LastMatchNear(re *lazyre.RE, words string, n int) bool

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.

func Program

func Program(args string) string

Program is the name of the program args run, and of the script an interpreter runs: never the rest of its arguments, which can carry a secret.

func TailAfter

func TailAfter(words string, end int) int

TailAfter is how many characters of words follow byte offset end.

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

func (d Dialog) Heading(words, ask string) bool

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

func (d Dialog) Options(words, ask string, rows ...*lazyre.RE) bool

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.

func NewGrid

func NewGrid(cols, rows int) *Grid

NewGrid is an empty screen of cols by rows.

func (*Grid) Draw

func (g *Grid) Draw(b []byte)

Draw draws b. An escape or character cut off at its end is kept for the next write.

func (*Grid) Text

func (g *Grid) Text() string

Text is the screen, a line a row, each without its trailing spaces.

type Reply

type Reply struct{ Ask, Answer string }

Reply is an answer the terminal gives: Answer, each time the agent draws Ask.

type Running

type Running struct {
	Age  time.Duration
	Args string
}

Running is a command the agent runs, and how long it has run.

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) BytesRead

func (t *Term) BytesRead() int64

BytesRead is how many bytes the agent has drawn.

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

func (t *Term) Ended(err error, open bool)

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) Exit

func (t *Term) Exit() error

Exit is how the agent ended, once Exited is closed.

func (*Term) Exited

func (t *Term) Exited() <-chan struct{}

Exited closes when the agent has ended (Exit).

func (*Term) Feed

func (t *Term) Feed(d []byte)

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

func (t *Term) LastScreen() string

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) Now

func (t *Term) Now() string

Now is the screen as it stands, "" before the agent draws.

func (*Term) NowLocked

func (t *Term) NowLocked() string

NowLocked is Now for a caller in Locked.

func (*Term) Plain

func (t *Term) Plain() string

Plain is what the agent drew since the screen was last cleared (ClearScreen), its escapes taken out.

func (*Term) Rows

func (t *Term) Rows() []string

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

func (t *Term) Screen() []string

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

func (t *Term) Seen() string

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

func (t *Term) Start(path string, args, env []string) error

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.

func (*Term) Stop

func (t *Term) Stop()

Stop ends the agent: asked, then made to.

func (*Term) Waiting

func (t *Term) Waiting() (cmds []Running, rssKiB int64, ok bool)

Waiting is what the agent is doing while it has drawn nothing: the commands it runs (its descendants, the longest-running first) and how much memory it holds, read from ps. ok is false when ps can't say.

func (*Term) Write

func (t *Term) Write(p []byte) (int, error)

Write types p to the agent.

Jump to

Keyboard shortcuts

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