Documentation
¶
Overview ¶
Package screen reads what a coding agent's terminal UI (Claude Code, Codex) shows in a captured tmux pane: whether it is working, which menu or permission prompt is open, what the input line holds, whether a queued message waits, and which keys answer an AskUserQuestion dialog. Every function is pure: the caller captures the pane (plain, or styled with SGR attributes where a function says so) and decides what to do with the answer.
The patterns follow the agents' screens as captured in testdata. Whitespace and "any character" mean what they mean in the regular expressions the previous runtime used (JavaScript's \s and .), so both read a screen the same way.
Index ¶
- Constants
- func AutoDenySeconds(plain string) (int, bool)
- func BuildAnswerSteps(shapes []QuestionShape, choices []QuestionChoice) [][]KeyStep
- func ChatAboutRow(screen string) (present, selected bool)
- func ClaudeBoxHoldsEnd(box, text string) bool
- func ClaudeBoxPieceOf(input, text string) bool
- func ClaudeStillInBox(input, text string) bool
- func CodexLimitLine(plain string) string
- func CodexStillInBox(screen, text string) bool
- func ConfirmFocus(plain string) (seen, yes bool)
- func CountEffortSet(plain string) int
- func CountKept(plain string) int
- func CountModelSet(plain string) int
- func CountPanelHints(plain string) int
- func CountUltracode(plain string, on bool) int
- func CountUltracodeRefused(plain string) int
- func CutShort(entry, text string) bool
- func HasPermissionPrompt(pane string) bool
- func InterruptedLast(plain string) bool
- func IsBusy(plain string) bool
- func IsBusyAnywhere(plain string) bool
- func IsChoiceComplete(c QuestionChoice, otherOn bool) bool
- func IsCodexComposerReady(content string) bool
- func IsInstantAnswer(shapes []QuestionShape) bool
- func IsLaunchConfirm(plain string) bool
- func IsLaunchedPromptBoxReady(pane string) bool
- func IsPromptBoxReady(pane string) bool
- func IsQuestionOnScreen(screen string) bool
- func LastEcho(plain, line string) (answer string, ok bool)
- func OptionNumber(label string) (string, bool)
- func PanelSnapshot(plain string) string
- func ParsePlanOptions(pane string) []string
- func PlayAnswerSteps(groups [][]KeyStep, io KeyIO) error
- func PreviewFromScreen(questionCount int, screen string) bool
- func QueueInPromptBox(styled string) bool
- func ReadInputLine(styled string) (text string, ok bool)
- func SameMessage(entry, text string) bool
- func StripANSI(s string) string
- func StripNumberPrefix(label string) string
- func TakesFreeText(s QuestionShape) bool
- func TrustAcceptKeys(text string) []string
- func TurnDoneLast(plain string) bool
- func ValidateAnswerRequest(shapes []QuestionShape, choices []QuestionChoice) error
- func WaitForQuestionGone(read func() (string, bool), sleep func(time.Duration)) bool
- type Aside
- type CodexUpdate
- type KeyIO
- type KeyStep
- type Menu
- type Options
- type PromptOptions
- type QuestionChoice
- type QuestionShape
- type SendNowFrame
- type State
- type TrustPrompt
Constants ¶
const ( CodexUpdatePrompt = "prompt" CodexUpdateUpdating = "updating" CodexUpdateSuccess = "success" )
Codex update screen states.
const ( // MaxAnswerOptions is the most options a question may have. MaxAnswerOptions = 8 // KeyStepDelay follows every key or text; KeyAdvanceDelay follows each question's keys. KeyStepDelay = 150 * time.Millisecond KeyAdvanceDelay = 350 * time.Millisecond // AnswerConfirmPoll and AnswerConfirmTimeout pace the wait for the dialog to leave the screen. AnswerConfirmPoll = 250 * time.Millisecond AnswerConfirmTimeout = 3000 * time.Millisecond )
Variables ¶
This section is empty.
Functions ¶
func AutoDenySeconds ¶ added in v0.16.5
AutoDenySeconds is how long Claude says it waits before it denies the request on the screen by itself ("1:17" is 77, "45" is 45); false when the screen says nothing of the kind. Only the last 40 lines are read.
func BuildAnswerSteps ¶
func BuildAnswerSteps(shapes []QuestionShape, choices []QuestionChoice) [][]KeyStep
BuildAnswerSteps is the keys for every question in turn, one group per question, then "1" for "Submit answers" unless the call is one single-choice question. The request must have passed ValidateAnswerRequest.
func ChatAboutRow ¶
ChatAboutRow reads the «Chat about this» row of Claude's AskUserQuestion dialog off the plain screen: whether the dialog has it among its last lines, and whether the cursor (❯) stands on it.
func ClaudeBoxHoldsEnd ¶ added in v0.15.46
ClaudeBoxHoldsEnd: Claude's box has read a paste of this message to its end — the box ends with the message's last 24 characters (white space and picture references aside), or Claude folded the paste into a "[Pasted text #n …]".
func ClaudeBoxPieceOf ¶ added in v0.15.40
ClaudeBoxPieceOf: what is left in Claude's box (picture references aside) is a piece of text, however short — the box being cleared of a message of ours row by row.
func ClaudeStillInBox ¶
ClaudeStillInBox: the text on Claude Code's input line (Read's Input, its first line) is this message — a "[Pasted text #n …]" token, or the message's start: the line's first 24 characters, image tokens and whitespace aside, begin the message, or the message's first 24 characters are on the line — or, for a message too long for the box, a piece of it at least 24 characters long. An empty line reads as gone.
func CodexLimitLine ¶ added in v0.15.40
CodexLimitLine is Codex's notice of the usage limit when it is the last thing above the composer — the turn that just ended stopped on it: «■ You've hit your usage limit. … try again at 6:05 PM.», its wrapped lines joined. "" when the last thing above the composer is anything else (an older notice the agent has written past).
func CodexStillInBox ¶
CodexStillInBox: Codex's prompt box (its last › line and the 5 lines under it) still holds the message — its first 24 characters, whitespace collapsed, or a "[Pasted Content …]" token. A placeholder or a steer list reads as gone.
func ConfirmFocus ¶ added in v0.15.40
ConfirmFocus reads the focused answer of Claude's "Switch model?"/"Change effort level?" from the lines under its title: seen is whether a focused numbered answer is on screen, yes whether it is "1. Yes…". It does not need the whole menu: in a short pane (a terminal docked under the chat) Claude's dialog is cut at the bottom and "2. No, go back" is not drawn, but its focused first answer is.
func CountEffortSet ¶
CountEffortSet counts Claude's "Set effort level to" and "Ultracode on" lines.
func CountKept ¶
CountKept counts Claude's "Kept model"/"Kept effort" lines; a new one means the change was declined.
func CountModelSet ¶
CountModelSet counts Claude's "Set model to" lines; a new one means a /model change was applied.
func CountPanelHints ¶
CountPanelHints counts the panel footer lines among the last 20 lines of a plain screen.
func CountUltracode ¶ added in v0.15.40
CountUltracode counts Claude's answers to "/effort ultracode on" ("Ultracode on (this session only): …") or, with on false, to "/effort ultracode off" ("Ultracode off. Effort stays high.").
func CountUltracodeRefused ¶ added in v0.15.40
CountUltracodeRefused counts Claude's refusals of "/effort ultracode on".
func CutShort ¶ added in v0.15.46
CutShort: a user entry of the transcript is the start of this message and not all of it — Claude took the box while the paste was still coming in (a loaded machine) and read the rest as gone. Long enough not to be a coincidence.
func HasPermissionPrompt ¶
HasPermissionPrompt: the pane shows a permission prompt.
func InterruptedLast ¶ added in v0.15.40
InterruptedLast: the newest turn on a plain screen ended by the person's stop — Claude's «Interrupted · What should Claude do instead?» comes after the last line of the agent's output (● …); the turn's summary, a spinner and the prompt lines below it do not count.
func IsBusy ¶
IsBusy: the bottom of a plain screen says the agent is working — Claude's spinner line, or "esc to interrupt" (Codex "Working (3s • esc to interrupt)", older Claude). While Claude streams text the spinner may be off screen, so callers also look at the hook-driven state.
func IsBusyAnywhere ¶
IsBusyAnywhere is IsBusy over the whole screen: a long todo list pushes the spinner above the last lines.
func IsChoiceComplete ¶
func IsChoiceComplete(c QuestionChoice, otherOn bool) bool
IsChoiceComplete: a pick, or text once "Other" is open.
func IsCodexComposerReady ¶
IsCodexComposerReady: a plain capture shows Codex's composer taking input — a composer line among the last 8 non-blank-ended lines, no "Input disabled." and no update prompt. Codex reports its session only after the first prompt, so this is how a fresh Codex tab is known to be up.
func IsInstantAnswer ¶
func IsInstantAnswer(shapes []QuestionShape) bool
IsInstantAnswer: one single-choice question answers on the key press itself; every other call ends with the review screen.
func IsLaunchConfirm ¶
IsLaunchConfirm: Claude asks "Switch model?" or "Change effort level?".
func IsLaunchedPromptBoxReady ¶
IsLaunchedPromptBoxReady is IsPromptBoxReady for an agent that was just launched in a pane that may still show the footer of the agent that ran there before: the footer counts only when it comes after the last launch line.
func IsPromptBoxReady ¶
IsPromptBoxReady: Claude's prompt box footer is among the last 8 non-empty lines of a plain screen (notices and the agents list may sit below it).
func IsQuestionOnScreen ¶
IsQuestionOnScreen: the bottom of a plain Claude screen is the AskUserQuestion dialog — one of its question tabs (the footer starts with "Enter to select" and may wrap) or the review screen. Only the last 5 non-empty lines count: the conversation above may quote the words.
func LastEcho ¶ added in v0.15.40
LastEcho is the answer Claude printed under the last echo of a command line («❯ /model opus» then «⎿ Set model to Opus 5.5 …»): the answer's first line, without its «⎿». ok is false when the command's echo is not on the screen. Counting answer lines alone misleads in a short pane, where an older answer scrolls off as the new one comes.
func OptionNumber ¶
OptionNumber is the number a label starts with ("2" for "2. No"); false when it has none.
func PanelSnapshot ¶
PanelSnapshot is the panel part of a plain screen: below Claude's ▔▔▔ top rule when there is one (the history above it is not the panel), without blank lines and rules, at most the last 30 lines.
func ParsePlanOptions ¶
ParsePlanOptions reads the options of Claude's plan approval prompt: the first run of option lines (indented or marked) whose labels start with "Yes", "No" or "Tell Claude", number prefixes dropped.
func PlayAnswerSteps ¶
PlayAnswerSteps sends the groups in order, KeyStepDelay after every step and KeyAdvanceDelay after every group. It stops at the first error.
func PreviewFromScreen ¶
PreviewFromScreen stands in for a question's missing preview flag (a page of an older release sends none): a lone question has the preview layout when its footer offers notes; several questions are tabs whose layouts differ, so they keep the plain keys.
func QueueInPromptBox ¶
QueueInPromptBox: Claude's prompt box shows queued messages — its input line holds the "Press up to edit queued messages" placeholder, or one of the 10 lines above it is the "ctrl+x ctrl+s to send now" hint. Only the prompt box counts: the agent's own output may quote the same words anywhere above it.
func ReadInputLine ¶
ReadInputLine reads the text typed into the TUI's own input line from a styled capture: the last line that starts at column 0 with ❯ or › (leading SGR resets aside). An input showing its dim placeholder reads "". ok is false when no input line is on screen. Only the input line itself counts: a wrapped draft is judged by its first line.
func SameMessage ¶ added in v0.15.40
SameMessage: a user entry of the transcript is this message — the same text, picture references and white space aside, or (a message long enough not to be a coincidence) the entry carries it whole, as Claude may wrap it.
func StripNumberPrefix ¶
StripNumberPrefix drops a label's "1. ".
func TakesFreeText ¶
func TakesFreeText(s QuestionShape) bool
TakesFreeText: the question has an "Other" row — every layout but the single-choice one with previews.
func TrustAcceptKeys ¶
TrustAcceptKeys are the keys that answer a folder trust dialog with yes: arrows from the focused option to the yes option, then Enter. Nil when the text is not such a dialog or its focus cannot be read.
func TurnDoneLast ¶ added in v0.15.40
TurnDoneLast: Claude says on a plain screen that its newest turn is over — the turn's summary or the stop mark lies between the person's last message (❯ …) and the input box. A turn waiting on the model shows neither, spinner or not, so a quiet screen without them is still a turn at work.
func ValidateAnswerRequest ¶
func ValidateAnswerRequest(shapes []QuestionShape, choices []QuestionChoice) error
ValidateAnswerRequest refuses what the dialog could not take; nil when the choices fit the questions.
Types ¶
type Aside ¶ added in v0.16.15
type Aside string
Aside is a screen of Claude Code the person opened in the terminal and left over the input box: a message from the chat cannot go in while it is up, and Esc closes it without changing anything.
func ClaudeAside ¶ added in v0.16.15
ClaudeAside reads which aside is up at the bottom of a plain screen. Only screens whose own footer says Esc cancels them count, and only these: the trust dialog and the first-run login say «Esc to cancel» too, and there Esc quits Claude or does nothing — those are never closed. The footer has to be the screen's last line: with the input box up the last lines are the box and its status, so words the agent printed into the transcript never read as a dialog.
type CodexUpdate ¶
type CodexUpdate struct {
Status string `json:"status"`
CurrentVersion *string `json:"currentVersion"`
LatestVersion *string `json:"latestVersion"`
UpdateCommand *string `json:"updateCommand"`
}
CodexUpdate is Codex's own update screen.
func MatchCodexUpdatePrompt ¶
func MatchCodexUpdatePrompt(text string) *CodexUpdate
MatchCodexUpdatePrompt reads Codex's update screen from terminal text (wrapped lines are joined first): the prompt with both versions and the update command, the update running, or done; nil for anything else.
type Menu ¶
type Menu struct {
Title *string `json:"title"`
Request *string `json:"request"`
Options []string `json:"options"`
FocusedIndex int `json:"focusedIndex"`
// Cut: the window is too low for the menu, and it shows only the options that fit (Claude scrolls the list).
Cut bool `json:"-"`
}
Menu is a numbered choice block with a focus marker near the bottom of the screen, with its title and question.
func FindMenu ¶
FindMenu is the choice block in the last 25 lines of a plain screen; nil when there is none.
func (*Menu) YesFocused ¶
YesFocused: the focus is on the first option and it reads "1. Yes…" — Claude's "Switch model?" asked with its default answer.
type Options ¶
type Options struct {
Options []string `json:"options"`
FocusedIndex int `json:"focusedIndex"`
Title string `json:"title,omitempty"`
Request string `json:"request,omitempty"`
}
Options is a prompt's choices as drawn on screen: labels in screen order, the focused one, and the text above them. Title is set only when the block above starts with a checkbox heading (AskUserQuestion's ☐ tab); Request is the paragraph right above the options. Both are "" when absent.
func ParseChoiceOptions ¶
ParseChoiceOptions reads the last numbered choice block of a pane that has a focus marker (❯, › or >) and at least two options: AskUserQuestion, model pickers, Codex menus. Numbers are kept as drawn (a scrolled list skips some); a number not above the previous one starts a new block. Nothing found gives no options and focus 0.
func ParsePermissionOptions ¶
ParsePermissionOptions reads a permission prompt: numbered options first, unnumbered keyword options second, and only when they form a known pattern (Yes/No, Accept/Decline, Open System Settings, Use this/Continue without, the resume and idle-return dialogs). Anything else gives no options and focus 0.
type PromptOptions ¶
type PromptOptions struct {
Options
// BypassPrompt: the pane is Claude's "Bypass Permissions" warning.
BypassPrompt bool `json:"isBypassPrompt,omitempty"`
}
PromptOptions is what the permission-options route answers for a pane.
func ReadPromptOptions ¶
func ReadPromptOptions(pane string) PromptOptions
ReadPromptOptions is a permission prompt when there is one, otherwise any choice menu.
type QuestionChoice ¶
QuestionChoice is what the person chose for one question: option indexes, and the free text of the "Other" row ("" when not used).
func ParseAnswer ¶
func ParseAnswer(answer string, labels []string) QuestionChoice
ParseAnswer reads back the answer Claude Code records for a question: the labels of the picked options joined by ", " in click order, or the free text of "Other". Labels may themselves hold ", ", so the text is consumed label by label, and what no label matches is the free text.
type QuestionShape ¶
type QuestionShape struct {
OptionCount int `json:"optionCount"`
MultiSelect bool `json:"multiSelect"`
// Preview: the options carry previews.
Preview bool `json:"preview"`
}
QuestionShape is the part of a question the keys depend on.
type SendNowFrame ¶
type SendNowFrame struct {
// Input is the typed text of the input line; InputLine whether one is on screen.
Input string
InputLine bool
Menu bool
// Queued: Claude shows a waiting queue in its prompt box.
Queued bool
}
SendNowFrame is what one look at the screen tells "send now".
func ObserveSendNow ¶
func ObserveSendNow(styled string) SendNowFrame
ObserveSendNow reads a styled capture for "send now": the input line, an open menu, and the queue.
type State ¶
type State struct {
// Plain is the capture without escape sequences.
Plain string
Busy bool
Menu *Menu
// Panel: a panel or picker footer is among the last lines.
Panel bool
// Input is the typed text of the input line ("" when empty or when there is no input line); InputLine says
// whether an input line is on screen at all.
Input string
InputLine bool
}
State is one look at a styled capture.
type TrustPrompt ¶
type TrustPrompt struct {
// Agent is "claude" or "codex".
Agent string `json:"agent"`
FolderPath string `json:"folderPath"`
}
TrustPrompt is an agent asking whether to trust the folder it starts in.
func MatchTrustPrompt ¶
func MatchTrustPrompt(text string) *TrustPrompt
MatchTrustPrompt reads Claude's or Codex's folder trust dialog and the folder it is about; nil when the text is not such a dialog or names no absolute path.