-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.go
92 lines (75 loc) · 2.12 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Package cmd provides all the commands to start parts of the application
package cmd
import (
"fmt"
"os"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/pkg/util"
"github.com/zeusWPI/scc/tui"
"github.com/zeusWPI/scc/tui/screen"
"github.com/zeusWPI/scc/tui/screen/cammie"
songScreen "github.com/zeusWPI/scc/tui/screen/song"
"github.com/zeusWPI/scc/tui/view"
"go.uber.org/zap"
)
var screens = map[string]func(*db.DB) screen.Screen{
"cammie": cammie.New,
"song": songScreen.New,
}
// TUI starts the terminal user interface
func TUI(db *db.DB) error {
args := os.Args
if len(args) < 2 {
return fmt.Errorf("No screen specified. Options are %v", util.Keys(screens))
}
selectedScreen := args[1]
val, ok := screens[selectedScreen]
if !ok {
return fmt.Errorf("Screen %s not found. Options are %v", selectedScreen, util.Keys(screens))
}
screen := val(db)
tui := tui.New(screen)
p := tea.NewProgram(tui, tea.WithAltScreen())
dones := make([]chan bool, 0, len(screen.GetUpdateViews()))
for _, updateData := range screen.GetUpdateViews() {
done := make(chan bool)
dones = append(dones, done)
go tuiPeriodicUpdates(p, updateData, done)
}
_, err := p.Run()
for _, done := range dones {
done <- true
}
return err
}
func tuiPeriodicUpdates(p *tea.Program, updateData view.UpdateData, done chan bool) {
zap.S().Info("TUI: Starting periodic update for ", updateData.Name, " with an interval of ", updateData.Interval, " seconds")
ticker := time.NewTicker(time.Duration(updateData.Interval) * time.Second)
defer ticker.Stop()
// Immediatly update once
msg, err := updateData.Update(updateData.View)
if err != nil {
zap.S().Error("TUI: Error updating ", updateData.Name, "\n", err)
}
if msg != nil {
p.Send(msg)
}
for {
select {
case <-done:
zap.S().Info("TUI: Stopping periodic update for ", updateData.Name)
return
case <-ticker.C:
// Update
msg, err := updateData.Update(updateData.View)
if err != nil {
zap.S().Error("TUI: Error updating ", updateData.Name, "\n", err)
}
if msg != nil {
p.Send(msg)
}
}
}
}