feat(day): table view with per-project footer totals and date nav
b4a03107a41809bf155952cfd2f989a81fe710cd
humdrum-tiv <45084903+humdrum-tiv@users.noreply.github.com> · 2026-07-07 16:53
parent 3f8f1ae5
6 files changed
go.mod +29 −1
@@ -2,9 +2,37 @@ module ticktock
go 1.26.4
-require github.com/spf13/cobra v1.10.2
+require (
+ github.com/charmbracelet/bubbles v1.0.0
+ github.com/charmbracelet/bubbletea v1.3.10
+ github.com/charmbracelet/lipgloss v1.1.0
+ github.com/charmbracelet/x/exp/teatest v0.0.0-20260705004817-2cc9a8fe1146
+ github.com/spf13/cobra v1.10.2
+)
require (
+ github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
+ github.com/aymanbagabas/go-udiff v0.3.1 // indirect
+ github.com/charmbracelet/colorprofile v0.4.1 // indirect
+ github.com/charmbracelet/x/ansi v0.11.6 // indirect
+ github.com/charmbracelet/x/cellbuf v0.0.15 // indirect
+ github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 // indirect
+ github.com/charmbracelet/x/term v0.2.2 // indirect
+ github.com/clipperhouse/displaywidth v0.9.0 // indirect
+ github.com/clipperhouse/stringish v0.1.1 // indirect
+ github.com/clipperhouse/uax29/v2 v2.5.0 // indirect
+ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
+ github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
+ github.com/mattn/go-isatty v0.0.20 // indirect
+ github.com/mattn/go-localereader v0.0.1 // indirect
+ github.com/mattn/go-runewidth v0.0.19 // indirect
+ github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
+ github.com/muesli/cancelreader v0.2.2 // indirect
+ github.com/muesli/termenv v0.16.0 // indirect
+ github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.9 // indirect
+ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
+ golang.org/x/sys v0.38.0 // indirect
+ golang.org/x/text v0.28.0 // indirect
)
go.sum +56 −0
@@ -1,10 +1,66 @@
+github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
+github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
+github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY=
+github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E=
+github.com/charmbracelet/bubbles v1.0.0 h1:12J8/ak/uCZEMQ6KU7pcfwceyjLlWsDLAxB5fXonfvc=
+github.com/charmbracelet/bubbles v1.0.0/go.mod h1:9d/Zd5GdnauMI5ivUIVisuEm3ave1XwXtD1ckyV6r3E=
+github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
+github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
+github.com/charmbracelet/colorprofile v0.4.1 h1:a1lO03qTrSIRaK8c3JRxJDZOvhvIeSco3ej+ngLk1kk=
+github.com/charmbracelet/colorprofile v0.4.1/go.mod h1:U1d9Dljmdf9DLegaJ0nGZNJvoXAhayhmidOdcBwAvKk=
+github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
+github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
+github.com/charmbracelet/x/ansi v0.11.6 h1:GhV21SiDz/45W9AnV2R61xZMRri5NlLnl6CVF7ihZW8=
+github.com/charmbracelet/x/ansi v0.11.6/go.mod h1:2JNYLgQUsyqaiLovhU2Rv/pb8r6ydXKS3NIttu3VGZQ=
+github.com/charmbracelet/x/cellbuf v0.0.15 h1:ur3pZy0o6z/R7EylET877CBxaiE1Sp1GMxoFPAIztPI=
+github.com/charmbracelet/x/cellbuf v0.0.15/go.mod h1:J1YVbR7MUuEGIFPCaaZ96KDl5NoS0DAWkskup+mOY+Q=
+github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
+github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
+github.com/charmbracelet/x/exp/teatest v0.0.0-20260705004817-2cc9a8fe1146 h1:nqRPYV88DBRdlHx+yhMK15UrSMfdVTHubb3jE90HdIU=
+github.com/charmbracelet/x/exp/teatest v0.0.0-20260705004817-2cc9a8fe1146/go.mod h1:aPVjFrBwbJgj5Qz1F0IXsnbcOVJcMKgu1ySUfTAxh7k=
+github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
+github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
+github.com/clipperhouse/displaywidth v0.9.0 h1:Qb4KOhYwRiN3viMv1v/3cTBlz3AcAZX3+y9OLhMtAtA=
+github.com/clipperhouse/displaywidth v0.9.0/go.mod h1:aCAAqTlh4GIVkhQnJpbL0T/WfcrJXHcj8C0yjYcjOZA=
+github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfatpWHKCs=
+github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA=
+github.com/clipperhouse/uax29/v2 v2.5.0 h1:x7T0T4eTHDONxFJsL94uKNKPHrclyFI0lm7+w94cO8U=
+github.com/clipperhouse/uax29/v2 v2.5.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
+github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
+github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=
+github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
+github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
+github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
+github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
+github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
+github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
+github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
+github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
+github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
+github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
+github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
+github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
+golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
+golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
+golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
+golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
+golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
+golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
internal/tui/day/format.go +60 −0
@@ -0,0 +1,60 @@
+package day
+
+import (
+ "fmt"
+ "time"
+
+ "ticktock/internal/store"
+)
+
+// fmtDur renders a duration like tock's summaries: "1h37m", "50m", "0m".
+func fmtDur(d time.Duration) string {
+ m := int(d.Minutes())
+ h, m := m/60, m%60
+ if h > 0 {
+ return fmt.Sprintf("%dh%dm", h, m)
+ }
+ return fmt.Sprintf("%dm", m)
+}
+
+// fmtTockDur renders the table's Duration cell like tock list: "51m0s", "1h37m0s".
+func fmtTockDur(d time.Duration) string {
+ s := int(d.Seconds())
+ h, s := s/3600, s%3600
+ m, s := s/60, s%60
+ if h > 0 {
+ return fmt.Sprintf("%dh%dm%ds", h, m, s)
+ }
+ return fmt.Sprintf("%dm%ds", m, s)
+}
+
+func fmtRange(e store.Entry) string {
+ end := " … "
+ if !e.Running() {
+ end = e.End.Format("15:04")
+ }
+ return e.Start.Format("15:04") + " - " + end
+}
+
+// ProjectTotal is one project's summed duration for the day.
+type ProjectTotal struct {
+ Project string
+ Total time.Duration
+}
+
+// Totals sums per-project and grand-total durations, preserving first-seen order.
+func Totals(es []store.Entry) ([]ProjectTotal, time.Duration) {
+ idx := map[string]int{}
+ var out []ProjectTotal
+ var grand time.Duration
+ for _, e := range es {
+ grand += e.Duration
+ if i, ok := idx[e.Project]; ok {
+ out[i].Total += e.Duration
+ continue
+ }
+ idx[e.Project] = len(out)
+ out = append(out, ProjectTotal{Project: e.Project, Total: e.Duration})
+ }
+ return out, grand
+}
internal/tui/day/model.go +103 −0
@@ -0,0 +1,103 @@
+package day
+
+import (
+ "strings"
+ "time"
+
+ "github.com/charmbracelet/bubbles/table"
+ tea "github.com/charmbracelet/bubbletea"
+
+ "ticktock/internal/store"
+)
+
+// Model is the day-view Bubble Tea model.
+type Model struct {
+ store store.Store
+ date string
+ entries []store.Entry
+ tbl table.Model
+ err error
+ quit bool
+}
+
+type entriesMsg struct {
+ entries []store.Entry
+ err error
+}
+
+// New builds a day model for the given date (YYYY-MM-DD).
+func New(s store.Store, date string) Model {
+ cols := []table.Column{
+ {Title: "Key", Width: 13},
+ {Title: "Time", Width: 15},
+ {Title: "Project", Width: 10},
+ {Title: "Description", Width: 30},
+ {Title: "Duration", Width: 10},
+ {Title: "Tags", Width: 14},
+ {Title: "Notes", Width: 30},
+ }
+ t := table.New(table.WithColumns(cols), table.WithFocused(true), table.WithHeight(14))
+ t.SetStyles(tableStyles())
+ return Model{store: s, date: date, tbl: t}
+}
+
+func (m Model) Init() tea.Cmd { return m.load() }
+
+func (m Model) load() tea.Cmd {
+ date, s := m.date, m.store
+ return func() tea.Msg {
+ es, err := s.Day(date)
+ return entriesMsg{entries: es, err: err}
+ }
+}
+
+func (m *Model) setRows() {
+ rows := make([]table.Row, 0, len(m.entries))
+ for _, e := range m.entries {
+ rows = append(rows, table.Row{
+ e.Key, fmtRange(e), e.Project, e.Description,
+ fmtTockDur(e.Duration), strings.Join(e.Tags, ","), e.Notes,
+ })
+ }
+ m.tbl.SetRows(rows)
+}
+
+func (m Model) selected() (store.Entry, bool) {
+ i := m.tbl.Cursor()
+ if i < 0 || i >= len(m.entries) {
+ return store.Entry{}, false
+ }
+ return m.entries[i], 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) {
+ switch msg := msg.(type) {
+ case entriesMsg:
+ m.entries, m.err = msg.entries, msg.err
+ m.setRows()
+ return m, nil
+ case tea.KeyMsg:
+ switch msg.String() {
+ case "q", "ctrl+c":
+ m.quit = true
+ return m, tea.Quit
+ case "left":
+ m.date = shiftDate(m.date, -1)
+ return m, m.load()
+ case "right":
+ m.date = shiftDate(m.date, 1)
+ return m, m.load()
+ }
+ }
+ var cmd tea.Cmd
+ m.tbl, cmd = m.tbl.Update(msg)
+ return m, cmd
+}
internal/tui/day/model_test.go +71 −0
@@ -0,0 +1,71 @@
+package day
+
+import (
+ "bytes"
+ "testing"
+ "time"
+
+ tea "github.com/charmbracelet/bubbletea"
+ "github.com/charmbracelet/x/exp/teatest"
+
+ "ticktock/internal/store"
+)
+
+type fakeStore struct {
+ days map[string][]store.Entry
+ replaced []string // keys passed to SafeReplace
+}
+
+func (f *fakeStore) Day(date string) ([]store.Entry, error) { return f.days[date], nil }
+func (f *fakeStore) Add(store.Entry) error { return nil }
+func (f *fakeStore) Remove(string) error { return nil }
+func (f *fakeStore) SafeReplace(key string, _, _ store.Entry) error {
+ f.replaced = append(f.replaced, key)
+ return nil
+}
+
+func sample() []store.Entry {
+ d := func(h, m int) time.Time { return time.Date(2026, 7, 7, h, m, 0, 0, time.Local) }
+ return []store.Entry{
+ {Key: "2026-07-07-01", Start: d(9, 1), End: d(9, 51), Project: "ARCHER",
+ Description: "Admin", Tags: []string{"emails"}, Duration: 50 * time.Minute},
+ {Key: "2026-07-07-02", Start: d(13, 0), End: d(14, 15), Project: "Acme",
+ Description: "Review", Duration: 75 * time.Minute},
+ }
+}
+
+func TestTotals(t *testing.T) {
+ pt, grand := Totals(sample())
+ if grand != 125*time.Minute {
+ t.Fatalf("grand total = %v, want 2h5m", grand)
+ }
+ if len(pt) != 2 {
+ t.Fatalf("want 2 project rows, got %d", len(pt))
+ }
+}
+
+func TestFmtDur(t *testing.T) {
+ cases := map[time.Duration]string{
+ 50 * time.Minute: "50m",
+ time.Hour + 37*time.Minute: "1h37m",
+ 0: "0m",
+ }
+ for in, want := range cases {
+ if got := fmtDur(in); got != want {
+ t.Errorf("fmtDur(%v) = %q, want %q", in, got, want)
+ }
+ }
+}
+
+func TestDayRendersTableAndTotals(t *testing.T) {
+ fs := &fakeStore{days: map[string][]store.Entry{"2026-07-07": sample()}}
+ tm := teatest.NewTestModel(t, New(fs, "2026-07-07"),
+ teatest.WithInitialTermSize(140, 24))
+ teatest.WaitFor(t, tm.Output(), func(b []byte) bool {
+ return bytes.Contains(b, []byte("ARCHER")) &&
+ bytes.Contains(b, []byte("Acme")) &&
+ bytes.Contains(b, []byte("TOTAL"))
+ }, teatest.WithDuration(2*time.Second))
+ tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}})
+ tm.WaitFinished(t, teatest.WithFinalTimeout(2*time.Second))
+}
internal/tui/day/view.go +65 −0
@@ -0,0 +1,65 @@
+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 ""
+ }
+ body := m.tbl.View()
+ if len(m.entries) == 0 {
+ body = footerStyle.Render(" no entries")
+ }
+ help := footerStyle.Render("enter edit · ←/→ date · q quit")
+ if m.err != nil {
+ help = footerStyle.Render("error: " + m.err.Error())
+ }
+ return strings.Join([]string{
+ headerLine(m.date), "", body, "", footerLine(m.entries), help,
+ }, "\n")
+}