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)
|
setProgressBar(s, s.lastView.ProgressBar)
|
||||||
}
|
}
|
||||||
if s.lastView.ProgramStatus != nil {
|
if s.lastView.ProgramStatus != nil {
|
||||||
setProgramStatus(s, nil, s.lastView.ProgramStatus)
|
setProgramStatus(s, s.lastView.ProgramStatus)
|
||||||
}
|
}
|
||||||
if !s.noInput {
|
if !s.noInput {
|
||||||
// Enable modifyOtherKeys and Kitty keyboard protocol.
|
// Enable modifyOtherKeys and Kitty keyboard protocol.
|
||||||
|
|
@ -248,9 +248,11 @@ func (s *cursedRenderer) close() (err error) {
|
||||||
if lv.ProgressBar != nil && lv.ProgressBar.State != ProgressBarNone {
|
if lv.ProgressBar != nil && lv.ProgressBar.State != ProgressBarNone {
|
||||||
_, _ = s.scr.WriteString(ansi.ResetProgressBar)
|
_, _ = s.scr.WriteString(ansi.ResetProgressBar)
|
||||||
}
|
}
|
||||||
// Unlike the progress bar, the program status is left in place: done
|
// Done and error should outlive the program; anything else would go
|
||||||
// and error reports should outlive the program, and terminals drop
|
// stale. start re-sends the status after a suspend or exec.
|
||||||
// working and blocked records when the process exits.
|
if ps := lv.ProgramStatus; ps != nil && ps.State != ProgramStateDone && ps.State != ProgramStateError {
|
||||||
|
_, _ = s.scr.WriteString(ansi.ClearProgramStatus)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.cellbuf.Method == ansi.GraphemeWidth {
|
if s.cellbuf.Method == ansi.GraphemeWidth {
|
||||||
|
|
@ -518,7 +520,7 @@ func (s *cursedRenderer) flush(closing bool) error {
|
||||||
lps = s.lastView.ProgramStatus
|
lps = s.lastView.ProgramStatus
|
||||||
}
|
}
|
||||||
if !programStatusEquals(lps, view.ProgramStatus) {
|
if !programStatusEquals(lps, view.ProgramStatus) {
|
||||||
setProgramStatus(s, lps, view.ProgramStatus)
|
setProgramStatus(s, view.ProgramStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render and queue changes to the screen buffer.
|
// Render and queue changes to the screen buffer.
|
||||||
|
|
@ -872,15 +874,14 @@ func programStatusEquals(a, b *ProgramStatus) bool {
|
||||||
return *a == *b
|
return *a == *b
|
||||||
}
|
}
|
||||||
|
|
||||||
// setProgramStatus reports cur to the terminal. When cur is nil, or addresses
|
// setProgramStatus reports ps, or clears it when ps is nil. An invalid status
|
||||||
// a different record than prev, the record of prev is cleared first.
|
// encodes to nothing, so the previous report stays.
|
||||||
func setProgramStatus(s *cursedRenderer, prev, cur *ProgramStatus) {
|
func setProgramStatus(s *cursedRenderer, ps *ProgramStatus) {
|
||||||
if prev != nil && (cur == nil || cur.ID != prev.ID) {
|
if ps == nil {
|
||||||
_, _ = s.scr.WriteString(ansi.ClearProgramStatusID(prev.ID))
|
_, _ = s.scr.WriteString(ansi.ClearProgramStatus)
|
||||||
}
|
return
|
||||||
if cur != nil {
|
|
||||||
_, _ = s.scr.WriteString(ansi.SetProgramStatus(*cur))
|
|
||||||
}
|
}
|
||||||
|
_, _ = s.scr.WriteString(ansi.SetProgramStatus(ps.toANSI()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewEquals(a, b *View) bool {
|
func viewEquals(a, b *View) bool {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package tea
|
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
|
// ProgramStatus is a Program Status Protocol (OSC 7501) report. It tells the
|
||||||
// terminal whether the program is idle, working, waiting on the user, done,
|
// 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.
|
// Support depends on the terminal. Terminals that don't support it ignore it.
|
||||||
//
|
//
|
||||||
// See: https://www.superlogical.com/rex/docs/build/program-status
|
// 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].
|
// ProgramState is the state of a [ProgramStatus].
|
||||||
type ProgramState = uv.ProgramState
|
type ProgramState = uv.ProgramState
|
||||||
|
|
@ -17,14 +51,13 @@ type ProgramState = uv.ProgramState
|
||||||
// ProgramStatusKind says what a blocked program waits for.
|
// ProgramStatusKind says what a blocked program waits for.
|
||||||
type ProgramStatusKind = uv.ProgramStatusKind
|
type ProgramStatusKind = uv.ProgramStatusKind
|
||||||
|
|
||||||
// Program states.
|
// Program states. Set [View.ProgramStatus] to nil to clear the status.
|
||||||
const (
|
const (
|
||||||
ProgramStateIdle = uv.ProgramStateIdle
|
ProgramStateIdle = uv.ProgramStateIdle
|
||||||
ProgramStateWorking = uv.ProgramStateWorking
|
ProgramStateWorking = uv.ProgramStateWorking
|
||||||
ProgramStateDone = uv.ProgramStateDone
|
ProgramStateDone = uv.ProgramStateDone
|
||||||
ProgramStateBlocked = uv.ProgramStateBlocked
|
ProgramStateBlocked = uv.ProgramStateBlocked
|
||||||
ProgramStateError = uv.ProgramStateError
|
ProgramStateError = uv.ProgramStateError
|
||||||
ProgramStateClear = uv.ProgramStateClear
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Program status kinds.
|
// Program status kinds.
|
||||||
|
|
|
||||||
|
|
@ -9,74 +9,119 @@ import (
|
||||||
"github.com/charmbracelet/x/ansi"
|
"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) {
|
func TestCursedRenderer_programStatus(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
pr := newProgramStatusRenderer(t)
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
working := &ProgramStatus{State: ProgramStateWorking, App: "tea", Message: "Building"}
|
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)
|
t.Fatalf("first frame: missing report in %q", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
same := *working
|
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)
|
t.Fatalf("unchanged status was written again: %q", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
changed := same
|
changed := same
|
||||||
changed.Message = "Linking"
|
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)
|
t.Fatalf("changed status not written: %q", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
child := &ProgramStatus{State: ProgramStateBlocked, ID: "job", Kind: ProgramStatusKindQuestion}
|
if got := pr.step(nil); !strings.Contains(got, ansi.ClearProgramStatus) {
|
||||||
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")) {
|
|
||||||
t.Fatalf("removed status not cleared: %q", got)
|
t.Fatalf("removed status not cleared: %q", got)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if got := step("invalid", &ProgramStatus{State: ProgramStateIdle, ID: "bad id"}); strings.Contains(got, "7501") {
|
func TestCursedRenderer_programStatusInvalid(t *testing.T) {
|
||||||
t.Fatalf("invalid status was written: %q", got)
|
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"}
|
for _, state := range []ProgramState{ProgramStateIdle, ProgramStateWorking, ProgramStateBlocked} {
|
||||||
step("done", done)
|
t.Run(string(state), func(t *testing.T) {
|
||||||
out.Reset()
|
t.Parallel()
|
||||||
if err := r.close(); err != nil {
|
pr := newProgramStatusRenderer(t)
|
||||||
t.Fatal(err)
|
ps := &ProgramStatus{State: state, App: "tea"}
|
||||||
}
|
pr.step(ps)
|
||||||
if got := out.String(); strings.Contains(got, "7501") {
|
if got := pr.close(); !strings.Contains(got, ansi.ClearProgramStatus) {
|
||||||
t.Fatalf("close should leave the status in place, got %q", got)
|
t.Fatalf("close should clear a %s status, got %q", state, got)
|
||||||
}
|
}
|
||||||
|
if got := pr.restart(); !strings.Contains(got, ansi.SetProgramStatus(ps.toANSI())) {
|
||||||
// Resuming re-sends the last status.
|
t.Fatalf("restart did not restore the status: %q", got)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
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
|
// idle, working, waiting on the user, done, or failed. Support depends on
|
||||||
// the terminal.
|
// the terminal.
|
||||||
//
|
//
|
||||||
// The status usually addresses the root record (empty ID). Changing it
|
// Changing the status sends a new report, and setting it back to nil
|
||||||
// sends a new report, and setting it back to nil clears the record.
|
// clears it.
|
||||||
//
|
//
|
||||||
// The status is not cleared when the program exits, so done and error
|
// When the program exits or is suspended, a done or error status is left
|
||||||
// reports stay visible to the user. Report [ProgramStateDone],
|
// for the user to find and any other status is cleared.
|
||||||
// [ProgramStateError], or [ProgramStateIdle] before quitting rather than
|
|
||||||
// leaving a working or blocked status behind.
|
|
||||||
ProgramStatus *ProgramStatus
|
ProgramStatus *ProgramStatus
|
||||||
|
|
||||||
// AltScreen puts the program in the alternate screen buffer
|
// AltScreen puts the program in the alternate screen buffer
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue