askdrive

package
v0.248.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 12 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 askdrive types an answer into Claude Code's own AskUserQuestion picker, the way a person would — one key at a time, READING THE PANE before and after every key — so a question asked in a mirrored session can be answered from muster while the terminal shows its picker too.

It is the host half of "answered in both places": the picker is on screen at once (no hook holds it), muster shows the same question, and whichever is answered first wins. So nothing here may type into a picker that is not the one it expects, in the state it expects:

  • before the first key it requires the picker UNTOUCHED (the first question, nothing chosen, the cursor on the first row) and every question's text and options to match what muster was asked — or, continuing an answer an earlier drive of muster's stopped part way through (DriveFrom), exactly the screen that drive's own keys left;
  • before EVERY key it re-reads the pane and requires the state the previous key was meant to produce — a person answering in the terminal meanwhile closes or changes the picker, and the driver stops without a key. It reads it TWICE, Options.Steady apart, and requires the same screen both times: a key someone typed that the CLI has not drawn yet shows up in the second look (Claude Code's redraw lag is measured below);
  • right before every key, when the pane can tell (Activity), it reads when the terminal last had input (muster's own pasted keys do not count); if a key from then might not be drawn by its second look, it waits until it would be and looks again. Input that changed nothing — a focus event, the mouse — does not stop it; a key that changed the picker does;
  • after every key it waits for the screen that key must produce; anything else stops it.

It never sends the final key (the submit) unless the screen shows exactly the answers it was asked to give — the review screen when there is one — so a stopped drive leaves at most an unsubmitted, partly filled picker, never a half answer.

The picker's behaviour is MEASURED, Claude Code 2.1.289 (the PR's gate):

  • redraw lag, key pasted to screen changed (capture-pane every 2 ms, an 80x23 pane): idle, median 8–9 ms and at most 19 ms over 55 keys; with every core of a 24-core host busy, median 14 ms and at most 72 ms over 40 keys. Options.Steady (250 ms) is over three times the worst of those;

  • single-select: a digit chooses its option; it answers the question (and submits a one-question ask, which has no review screen);

  • multi-select: a digit toggles its option and leaves the cursor where it was; the rows are the options, the "Type something" row, then "Next" (or "Submit" on a one-question ask), reached with Down and taken with Enter;

  • the "Type something" row (option k+1): on a single-select question its digit focuses it for typing; on a multi-select question Down moves onto it; typed text is the answer (and checks it, multi-select); in that row digits and letters are TEXT;

  • an ask with several questions (or a multi-select one) ends on a review screen listing every "question → answer"; "1" submits it;

  • LONG TEXT (measured 2.1.289, synthetic text at 80, 100 and 120 columns: testdata/long-*): a question over 80 characters is drawn inside a box, "│ " down its left edge (on the review screen " │ ● …"), wrapped inside it when it does not fit the line — and on ONE line, the bar and no right edge, when it does (a wide pane; gutterOver, testdata/boxrule-*, oneline-*); a long label, and a long answer typed into the "Type something" row, wrap onto the row's further lines at its label's column — the answer is never truncated or scrolled within the row (650+ characters drawn in full), and a word longer than a line breaks mid-word; a picker taller than the pane loses its TOP (header and question) off the screen. Parse takes the box off and keeps each text's lines, and where each ends on screen; Match reads them back together (wrapEq): a line break stands for a space, or — only after a line that fills the pane, where the CLI breaks a word — for a space or none.

Index

Constants

View Source
const (
	KeyEnter = "\r"
	KeyDown  = "\x1b[B"
	KeyUp    = "\x1b[A"
)

The keys, as the bytes a terminal sends for them.

View Source
const KeyEscape = "\x1b"

KeyEscape is the Escape key, as the byte a terminal sends for it.

Measured (Claude Code 2.1.289, a private tmux server): ONE Escape on the AskUserQuestion picker — on a question screen, with or without previews, or on the review screen — dismisses the whole ask; the CLI records "User declined to answer questions" and returns to its prompt.

View Source
const NotesUntouched = "Notes: press n to add notes"

NotesUntouched is a preview question's notes line while it has no note.

View Source
const SteadyDefault = 250 * time.Millisecond

SteadyDefault is Options.Steady's default: over three times the worst redraw lag measured (72 ms, every core busy — see the package doc).

Variables

View Source
var (
	// ErrNoPicker: the question's picker is not on screen — it was answered or
	// cancelled in the terminal, or this is not the pane showing it.
	ErrNoPicker = errors.New("askdrive: the question's picker is not on screen")
	// ErrNotUntouched: the picker shows this question but someone has started
	// answering it in the terminal (or it shows another question).
	ErrNotUntouched = errors.New("askdrive: the picker is not untouched — someone is answering it in the terminal")
	// ErrLost: mid-drive, the screen is not what the last key should have
	// produced. Nothing more is sent; the picker is left unsubmitted.
	ErrLost = errors.New("askdrive: the picker did not show what the last key should have produced")
	// ErrUnrecognised: the pane shows something like the picker that does not
	// parse (Kind Unknown) — Claude Code's layout has probably changed.
	// Nothing is sent.
	ErrUnrecognised = errors.New("askdrive: could not recognise the terminal's picker (Claude Code may have changed)")
	// ErrUnreadable: muster could not read the question off the screen as the
	// one it registered — NOT that anyone is answering it. Before the first
	// key: the picker is untouched (every option, the cursor, the tabs as
	// expected) but its question's text does not read as the registered one;
	// or the picker is taller than the pane and its question is off screen
	// (Clipped). Nothing was sent. After a key it is wrapped in ErrLost too:
	// the picker went off the top of the pane mid-way, keys were sent, and
	// nothing was submitted.
	ErrUnreadable = errors.New("askdrive: could not read the question off the terminal's screen as the one muster registered")
)
View Source
var ErrUntypeable = errors.New("askdrive: this answer cannot be typed into the picker")

ErrUntypeable: an answer the picker cannot be given by keys (a choice out of range, text with a line break, several picks on a single-select question).

Functions

func Answers

func Answers(qs []askq.Question, choices []Choice) map[string]string

Answers is the answers choices give, keyed by question text, as the CLI records them (askq.AnswersOf).

func Cancel

func Cancel(ctx context.Context, p Pane, opt Options) (gone bool, err error)

Cancel dismisses the picker in p with ONE Escape — the only key it can send — and waits for the picker to go (the CLI records the ask declined). gone false: the Escape was sent and a picker was still on screen when Settle ran out; no second Escape is sent either way.

🔴 ONLY INTO A PICKER: before the key it reads the pane once, and it must show a picker — a question screen, the review screen, or a picker taller than the pane (Clipped). With none on screen the Escape would reach Claude Code itself (an Escape there interrupts a turn), so nothing is sent (ErrNoPicker); something like a picker that does not parse is refused too (ErrUnrecognised). It does NOT check that the picker is the card's own ask: a clipped picker's question cannot be read, so it accepts ANY picker. One picker shows at a time, and a newer ask supersedes the older card, whose Cancel is then gone (askq.Store.Open).

func Drive

func Drive(ctx context.Context, p Pane, qs []askq.Question, choices []Choice, opt Options) error

Drive types choices into the picker in p, verifying the screen before and after every key (see the package doc). nil means every key landed and the final screen showed the picker gone — the CLI took the answers; that it took THESE answers is for the caller to confirm (the transcript's result).

🔴 A REFUSAL AFTER THE FIRST KEY (ErrLost) LEAVES WHAT WAS TYPED IN THE PICKER, unsubmitted — a ticked box, a moved cursor, part or all of an answer's own words in its "Type something" row. Drive never sends a key to take it back: clearing it means typing into a picker it has just failed to recognise, the very thing it refuses. The person finishes, corrects or cancels it in the terminal — or answers again from muster, whose next drive continues it (DriveFrom).

func DriveFrom

func DriveFrom(ctx context.Context, p Pane, qs []askq.Question, choices []Choice, prior *Progress, opt Options) (int, error)

DriveFrom is Drive for a picker an earlier drive may have left part of an answer in: prior is what that drive sent (nil: nothing). It returns how many keys of choices' Plan the picker has received from muster, this drive's and any it continued — what the next drive's prior is.

🔴 IT CONTINUES ONLY MUSTER'S OWN PARTIAL ANSWER. Before its first key it reads the picker; when that is EXACTLY the screen prior's own keys led to — after some k of them, k at most the keys prior sent — and choices' plan begins with those same k keys, it continues from key k+1. A picker in any other state is refused as before: untouched is required (ErrNotUntouched), and a picker in a state prior's keys reached that choices' plan does not pass through holds muster's earlier, different answer, and is refused the same way (ErrNotUntouched): it is not untouched, and not this answer's.

What the screen cannot show — WHICH answer an answered question was given — is what prior vouches for: muster's own keys answered it, and the screen confirmed each. Someone who changed such an answer in the terminal is caught before the submit: the review screen lists every answer, and Drive submits it only when they are exactly choices'.

func Match

func Match(s Screen, w Want, qs []askq.Question, answers map[string]string) error

Match reports why s is not the screen w describes for qs (nil when it is). answers is what the review screen must list (Answers).

Types

type Activity

type Activity interface {
	// LastInput is when a client that can type into the pane last had input
	// (zero: never) — the driver's own keys not counted. It need not be a
	// KEYSTROKE: tmux's only such clock (#{client_activity}) also moves for a
	// focus event or the mouse, and keeps whole seconds, rounded down
	// (Options.Grain). So the driver never refuses on it alone: it waits until
	// any key from that moment would be drawn, and looks again.
	LastInput(ctx context.Context) (time.Time, error)
}

Activity is a Pane that can tell when a person last gave its terminal input.

type Choice

type Choice = askq.Choice

Choice is the answer to one question (askq.Choice): the options chosen (0-based indexes into its Options; one for a single-select question, any number for a multi-select one) and the person's own text ("" for none). A single-select question takes exactly one of the two.

type FakePicker

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

FakePicker is a stand-in for Claude Code's AskUserQuestion picker: the same screens (laid out as the CLI draws them — Parse reads both, and the real screens are its test fixtures) and the same response to keys as measured on 2.1.289 (the package doc). The browser suite's fake host and the real-tmux test run it where no Claude Code is; it is never a substitute for the measured behaviour, only a copy of it.

func NewFakePicker

func NewFakePicker(qs []askq.Question) *FakePicker

NewFakePicker shows qs, untouched, in an 80-column pane.

func (*FakePicker) Result

func (f *FakePicker) Result() (answers map[string]string, cancelled, done bool)

Result is the answers the picker submitted (nil while it is open), and whether it was cancelled instead.

func (*FakePicker) Screen

func (f *FakePicker) Screen() string

Screen is the picker as a pane would show it ("" once it is closed: the session's prompt box instead).

func (*FakePicker) Type

func (f *FakePicker) Type(keys string)

Type feeds keys (terminal bytes) to the picker.

type Kind

type Kind int

Kind is what the pane shows.

const (
	// None: no picker (it was answered, cancelled, or was never there).
	None Kind = iota
	// Question: one question's options.
	Question
	// Review: the review screen of a multi-question (or multi-select) ask.
	Review
	// Unknown: something that looks like the picker (its footer's words, or
	// the review's Submit row) that does not parse as either screen — the
	// CLI's layout has probably changed (ErrUnrecognised).
	Unknown
	// Clipped: the BOTTOM of a picker — its footer (or the review's Submit
	// and Cancel rows) under numbered rows — whose header is not on screen: a
	// picker taller than the pane, its top scrolled off (measured 2.1.289: a
	// long typed answer in an 80x20 pane). Which question it is, and what the
	// rows above say, cannot be read, so nothing may be typed into it; it is
	// never what a key should produce (ErrUnreadable).
	Clipped
)

type Options

type Options struct {
	// Settle bounds the wait for one key's screen (default 3 s); Poll is how
	// often the pane is re-read meanwhile (default 40 ms).
	Settle, Poll time.Duration
	// Steady is how far apart the two looks before every key are (default
	// SteadyDefault): a key typed in the terminal but not drawn yet at the
	// first look is drawn by the second.
	Steady time.Duration
	// Grain is Activity's resolution (default 1 s: tmux's client_activity is
	// whole seconds, rounded down) — input stamped t happened before t+Grain.
	Grain time.Duration
}

Options tunes a drive.

type Pane

type Pane interface {
	// Capture is the pane's screen, wrapped lines joined (tmux capture-pane
	// -p -J).
	Capture(ctx context.Context) (string, error)
	// Send writes keys to the program in the pane as typed bytes.
	Send(ctx context.Context, keys string) error
}

Pane is a terminal pane the driver reads and types into.

type Progress

type Progress = askq.Progress

Progress is how far a drive got (askq.Progress): the choices it was typing, and how many keys of their Plan it sent.

type ReviewItem

type ReviewItem struct {
	Question string
	Answer   string
	// QuestionEnds and AnswerEnds are, per line of each, the screen column
	// its text ends in (Row.Ends).
	QuestionEnds []int
	AnswerEnds   []int
}

ReviewItem is one "question → answer" of the review screen: each is its screen lines joined by "\n" (a long one is wrapped, and its question boxed — see Screen.Question), so where the CLI wrapped it is kept.

type Row

type Row struct {
	Num int
	// Label is the row's text: an option's label, or the "Type something" row's
	// placeholder or typed text.
	Label string
	// Box is "" for a single-select row, else "[ ]" / "[✔]".
	Box    string
	Cursor bool
	// More is the row's further lines, trimmed, in order: the rest of a label
	// (or of typed text) the CLI wrapped, then the option's description. The
	// screen does not say where the one ends and the other begins; Match
	// decides it against the label it expects (labelMatch). The "Type
	// something" row has no description, so all of its More is typed text.
	More []string
	// Ends is, for the Label line and then each More line, the screen column
	// its text ends in: whether the line is FULL (fullLines), the one place
	// the CLI breaks a word.
	Ends []int
}

Row is one numbered row of a question screen.

type Screen

type Screen struct {
	Kind Kind
	// Tabs are the question tabs (the header line), in order; Current is the
	// index of the one shown (Question), -1 when no tab is marked current.
	Tabs []Tab
	// Question is the shown question's text: its screen lines, the box border
	// and its padding stripped, joined by "\n" — the CLI draws a question over
	// 80 characters inside a box ("│ " down its left edge, measured 2.1.289:
	// gutterOver) and wraps one too long for the line, and where it wrapped is
	// kept so Match can tell a wrap from the text
	// (wrapEq).
	Question string
	// QuestionEnds is, per line of Question, the screen column its text ends
	// in (Row.Ends).
	QuestionEnds []int
	// Width is the pane's width in columns: the picker's rule, a line of "─"
	// drawn across the whole pane (the widest one on the capture). 0 when
	// there is none, and then no line is full (fullLines).
	Width int
	// Rows are the numbered rows above the "Chat about this" separator.
	Rows []Row
	// Nav is the "Next"/"Submit" row's text ("" when there is none), and
	// NavCursor whether the cursor is on it.
	Nav       string
	NavCursor bool
	// Review is the review screen's items, and ReviewCursor the numbered row
	// the cursor is on there (1 = Submit answers).
	Review       []ReviewItem
	ReviewCursor int
	// PreviewCol is, on a question drawn with previews (the package doc), the
	// screen column the preview box starts in: every row line was cut there.
	// 0: no preview box.
	PreviewCol int
	// Notes is a preview question's notes line, as drawn under the box (from
	// PreviewCol, trimmed; "" when there is none): NotesUntouched when nobody
	// has added a note. NoteEditing says the footer offers "ctrl+g to edit":
	// the notes line is being typed into (measured 2.1.289: n opens it, keys —
	// digits too — then go INTO the note, and Escape leaves it, the note
	// kept).
	Notes       string
	NoteEditing bool
}

Screen is the picker as the pane shows it.

func Parse

func Parse(capture string) Screen

Parse reads the picker from a pane capture (tmux capture-pane -p -J). The picker is the bottom of the screen: its header line is the LAST tabs line above a footer ("Enter to select · …") or a review screen's Submit row. Anything that does not parse as one of the two screens is None.

type Step

type Step struct {
	Keys string
	Want Want
}

Step is one key (or one typed text) and the screen it must produce.

func Plan

func Plan(qs []askq.Question, choices []Choice) ([]Step, error)

Plan is the keys that answer qs with choices, each with the screen it must leave — the whole drive, from the untouched picker (Start) to the picker gone. It does not touch a pane.

type Tab

type Tab struct {
	Header   string
	Answered bool
}

Tab is one question's tab in a multi-question header.

type Want

type Want struct {
	Kind Kind
	// Q is the question shown (Question): its index.
	Q int
	// Cursor is the numbered row the cursor must be on (Question), 0 for the
	// Next/Submit row.
	Cursor int
	// Checked is the options that must be checked, as 1-based row numbers
	// (multi-select); Text the "Type something" row's typed text ("" = its
	// placeholder).
	Checked []int
	Text    string
}

Want is the screen a step must leave the pane in — and, re-read, the state the NEXT step requires before it sends anything.

func At

func At(steps []Step, k int) Want

At is the screen steps leave the picker in after their first k keys: Start for none.

func Start

func Start() Want

Start is the screen the first key requires: the first question, untouched.

func (Want) String

func (w Want) String() string

Directories

Path Synopsis
Command fakepicker runs askdrive.FakePicker on a terminal, for the tests that need a picker in a REAL tmux pane (internal/mirrorhost's tmuxit suite) where no Claude Code is.
Command fakepicker runs askdrive.FakePicker on a terminal, for the tests that need a picker in a REAL tmux pane (internal/mirrorhost's tmuxit suite) where no Claude Code is.

Jump to

Keyboard shortcuts

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