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
|
package tui
import (
"slices"
tea "github.com/charmbracelet/bubbletea"
)
type KeyBindingScope int
const (
ScopeGlobal KeyBindingScope = iota
ScopeTimerBox
ScopeProjectsBox
ScopeHistoryBoxSummaries
ScopeHistoryBoxDetails
)
// KeyBinding represents the available key bindings for a view
type KeyBinding struct {
Key string
Description func(AppModel) string
Scope KeyBindingScope
Result func(AppModel) tea.Cmd
Hide bool
}
type (
createClientMsg struct{}
createProjectMsg struct{}
deleteHistoryEntry struct{}
editHistoryEntry struct{}
)
func msgAsCmd(msg tea.Msg) tea.Cmd {
return func() tea.Msg { return msg }
}
var Bindings map[KeyBindingScope]map[string]KeyBinding = map[KeyBindingScope]map[string]KeyBinding{
ScopeGlobal: {
"ctrl+n": KeyBinding{
Key: "Ctrl+n",
Description: func(AppModel) string { return "Next Pane" },
Scope: ScopeGlobal,
Result: func(AppModel) tea.Cmd { return navigate(true) },
},
"ctrl+p": KeyBinding{
Key: "Ctrl+p",
Description: func(AppModel) string { return "Prev Pane" },
Scope: ScopeGlobal,
Result: func(AppModel) tea.Cmd { return navigate(false) },
},
"p": KeyBinding{
Key: "p",
Description: func(am AppModel) string {
if am.timerBox.timerInfo.IsActive {
return "Punch Out"
}
return "Punch In"
},
Scope: ScopeGlobal,
Result: func(am AppModel) tea.Cmd {
if am.timerBox.timerInfo.IsActive {
return punchOut(am)
}
return punchIn(am)
},
},
"/": KeyBinding{
Key: "/",
Description: func(am AppModel) string { return "Search" },
Scope: ScopeGlobal,
Result: func(AppModel) tea.Cmd { return activateSearch() },
},
"r": KeyBinding{
Key: "r",
Description: func(am AppModel) string { return "Refresh" },
Scope: ScopeGlobal,
Result: func(am AppModel) tea.Cmd { return am.refreshCmd },
},
"q": KeyBinding{
Key: "q",
Description: func(am AppModel) string { return "Quit" },
Scope: ScopeGlobal,
Result: func(AppModel) tea.Cmd { return tea.Quit },
},
"ctrl+c": KeyBinding{
Key: "Ctrl+c",
Description: func(am AppModel) string { return "Quit" },
Scope: ScopeGlobal,
Result: func(AppModel) tea.Cmd { return tea.Quit },
Hide: true,
},
"ctrl+d": KeyBinding{
Key: "Ctrl+d",
Description: func(am AppModel) string { return "Quit" },
Scope: ScopeGlobal,
Result: func(AppModel) tea.Cmd { return tea.Quit },
Hide: true,
},
},
ScopeTimerBox: {
"enter": KeyBinding{
Key: "Enter",
Description: func(am AppModel) string {
if am.timerBox.timerInfo.IsActive {
return "Punch Out"
}
return "Punch In"
},
Scope: ScopeTimerBox,
Result: func(am AppModel) tea.Cmd {
if am.timerBox.timerInfo.IsActive {
return punchOut(am)
}
return punchIn(am)
},
},
},
ScopeProjectsBox: {
"j": KeyBinding{
Key: "j",
Description: func(AppModel) string { return "Down" },
Scope: ScopeProjectsBox,
Result: func(AppModel) tea.Cmd { return changeSelection(true) },
},
"k": KeyBinding{
Key: "k",
Description: func(AppModel) string { return "Up" },
Scope: ScopeProjectsBox,
Result: func(AppModel) tea.Cmd { return changeSelection(false) },
},
"down": KeyBinding{
Key: "down",
Description: func(AppModel) string { return "Down" },
Scope: ScopeProjectsBox,
Result: func(AppModel) tea.Cmd { return changeSelection(true) },
Hide: true,
},
"up": KeyBinding{
Key: "up",
Description: func(AppModel) string { return "Up" },
Scope: ScopeProjectsBox,
Result: func(AppModel) tea.Cmd { return changeSelection(false) },
Hide: true,
},
"enter": KeyBinding{
Key: "Enter",
Description: func(AppModel) string { return "Punch In on Selection" },
Scope: ScopeProjectsBox,
Result: func(am AppModel) tea.Cmd { return punchInOnSelection(am) },
},
"n": KeyBinding{
Key: "n",
Description: func(AppModel) string { return "New Project" },
Scope: ScopeProjectsBox,
Result: func(AppModel) tea.Cmd { return msgAsCmd(createProjectMsg{}) },
},
"N": KeyBinding{
Key: "N",
Description: func(AppModel) string { return "New Client" },
Scope: ScopeProjectsBox,
Result: func(AppModel) tea.Cmd { return msgAsCmd(createClientMsg{}) },
},
},
ScopeHistoryBoxSummaries: {
"j": KeyBinding{
Key: "j",
Description: func(AppModel) string { return "Down" },
Scope: ScopeHistoryBoxSummaries,
Result: func(AppModel) tea.Cmd { return changeSelection(true) },
},
"k": KeyBinding{
Key: "k",
Description: func(AppModel) string { return "Up" },
Scope: ScopeHistoryBoxSummaries,
Result: func(AppModel) tea.Cmd { return changeSelection(false) },
},
"down": KeyBinding{
Key: "down",
Description: func(AppModel) string { return "Down" },
Scope: ScopeHistoryBoxSummaries,
Result: func(AppModel) tea.Cmd { return changeSelection(true) },
Hide: true,
},
"up": KeyBinding{
Key: "up",
Description: func(AppModel) string { return "Up" },
Scope: ScopeHistoryBoxSummaries,
Result: func(AppModel) tea.Cmd { return changeSelection(false) },
Hide: true,
},
"enter": KeyBinding{
Key: "Enter",
Description: func(AppModel) string { return "Select" },
Scope: ScopeHistoryBoxSummaries,
Result: func(AppModel) tea.Cmd { return selectHistorySummary() },
},
},
ScopeHistoryBoxDetails: {
"j": KeyBinding{
Key: "j",
Description: func(AppModel) string { return "Down" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return changeSelection(true) },
},
"k": KeyBinding{
Key: "k",
Description: func(AppModel) string { return "Up" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return changeSelection(false) },
},
"down": KeyBinding{
Key: "Down",
Description: func(AppModel) string { return "Down" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return changeSelection(true) },
Hide: true,
},
"up": KeyBinding{
Key: "Up",
Description: func(AppModel) string { return "Up" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return changeSelection(false) },
Hide: true,
},
"e": KeyBinding{
Key: "e",
Description: func(AppModel) string { return "Edit" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return msgAsCmd(editHistoryEntry{}) },
},
"d": KeyBinding{
Key: "d",
Description: func(AppModel) string { return "Delete" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return msgAsCmd(deleteHistoryEntry{}) },
},
"enter": KeyBinding{
Key: "Enter",
Description: func(AppModel) string { return "Resume" },
Scope: ScopeHistoryBoxDetails,
Result: func(am AppModel) tea.Cmd { return punchInOnSelection(am) },
},
"b": KeyBinding{
Key: "b",
Description: func(AppModel) string { return "Back" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return backToHistorySummary() },
},
"esc": KeyBinding{
Key: "Esc",
Description: func(AppModel) string { return "Back" },
Scope: ScopeHistoryBoxDetails,
Result: func(AppModel) tea.Cmd { return backToHistorySummary() },
Hide: true,
},
},
}
// KeyHandler processes key messages and returns the appropriate action
func HandleKeyPress(msg tea.KeyMsg, data AppModel) tea.Cmd {
key := msg.String()
if binding, ok := Bindings[ScopeGlobal][key]; ok {
return binding.Result(data)
}
var local map[string]KeyBinding
switch data.selectedBox {
case TimerBox:
local = Bindings[ScopeTimerBox]
case ProjectsBox:
local = Bindings[ScopeProjectsBox]
case HistoryBox:
switch data.historyBox.viewLevel {
case HistoryLevelSummary:
local = Bindings[ScopeHistoryBoxSummaries]
case HistoryLevelDetails:
local = Bindings[ScopeHistoryBoxDetails]
}
}
if binding, ok := local[key]; ok {
return binding.Result(data)
}
return nil
}
func activeBindings(box BoxType, level HistoryViewLevel) []KeyBinding {
out := make([]KeyBinding, 0, len(Bindings[ScopeGlobal]))
for _, binding := range Bindings[ScopeGlobal] {
out = append(out, binding)
}
var scope KeyBindingScope
switch box {
case TimerBox:
scope = ScopeTimerBox
case ProjectsBox:
scope = ScopeProjectsBox
case HistoryBox:
switch level {
case HistoryLevelSummary:
scope = ScopeHistoryBoxSummaries
case HistoryLevelDetails:
scope = ScopeHistoryBoxDetails
}
}
for _, binding := range Bindings[scope] {
out = append(out, binding)
}
slices.SortFunc(out, func(a, b KeyBinding) int {
if a.Key < b.Key {
return -1
}
return 1
})
return out
}
|