termstatus

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 10 Imported by: ? 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 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

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

View Source
const MaxCopy = 1 << 20

MaxCopy is the most text, in bytes, a program may copy at once.

View Source
const NoProgress = -1

NoProgress is Report.Progress for a program that doesn't know how far along it is.

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

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

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

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

func IsQuery(body string) bool

IsQuery reports whether body, a report's body after "7501;", is the feature query, "?".

func Name

func Name(s string) string

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

func ParseCopy(body string) (string, bool)

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

func ProgressSequence(r Report) string

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

func ProgressTerminal(getenv func(string) string) bool

ProgressTerminal reports whether the terminal the environment describes shows OSC 9;4 progress rather than reading it as a notification.

func Strip

func Strip(p []byte) []byte

Strip is p without the OSC 7501 reports in it, for output kept whole, such as a background block's.

func Summary

func Summary(recs []Report) string

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

func TitleSequence(title string) string

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.

func TitleText

func TitleText(app, name string, r Report) string

TitleText is the window title for the file name while the program, app, is in r's state: "[42%] deploy.md - mdsh" while working, "[waiting]" while blocked, "[failed]" after an error, "[done]" until the result is seen. With no name it is the app alone.

Types

type Kind

type Kind string

Kind says what a blocked program waits for.

const (
	// Permission is a yes or no before something happens, as a confirm
	// block asks.
	Permission Kind = "permission"
	// Question is an answer typed or picked, as mdsh ctl ask asks.
	Question Kind = "question"
	// Auth is a credential: a password, a token, a login.
	Auth Kind = "auth"
)

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

func Parse(body string) (Report, bool)

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

func ProgressReport(body string) (Report, bool)

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

func Root(recs []Report) Report

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

func (r Report) Sequence() string

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

func NewScanner(w io.Writer, report func(Report), query func()) *Scanner

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.

func (*Scanner) Write

func (s *Scanner) Write(p []byte) (int, error)

Write passes p on to w and scans it.

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

func (s *Store) Apply(r Report) bool

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

func (s *Store) Len() int

Len is how many records there are.

func (*Store) ProcessEnded

func (s *Store) ProcessEnded() bool

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.

func (*Store) Records

func (s *Store) Records() []Report

Records is every record, the root first and then by id, each with the App of its nearest ancestor that has one when it has none itself.

func (*Store) Reset

func (s *Store) Reset() bool

Reset drops every record, as RIS does, and reports whether there were any.

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

func NewStripper(w io.Writer, report func(Report)) *Stripper

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.

func (*Stripper) Write

func (s *Stripper) Write(p []byte) (int, error)

Write passes p on, less its reports.

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

func NewTracker(w io.Writer, app string) *Tracker

NewTracker reports to w, the program's terminal, as app. A nil w reports nothing.

func (*Tracker) Blocked

func (t *Tracker) Blocked(kind Kind, msg string)

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

func (t *Tracker) Child(id string, r Report)

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

func (t *Tracker) ClearChild(id string)

ClearChild removes the record id names, and those under it, as Child made them.

func (*Tracker) Done

func (t *Tracker) Done(msg string)

Done says the program finished, msg saying how. The terminal keeps it after the program exits, until the user sees it.

func (*Tracker) Error

func (t *Tracker) Error(msg string)

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

func (t *Tracker) Settled() bool

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

func (t *Tracker) Title(name string)

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

func (t *Tracker) WithProgress(on bool) *Tracker

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.

func (*Tracker) Working

func (t *Tracker) Working(progress int, msg string)

Working says the program is busy with msg, progress percent of the way along, or NoProgress.

Jump to

Keyboard shortcuts

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