summaryrefslogtreecommitdiff
path: root/internal/generator/themes.go
blob: a3a9881a7aa8cc9ee7f9d93a2e635112cb146aa1 (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
package generator

import (
	"log"

	"codeberg.org/snonux/snonux/internal/generator/templates"
)

// fallbackThemeName is used when an unknown name is requested for default
// metadata, matching the previous behaviour of the per-theme registry.
const fallbackThemeName = "neon"

// themeSet caches the list of theme names available in the embedded template FS
// so ListThemes does not re-read the directory on every call.
var themeSet = loadThemeSet()

func loadThemeSet() map[string]struct{} {
	names, err := templates.ThemeNames()
	if err != nil {
		// At build time the embed //go:embed directive guarantees the FS is
		// populated, so this should never happen; log and continue with an
		// empty set so callers can fall back cleanly.
		log.Printf("warning: could not enumerate themes from embedded FS: %v", err)
		return map[string]struct{}{}
	}

	out := make(map[string]struct{}, len(names))
	for _, n := range names {
		out[n] = struct{}{}
	}
	return out
}

// validThemeName returns name if it is a known theme, otherwise the fallback.
// Callers use this to coerce CLI input ("--theme random" already resolves
// upstream) so downstream lookups never miss.
func validThemeName(name string) string {
	if _, ok := themeSet[name]; ok {
		return name
	}
	return fallbackThemeName
}

// ListThemes returns a sorted list of all available theme names.
func ListThemes() []string {
	names, err := templates.ThemeNames()
	if err != nil {
		log.Printf("warning: could not list themes from embedded FS: %v", err)
		return nil
	}
	return names
}