โ– humdrum codex / custard
license AGPL-3.0
7.7 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
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
// Package templates holds the templ components and their view-models. View-models
// are plain structs assembled by the server layer; templ files render them.
package templates

import (
	"fmt"
	"html/template"
	"strings"
	"time"

	"custard/internal/backlog"
	"custard/internal/gitread"
	"custard/internal/license"
	"custard/internal/render"
)

// Meta is the shared chrome data every page needs.
type Meta struct {
	Title     string
	Repo      string           // current repo name, "" on the index
	Ref       string           // current ref, when relevant
	HasIssues bool             // show the issues tab only when backlog tasks exist
	HasReadme bool             // show the readme tab only when a README exists
	Theme     string           // active data-theme, resolved from cookie (default flexoki)
	Tab       string           // active repo tab: code | readme | log | refs | issues
	CloneURL  string           // read-only HTTP clone URL, shown in the footer bar
	License   *license.License // detected repo license, shown as a badge
}

// LicenseCategoryColor maps a license category to a theme color family.
func LicenseCategoryColor(cat string) string {
	switch cat {
	case "permissive":
		return "green"
	case "public-domain":
		return "cyan"
	case "weak-copyleft":
		return "yellow"
	case "copyleft":
		return "orange"
	case "cc":
		return "purple"
	default:
		return "accent"
	}
}

// Families is what the picker offers: a palette choice only. Light vs dark is
// resolved from the OS (prefers-color-scheme), not chosen here. E-ink is its own
// fixed mode. The cookie stores one of these; the client resolves the actual
// data-theme (e.g. flexoki โ†’ flexoki-dark) before paint.
var Families = []string{"flexoki", "uchu", "humdrum", "eink"}

// DefaultTheme (family) applies when no valid cookie is present.
const DefaultTheme = "flexoki"

// ValidTheme returns t if it is a known family, else DefaultTheme.
func ValidTheme(t string) string {
	for _, k := range Families {
		if k == t {
			return t
		}
	}
	return DefaultTheme
}

// Crumb is one breadcrumb segment with the cumulative path up to it.
type Crumb struct {
	Name string
	Path string
}

// IndexPage lists all repositories.
type IndexPage struct {
	Meta  Meta
	Repos []gitread.Repo
}

// RepoPage is the repo home (code tab): the root file tree plus a ref summary.
// The README lives on its own tab, not here.
type RepoPage struct {
	Meta          Meta
	DefaultBranch string
	Entries       []gitread.Entry
	Last          *gitread.Commit
	Branches      int
	Tags          int
}

// ReadmePage renders a repo's README on its own tab.
type ReadmePage struct {
	Meta   Meta
	Readme template.HTML
}

// TreePage browses a directory at a ref.
type TreePage struct {
	Meta    Meta
	Path    string
	Crumbs  []Crumb
	Entries []gitread.Entry
}

// BlobPage shows one file.
type BlobPage struct {
	Meta        Meta
	Path        string
	Crumbs      []Crumb
	Size        int64
	IsMarkdown  bool
	Markdown    template.HTML
	Frontmatter []render.FMPair // YAML frontmatter of a Markdown file, if any
	Code        template.HTML
	IsBinary    bool
}

// LogPage is a commit list for a ref.
type LogPage struct {
	Meta    Meta
	Commits []gitread.Commit
}

// CommitPage shows a single commit and its diff, split per file.
type CommitPage struct {
	Meta   Meta
	Detail *gitread.CommitDetail
	Files  []render.FileDiff
}

// RefsPage lists branches and tags.
type RefsPage struct {
	Meta Meta
	Refs *gitread.Refs
}

// IssueGroup is a status bucket of tasks in the issues list.
type IssueGroup struct {
	Status string
	Tasks  []backlog.Task
}

// IssuesPage is the GitHub-issues-style list, grouped by status.
type IssuesPage struct {
	Meta   Meta
	Groups []IssueGroup
	Total  int
}

// IssuePage is a single task with its rendered Markdown body.
type IssuePage struct {
	Meta Meta
	Task backlog.Task
	Body template.HTML
}

// GroupByStatus buckets tasks following the repo's configured status order,
// appending any statuses not in that order at the end. Empty buckets are
// dropped. An empty order falls back to the Backlog.md defaults.
func GroupByStatus(tasks []backlog.Task, order []string) []IssueGroup {
	if len(order) == 0 {
		order = backlog.DefaultStatuses
	}
	known := make(map[string]bool, len(order))
	for _, s := range order {
		known[s] = true
	}
	byStatus := map[string][]backlog.Task{}
	var extra []string
	for _, t := range tasks {
		if _, seen := byStatus[t.Status]; !seen && !known[t.Status] {
			extra = append(extra, t.Status)
		}
		byStatus[t.Status] = append(byStatus[t.Status], t)
	}
	var groups []IssueGroup
	for _, s := range append(append([]string{}, order...), extra...) {
		if ts := byStatus[s]; len(ts) > 0 {
			groups = append(groups, IssueGroup{Status: s, Tasks: ts})
		}
	}
	return groups
}

// LabelColor maps a label to a theme color-family name (phase-3 tokens key off
// these via .chip--<color>). Unknown labels get the neutral accent.
func LabelColor(label string) string {
	switch strings.ToLower(label) {
	case "bug":
		return "red"
	case "feature":
		return "green"
	case "enhancement", "ui":
		return "blue"
	case "docs", "documentation":
		return "cyan"
	case "chore", "refactor":
		return "purple"
	case "question":
		return "yellow"
	default:
		return "accent"
	}
}

// PriorityClass maps a priority to a css-class-safe level (high/medium/low),
// or "" when absent so the template can skip the pill.
func PriorityClass(p string) string {
	switch strings.ToLower(strings.TrimSpace(p)) {
	case "high", "critical", "urgent":
		return "high"
	case "medium", "med", "normal":
		return "medium"
	case "low", "minor":
		return "low"
	default:
		return ""
	}
}

// StatusKind maps an arbitrary status label (emoji and all) to a stable
// css-class-safe kind, so themes can style columns regardless of the exact
// wording a repo uses. Unrecognized statuses fall back to an alnum slug.
func StatusKind(status string) string {
	s := strings.ToLower(status)
	switch {
	case strings.Contains(s, "progress"):
		return "in-progress"
	case strings.Contains(s, "done"), strings.Contains(s, "ship"), strings.Contains(s, "complete"):
		return "done"
	case strings.Contains(s, "paus"), strings.Contains(s, "block"), strings.Contains(s, "hold"):
		return "paused"
	case strings.Contains(s, "backlog"), strings.Contains(s, "to do"), strings.Contains(s, "todo"):
		return "backlog"
	}
	return slugify(s)
}

// slugify reduces a string to lowercase alphanumerics joined by single dashes.
func slugify(s string) string {
	var b strings.Builder
	dash := false
	for _, r := range strings.ToLower(s) {
		switch {
		case r >= 'a' && r <= 'z', r >= '0' && r <= '9':
			if dash && b.Len() > 0 {
				b.WriteByte('-')
			}
			b.WriteRune(r)
			dash = false
		default:
			dash = true
		}
	}
	if b.Len() == 0 {
		return "other"
	}
	return b.String()
}

// HumanSize formats a byte count as a short human-readable string.
func HumanSize(n int64) string {
	const unit = 1024
	if n < unit {
		return fmt.Sprintf("%d B", n)
	}
	div, exp := int64(unit), 0
	for m := n / unit; m >= unit; m /= unit {
		div *= unit
		exp++
	}
	return fmt.Sprintf("%.1f %cB", float64(n)/float64(div), "KMGT"[exp])
}

// ShortHash returns the first 8 characters of a hash, or the whole thing.
func ShortHash(h string) string {
	if len(h) >= 8 {
		return h[:8]
	}
	return h
}

// FmtTime renders a timestamp in a compact, stable form.
func FmtTime(t time.Time) string {
	return t.Format("2006-01-02 15:04")
}

// BuildCrumbs splits a "/"-separated path into cumulative breadcrumbs.
func BuildCrumbs(path string) []Crumb {
	path = strings.Trim(path, "/")
	if path == "" {
		return nil
	}
	parts := strings.Split(path, "/")
	crumbs := make([]Crumb, 0, len(parts))
	acc := ""
	for _, p := range parts {
		if acc == "" {
			acc = p
		} else {
			acc = acc + "/" + p
		}
		crumbs = append(crumbs, Crumb{Name: p, Path: acc})
	}
	return crumbs
}