▍ humdrum codex / ticktock v0.0.2
license AGPL-3.0
1.8 KB raw
 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
package day

import (
	"fmt"
	"strings"
	"time"

	"github.com/charmbracelet/bubbles/table"
	"github.com/charmbracelet/lipgloss"

	"ticktock/internal/store"
)

var (
	pink   = lipgloss.Color("#D188C4")
	purple = lipgloss.Color("#5A2FE0")
	muted  = lipgloss.Color("#878580")

	headerStyle = lipgloss.NewStyle().Foreground(pink).Bold(true)
	footerStyle = lipgloss.NewStyle().Foreground(muted)
)

func tableStyles() table.Styles {
	s := table.DefaultStyles()
	s.Header = s.Header.Bold(true).BorderBottom(true)
	s.Selected = s.Selected.Foreground(lipgloss.Color("#FFFFFF")).Background(purple).Bold(false)
	return s
}

func headerLine(date string) string {
	t, err := time.ParseInLocation("2006-01-02", date, time.Local)
	label := date
	if err == nil {
		label = t.Format("Monday, 02 Jan 2006")
	}
	return headerStyle.Render("<< " + label + " >>")
}

func footerLine(entries []store.Entry) string {
	pt, grand := Totals(entries)
	parts := make([]string, 0, len(pt))
	for _, p := range pt {
		parts = append(parts, fmt.Sprintf("%s %s", p.Project, fmtDur(p.Total)))
	}
	summary := "─ by project ─ " + strings.Join(parts, "   ") +
		fmt.Sprintf("   TOTAL %s", fmtDur(grand))
	return footerStyle.Render(summary)
}

func (m Model) View() string {
	if m.quit {
		return ""
	}
	if m.editing && m.form != nil {
		hint := footerStyle.Render("ctrl+s save · esc cancel")
		return headerLine(m.date) + "\n\n" + m.form.View() + "\n" + hint
	}
	body := m.tbl.View()
	if len(m.entries) == 0 {
		body = footerStyle.Render("  no entries")
	}
	help := footerStyle.Render("enter edit · ←/→ date · q quit")
	if m.status != "" {
		help = footerStyle.Render(m.status)
	}
	if m.err != nil {
		help = footerStyle.Render("error: " + m.err.Error())
	}
	return strings.Join([]string{
		headerLine(m.date), "", body, "", footerLine(m.entries), help,
	}, "\n")
}