summaryrefslogtreecommitdiff
path: root/internal/ui/table.go
blob: 4b13b4ba53734004e9f3e9dd4aa1e8d8c6a2e824 (plain)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package ui

import (
	"fmt"
	"math/rand"
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/charmbracelet/x/ansi"

	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	atable "tasksamurai/internal/atable"
	"tasksamurai/internal/task"
)

var priorityOptions = []string{"H", "M", "L", ""}

func init() {
	rand.Seed(time.Now().UnixNano())
}

type cellMatch struct {
	row int
	col int
}

// Model wraps a Bubble Tea table.Model to display tasks.

type Model struct {
	tbl      atable.Model
	showHelp bool

	annotating         bool
	annotateID         int
	annotateInput      textinput.Model
	replaceAnnotations bool

	dueEditing bool
	dueID      int
	dueDate    time.Time

	searching     bool
	searchInput   textinput.Model
	searchRegex   *regexp.Regexp
	searchMatches []cellMatch
	searchIndex   int

	prioritySelecting bool
	priorityID        int
	priorityIndex     int

	filters []string
	tasks   []task.Task

	undoStack []string

	total      int
	inProgress int
	due        int
}

// editDoneMsg is emitted when the external editor process finishes.
type editDoneMsg struct{ err error }

// editCmd returns a command that edits the task and sends an
// editDoneMsg once the process is complete.
func editCmd(id int) tea.Cmd {
	c := task.EditCmd(id)
	return tea.ExecProcess(c, func(err error) tea.Msg { return editDoneMsg{err: err} })
}

// New creates a new UI model with the provided rows.
func New(filters []string) (Model, error) {
	m := Model{filters: filters}
	m.annotateInput = textinput.New()
	m.annotateInput.Prompt = "annotation: "
	m.dueDate = time.Now()
	m.searchInput = textinput.New()
	m.searchInput.Prompt = "search: "

	if err := m.reload(); err != nil {
		return Model{}, err
	}

	return m, nil
}

func newTable(rows []atable.Row) atable.Model {
	cols := []atable.Column{
		{Title: "ID", Width: 4},
		{Title: "Pri", Width: 4},
		{Title: "Age", Width: 6},
		{Title: "Urg", Width: 5},
		{Title: "Due", Width: 10},
		{Title: "Tags", Width: 15},
		{Title: "Description", Width: 45},
		{Title: "Annotations", Width: 20},
	}
	t := atable.New(
		atable.WithColumns(cols),
		atable.WithRows(rows),
		atable.WithFocused(true),
		atable.WithShowHeaders(false),
	)
	styles := atable.DefaultStyles()
	styles.Header = styles.Header.Foreground(lipgloss.Color("205"))
	styles.Selected = styles.Selected.Foreground(lipgloss.Color("229")).Background(lipgloss.Color("57"))
	styles.Cell = styles.Cell.Padding(0, 1)
	t.SetStyles(styles)
	return t
}

func (m *Model) reload() error {
	// Always show only pending tasks by default.
	filters := append([]string(nil), m.filters...)
	filters = append(filters, "status:pending")
	tasks, err := task.Export(filters...)
	if err != nil {
		return err
	}

	task.SortTasks(tasks)

	var rows []atable.Row
	m.searchMatches = nil
	for i, tsk := range tasks {
		rows = append(rows, taskToRowSearch(tsk, m.searchRegex))
		if m.searchRegex != nil {
			tags := strings.Join(tsk.Tags, " ")
			if m.searchRegex.MatchString(tags) {
				m.searchMatches = append(m.searchMatches, cellMatch{row: i, col: 5})
			}
			if m.searchRegex.MatchString(tsk.Description) {
				m.searchMatches = append(m.searchMatches, cellMatch{row: i, col: 6})
			}
			for _, a := range tsk.Annotations {
				if m.searchRegex.MatchString(a.Description) {
					m.searchMatches = append(m.searchMatches, cellMatch{row: i, col: 7})
					break
				}
			}
		}
	}
	if len(m.searchMatches) > 0 {
		m.searchIndex = 0
	}

	m.tasks = tasks
	m.total = task.TotalTasks(tasks)
	m.inProgress = task.InProgressTasks(tasks)
	m.due = task.DueTasks(tasks, time.Now())

	if m.tbl.Columns() == nil {
		m.tbl = newTable(rows)
	} else {
		m.tbl.SetRows(rows)
	}
	return nil
}

// Init implements tea.Model.
func (m Model) Init() tea.Cmd { return nil }

// Update handles key and window events.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		m.tbl.SetWidth(msg.Width)
		// Leave room for two status bars and the optional annotation
		// input line.
		m.tbl.SetHeight(msg.Height - 3)
		return m, nil
	case editDoneMsg:
		// Ignore any error and reload tasks once editing completes.
		_ = msg.err
		m.reload()
		return m, nil
	case tea.KeyMsg:
		if m.annotating {
			switch msg.Type {
			case tea.KeyEnter:
				if m.replaceAnnotations {
					task.ReplaceAnnotations(m.annotateID, m.annotateInput.Value())
					m.replaceAnnotations = false
				} else {
					task.Annotate(m.annotateID, m.annotateInput.Value())
				}
				m.annotating = false
				m.annotateInput.Blur()
				m.reload()
				return m, nil
			case tea.KeyEsc:
				m.annotating = false
				m.replaceAnnotations = false
				m.annotateInput.Blur()
				return m, nil
			}
			var cmd tea.Cmd
			m.annotateInput, cmd = m.annotateInput.Update(msg)
			return m, cmd
		}
		if m.dueEditing {
			switch msg.Type {
			case tea.KeyEnter:
				task.SetDueDate(m.dueID, m.dueDate.Format("2006-01-02"))
				m.dueEditing = false
				m.reload()
				return m, nil
			case tea.KeyEsc:
				m.dueEditing = false
				return m, nil
			}
			switch msg.String() {
			case "h", "left":
				m.dueDate = m.dueDate.AddDate(0, 0, -1)
			case "l", "right":
				m.dueDate = m.dueDate.AddDate(0, 0, 1)
			case "k", "up":
				m.dueDate = m.dueDate.AddDate(0, 0, -7)
			case "j", "down":
				m.dueDate = m.dueDate.AddDate(0, 0, 7)
			}
			return m, nil
		}
		if m.prioritySelecting {
			switch msg.Type {
			case tea.KeyEnter:
				task.SetPriority(m.priorityID, priorityOptions[m.priorityIndex])
				m.prioritySelecting = false
				m.reload()
				return m, nil
			case tea.KeyEsc:
				m.prioritySelecting = false
				return m, nil
			}
			switch msg.String() {
			case "h", "left":
				m.priorityIndex = (m.priorityIndex + len(priorityOptions) - 1) % len(priorityOptions)
			case "l", "right":
				m.priorityIndex = (m.priorityIndex + 1) % len(priorityOptions)
			}
			return m, nil
		}
		if m.searching {
			switch msg.Type {
			case tea.KeyEnter:
				re, err := regexp.Compile(m.searchInput.Value())
				if err == nil {
					m.searchRegex = re
				} else {
					m.searchRegex = nil
				}
				m.searching = false
				m.searchInput.Blur()
				m.reload()
				if len(m.searchMatches) > 0 {
					match := m.searchMatches[m.searchIndex]
					m.tbl.SetCursor(match.row)
					m.tbl.SetColumnCursor(match.col)
				}
				return m, nil
			case tea.KeyEsc:
				m.searching = false
				m.searchInput.Blur()
				return m, nil
			}
			var cmd tea.Cmd
			m.searchInput, cmd = m.searchInput.Update(msg)
			return m, cmd
		}
		switch msg.String() {
		case "H":
			m.showHelp = true
			return m, nil
		case "q", "esc":
			if m.showHelp {
				m.showHelp = false
				return m, nil
			}
			if m.searchRegex != nil {
				m.searchRegex = nil
				m.searchMatches = nil
				m.searchIndex = 0
				m.reload()
				return m, nil
			}
			if msg.String() == "q" {
				return m, tea.Quit
			}
			return m, nil
		case "e", "E":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					return m, editCmd(id)
				}
			}
		case "s":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					started := false
					for _, tsk := range m.tasks {
						if tsk.ID == id {
							started = tsk.Start != ""
							break
						}
					}
					if started {
						task.Stop(id)
					} else {
						task.Start(id)
					}
					m.reload()
				}
			}
		case "D":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					task.Done(id)
					for _, tsk := range m.tasks {
						if tsk.ID == id {
							m.undoStack = append(m.undoStack, tsk.UUID)
							break
						}
					}
					m.reload()
				}
			}
		case "U":
			if n := len(m.undoStack); n > 0 {
				uuid := m.undoStack[n-1]
				m.undoStack = m.undoStack[:n-1]
				task.SetStatusUUID(uuid, "pending")
				m.reload()
			}
		case "d":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					m.dueID = id
					m.dueEditing = true
					m.dueDate = time.Now()
					return m, nil
				}
			}
		case "r":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					days := rand.Intn(31) + 7
					due := time.Now().AddDate(0, 0, days).Format("2006-01-02")
					task.SetDueDate(id, due)
					m.reload()
				}
			}
		case "p":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					m.priorityID = id
					m.prioritySelecting = true
					m.priorityIndex = 0
					return m, nil
				}
			}
		case "a":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					m.annotateID = id
					m.annotating = true
					m.replaceAnnotations = false
					m.annotateInput.SetValue("")
					m.annotateInput.Focus()
					return m, nil
				}
			}
		case "A":
			if row := m.tbl.SelectedRow(); row != nil {
				idStr := ansi.Strip(row[0])
				if id, err := strconv.Atoi(idStr); err == nil {
					m.annotateID = id
					m.annotating = true
					m.replaceAnnotations = true
					m.annotateInput.SetValue("")
					m.annotateInput.Focus()
					return m, nil
				}
			}
		case "/", "?":
			m.searching = true
			m.searchInput.SetValue("")
			m.searchInput.Focus()
			return m, nil
		case "n":
			if len(m.searchMatches) > 0 {
				m.searchIndex = (m.searchIndex + 1) % len(m.searchMatches)
				match := m.searchMatches[m.searchIndex]
				m.tbl.SetCursor(match.row)
				m.tbl.SetColumnCursor(match.col)
				return m, nil
			}
		case "N":
			if len(m.searchMatches) > 0 {
				m.searchIndex = (m.searchIndex - 1 + len(m.searchMatches)) % len(m.searchMatches)
				match := m.searchMatches[m.searchIndex]
				m.tbl.SetCursor(match.row)
				m.tbl.SetColumnCursor(match.col)
				return m, nil
			}
		}
	}

	if m.showHelp {
		return m, nil
	}

	var cmd tea.Cmd
	m.tbl, cmd = m.tbl.Update(msg)
	return m, cmd
}

// View renders the table UI.
func (m Model) View() string {
	if m.showHelp {
		return lipgloss.JoinVertical(lipgloss.Left,
			m.tbl.HelpView(),
			"E: edit task",
			"s: toggle start/stop",
			"D: mark task done",
			"U: undo done",
			"d: set due date",
			"r: random due date",
			"a: annotate task",
			"A: replace annotations",
			"p: set priority",
			"/, ?: search",
			"q: quit",
			"H: help", // show help toggle line
		)
	}
	view := lipgloss.JoinVertical(lipgloss.Left,
		m.topStatusLine(),
		m.tbl.View(),
		m.statusLine(),
	)
	if m.annotating {
		view = lipgloss.JoinVertical(lipgloss.Left,
			view,
			m.annotateInput.View(),
		)
	}
	if m.dueEditing {
		view = lipgloss.JoinVertical(lipgloss.Left,
			view,
			m.dueView(),
		)
	}
	if m.prioritySelecting {
		view = lipgloss.JoinVertical(lipgloss.Left,
			view,
			m.priorityView(),
		)
	}
	if m.searching {
		view = lipgloss.JoinVertical(lipgloss.Left,
			view,
			m.searchInput.View(),
		)
	}
	return view
}

func (m Model) statusLine() string {
	status := fmt.Sprintf("Total:%d InProgress:%d Due:%d", m.total, m.inProgress, m.due)
	return lipgloss.NewStyle().
		Foreground(lipgloss.Color("229")).
		Background(lipgloss.Color("57")).
		Width(m.tbl.Width()).
		Render(status)
}

func (m Model) topStatusLine() string {
	header := ""
	cols := m.tbl.Columns()
	if idx := m.tbl.ColumnCursor(); idx >= 0 && idx < len(cols) {
		header = cols[idx].Title
	}
	return lipgloss.NewStyle().
		Foreground(lipgloss.Color("229")).
		Background(lipgloss.Color("57")).
		Width(m.tbl.Width()).
		Render(header)
}

func taskToRow(t task.Task) atable.Row {
	style := lipgloss.NewStyle()
	if t.Start != "" {
		style = style.Background(lipgloss.Color("6"))
	}

	age := ""
	if ts, err := time.Parse("20060102T150405Z", t.Entry); err == nil {
		days := int(time.Since(ts).Hours() / 24)
		age = fmt.Sprintf("%dd", days)
	}

	tags := strings.Join(t.Tags, " ")
	urg := fmt.Sprintf("%.1f", t.Urgency)

	var anns []string
	for _, a := range t.Annotations {
		anns = append(anns, a.Description)
	}

	return atable.Row{
		style.Render(strconv.Itoa(t.ID)),
		formatPriority(t.Priority),
		style.Render(age),
		style.Render(urg),
		formatDue(t.Due),
		style.Render(tags),
		style.Render(t.Description),
		style.Render(strings.Join(anns, "; ")),
	}
}

// formatDue returns a formatted due date string. Dates due today or tomorrow
// are returned as "today" or "tomorrow" respectively. Past due dates are
// highlighted in red.
func formatDue(s string) string {
	if s == "" {
		return ""
	}
	ts, err := time.Parse("20060102T150405Z", s)
	if err != nil {
		return s
	}

	days := int(time.Until(ts).Hours() / 24)
	var val string
	switch days {
	case 0:
		val = "today"
	case 1:
		val = "tomorrow"
	default:
		val = fmt.Sprintf("%dd", days)
	}
	style := lipgloss.NewStyle()
	if days < 0 {
		style = style.Background(lipgloss.Color("1"))
	}
	return style.Render(val)
}

func formatPriority(p string) string {
	style := lipgloss.NewStyle()
	switch p {
	case "L":
		style = style.Background(lipgloss.Color("10"))
	case "M":
		style = style.Background(lipgloss.Color("12"))
	case "H":
		style = style.Background(lipgloss.Color("9"))
	default:
		return p
	}
	return style.Render(p)
}

func (m Model) dueView() string {
	return fmt.Sprintf("due: %s", m.dueDate.Format("2006-01-02"))
}

func (m Model) priorityView() string {
	var parts []string
	for i, p := range priorityOptions {
		label := p
		if label == "" {
			label = "none"
		}
		style := lipgloss.NewStyle()
		if i == m.priorityIndex {
			style = style.Foreground(lipgloss.Color("229")).Background(lipgloss.Color("57"))
		}
		parts = append(parts, style.Render(label))
	}
	return "priority: " + strings.Join(parts, " ")
}

func highlightCell(rendered string, re *regexp.Regexp, raw string) string {
	if re == nil || !re.MatchString(raw) {
		return rendered
	}
	style := lipgloss.NewStyle().Background(lipgloss.Color("226")).Foreground(lipgloss.Color("21"))
	return style.Render(rendered)
}

func taskToRowSearch(t task.Task, re *regexp.Regexp) atable.Row {
	style := lipgloss.NewStyle()
	if t.Start != "" {
		style = style.Background(lipgloss.Color("6"))
	}

	age := ""
	if ts, err := time.Parse("20060102T150405Z", t.Entry); err == nil {
		days := int(time.Since(ts).Hours() / 24)
		age = fmt.Sprintf("%dd", days)
	}

	tags := strings.Join(t.Tags, " ")
	urg := fmt.Sprintf("%.1f", t.Urgency)

	var anns []string
	for _, a := range t.Annotations {
		anns = append(anns, a.Description)
	}

	idStr := style.Render(strconv.Itoa(t.ID))
	priStr := formatPriority(t.Priority)
	ageStr := style.Render(age)
	dueStr := formatDue(t.Due)
	urgStr := style.Render(urg)
	tagStr := style.Render(tags)
	descStr := style.Render(t.Description)
	annRaw := strings.Join(anns, "; ")
	annStr := style.Render(annRaw)

	tagStr = highlightCell(tagStr, re, tags)
	descStr = highlightCell(descStr, re, t.Description)
	annStr = highlightCell(annStr, re, annRaw)

	return atable.Row{
		idStr,
		priStr,
		ageStr,
		urgStr,
		dueStr,
		tagStr,
		descStr,
		annStr,
	}
}