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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
package grid
import (
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"ticktock/internal/activity"
"ticktock/internal/config"
"ticktock/internal/store"
"ticktock/internal/tui/form"
)
// Column identifies the focusable column.
type Column int
const (
ColActivity Column = iota
ColLogged
)
// Model is the grid-view Bubble Tea model.
type Model struct {
store store.Store
cfg config.Config
date string
entries []store.Entry
grid Grid
logged []Cell
src ActivitySource
segs []activity.Segment
events []activity.Event
activity []Cell
cursor int
top int // first visible row (vertical scroll offset)
col Column
anchor *int // non-nil while a selection is in progress
width int
height int
// editing state (Tasks 6-7)
editing bool
adding bool
form *huh.Form
fv *form.Values
editKey string
editOrig store.Entry
confirmDelete bool
confirmKey string
sug form.Suggest
status string
err error
quit bool
}
type entriesMsg struct {
entries []store.Entry
segs []activity.Segment
events []activity.Event
err error
}
// ActivitySource tells the grid where to read the ACTIVITY lane's data.
// The zero value disables the lane (static gap lane, as before).
type ActivitySource struct {
DataDir string // ~/.local/share/ticktock; "" disables activity
CalBin string // path to the cal-events helper; "" disables calendar
Filter activity.CalendarFilter
}
// WithActivity returns a copy of the model wired to read activity + calendar
// data on every load.
func (m Model) WithActivity(src ActivitySource) Model {
m.src = src
return m
}
// New builds a grid model for date (YYYY-MM-DD).
func New(s store.Store, cfg config.Config, date string, sug form.Suggest) Model {
return Model{store: s, cfg: cfg, date: date, col: ColActivity, sug: sug}
}
func (m Model) Init() tea.Cmd { return m.load() }
func (m Model) load() tea.Cmd {
date, s, src := m.date, m.store, m.src
return func() tea.Msg {
es, err := s.Day(date)
segs, _ := activity.LoadDay(src.DataDir, date) // tolerant: never fails the view
events, _ := activity.LoadCalendar(src.CalBin, date, src.Filter)
return entriesMsg{entries: es, segs: segs, events: events, err: err}
}
}
// rebuild recomputes the grid window and painted cells from current entries.
func (m *Model) rebuild() {
startMin, err := parseHM(m.cfg.GridStart)
if err != nil {
startMin = 7 * 60
}
endMin, err := parseHM(m.cfg.GridEnd)
if err != nil {
endMin = 21 * 60
}
m.grid = BuildGrid(m.cfg.SlotMinutes, startMin, endMin, m.entries)
m.logged = paintLogged(m.grid, m.entries)
m.activity = paintActivity(m.grid, m.segs, m.events)
if m.cursor > m.grid.Rows-1 {
m.cursor = m.grid.Rows - 1
}
if m.cursor < 0 {
m.cursor = 0
}
m.clampScroll()
}
// chromeRows is the number of non-grid lines View always draws: header + blank
// + column header + rule (4 top) and blank + legend + help (3 bottom).
const chromeRows = 7
// visibleRows is how many grid rows fit in the current terminal height. When the
// height is unknown (before the first WindowSizeMsg) it returns all rows so the
// view degrades to the pre-viewport behavior.
func (m Model) visibleRows() int {
if m.height <= 0 {
return m.grid.Rows
}
v := m.height - chromeRows
if v < 1 {
v = 1
}
if v > m.grid.Rows {
v = m.grid.Rows
}
return v
}
// clampScroll keeps the scroll offset (m.top) valid and the cursor within the
// visible window, scrolling just enough to reveal the cursor at either edge.
func (m *Model) clampScroll() {
vis := m.visibleRows()
if m.cursor < m.top {
m.top = m.cursor
}
if m.cursor >= m.top+vis {
m.top = m.cursor - vis + 1
}
if max := m.grid.Rows - vis; m.top > max {
m.top = max
}
if m.top < 0 {
m.top = 0
}
}
// selectionRange returns the inclusive band [lo,hi] and whether a selection is
// active.
func (m Model) selectionRange() (int, int, bool) {
if m.anchor == nil {
return 0, 0, false
}
lo, hi := *m.anchor, m.cursor
if hi < lo {
lo, hi = hi, lo
}
return lo, hi, true
}
func shiftDate(date string, days int) string {
t, err := time.ParseInLocation("2006-01-02", date, time.Local)
if err != nil {
return date
}
return t.AddDate(0, 0, days).Format("2006-01-02")
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.confirmDelete {
if km, ok := msg.(tea.KeyMsg); ok {
switch km.String() {
case "y":
m.confirmDelete = false
key := m.confirmKey
m.confirmKey = ""
if err := m.store.Remove(key); err != nil {
m.status = "delete failed: " + err.Error()
} else {
m.status = "removed " + key
}
return m, m.load()
case "n", "esc":
m.confirmDelete = false
m.confirmKey = ""
m.status = "delete cancelled"
return m, nil
}
}
return m, nil
}
if m.editing {
return m.updateEditing(msg)
}
switch msg := msg.(type) {
case entriesMsg:
m.entries, m.segs, m.events, m.err = msg.entries, msg.segs, msg.events, msg.err
m.rebuild()
return m, nil
case tea.WindowSizeMsg:
m.width, m.height = msg.Width, msg.Height
m.clampScroll()
return m, nil
case tea.KeyMsg:
return m.handleKey(msg)
}
return m, nil
}
func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "q", "ctrl+c":
m.quit = true
return m, tea.Quit
case "left":
m.col = ColActivity
return m, nil
case "right":
m.col = ColLogged
return m, nil
case "up":
if m.cursor > 0 {
m.cursor--
}
m.clampScroll()
return m, nil
case "down":
if m.cursor < m.grid.Rows-1 {
m.cursor++
}
m.clampScroll()
return m, nil
case "esc":
m.anchor = nil
return m, nil
case " ":
return m.handleSpace()
case "enter":
return m.handleEnter()
case "d":
return m.handleDelete()
case "p":
m.date = shiftDate(m.date, -1)
m.anchor = nil
return m, m.load()
case "n":
m.date = shiftDate(m.date, 1)
m.anchor = nil
return m, m.load()
case "t":
m.date = time.Now().Format("2006-01-02")
m.anchor = nil
return m, m.load()
case "r":
return m, m.load()
}
return m, nil
}
// handleSpace anchors a selection or finalizes it into an add form.
func (m Model) handleSpace() (tea.Model, tea.Cmd) {
if m.col != ColActivity {
return m, nil
}
if m.anchor == nil {
a := m.cursor
m.anchor = &a
return m, nil
}
lo, hi, _ := m.selectionRange()
m.anchor = nil
start, end := m.rangeTimes(lo, hi)
m.editing, m.adding = true, true
m.fv = &form.Values{}
m.form = form.AddForm(start, end, m.cfg.Project, m.fv, m.sug)
return m, m.form.Init()
}
// rangeTimes returns HH:MM for the selection's start slot and the slot after
// its last (exclusive end).
func (m Model) rangeTimes(lo, hi int) (string, string) {
start := minuteLabel(m.grid.SlotMinute(lo))
end := minuteLabel(m.grid.SlotMinute(hi) + m.grid.SlotMin)
return start, end
}
func (m Model) entryByKey(k string) (store.Entry, bool) {
for _, e := range m.entries {
if e.Key == k {
return e, true
}
}
return store.Entry{}, false
}
// handleEnter opens the edit form for the logged session under the cursor.
func (m Model) handleEnter() (tea.Model, tea.Cmd) {
if m.col != ColLogged {
return m, nil
}
k, ok := entryKeyAtRow(m.logged, m.cursor)
if !ok {
return m, nil
}
e, ok := m.entryByKey(k)
if !ok || e.Running() {
return m, nil
}
m.editing, m.adding = true, false
m.editKey, m.editOrig = e.Key, e
m.fv = &form.Values{}
m.form = form.New(e, m.fv, m.sug)
return m, m.form.Init()
}
// handleDelete arms the yes/no confirm for the session under the cursor.
func (m Model) handleDelete() (tea.Model, tea.Cmd) {
if m.col != ColLogged {
return m, nil
}
k, ok := entryKeyAtRow(m.logged, m.cursor)
if !ok {
return m, nil
}
m.confirmDelete = true
m.confirmKey = k
m.status = "delete " + k + "? y/n"
return m, nil
}
func (m Model) updateEditing(msg tea.Msg) (tea.Model, tea.Cmd) {
if km, ok := msg.(tea.KeyMsg); ok {
switch km.String() {
case "esc":
m.editing, m.adding = false, false
m.form = nil
return m, nil
case "ctrl+s":
return m.finishEditing()
}
}
f, cmd := m.form.Update(msg)
if ff, ok := f.(*huh.Form); ok {
m.form = ff
}
if m.form.State == huh.StateCompleted {
return m.finishEditing()
}
return m, cmd
}
// finishEditing routes to Add (new range) or SafeReplace (existing) by intent.
func (m Model) finishEditing() (tea.Model, tea.Cmd) {
adding := m.adding
m.editing, m.adding = false, false
up, err := form.BuildEntry(m.date, m.editOrig, *m.fv)
if err != nil {
m.status = "invalid: " + err.Error()
m.form = nil
return m, nil
}
if adding {
if err := m.store.Add(up); err != nil {
m.status = "add failed: " + err.Error()
} else {
m.status = "added entry"
}
} else {
if err := m.store.SafeReplace(m.editKey, up, m.editOrig); err != nil {
m.status = "edit failed: " + err.Error()
} else {
m.status = "updated " + m.editKey
}
}
m.form = nil
return m, m.load()
}
|