internal/control/theme.go

f8b976a97290a20d552056a999511f5d27d8e8ec
gitbay/internal/control/theme.go history · blame · raw

51 lines · 1396 bytes

 1package control
 2
 3import (
 4	"fmt"
 5	"io"
 6
 7	"gitbay.org/gitbay/internal/protocol"
 8)
 9
10func init() {
11	register(Command{Path: []string{"web", "theme", "show"},
12		Summary:  "the colour scheme the web UI uses for you",
13		Usage:    "web theme show",
14		Examples: []string{"web theme show"},
15		ReadOnly: true, Run: runWebThemeShow})
16	register(Command{Path: []string{"web", "theme", "set"},
17		Summary:  "follow the browser's scheme, or force light or dark",
18		Usage:    "web theme set system|light|dark",
19		Examples: []string{"web theme set dark"}, Run: runWebThemeSet})
20}
21
22// themes are the values the layout knows how to stamp. system is the
23// default and stamps nothing: the stylesheet's media query decides.
24var themes = map[string]bool{"system": true, "light": true, "dark": true}
25
26func runWebThemeShow(c *Ctx, args []string) int {
27	if len(args) != 0 {
28		return c.usage()
29	}
30	return emitTheme(c)
31}
32
33func runWebThemeSet(c *Ctx, args []string) int {
34	if len(args) != 1 || !themes[args[0]] {
35		return c.usage()
36	}
37	if err := c.Store.SetTheme(c.User.ID, args[0]); err != nil {
38		return c.fail(protocol.ExitFailure, "%v", err)
39	}
40	return emitTheme(c)
41}
42
43func emitTheme(c *Ctx) int {
44	theme, err := c.Store.Theme(c.User.ID)
45	if err != nil {
46		return c.fail(protocol.ExitFailure, "%v", err)
47	}
48	return c.emit(map[string]string{"theme": theme}, func(w io.Writer) {
49		fmt.Fprintf(w, "theme: %s\n", theme)
50	})
51}