-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
329 lines (277 loc) · 7.03 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
_ "embed"
"log"
"strconv"
"strings"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)
//go:generate go run github.com/dmarkham/[email protected] -type=ViewType -trimprefix ViewType
type ViewType int
const (
ViewTypeMain ViewType = iota
ViewTypeHelp
)
type View interface {
Update(m Model, msg tea.Msg) (tea.Model, tea.Cmd)
View(m Model) string
}
func NewModel(cam *Webcam, configFile ConfigFile) Model {
barWidth := 50
controls := cam.getControls()
var rows []table.Row
for _, c := range controls {
rows = append(rows, c.Row(barWidth))
}
helpView, err := newHelpView()
if err != nil {
log.Fatal(err)
}
return Model{
table: table.New([]table.Column{
table.NewColumn("name", "Name", 30).WithFiltered(true),
table.NewColumn("bar", "", barWidth).WithStyle(lipgloss.NewStyle().Align(lipgloss.Center)),
table.NewColumn("value", "Value", 7).WithStyle(lipgloss.NewStyle().Align(lipgloss.Left)),
table.NewColumn("min", "Min", 6),
table.NewColumn("max", "Max", 6),
}).WithRows(rows).
HighlightStyle(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.ANSIColor(5))).
Filtered(true).
// WithPageSize(10).
// BorderRounded().
// SelectableRows(true).
Focused(true),
cam: cam,
current: controls[0],
barWidth: barWidth,
help: help.New(),
config: configFile,
currentView: ViewTypeMain,
mainView: &MainView{},
helpView: helpView,
}
}
type Model struct {
table table.Model
current Control
cam *Webcam
lastSelectedEvent table.UserEventRowSelectToggled
lastError string
barWidth int
help help.Model
config ConfigFile
quitting bool
mainView *MainView
helpView *HelpView
currentView ViewType
}
func (m Model) Init() tea.Cmd {
return doTick()
}
func (m Model) GetCurrentView() View {
switch m.currentView {
case ViewTypeHelp:
return m.helpView
default:
return m.mainView
}
}
func (m Model) View() string {
return m.GetCurrentView().View(m)
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg, ok := msg.(tea.KeyMsg); ok {
k := msg.String()
switch k {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
case "?":
if m.currentView == ViewTypeMain {
m.currentView = ViewTypeHelp
} else {
m.currentView = ViewTypeMain
}
}
}
return m.GetCurrentView().Update(m, msg)
}
type TickMsg time.Time
func doTick() tea.Cmd {
return tea.Tick(2*time.Second, func(t time.Time) tea.Msg {
return TickMsg(t)
})
}
// MainView .
type MainView struct{}
func (v MainView) View(m Model) string {
view := lipgloss.JoinVertical(
lipgloss.Left,
m.table.View(),
m.lastError,
)
return lipgloss.NewStyle().MarginLeft(1).Render(view)
}
func (v MainView) Update(m Model, msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
m.table, cmd = m.table.Update(msg)
cmds = append(cmds, cmd)
for _, e := range m.table.GetLastUpdateUserEvents() {
switch e := e.(type) {
case table.UserEventHighlightedIndexChanged:
selected := m.table.HighlightedRow().Data["control"].(Control)
cmds = append(cmds, func() tea.Msg {
return selected
})
case table.UserEventRowSelectToggled:
m.lastSelectedEvent = e
}
}
var updateControls bool
switch msg := msg.(type) {
case tea.KeyMsg:
key := msg.String()
switch key {
case "u":
updateControls = true
case "`", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
var v int64
var err error
switch {
case key == "`":
v = 0
case key == "0":
v = 10
default:
v, err = strconv.ParseInt(key, 10, 32)
}
if err == nil {
if m.current.IsMenu() || m.current.IsBoolean() {
if err := m.cam.webcam.SetControl(m.current.ID, int32(v)); err != nil {
m.lastError = err.Error()
} else {
m.lastError = ""
}
} else {
percent := float64(v) / 10
if err := m.cam.webcam.SetControl(m.current.ID, controlValue(m.current, percent)); err != nil {
m.lastError = err.Error()
} else {
m.lastError = ""
}
}
updateControls = true
}
case "left", "h":
if err := m.cam.webcam.SetControl(m.current.ID, m.current.GetValueDecreasePercent()); err != nil {
m.lastError = err.Error()
} else {
m.lastError = ""
}
updateControls = true
case "right", "l":
if err := m.cam.webcam.SetControl(m.current.ID, m.current.GetValueIncreasePercent()); err != nil {
m.lastError = err.Error()
} else {
m.lastError = ""
}
updateControls = true
case "y":
if err := m.cam.webcam.SetControl(m.current.ID, m.current.GetValueDecreseStep()); err != nil {
m.lastError = err.Error()
} else {
m.lastError = ""
}
updateControls = true
case "o":
if err := m.cam.webcam.SetControl(m.current.ID, m.current.GetValueIncreaseStep()); err != nil {
m.lastError = err.Error()
} else {
m.lastError = ""
}
updateControls = true
case "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12":
presetID := strings.TrimPrefix(key, "f")
preset, ok := m.config.Presets[presetID]
if ok {
err := applyPreset(m.cam, preset)
if err != nil {
m.lastError = err.Error()
}
updateControls = true
}
case "alt+f1", "alt+f2", "alt+f3", "alt+f4", "alt+f5", "alt+f6",
"alt+f7", "alt+f8", "alt+f9", "alt+f10", "alt+f11", "alt+f12":
presetID := strings.TrimPrefix(key, "alt+f")
preset, err := newPreset(m.cam)
if err != nil {
m.lastError = err.Error()
}
m.config.Presets[presetID] = preset
}
case Control:
m.current = msg
m.lastError = ""
case TickMsg:
updateControls = true
cmds = append(cmds, doTick())
}
if updateControls {
controls := m.cam.getControls()
var rows []table.Row
for _, c := range controls {
if c.ID == m.current.ID {
m.current = c
}
rows = append(rows, c.Row(m.barWidth))
}
m.table = m.table.WithRows(rows)
}
if m.lastError != "" {
m.table = m.table.HighlightStyle(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.ANSIColor(1)))
} else {
m.table = m.table.HighlightStyle(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.ANSIColor(5)))
}
return m, tea.Batch(cmds...)
}
//go:embed help.md
var helpText string
// MainView .
type HelpView struct {
viewport viewport.Model
}
func (h HelpView) Update(m Model, msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
func (v HelpView) View(m Model) string {
return v.viewport.View()
}
func newHelpView() (*HelpView, error) {
const width = 78
vp := viewport.New(width, 20)
vp.Style = lipgloss.NewStyle()
renderer, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(width),
)
if err != nil {
return nil, err
}
str, err := renderer.Render(helpText)
if err != nil {
return nil, err
}
vp.SetContent(str)
return &HelpView{
viewport: vp,
}, nil
}