Skip to content

Instantly share code, notes, and snippets.

@taigrr
Forked from virtuallyunknown/program-block.go
Created October 15, 2023 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taigrr/2abeb1b8c49ba888fceb763e491c4278 to your computer and use it in GitHub Desktop.
Save taigrr/2abeb1b8c49ba888fceb763e491c4278 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/bubbles/stopwatch"
tea "github.com/charmbracelet/bubbletea"
)
type SomeMessage struct{}
type model struct {
hello string
items []string
stopwatch stopwatch.Model
}
func (m model) Init() tea.Cmd {
return m.stopwatch.Start()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.Type == tea.KeyEnter {
msg := fakeNetworkRequest(&m)
return m, func() tea.Msg {
return msg
}
} else {
return m, tea.Quit
}
default:
var cmd tea.Cmd
m.stopwatch, cmd = m.stopwatch.Update(msg)
return m, cmd
}
}
func fakeNetworkRequest(m *model) tea.Msg {
m.hello = "world"
time.Sleep(time.Second * 1)
return SomeMessage{}
}
func (m model) View() string {
view := ""
view += fmt.Sprintf("hello: %v\n", m.hello)
view += fmt.Sprintf("items: %v %v %v\n", m.items[0], m.items[1], m.items[2])
view += fmt.Sprintf("time: %v\n", m.stopwatch.View())
view += "press enter to block or any key to quit"
return view
}
func main() {
model := model{
items: []string{"one", "two", "three"},
stopwatch: stopwatch.NewWithInterval(time.Millisecond),
}
program := tea.NewProgram(model)
if _, err := program.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment