▍ humdrum codex / ticktock v0.0.2
license AGPL-3.0
7.4 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
 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
package grid

import (
	"fmt"
	"hash/fnv"
	"strings"
	"time"

	"github.com/charmbracelet/lipgloss"

	"ticktock/internal/store"
)

var (
	gPink   = lipgloss.Color("#D188C4")
	gMuted  = lipgloss.Color("#878580")
	gGreen  = lipgloss.Color("#78B892")
	gPurple = lipgloss.Color("#5A2FE0")
	gRed    = lipgloss.Color("#D16969")

	gHeader = lipgloss.NewStyle().Foreground(gPink).Bold(true)
	gDim    = lipgloss.NewStyle().Foreground(gMuted)
	gLabel  = lipgloss.NewStyle().Foreground(gGreen)
	gBand   = lipgloss.NewStyle().Foreground(gPurple)
	gCursor = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Background(gPurple)
	gCurLog = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Background(gPurple).Bold(true)
	gDelHi  = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Background(gRed).Bold(true)
	gCal    = lipgloss.NewStyle().Foreground(lipgloss.Color("#6CB6FF")) // calendar accent
)

// projectPalette gives each project a stable, distinct color. Assignment is by
// a hash of the project name, so a project keeps the same color across runs.
var projectPalette = []lipgloss.Color{
	"#78B892", // green
	"#D188C4", // pink
	"#6CB6FF", // blue
	"#E0A458", // orange
	"#B48EAD", // mauve
	"#E8C547", // yellow
	"#5FB3B3", // teal
	"#EC6A88", // red
}

func projectColor(project string) lipgloss.Color {
	if project == "" {
		return gMuted
	}
	h := fnv.New32a()
	_, _ = h.Write([]byte(project))
	return projectPalette[int(h.Sum32())%len(projectPalette)]
}

func projectStyle(project string) lipgloss.Style {
	return lipgloss.NewStyle().Foreground(projectColor(project))
}

// distinctProjects lists the projects present in entries, in first-seen order.
func distinctProjects(entries []store.Entry) []string {
	seen := map[string]bool{}
	var out []string
	for _, e := range entries {
		if e.Project != "" && !seen[e.Project] {
			seen[e.Project] = true
			out = append(out, e.Project)
		}
	}
	return out
}

func gHeaderLine(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 gHeader.Render("<< " + label + " >>")
}

const (
	actWidth    = 22
	loggedWidth = 40
)

func minuteLabel(min int) string {
	return fmt.Sprintf("%02d:%02d", (min/60)%24, min%60)
}

func trunc(s string, w int) string {
	r := []rune(s)
	if len(r) <= w {
		return s
	}
	if w <= 1 {
		return string(r[:w])
	}
	return string(r[:w-1]) + "…"
}

// gridRow renders one line with fixed 1-col gutters on both sides of the
// dividers so separators align and the cursor marker can sit next to whichever
// column is active: [gutterL] time(5) │ activity(actWidth) │[gutterR] logged.
func gridRow(gutterL, timeStr, actCell, gutterR, logCell string) string {
	return fmt.Sprintf("%s %-5s │ %-*s │%s %s", gutterL, timeStr, actWidth, actCell, gutterR, logCell)
}

// ruleLine draws the header underline with ┼ crossings at the │ columns.
func ruleLine() string {
	// gridRow prefix before first │: gutter(1)+sp(1)+time(5)+sp(1) = 8 chars.
	return gDim.Render(
		strings.Repeat("─", 8) + "┼" +
			strings.Repeat("─", actWidth+2) + "┼" +
			strings.Repeat("─", 14))
}

func (m Model) View() string {
	if m.quit {
		return ""
	}
	if m.editing && m.form != nil {
		head := gHeaderLine(m.date)
		if m.adding && m.fv != nil {
			head += "\n" + gDim.Render(fmt.Sprintf("new entry  %s–%s", m.fv.Start, m.fv.End))
		}
		hint := gDim.Render("ctrl+s save · esc cancel")
		return head + "\n\n" + m.form.View() + "\n" + hint
	}

	var b strings.Builder
	b.WriteString(gHeaderLine(m.date) + "\n\n")
	b.WriteString(gridRow(" ", "TIME", "ACTIVITY", " ", "LOGGED → tock") + "\n")
	b.WriteString(ruleLine() + "\n")

	lo, hi, selecting := m.selectionRange()
	for row := 0; row < m.grid.Rows; row++ {
		onCursor := row == m.cursor

		// ACTIVITY column: painted lane (calendar/active/idle) over the gap
		// lane; the selection band still overrides while selecting.
		act := strings.Repeat("░", actWidth)
		if row < len(m.activity) && m.activity[row].Kind != CellEmpty {
			act = renderActCell(m.activity[row])
		}
		if selecting && row >= lo && row <= hi {
			act = gBand.Render(strings.Repeat("▓", actWidth))
		}

		// LOGGED column
		var logged string
		if row < len(m.logged) {
			c := m.logged[row]
			switch c.Kind {
			case CellLabel:
				logged = trunc(c.Text, loggedWidth)
			case CellCont:
				logged = "│"
			}
			// highlight the whole session targeted for deletion
			if m.confirmDelete && c.Key != "" && c.Key == m.confirmKey {
				logged = gDelHi.Render(logged)
			} else if onCursor && m.col == ColLogged && c.Kind != CellEmpty {
				logged = gCurLog.Render(logged)
			} else if c.Kind == CellLabel {
				logged = projectStyle(c.Project).Render(logged)
			} else if c.Kind == CellCont {
				logged = projectStyle(c.Project).Faint(true).Render(logged)
			}
		}

		gutterL, gutterR := " ", " "
		if onCursor {
			if m.col == ColActivity {
				gutterL = gCursor.Render(">")
			} else {
				gutterR = gCursor.Render(">")
			}
		}
		timeStr := gDim.Render(minuteLabel(m.grid.SlotMinute(row)))
		b.WriteString(gridRow(gutterL, timeStr, act, gutterR, logged) + "\n")
	}

	b.WriteString("\n")
	if legend := m.projectLegend(); legend != "" {
		b.WriteString(legend + "\n")
	}
	if m.confirmDelete {
		// pre-styled; write without an outer dim wrap
		b.WriteString(gDelHi.Render(" delete ") + " " + m.confirmLabel() + "   " + gDim.Render("y = yes · n = no"))
		return b.String()
	}
	help := "←/→ col · ↑/↓ slot · space select/add · enter edit · d delete · n/p/t day · r reload · q quit"
	if m.status != "" {
		help = m.status
	}
	if m.err != nil {
		help = "error: " + m.err.Error()
	}
	b.WriteString(gDim.Render(help))
	return b.String()
}

// projectLegend renders each project present today in its own color so the
// grid's colors are legible.
func (m Model) projectLegend() string {
	projects := distinctProjects(m.entries)
	if len(projects) == 0 {
		return ""
	}
	parts := make([]string, 0, len(projects))
	for _, p := range projects {
		parts = append(parts, projectStyle(p).Render(p))
	}
	return gDim.Render("projects: ") + strings.Join(parts, "  ")
}

// confirmLabel describes the session pending deletion (description + range),
// falling back to the tock key if the entry can't be found.
func (m Model) confirmLabel() string {
	e, ok := m.entryByKey(m.confirmKey)
	if !ok {
		return m.confirmKey
	}
	rng := e.Start.Format("15:04") + "–" + e.End.Format("15:04")
	if e.Running() {
		rng = e.Start.Format("15:04") + "–…"
	}
	desc := e.Description
	if desc == "" {
		desc = e.Project
	}
	return fmt.Sprintf("%q  %s", desc, rng)
}

// padCell pads s with spaces to exactly w runes. The activity cell must be
// padded BEFORE styling: gridRow's %-*s pads by byte length, which ANSI
// escapes would defeat, misaligning the column divider.
func padCell(s string, w int) string {
	if n := len([]rune(s)); n < w {
		return s + strings.Repeat(" ", w-n)
	}
	return s
}

// renderActCell paints one ACTIVITY-lane cell: label text or continuation
// glyph, colored by flavor — calendar accent, idle dimmed, active plain;
// continuation glyphs render faint.
func renderActCell(c Cell) string {
	text := trunc(c.Text, actWidth)
	if c.Kind == CellCont {
		text = "│"
	}
	text = padCell(text, actWidth)
	style := lipgloss.NewStyle()
	switch c.Act {
	case ActCalendar:
		style = gCal
	case ActIdle:
		style = gDim
	}
	if c.Kind == CellCont {
		style = style.Faint(true)
	}
	return style.Render(text)
}