parent
96d69d2f7e
commit
41bc626564
11 changed files with 335 additions and 42 deletions
|
|
@ -98,6 +98,7 @@ The `tea.View` struct has fields for everything that used to be controlled by op
|
|||
| `ForegroundColor` | Set the terminal foreground color |
|
||||
| `BackgroundColor` | Set the terminal background color |
|
||||
| `ProgressBar` | Show a native terminal progress bar |
|
||||
| `ProgramStatus` | Report whether the program is working, waiting on the user, done, or failed (OSC 7501) |
|
||||
| `KeyboardEnhancements` | Request keyboard enhancement features |
|
||||
| `OnMouse` | Intercept mouse messages based on view content |
|
||||
|
||||
|
|
|
|||
|
|
@ -156,6 +156,9 @@ func (s *cursedRenderer) start() {
|
|||
if s.lastView.ProgressBar != nil {
|
||||
setProgressBar(s, s.lastView.ProgressBar)
|
||||
}
|
||||
if s.lastView.ProgramStatus != nil {
|
||||
setProgramStatus(s, nil, s.lastView.ProgramStatus)
|
||||
}
|
||||
if !s.noInput {
|
||||
// Enable modifyOtherKeys and Kitty keyboard protocol.
|
||||
// Both can coexist; terminals ignore what they don't support.
|
||||
|
|
@ -245,6 +248,9 @@ 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.
|
||||
}
|
||||
|
||||
if s.cellbuf.Method == ansi.GraphemeWidth {
|
||||
|
|
@ -506,6 +512,15 @@ func (s *cursedRenderer) flush(closing bool) error {
|
|||
setProgressBar(s, view.ProgressBar)
|
||||
}
|
||||
|
||||
// Report the program status if it's changed.
|
||||
var lps *ProgramStatus
|
||||
if s.lastView != nil {
|
||||
lps = s.lastView.ProgramStatus
|
||||
}
|
||||
if !programStatusEquals(lps, view.ProgramStatus) {
|
||||
setProgramStatus(s, lps, view.ProgramStatus)
|
||||
}
|
||||
|
||||
// Render and queue changes to the screen buffer.
|
||||
s.scr.Render(s.cellbuf.RenderBuffer)
|
||||
|
||||
|
|
@ -850,11 +865,33 @@ func setProgressBar(s *cursedRenderer, pb *ProgressBar) {
|
|||
}
|
||||
}
|
||||
|
||||
func programStatusEquals(a, b *ProgramStatus) bool {
|
||||
if a == nil || b == nil {
|
||||
return a == b
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
func viewEquals(a, b *View) bool {
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if !programStatusEquals(a.ProgramStatus, b.ProgramStatus) {
|
||||
return false
|
||||
}
|
||||
|
||||
if a.Content != b.Content ||
|
||||
a.AltScreen != b.AltScreen ||
|
||||
a.DisableBracketedPasteMode != b.DisableBracketedPasteMode ||
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@ require (
|
|||
charm.land/lipgloss/v2 v2.0.3
|
||||
github.com/charmbracelet/colorprofile v0.4.3
|
||||
github.com/charmbracelet/harmonica v0.2.0
|
||||
github.com/charmbracelet/x/ansi v0.11.7
|
||||
github.com/charmbracelet/x/ansi v0.11.9
|
||||
github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb
|
||||
github.com/charmbracelet/x/term v0.2.2
|
||||
github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776
|
||||
github.com/lucasb-eyer/go-colorful v1.4.0
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1
|
||||
github.com/segmentio/ksuid v1.0.4
|
||||
)
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ require (
|
|||
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
|
||||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aymerick/douceur v0.2.0 // indirect
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260703014108-f5a850f9c2b7 // indirect
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4 // indirect
|
||||
github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect
|
||||
github.com/charmbracelet/x/termios v0.1.1 // indirect
|
||||
github.com/charmbracelet/x/windows v0.2.2 // indirect
|
||||
|
|
@ -32,16 +32,16 @@ require (
|
|||
github.com/dlclark/regexp2 v1.11.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/gorilla/css v1.0.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.23 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.30 // indirect
|
||||
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/sahilm/fuzzy v0.1.1 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
github.com/xo/terminfo v1.0.0 // indirect
|
||||
github.com/yuin/goldmark v1.7.8 // indirect
|
||||
github.com/yuin/goldmark-emoji v1.0.5 // indirect
|
||||
golang.org/x/net v0.39.0 // indirect
|
||||
golang.org/x/sync v0.21.0 // indirect
|
||||
golang.org/x/sys v0.46.0 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
golang.org/x/text v0.24.0 // indirect
|
||||
)
|
||||
|
|
|
|||
|
|
@ -22,10 +22,10 @@ github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex
|
|||
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
|
||||
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
|
||||
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260703014108-f5a850f9c2b7 h1:3FmWoGNWK4STvqg0O0Aeav2T7rodWJAPeF0QpH+8gFw=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260703014108-f5a850f9c2b7/go.mod h1:f/jRa757WUmaOZrbPspXymbg/GnbF+rwe4OLsG7aXYo=
|
||||
github.com/charmbracelet/x/ansi v0.11.7 h1:kzv1kJvjg2S3r9KHo8hDdHFQLEqn4RBCb39dAYC84jI=
|
||||
github.com/charmbracelet/x/ansi v0.11.7/go.mod h1:9qGpnAVYz+8ACONkZBUWPtL7lulP9No6p1epAihUZwQ=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4 h1:hISLzUbwcvJbGoNR6A3L/TpguRRnACBmZOee8LcHLJw=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4/go.mod h1:aJDVk+WOVM1K04rdc/71PMPoUGfjxjhvXfP3Pjz1Zio=
|
||||
github.com/charmbracelet/x/ansi v0.11.9 h1:kIrqLBxtTVbLyyHO2Evoi93EjQo1mylWmpNEsfPxXAM=
|
||||
github.com/charmbracelet/x/ansi v0.11.9/go.mod h1:DG/hpOnaI2QJ/KMMqLL2h0KVhluuJLazf8973P+Vy5w=
|
||||
github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb h1:oTM8tZxV7FY0ehvYjFuICouuhzE08UZYNqUIp/lDQdY=
|
||||
github.com/charmbracelet/x/exp/charmtone v0.0.0-20250602192518-9e722df69bbb/go.mod h1:T9jr8CzFpjhFVHjNjKwbAD7KwBNyFnj2pntAO7F2zw0=
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA=
|
||||
|
|
@ -54,10 +54,10 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
|
|||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.23 h1:7ykA0T0jkPpzSvMS5i9uoNn2Xy3R383f9HDx3RybWcw=
|
||||
github.com/mattn/go-runewidth v0.0.23/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.30 h1:+KUuiDA4fF0R1p5FeueHefjDm+GIM+kWfFnDjybOPgk=
|
||||
github.com/mattn/go-runewidth v0.0.30/go.mod h1:3qAiGCV4Koz/yuveO58qUefmUTRm8r0IGEXZ9jeHp/8=
|
||||
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
|
||||
github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
|
||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||
|
|
@ -68,8 +68,8 @@ github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
|
|||
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
||||
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
|
||||
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
github.com/xo/terminfo v1.0.0 h1:2ZpYzqWzyyytjk3TP6aJVDhkMAkc99/1xKQdA3TDTBY=
|
||||
github.com/xo/terminfo v1.0.0/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
|
||||
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
|
|
@ -79,9 +79,9 @@ golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM
|
|||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
||||
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
||||
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
|
||||
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
|
||||
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||
|
|
|
|||
93
examples/program-status/main.go
Normal file
93
examples/program-status/main.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
)
|
||||
|
||||
type tickMsg struct{}
|
||||
|
||||
func tick() tea.Cmd {
|
||||
return tea.Tick(200*time.Millisecond, func(time.Time) tea.Msg { return tickMsg{} })
|
||||
}
|
||||
|
||||
var states = []tea.ProgramState{
|
||||
tea.ProgramStateIdle,
|
||||
tea.ProgramStateWorking,
|
||||
tea.ProgramStateBlocked,
|
||||
tea.ProgramStateDone,
|
||||
tea.ProgramStateError,
|
||||
}
|
||||
|
||||
var messages = map[tea.ProgramState]string{
|
||||
tea.ProgramStateIdle: "Waiting for instructions",
|
||||
tea.ProgramStateWorking: "Building the project",
|
||||
tea.ProgramStateBlocked: "Deploy to production?",
|
||||
tea.ProgramStateDone: "Build finished",
|
||||
tea.ProgramStateError: "Build failed",
|
||||
}
|
||||
|
||||
type model struct {
|
||||
state int
|
||||
progress int
|
||||
supported bool
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return tea.Batch(tea.RequestProgramStatusSupport, tick())
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.ProgramStatusSupportMsg:
|
||||
m.supported = true
|
||||
case tickMsg:
|
||||
if states[m.state] == tea.ProgramStateWorking {
|
||||
m.progress = (m.progress + 5) % 105
|
||||
}
|
||||
return m, tick()
|
||||
case tea.KeyPressMsg:
|
||||
switch msg.String() {
|
||||
case "q", "ctrl+c":
|
||||
return m, tea.Quit
|
||||
case "right", "l", "space":
|
||||
m.state = (m.state + 1) % len(states)
|
||||
m.progress = 0
|
||||
case "left", "h":
|
||||
m.state = (m.state + len(states) - 1) % len(states)
|
||||
m.progress = 0
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m model) View() tea.View {
|
||||
state := states[m.state]
|
||||
support := "no reply yet (unsupported, or behind tmux)"
|
||||
if m.supported {
|
||||
support = "supported"
|
||||
}
|
||||
|
||||
v := tea.NewView(fmt.Sprintf(
|
||||
"Program Status Protocol (OSC 7501): %s\n\nState: %s\nMessage: %s\n\nPress left/right to change state, q to quit.\n",
|
||||
support, state, messages[state],
|
||||
))
|
||||
v.ProgramStatus = &tea.ProgramStatus{
|
||||
State: state,
|
||||
App: "program-status-example",
|
||||
Kind: tea.ProgramStatusKindPermission,
|
||||
Progress: m.progress,
|
||||
HasProgress: state == tea.ProgramStateWorking,
|
||||
Message: messages[state],
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func main() {
|
||||
if _, err := tea.NewProgram(model{}).Run(); err != nil {
|
||||
log.Fatalf("Error: %v", err)
|
||||
}
|
||||
}
|
||||
14
go.mod
14
go.mod
|
|
@ -6,13 +6,13 @@ go 1.26.0
|
|||
|
||||
require (
|
||||
github.com/charmbracelet/colorprofile v0.4.3
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260703014108-f5a850f9c2b7
|
||||
github.com/charmbracelet/x/ansi v0.11.7
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4
|
||||
github.com/charmbracelet/x/ansi v0.11.9
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f
|
||||
github.com/charmbracelet/x/term v0.2.2
|
||||
github.com/lucasb-eyer/go-colorful v1.4.0
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1
|
||||
github.com/muesli/cancelreader v0.2.2
|
||||
golang.org/x/sys v0.46.0
|
||||
golang.org/x/sys v0.47.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
@ -21,8 +21,8 @@ require (
|
|||
github.com/charmbracelet/x/windows v0.2.2 // indirect
|
||||
github.com/clipperhouse/displaywidth v0.11.0 // indirect
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.23 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.30 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
golang.org/x/sync v0.21.0 // indirect
|
||||
github.com/xo/terminfo v1.0.0 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
)
|
||||
|
|
|
|||
28
go.sum
28
go.sum
|
|
@ -2,10 +2,10 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp
|
|||
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
|
||||
github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=
|
||||
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260703014108-f5a850f9c2b7 h1:3FmWoGNWK4STvqg0O0Aeav2T7rodWJAPeF0QpH+8gFw=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20260703014108-f5a850f9c2b7/go.mod h1:f/jRa757WUmaOZrbPspXymbg/GnbF+rwe4OLsG7aXYo=
|
||||
github.com/charmbracelet/x/ansi v0.11.7 h1:kzv1kJvjg2S3r9KHo8hDdHFQLEqn4RBCb39dAYC84jI=
|
||||
github.com/charmbracelet/x/ansi v0.11.7/go.mod h1:9qGpnAVYz+8ACONkZBUWPtL7lulP9No6p1epAihUZwQ=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4 h1:hISLzUbwcvJbGoNR6A3L/TpguRRnACBmZOee8LcHLJw=
|
||||
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4/go.mod h1:aJDVk+WOVM1K04rdc/71PMPoUGfjxjhvXfP3Pjz1Zio=
|
||||
github.com/charmbracelet/x/ansi v0.11.9 h1:kIrqLBxtTVbLyyHO2Evoi93EjQo1mylWmpNEsfPxXAM=
|
||||
github.com/charmbracelet/x/ansi v0.11.9/go.mod h1:DG/hpOnaI2QJ/KMMqLL2h0KVhluuJLazf8973P+Vy5w=
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f h1:UytXHv0UxnsDFmL/7Z9Q5SBYPwSuRLXHbwx+6LycZ2w=
|
||||
github.com/charmbracelet/x/exp/golden v0.0.0-20241212170349-ad4b7ae0f25f/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
|
||||
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
|
||||
|
|
@ -18,19 +18,19 @@ github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSE
|
|||
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.23 h1:7ykA0T0jkPpzSvMS5i9uoNn2Xy3R383f9HDx3RybWcw=
|
||||
github.com/mattn/go-runewidth v0.0.23/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss=
|
||||
github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.30 h1:+KUuiDA4fF0R1p5FeueHefjDm+GIM+kWfFnDjybOPgk=
|
||||
github.com/mattn/go-runewidth v0.0.30/go.mod h1:3qAiGCV4Koz/yuveO58qUefmUTRm8r0IGEXZ9jeHp/8=
|
||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
github.com/xo/terminfo v1.0.0 h1:2ZpYzqWzyyytjk3TP6aJVDhkMAkc99/1xKQdA3TDTBY=
|
||||
github.com/xo/terminfo v1.0.0/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
|
||||
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
|
||||
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
|
|
|
|||
2
input.go
2
input.go
|
|
@ -49,6 +49,8 @@ func (p *Program) translateInputEvent(e uv.Event) Msg {
|
|||
return KeyboardEnhancementsMsg(e)
|
||||
case uv.ModeReportEvent:
|
||||
return ModeReportMsg(e)
|
||||
case uv.ProgramStatusSupportEvent:
|
||||
return ProgramStatusSupportMsg{}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
|
|
|||
55
programstatus.go
Normal file
55
programstatus.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package tea
|
||||
|
||||
import uv "github.com/charmbracelet/ultraviolet"
|
||||
|
||||
// ProgramStatus is a Program Status Protocol (OSC 7501) report. It tells the
|
||||
// terminal whether the program is idle, working, waiting on the user, done,
|
||||
// or failed, and why. Set it on [View.ProgramStatus].
|
||||
//
|
||||
// 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
|
||||
|
||||
// ProgramState is the state of a [ProgramStatus].
|
||||
type ProgramState = uv.ProgramState
|
||||
|
||||
// ProgramStatusKind says what a blocked program waits for.
|
||||
type ProgramStatusKind = uv.ProgramStatusKind
|
||||
|
||||
// Program states.
|
||||
const (
|
||||
ProgramStateIdle = uv.ProgramStateIdle
|
||||
ProgramStateWorking = uv.ProgramStateWorking
|
||||
ProgramStateDone = uv.ProgramStateDone
|
||||
ProgramStateBlocked = uv.ProgramStateBlocked
|
||||
ProgramStateError = uv.ProgramStateError
|
||||
ProgramStateClear = uv.ProgramStateClear
|
||||
)
|
||||
|
||||
// Program status kinds.
|
||||
const (
|
||||
ProgramStatusKindPermission = uv.ProgramStatusKindPermission
|
||||
ProgramStatusKindQuestion = uv.ProgramStatusKindQuestion
|
||||
ProgramStatusKindAuth = uv.ProgramStatusKindAuth
|
||||
)
|
||||
|
||||
// ProgramStatusSupportMsg is sent when the terminal replies to
|
||||
// [RequestProgramStatusSupport], meaning it supports the Program Status
|
||||
// Protocol.
|
||||
type ProgramStatusSupportMsg struct{}
|
||||
|
||||
// programStatusSupportMsg is an internal message that queries the terminal
|
||||
// for Program Status Protocol support.
|
||||
type programStatusSupportMsg struct{}
|
||||
|
||||
// RequestProgramStatusSupport is a command that asks the terminal whether it
|
||||
// supports the Program Status Protocol (OSC 7501). A supporting terminal
|
||||
// replies with a [ProgramStatusSupportMsg]. Terminals that don't support it
|
||||
// never reply, so pick your own timeout. Use [WithInput] to enable input,
|
||||
// since the reply cannot be read otherwise.
|
||||
//
|
||||
// Detection is optional: reports can be sent without it.
|
||||
func RequestProgramStatusSupport() Msg {
|
||||
return programStatusSupportMsg{}
|
||||
}
|
||||
88
programstatus_test.go
Normal file
88
programstatus_test.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package tea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
uv "github.com/charmbracelet/ultraviolet"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
working := &ProgramStatus{State: ProgramStateWorking, App: "tea", Message: "Building"}
|
||||
if got := step("first", working); !strings.Contains(got, ansi.SetProgramStatus(*working)) {
|
||||
t.Fatalf("first frame: missing report in %q", got)
|
||||
}
|
||||
|
||||
same := *working
|
||||
if got := step("unchanged", &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)) {
|
||||
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")) {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateProgramStatusSupport(t *testing.T) {
|
||||
var p Program
|
||||
if got := p.translateInputEvent(uv.ProgramStatusSupportEvent{}); got != (ProgramStatusSupportMsg{}) {
|
||||
t.Fatalf("got %T, want ProgramStatusSupportMsg", got)
|
||||
}
|
||||
}
|
||||
17
tea.go
17
tea.go
|
|
@ -146,6 +146,20 @@ type View struct {
|
|||
// progress bar section. Support depends on the terminal.
|
||||
ProgressBar *ProgressBar
|
||||
|
||||
// ProgramStatus when not nil, reports the program status to the terminal
|
||||
// using the Program Status Protocol (OSC 7501): whether the program is
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
ProgramStatus *ProgramStatus
|
||||
|
||||
// AltScreen puts the program in the alternate screen buffer
|
||||
// (i.e. the program goes into full window mode). Note that the altscreen will
|
||||
// be automatically exited when the program quits.
|
||||
|
|
@ -838,6 +852,9 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
|
|||
case cursorColorMsg:
|
||||
p.executeQuery(ansi.RequestCursorColor)
|
||||
|
||||
case programStatusSupportMsg:
|
||||
p.executeQuery(ansi.RequestProgramStatusSupport)
|
||||
|
||||
case execMsg:
|
||||
// NB: this blocks.
|
||||
p.exec(msg.cmd, msg.fn)
|
||||
|
|
|
|||
Loading…
Reference in a new issue