forked from rockset/rockset-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
47 lines (38 loc) · 823 Bytes
/
tui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/mongodb/mongo-tools/common/log"
)
type model struct {
d *Driver
}
type tickMsg time.Time
var _ (tea.Model) = (*model)(nil)
func (m *model) Init() tea.Cmd {
return tickCmd()
}
// Update implements tea.Model.
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
log.Logvf(log.DebugLow, "received %v", msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
}
case tickMsg:
return m, tickCmd()
}
return m, nil
}
// View implements tea.Model.
func (m *model) View() string {
log.Logv(log.DebugLow, "refreshing")
return m.d.stateString()
}
func tickCmd() tea.Cmd {
return tea.Tick(500*time.Millisecond, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}