Documentation
¶
Overview ¶
Package termstatus speaks the Program Status Protocol, OSC 7501: a program tells the terminal it runs in what it is doing (working, blocked on the user, done, failed), and the terminal shows that where it likes, such as in a tab, a notification or an inbox of running programs. mdsh uses it on both sides of the terminal: mdsh and mdsh -i report their own state to the terminal they run in, and the terminals mdsh draws itself (the History pane, tty blocks) take reports from the programs blocks run.
The protocol is Mitchell Hashimoto's, revision 0.4 (2026-10-10): https://www.superlogical.com/rex/docs/build/program-status. A report is ESC ] 7501 ; key=value:key=value ESC \. Only state is required. Terminals that don't know the sequence ignore it, so a report costs nothing where it isn't understood.
Index ¶
- Constants
- func IsQuery(body string) bool
- func Name(s string) string
- func ParseCopy(body string) (string, bool)
- func ProgressSequence(r Report) string
- func ProgressTerminal(getenv func(string) string) bool
- func Strip(p []byte) []byte
- func Summary(recs []Report) string
- func TitleSequence(title string) string
- func TitleText(app, name string, r Report) string
- type Kind
- type ProgressState
- type Report
- type Scanner
- type State
- type Store
- type Stripper
- type Tracker
- func (t *Tracker) Blocked(kind Kind, msg string)
- func (t *Tracker) Child(id string, r Report)
- func (t *Tracker) Clear()
- func (t *Tracker) ClearChild(id string)
- func (t *Tracker) Done(msg string)
- func (t *Tracker) Error(msg string)
- func (t *Tracker) Exit()
- func (t *Tracker) Idle()
- func (t *Tracker) Settled() bool
- func (t *Tracker) Title(name string)
- func (t *Tracker) Unblocked()
- func (t *Tracker) WithProgress(on bool) *Tracker
- func (t *Tracker) Working(progress int, msg string)
Constants ¶
const ( // MaxSequence is the length of a whole report, OSC to ST. MaxSequence = 4096 // MaxMsg and MaxTitle are the decoded lengths of msg and title, in // bytes. MaxMsg = 2048 MaxTitle = 192 // MaxApp is the length of an app name, and of one segment of an id. MaxApp = 32 // MaxID is the length of a whole id, and MaxDepth how many segments // it may have. MaxID = 128 MaxDepth = 8 // MaxRecords is how many records a terminal must keep at least; // past its own cap it drops the one updated longest ago. MaxRecords = 64 )
The protocol's limits. A report over any of them is thrown away whole by the terminal, so Sequence cuts a message to fit.
const MaxCopy = 1 << 20
MaxCopy is the most text, in bytes, a program may copy at once.
const NoProgress = -1
NoProgress is Report.Progress for a program that doesn't know how far along it is.
const Query = "\x1b]7501;?\x1b\\"
Query asks the terminal whether it speaks the protocol. One that does answers with the same sequence, listing the states and kinds it knows; one that doesn't answers nothing, so a program that asks should send CSI c right after and give up on the first reply.
const QueryReply = "\x1b]7501;?:states=idle,working,done,blocked,error:kinds=permission,question,auth\x1b\\"
QueryReply is what a terminal that speaks the protocol answers Query with: the states and kinds it knows, which are the spec's.
const RefreshEvery = 10 * time.Second
RefreshEvery is how often the progress bar is sent again while the program works or waits: Ghostty hides a bar it hasn't heard of for 15 seconds.
const StoreCap = 256
StoreCap is how many records a Store keeps; past it, the one updated longest ago goes. The spec asks for at least MaxRecords.
Variables ¶
This section is empty.
Functions ¶
func IsQuery ¶
IsQuery reports whether body, a report's body after "7501;", is the feature query, "?".
func Name ¶
Name makes s fit an id segment or an app name: anything but letters, digits and _ . + - becomes _, and it is cut to MaxApp bytes. An empty s is "_".
func ParseCopy ¶
ParseCopy is the text an OSC 52's body, what follows "52;", puts on the clipboard. It is false for a read ("?"), which is never answered, for an empty payload, which asks some terminals to clear the clipboard, for a selection that isn't c, p, q, s or 0-7, for bad base64, for text that isn't UTF-8 and for more than MaxCopy.
func ProgressSequence ¶
ProgressSequence is r as an OSC 9;4 progress bar: working with progress is a bar at that percent, working without is the busy bar, blocked is the paused (Windows Terminal: warning) bar, error is a full red one, and anything else hides the bar.
func ProgressTerminal ¶
ProgressTerminal reports whether the terminal the environment describes shows OSC 9;4 progress rather than reading it as a notification.
func Strip ¶
Strip is p without the OSC 7501 reports in it, for output kept whole, such as a background block's.
func Summary ¶
Summary says what a program reports, in words: "working 42% building", "waiting: permission Apply?", "✗ failed", "✓ done", with its app first when it names one. "" with nothing reported. The words come from Parse, so they hold no control characters.
func TitleSequence ¶
TitleSequence is the OSC 0 that sets the window title and icon name to title, made one line without control characters, at most MaxTitle bytes.
Types ¶
type ProgressState ¶
type ProgressState int
ProgressState is the state of a ConEmu progress sequence, OSC 9;4, "state;percent", as Windows Terminal, Ghostty and VS Code show it.
const ( ProgressHidden ProgressState = 0 // no progress shown ProgressNormal ProgressState = 1 // a bar at percent ProgressError ProgressState = 2 // the bar in red ProgressIndeterminate ProgressState = 3 // a bar with no percent ProgressPaused ProgressState = 4 // the bar in yellow: paused or a warning )
type Report ¶
type Report struct {
// ID names the record, as a path: "" is the root, the program
// itself; "build/test" is a record under "build". Segments are
// letters, digits and _ . + -, at most MaxApp bytes each, MaxDepth
// deep and MaxID in all.
ID string
// State is required.
State State
// Kind says what a Blocked program waits for; it is dropped from
// other states.
Kind Kind
// Progress is how far along a Working or Blocked program is, 0 to
// 100, or NoProgress; it is dropped from other states.
Progress int
// App is a stable name for the program, such as "mdsh" or "cargo",
// for the terminal to group and filter by and pick an icon for. It
// is letters, digits and _ . + -, at most MaxApp bytes. A record
// under one with an App inherits it.
App string
// Title is a short label, at most MaxTitle bytes.
Title string
// Msg is one line on what is happening or what is wanted, at most
// MaxMsg bytes. The terminal may shorten it and reads nothing into
// it.
Msg string
}
Report is one report: the whole state of the record ID names. Each report replaces the record, so a field left out is gone from it.
func Parse ¶
Parse reads body, the body of an OSC 7501 report after "7501;", as the spec says a terminal should: pairs split on :, a pair with no = or a value with characters outside the grammar skipped, unknown keys ignored, the last of a repeated key kept. It returns false for a report to ignore whole: no state or an unknown one, a bad id, a title or msg that isn't base64, is too long, or decodes to something with a control character in it. Progress is NoProgress when there is none.
func ProgressReport ¶
ProgressReport reads the body of a ConEmu progress sequence, OSC 9;4, "state;percent", as a report on the root record: 1 (normal) is working with the percentage, 3 (indeterminate) working with none, 2 (error) is error, 4 (warning or paused) working with the percentage, and 0 (hidden) is idle, which the program's end drops. A body that doesn't parse is no report.
func Root ¶
Root is the record that stands for the whole program: the one with no id, else the first. The zero Report when there are none.
func (Report) Sequence ¶
Sequence is r as the bytes to write to the terminal, or "" when r has no State, or an ID or App the protocol doesn't allow. Title and Msg are cut to fit, at a character boundary, and their control characters, which the terminal would refuse the whole report for, become spaces.
type Scanner ¶
type Scanner struct {
// Report is called with each report that parses, and Query when
// the program asks whether the terminal speaks the protocol, so the
// caller can write QueryReply to the program.
Report func(Report)
Query func()
// Clipboard, if set, is called with the text of each OSC 52 a
// program writes to put it on the clipboard (see ParseCopy).
Clipboard func(text string)
// contains filtered or unexported fields
}
Scanner is a terminal's end of a program's stream: it passes everything written to it on to w unchanged, as the terminal shows it, and finds the OSC 7501 reports in it on the way, however they are split across writes. ConEmu progress (OSC 9;4, which build tools and package managers send and Windows Terminal, Ghostty and VS Code show) counts as a report on the root record too; see ProgressReport. A nil Scanner passes nothing and finds nothing.
func NewScanner ¶
NewScanner passes what is written to it on to w and reports the OSC 7501 reports it finds to report, and the feature query to query. Both may be nil.
type State ¶
type State string
State is what a program is doing.
const ( // Idle is a program waiting for nothing and doing nothing, as an // editor at rest. Idle State = "idle" // Working is a program busy, with Progress if it knows how far // along it is. Working State = "working" // Done is a program that finished, and whose result nobody has // seen yet. The terminal keeps it after the program exits. Done State = "done" // Blocked is a program waiting on the user, and Kind says for // what. The terminal drops it when the program exits or the shell // prints its next prompt. Blocked State = "blocked" // Error is a program that failed and stopped. The terminal keeps // it after the program exits. Error State = "error" // Clear removes the record, and every record under it; with no ID, // every record the terminal has for the program. Clear State = "clear" )
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the records of one terminal: what the programs on it have reported, one record per id, kept as the spec says. Its methods may be called from any goroutine.
func (*Store) Apply ¶
Apply takes a report in, replacing the record its id names, or for Clear removing that record and those under it (every record, with no id), and reports whether anything changed.
func (*Store) ProcessEnded ¶
ProcessEnded drops what doesn't outlive the program, as a terminal does when the process exits or the shell prints its next prompt: Working and Blocked records, and Idle ones. Done and Error stay until something replaces or clears them. It reports whether anything changed.
type Stripper ¶
type Stripper struct {
Report func(Report)
// contains filtered or unexported fields
}
Stripper is a pipe's end of a program's stream: it passes what is written to it on to w with the OSC 7501 reports taken out, since a pipe shows them as noise, and reports each one that parses to Report, however it is split across writes. The feature query is dropped without an answer, as a pipe gives none. Other escape sequences pass unchanged.
func NewStripper ¶
NewStripper passes what is written to it on to w without the OSC 7501 reports in it, which go to report, which may be nil.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker is a program's own state, the root record, reported to the terminal it runs in as it changes. Its methods may be called from any goroutine, and on a nil Tracker, or one made with a nil writer, they do nothing: a program with no terminal keeps the same calls.
func NewTracker ¶
NewTracker reports to w, the program's terminal, as app. A nil w reports nothing.
func (*Tracker) Blocked ¶
Blocked says the program waits on the user for kind, msg saying what for. Unblocked goes back to what it was doing.
func (*Tracker) Child ¶
Child reports r as a record under the program's own, id naming it: what a program the tracker's program runs is doing, as a block's program under mdsh. r's ID, if any, goes under id; its App is kept, so the terminal can tell whose record it is, and with none the terminal takes the tracker's. ClearChild takes the record back.
func (*Tracker) Clear ¶
func (t *Tracker) Clear()
Clear removes the program's records from the terminal, as when it quits with nothing to show, and does what Exit does.
func (*Tracker) ClearChild ¶
ClearChild removes the record id names, and those under it, as Child made them.
func (*Tracker) Done ¶
Done says the program finished, msg saying how. The terminal keeps it after the program exits, until the user sees it.
func (*Tracker) Error ¶
Error says the program failed and stopped, msg saying how. The terminal keeps it after the program exits.
func (*Tracker) Exit ¶
func (t *Tracker) Exit()
Exit drops what lasts only while the program runs: the progress bar goes and the terminal's own title comes back. The OSC 7501 record stays, as the protocol keeps done and error after the program ends.
func (*Tracker) Idle ¶
func (t *Tracker) Idle()
Idle says the program waits for nothing: the user is back, or there is nothing to do.
func (*Tracker) Settled ¶
Settled reports whether the last report was Done or Error: the result is on show, and a sign of life from the user means it has been seen.
func (*Tracker) Title ¶
Title has the window title follow t's state, for the file name, or for the program alone when name is "". The first call saves the terminal's own title, which Exit and Clear put back.
func (*Tracker) Unblocked ¶
func (t *Tracker) Unblocked()
Unblocked says the user answered: the program is working again, as it last reported before Blocked. Before any Working, it is Idle.
func (*Tracker) WithProgress ¶
WithProgress has t also show a progress bar (OSC 9;4) when on, as ProgressTerminal says for the program's terminal. It returns t, and is called before any report.