Compare commits
2 commits
main
...
program-st
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91651c6f36 | ||
|
|
166036ae8d |
4 changed files with 147 additions and 70 deletions
|
|
@ -157,7 +157,7 @@ func (s *cursedRenderer) start() {
|
|||
setProgressBar(s, s.lastView.ProgressBar)
|
||||
}
|
||||
if s.lastView.ProgramStatus != nil {
|
||||
setProgramStatus(s, nil, s.lastView.ProgramStatus)
|
||||
setProgramStatus(s, s.lastView.ProgramStatus)
|
||||
}
|
||||
if !s.noInput {
|
||||
// Enable modifyOtherKeys and Kitty keyboard protocol.
|
||||
|
|
@ -248,9 +248,11 @@ func (s *cursedRenderer) close() (err error) {
|
|||
if lv.ProgressBar != nil && lv.ProgressBar.State != ProgressBarNone {
|
||||
_, _ = s.scr.WriteString(ansi.ResetProgressBar)
|
||||
}
|
||||
// Unlike the progress bar, the program status is left in place: done
|
||||
// and error reports should outlive the program, and terminals drop
|
||||
// working and blocked records when the process exits.
|
||||
// Done and error should outlive the program; anything else would go
|
||||
// stale. start re-sends the status after a suspend or exec.
|
||||
if ps := lv.ProgramStatus; ps != nil && ps.State != ProgramStateDone && ps.State != ProgramStateError {
|
||||
_, _ = s.scr.WriteString(ansi.ClearProgramStatus)
|
||||
}
|
||||
}
|
||||
|
||||
if s.cellbuf.Method == ansi.GraphemeWidth {
|
||||
|
|
@ -518,7 +520,7 @@ func (s *cursedRenderer) flush(closing bool) error {
|
|||
lps = s.lastView.ProgramStatus
|
||||
}
|
||||
if !programStatusEquals(lps, view.ProgramStatus) {
|
||||
setProgramStatus(s, lps, view.ProgramStatus)
|
||||
setProgramStatus(s, view.ProgramStatus)
|
||||
}
|
||||
|
||||
// Render and queue changes to the screen buffer.
|
||||
|
|
@ -872,15 +874,14 @@ func programStatusEquals(a, b *ProgramStatus) bool {
|
|||
return *a == *b
|
||||
}
|
||||
|
||||
// setProgramStatus reports cur to the terminal. When cur is nil, or addresses
|
||||
// a different record than prev, the record of prev is cleared first.
|
||||
func setProgramStatus(s *cursedRenderer, prev, cur *ProgramStatus) {
|
||||
if prev != nil && (cur == nil || cur.ID != prev.ID) {
|
||||
_, _ = s.scr.WriteString(ansi.ClearProgramStatusID(prev.ID))
|
||||
}
|
||||
if cur != nil {
|
||||
_, _ = s.scr.WriteString(ansi.SetProgramStatus(*cur))
|
||||
// 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
|
||||
}
|
||||
_, _ = s.scr.WriteString(ansi.SetProgramStatus(ps.toANSI()))
|
||||
}
|
||||
|
||||
func viewEquals(a, b *View) bool {
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -9,74 +9,119 @@ import (
|
|||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
type programStatusRenderer struct {
|
||||
t *testing.T
|
||||
out bytes.Buffer
|
||||
r *cursedRenderer
|
||||
}
|
||||
|
||||
func newProgramStatusRenderer(t *testing.T) *programStatusRenderer {
|
||||
t.Helper()
|
||||
pr := &programStatusRenderer{t: t}
|
||||
pr.r = newCursedRenderer(&pr.out, []string{"TERM=xterm-256color"}, 80, 24)
|
||||
pr.r.start()
|
||||
return pr
|
||||
}
|
||||
|
||||
// step renders a frame with ps and returns what was written.
|
||||
func (pr *programStatusRenderer) step(ps *ProgramStatus) string {
|
||||
pr.t.Helper()
|
||||
pr.out.Reset()
|
||||
v := NewView("hello")
|
||||
v.ProgramStatus = ps
|
||||
pr.r.render(v)
|
||||
if err := pr.r.flush(false); err != nil {
|
||||
pr.t.Fatal(err)
|
||||
}
|
||||
return pr.out.String()
|
||||
}
|
||||
|
||||
// close closes the renderer and returns what was written.
|
||||
func (pr *programStatusRenderer) close() string {
|
||||
pr.t.Helper()
|
||||
pr.out.Reset()
|
||||
if err := pr.r.close(); err != nil {
|
||||
pr.t.Fatal(err)
|
||||
}
|
||||
return pr.out.String()
|
||||
}
|
||||
|
||||
// restart starts the renderer again and returns what was written.
|
||||
func (pr *programStatusRenderer) restart() string {
|
||||
pr.t.Helper()
|
||||
pr.out.Reset()
|
||||
pr.r.start()
|
||||
if err := pr.r.flush(false); err != nil {
|
||||
pr.t.Fatal(err)
|
||||
}
|
||||
return pr.out.String()
|
||||
}
|
||||
|
||||
func TestCursedRenderer_programStatus(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var out bytes.Buffer
|
||||
r := newCursedRenderer(&out, []string{"TERM=xterm-256color"}, 80, 24)
|
||||
r.start()
|
||||
|
||||
step := func(name string, ps *ProgramStatus) string {
|
||||
t.Helper()
|
||||
out.Reset()
|
||||
v := NewView("hello")
|
||||
v.ProgramStatus = ps
|
||||
r.render(v)
|
||||
if err := r.flush(false); err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
pr := newProgramStatusRenderer(t)
|
||||
|
||||
working := &ProgramStatus{State: ProgramStateWorking, App: "tea", Message: "Building"}
|
||||
if got := step("first", 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)
|
||||
}
|
||||
|
||||
same := *working
|
||||
if got := step("unchanged", &same); strings.Contains(got, "7501") {
|
||||
if got := pr.step(&same); strings.Contains(got, "7501") {
|
||||
t.Fatalf("unchanged status was written again: %q", got)
|
||||
}
|
||||
|
||||
changed := same
|
||||
changed.Message = "Linking"
|
||||
if got := step("changed", &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)
|
||||
}
|
||||
|
||||
child := &ProgramStatus{State: ProgramStateBlocked, ID: "job", Kind: ProgramStatusKindQuestion}
|
||||
got := step("id change", child)
|
||||
if strings.Contains(got, ansi.ClearProgramStatusID("job")) {
|
||||
t.Fatalf("new record should not be cleared: %q", got)
|
||||
}
|
||||
assertInOrder(t, got, ansi.ClearProgramStatus, ansi.SetProgramStatus(*child))
|
||||
|
||||
if got := step("removed", nil); !strings.Contains(got, ansi.ClearProgramStatusID("job")) {
|
||||
if got := pr.step(nil); !strings.Contains(got, ansi.ClearProgramStatus) {
|
||||
t.Fatalf("removed status not cleared: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
if got := step("invalid", &ProgramStatus{State: ProgramStateIdle, ID: "bad id"}); strings.Contains(got, "7501") {
|
||||
t.Fatalf("invalid status was written: %q", got)
|
||||
func TestCursedRenderer_programStatusInvalid(t *testing.T) {
|
||||
t.Parallel()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursedRenderer_programStatusClose(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, state := range []ProgramState{ProgramStateDone, ProgramStateError} {
|
||||
t.Run(string(state), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
pr := newProgramStatusRenderer(t)
|
||||
ps := &ProgramStatus{State: state, App: "tea"}
|
||||
pr.step(ps)
|
||||
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.toANSI())) {
|
||||
t.Fatalf("restart did not restore the status: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
done := &ProgramStatus{State: ProgramStateDone, App: "tea"}
|
||||
step("done", done)
|
||||
out.Reset()
|
||||
if err := r.close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := out.String(); strings.Contains(got, "7501") {
|
||||
t.Fatalf("close should leave the status in place, got %q", got)
|
||||
}
|
||||
|
||||
// Resuming re-sends the last status.
|
||||
out.Reset()
|
||||
r.start()
|
||||
if err := r.flush(false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := out.String(); !strings.Contains(got, ansi.SetProgramStatus(*done)) {
|
||||
t.Fatalf("restart did not restore the status: %q", got)
|
||||
for _, state := range []ProgramState{ProgramStateIdle, ProgramStateWorking, ProgramStateBlocked} {
|
||||
t.Run(string(state), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
pr := newProgramStatusRenderer(t)
|
||||
ps := &ProgramStatus{State: state, App: "tea"}
|
||||
pr.step(ps)
|
||||
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.toANSI())) {
|
||||
t.Fatalf("restart did not restore the status: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
10
tea.go
10
tea.go
|
|
@ -151,13 +151,11 @@ type View struct {
|
|||
// idle, working, waiting on the user, done, or failed. Support depends on
|
||||
// the terminal.
|
||||
//
|
||||
// The status usually addresses the root record (empty ID). Changing it
|
||||
// sends a new report, and setting it back to nil clears the record.
|
||||
// Changing the status sends a new report, and setting it back to nil
|
||||
// clears it.
|
||||
//
|
||||
// The status is not cleared when the program exits, so done and error
|
||||
// reports stay visible to the user. Report [ProgramStateDone],
|
||||
// [ProgramStateError], or [ProgramStateIdle] before quitting rather than
|
||||
// leaving a working or blocked status behind.
|
||||
// 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.
|
||||
ProgramStatus *ProgramStatus
|
||||
|
||||
// AltScreen puts the program in the alternate screen buffer
|
||||
|
|
|
|||
Loading…
Reference in a new issue