From 91651c6f3673789dae8bda4798b00d347c286b6b Mon Sep 17 00:00:00 2001 From: Kieran Klukas Date: Thu, 8 Oct 2026 16:50:40 -0400 Subject: [PATCH] refactor: give the view's program status its own type without a record id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- cursed_renderer.go | 10 +++------- programstatus.go | 41 +++++++++++++++++++++++++++++++++++++---- programstatus_test.go | 29 +++++++++-------------------- tea.go | 5 ++--- 4 files changed, 51 insertions(+), 34 deletions(-) diff --git a/cursed_renderer.go b/cursed_renderer.go index e01adb8c..d4afcd1f 100644 --- a/cursed_renderer.go +++ b/cursed_renderer.go @@ -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 { diff --git a/programstatus.go b/programstatus.go index f73c45a3..5e48bd6f 100644 --- a/programstatus.go +++ b/programstatus.go @@ -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. diff --git a/programstatus_test.go b/programstatus_test.go index a02a78f3..290c0d51 100644 --- a/programstatus_test.go +++ b/programstatus_test.go @@ -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) } }) diff --git a/tea.go b/tea.go index 3220fa55..ff0571bb 100644 --- a/tea.go +++ b/tea.go @@ -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.