blob: c0ba7eab7e292e910146d811dd3d71823ca66906 (
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
53
54
55
56
57
58
59
60
|
package generator
import (
"log"
"sync"
"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 (
themeSetCache map[string]struct{}
themeSetOnce sync.Once
)
func getThemeSet() map[string]struct{} {
themeSetOnce.Do(func() {
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)
themeSetCache = map[string]struct{}{}
return
}
out := make(map[string]struct{}, len(names))
for _, n := range names {
out[n] = struct{}{}
}
themeSetCache = out
})
return themeSetCache
}
// 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 := getThemeSet()[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
}
|