refactor: give the view's program status its own type without a record id

A View holds one status, so it can only ever describe the program itself.
Its own type makes child ids and the clear state impossible to write
instead of silently ignoring them.

💘 Generated with Crush

Assisted-by: Crush:claude-opus-5-5
This commit is contained in:
Kieran Klukas 2026-10-08 16:50:40 -04:00
parent 166036ae8d
commit 91651c6f36
No known key found for this signature in database
4 changed files with 51 additions and 34 deletions

View file

@ -874,18 +874,14 @@ func programStatusEquals(a, b *ProgramStatus) bool {
return *a == *b
}
// setProgramStatus reports ps on the root record, or clears it when ps is
// nil. A status the View cannot express (a child ID, the clear state, or one
// the terminal would discard) is not sent, so the previous report stays.
// setProgramStatus reports ps, or clears it when ps is nil. An invalid status
// encodes to nothing, so the previous report stays.
func setProgramStatus(s *cursedRenderer, ps *ProgramStatus) {
if ps == nil {
_, _ = s.scr.WriteString(ansi.ClearProgramStatus)
return
}
if ps.ID != "" || ps.State == ProgramStateClear {
return
}
_, _ = s.scr.WriteString(ansi.SetProgramStatus(*ps))
_, _ = s.scr.WriteString(ansi.SetProgramStatus(ps.toANSI()))
}
func viewEquals(a, b *View) bool {

View file

@ -1,6 +1,9 @@
package tea
import uv "github.com/charmbracelet/ultraviolet"
import (
uv "github.com/charmbracelet/ultraviolet"
"github.com/charmbracelet/x/ansi"
)
// ProgramStatus is a Program Status Protocol (OSC 7501) report. It tells the
// terminal whether the program is idle, working, waiting on the user, done,
@ -9,7 +12,38 @@ import uv "github.com/charmbracelet/ultraviolet"
// Support depends on the terminal. Terminals that don't support it ignore it.
//
// See: https://www.superlogical.com/rex/docs/build/program-status
type ProgramStatus = uv.ProgramStatus
type ProgramStatus struct {
// State is the program state. Required.
State ProgramState
// App is a stable machine-readable program name matching
// [A-Za-z0-9_.+-]{1,32}. Invalid values are omitted.
App string
// Kind says what a blocked program waits for. Only used with
// [ProgramStateBlocked].
Kind ProgramStatusKind
// Progress is a percentage between 0 and 100. Only used with
// [ProgramStateWorking] and [ProgramStateBlocked] when HasProgress is
// true. Otherwise progress is indeterminate.
Progress int
// HasProgress reports whether Progress is set.
HasProgress bool
// Title is a short human-readable label.
Title string
// Message is one human-readable line describing the status.
Message string
}
func (ps ProgramStatus) toANSI() ansi.ProgramStatus {
return ansi.ProgramStatus{
State: ps.State,
App: ps.App,
Kind: ps.Kind,
Progress: ps.Progress,
HasProgress: ps.HasProgress,
Title: ps.Title,
Message: ps.Message,
}
}
// ProgramState is the state of a [ProgramStatus].
type ProgramState = uv.ProgramState
@ -17,14 +51,13 @@ type ProgramState = uv.ProgramState
// ProgramStatusKind says what a blocked program waits for.
type ProgramStatusKind = uv.ProgramStatusKind
// Program states.
// Program states. Set [View.ProgramStatus] to nil to clear the status.
const (
ProgramStateIdle = uv.ProgramStateIdle
ProgramStateWorking = uv.ProgramStateWorking
ProgramStateDone = uv.ProgramStateDone
ProgramStateBlocked = uv.ProgramStateBlocked
ProgramStateError = uv.ProgramStateError
ProgramStateClear = uv.ProgramStateClear
)
// Program status kinds.

View file

@ -62,7 +62,7 @@ func TestCursedRenderer_programStatus(t *testing.T) {
pr := newProgramStatusRenderer(t)
working := &ProgramStatus{State: ProgramStateWorking, App: "tea", Message: "Building"}
if got := pr.step(working); !strings.Contains(got, ansi.SetProgramStatus(*working)) {
if got := pr.step(working); !strings.Contains(got, ansi.SetProgramStatus(working.toANSI())) {
t.Fatalf("first frame: missing report in %q", got)
}
@ -73,7 +73,7 @@ func TestCursedRenderer_programStatus(t *testing.T) {
changed := same
changed.Message = "Linking"
if got := pr.step(&changed); !strings.Contains(got, ansi.SetProgramStatus(changed)) {
if got := pr.step(&changed); !strings.Contains(got, ansi.SetProgramStatus(changed.toANSI())) {
t.Fatalf("changed status not written: %q", got)
}
@ -82,23 +82,12 @@ func TestCursedRenderer_programStatus(t *testing.T) {
}
}
func TestCursedRenderer_programStatusUnsendable(t *testing.T) {
func TestCursedRenderer_programStatusInvalid(t *testing.T) {
t.Parallel()
for name, ps := range map[string]*ProgramStatus{
"child id": {State: ProgramStateBlocked, ID: "job"},
"invalid id": {State: ProgramStateIdle, ID: "bad id"},
"clear state": {State: ProgramStateClear},
"unknown state": {State: "busy"},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
pr := newProgramStatusRenderer(t)
pr.step(&ProgramStatus{State: ProgramStateWorking})
if got := pr.step(ps); strings.Contains(got, "7501") {
t.Fatalf("unsendable status touched the terminal: %q", got)
}
})
pr := newProgramStatusRenderer(t)
pr.step(&ProgramStatus{State: ProgramStateWorking})
if got := pr.step(&ProgramStatus{State: "busy"}); strings.Contains(got, "7501") {
t.Fatalf("invalid status touched the terminal: %q", got)
}
}
@ -114,7 +103,7 @@ func TestCursedRenderer_programStatusClose(t *testing.T) {
if got := pr.close(); strings.Contains(got, "7501") {
t.Fatalf("close should leave a %s status, got %q", state, got)
}
if got := pr.restart(); !strings.Contains(got, ansi.SetProgramStatus(*ps)) {
if got := pr.restart(); !strings.Contains(got, ansi.SetProgramStatus(ps.toANSI())) {
t.Fatalf("restart did not restore the status: %q", got)
}
})
@ -129,7 +118,7 @@ func TestCursedRenderer_programStatusClose(t *testing.T) {
if got := pr.close(); !strings.Contains(got, ansi.ClearProgramStatus) {
t.Fatalf("close should clear a %s status, got %q", state, got)
}
if got := pr.restart(); !strings.Contains(got, ansi.SetProgramStatus(*ps)) {
if got := pr.restart(); !strings.Contains(got, ansi.SetProgramStatus(ps.toANSI())) {
t.Fatalf("restart did not restore the status: %q", got)
}
})

5
tea.go
View file

@ -151,9 +151,8 @@ type View struct {
// idle, working, waiting on the user, done, or failed. Support depends on
// the terminal.
//
// The status describes the program itself, so ID must be empty and State
// must not be [ProgramStateClear]; such statuses are not sent. Changing
// the status sends a new report, and setting it back to nil clears it.
// Changing the status sends a new report, and setting it back to nil
// clears it.
//
// When the program exits or is suspended, a done or error status is left
// for the user to find and any other status is cleared.