main.go
This file
Path
examples/program-status/main.go
93 lines · 2.1 KiB · Go
Last changed by
feat: OSC 7501 (#1841)Drew Smirnoffbefore adoption41bc626
History of this fileTake it
feat: OSC 7501 (#1841)Drew Smirnoffbefore adoption41bc626
lines 1–93why, one step back1package main23import (4 "fmt"5 "log"6 "time"78 tea "charm.land/bubbletea/v2"9)1011type tickMsg struct{}1213func tick() tea.Cmd {14 return tea.Tick(200*time.Millisecond, func(time.Time) tea.Msg { return tickMsg{} })15}1617var states = []tea.ProgramState{18 tea.ProgramStateIdle,19 tea.ProgramStateWorking,20 tea.ProgramStateBlocked,21 tea.ProgramStateDone,22 tea.ProgramStateError,23}2425var messages = map[tea.ProgramState]string{26 tea.ProgramStateIdle: "Waiting for instructions",27 tea.ProgramStateWorking: "Building the project",28 tea.ProgramStateBlocked: "Deploy to production?",29 tea.ProgramStateDone: "Build finished",30 tea.ProgramStateError: "Build failed",31}3233type model struct {34 state int35 progress int36 supported bool37}3839func (m model) Init() tea.Cmd {40 return tea.Batch(tea.RequestProgramStatusSupport, tick())41}4243func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {44 switch msg := msg.(type) {45 case tea.ProgramStatusSupportMsg:46 m.supported = true47 case tickMsg:48 if states[m.state] == tea.ProgramStateWorking {49 m.progress = (m.progress + 5) % 10550 }51 return m, tick()52 case tea.KeyPressMsg:53 switch msg.String() {54 case "q", "ctrl+c":55 return m, tea.Quit56 case "right", "l", "space":57 m.state = (m.state + 1) % len(states)58 m.progress = 059 case "left", "h":60 m.state = (m.state + len(states) - 1) % len(states)61 m.progress = 062 }63 }64 return m, nil65}6667func (m model) View() tea.View {68 state := states[m.state]69 support := "no reply yet (unsupported, or behind tmux)"70 if m.supported {71 support = "supported"72 }7374 v := tea.NewView(fmt.Sprintf(75 "Program Status Protocol (OSC 7501): %s\n\nState: %s\nMessage: %s\n\nPress left/right to change state, q to quit.\n",76 support, state, messages[state],77 ))78 v.ProgramStatus = &tea.ProgramStatus{79 State: state,80 App: "program-status-example",81 Kind: tea.ProgramStatusKindPermission,82 Progress: m.progress,83 HasProgress: state == tea.ProgramStateWorking,84 Message: messages[state],85 }86 return v87}8889func main() {90 if _, err := tea.NewProgram(model{}).Run(); err != nil {91 log.Fatalf("Error: %v", err)92 }93}