screen

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 8 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 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

View Source
const (
	CodexUpdatePrompt   = "prompt"
	CodexUpdateUpdating = "updating"
	CodexUpdateSuccess  = "success"
)

Codex update screen states.

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

func ChatAboutRow(screen string) (present, selected bool)

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 ClaudeStillInBox

func ClaudeStillInBox(input, text string) bool

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. An empty line reads as gone.

func CodexStillInBox

func CodexStillInBox(screen, text string) bool

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 CountEffortSet

func CountEffortSet(plain string) int

CountEffortSet counts Claude's "Set effort level to" and "Ultracode on" lines.

func CountKept

func CountKept(plain string) int

CountKept counts Claude's "Kept model"/"Kept effort" lines; a new one means the change was declined.

func CountModelSet

func CountModelSet(plain string) int

CountModelSet counts Claude's "Set model to" lines; a new one means a /model change was applied.

func CountPanelHints

func CountPanelHints(plain string) int

CountPanelHints counts the panel footer lines among the last 20 lines of a plain screen.

func HasPermissionPrompt

func HasPermissionPrompt(pane string) bool

HasPermissionPrompt: the pane shows a permission prompt.

func IsBusy

func IsBusy(plain string) bool

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

func IsBusyAnywhere(plain string) bool

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

func IsCodexComposerReady(content string) bool

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

func IsLaunchConfirm(plain string) bool

IsLaunchConfirm: Claude asks "Switch model?" or "Change effort level?".

func IsLaunchedPromptBoxReady

func IsLaunchedPromptBoxReady(pane string) bool

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

func IsPromptBoxReady(pane string) bool

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

func IsQuestionOnScreen(screen string) bool

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 OptionNumber

func OptionNumber(label string) (string, bool)

OptionNumber is the number a label starts with ("2" for "2. No"); false when it has none.

func PanelSnapshot

func PanelSnapshot(plain string) string

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

func ParsePlanOptions(pane string) []string

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

func PlayAnswerSteps(groups [][]KeyStep, io KeyIO) error

PlayAnswerSteps sends the groups in order, KeyStepDelay after every step and KeyAdvanceDelay after every group. It stops at the first error.

func PreviewFromScreen

func PreviewFromScreen(questionCount int, screen string) bool

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

func QueueInPromptBox(styled string) bool

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

func ReadInputLine(styled string) (text string, ok bool)

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 StripANSI

func StripANSI(s string) string

StripANSI removes terminal escape sequences from a styled capture.

func StripNumberPrefix

func StripNumberPrefix(label string) string

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

func TrustAcceptKeys(text string) []string

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 ValidateAnswerRequest

func ValidateAnswerRequest(shapes []QuestionShape, choices []QuestionChoice) error

ValidateAnswerRequest refuses what the dialog could not take; nil when the choices fit the questions.

func WaitForQuestionGone

func WaitForQuestionGone(read func() (string, bool), sleep func(time.Duration)) bool

WaitForQuestionGone polls the screen every AnswerConfirmPoll for up to AnswerConfirmTimeout until the dialog is gone. read returns false when the screen cannot be read.

Types

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 KeyIO

type KeyIO interface {
	Key(name string) error
	Text(text string) error
	Sleep(d time.Duration)
}

KeyIO sends answer steps to the pane.

type KeyStep

type KeyStep struct {
	Literal bool
	Value   string
}

KeyStep is one thing to send: a tmux key name, or text typed literally.

type Menu struct {
	Title        *string  `json:"title"`
	Request      *string  `json:"request"`
	Options      []string `json:"options"`
	FocusedIndex int      `json:"focusedIndex"`
}

Menu is a numbered choice block with a focus marker near the bottom of the screen, with its title and question.

func FindMenu

func FindMenu(plain string) *Menu

FindMenu is the choice block in the last 25 lines of a plain screen; nil when there is none.

func (m *Menu) YesFocused() bool

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

func ParseChoiceOptions(pane string) Options

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

func ParsePermissionOptions(pane string) Options

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

type QuestionChoice struct {
	Picked []int  `json:"picked"`
	Other  string `json:"other"`
}

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.

func Read

func Read(styled string) State

Read classifies 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.

Jump to

Keyboard shortcuts

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