internal/control/theme.go

49 lines · 1310 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		ReadOnly: true, Run: runWebThemeShow})
15	register(Command{Path: []string{"web", "theme", "set"},
16		Summary: "follow the browser's scheme, or force light or dark",
17		Usage:   "web theme set system|light|dark", Run: runWebThemeSet})
18}
19
20// themes are the values the layout knows how to stamp. system is the
21// default and stamps nothing: the stylesheet's media query decides.
22var themes = map[string]bool{"system": true, "light": true, "dark": true}
23
24func runWebThemeShow(c *Ctx, args []string) int {
25	if len(args) != 0 {
26		return c.usage()
27	}
28	return emitTheme(c)
29}
30
31func runWebThemeSet(c *Ctx, args []string) int {
32	if len(args) != 1 || !themes[args[0]] {
33		return c.usage()
34	}
35	if err := c.Store.SetTheme(c.User.ID, args[0]); err != nil {
36		return c.fail(protocol.ExitFailure, "%v", err)
37	}
38	return emitTheme(c)
39}
40
41func emitTheme(c *Ctx) int {
42	theme, err := c.Store.Theme(c.User.ID)
43	if err != nil {
44		return c.fail(protocol.ExitFailure, "%v", err)
45	}
46	return c.emit(map[string]string{"theme": theme}, func(w io.Writer) {
47		fmt.Fprintf(w, "theme: %s\n", theme)
48	})
49}