~chrisppy/beagles

beagles/config/theme.go -rw-r--r-- 6.4 KiB
8e6595ccChris Palmer update to go-gemini 0.2.0 6 months ago
                                                                                
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
// This file is part of beagles.
//
// Copyright © 2020-2021 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package config

import (
	"fmt"

	"git.sr.ht/~emersion/go-scfg"
	"github.com/gdamore/tcell/v2"
)

// Theme config
type Theme struct {
	AudioIcon      string
	ErrColor       tcell.Color
	SeparatorColor tcell.Color
	ReadColor      tcell.Color
	CMDBGColor     tcell.Color
	CMDFGColor     tcell.Color
	StatusBGColor  tcell.Color
	StatusFGColor  tcell.Color
	TitleBGColor   tcell.Color
	TitleFGColor   tcell.Color
	ListBGColor    tcell.Color
	ListFGColor    tcell.Color
	TreeBGColor    tcell.Color
	TreeFGColor    tcell.Color
	ContentBGColor tcell.Color
	ContentFGColor tcell.Color
}

func parseTheme(cfg *Config, dir *scfg.Directive) map[error]bool {
	errors := make(map[error]bool)
	cfg.Theme = Theme{}

	var bg, fg string
	var faudio, fread, ferr, fsep, ftitle, fstat, fcmd, fcnt, flist, ftree bool
	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "audio-icon":
			faudio = true
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cfg.Theme.AudioIcon = d.Params[0]
			}
		case "background-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				bg = d.Params[0]
			}
		case "command-line":
			fcmd = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				cfg.Theme.CMDBGColor = tcell.GetColor(bga)
				cfg.Theme.CMDFGColor = tcell.GetColor(fga)
			}
		case "content":
			fcnt = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				cfg.Theme.ContentBGColor = tcell.GetColor(bga)
				cfg.Theme.ContentFGColor = tcell.GetColor(fga)
			}
		case "error-color":
			ferr = true
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cfg.Theme.ErrColor = tcell.GetColor(d.Params[0])
			}
		case "foreground-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				fg = d.Params[0]
			}
		case "list":
			flist = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				cfg.Theme.ListBGColor = tcell.GetColor(bga)
				cfg.Theme.ListFGColor = tcell.GetColor(fga)
			}
		case "read-color":
			fread = true
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cfg.Theme.ReadColor = tcell.GetColor(d.Params[0])
			}
		case "separator-color":
			fsep = true
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cfg.Theme.SeparatorColor = tcell.GetColor(d.Params[0])
			}
		case "status-bar":
			fstat = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				cfg.Theme.StatusBGColor = tcell.GetColor(bga)
				cfg.Theme.StatusFGColor = tcell.GetColor(fga)
			}
		case "title-bar":
			ftitle = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				cfg.Theme.TitleBGColor = tcell.GetColor(bga)
				cfg.Theme.TitleFGColor = tcell.GetColor(fga)
			}
		case "tree":
			ftree = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				cfg.Theme.TreeBGColor = tcell.GetColor(bga)
				cfg.Theme.TreeFGColor = tcell.GetColor(fga)
			}
		default:
			errors[fmt.Errorf("unknown directive within theme %q", n)] = false
		}
	}

	if bg == "" {
		errors[fmt.Errorf("background-color is a required directive for theme")] = false
	}
	if fg == "" {
		errors[fmt.Errorf("foreground-color is a required directive for theme")] = false
	}
	if !faudio {
		errors[fmt.Errorf("audio-icon is a required directive for theme")] = false
	}
	if !ferr {
		errors[fmt.Errorf("error-color is a required directive for theme")] = false
	}
	if !fread {
		errors[fmt.Errorf("read-color is a required directive for theme")] = false
	}
	if !fsep {
		errors[fmt.Errorf("separator-color is a required directive for theme")] = false
	}

	if !fcmd && bg != "" && fg != "" {
		cfg.Theme.CMDBGColor = tcell.GetColor(bg)
		cfg.Theme.CMDFGColor = tcell.GetColor(fg)
	}
	if !fcnt && bg != "" && fg != "" {
		cfg.Theme.ContentBGColor = tcell.GetColor(bg)
		cfg.Theme.ContentFGColor = tcell.GetColor(fg)
	}
	if !flist && bg != "" && fg != "" {
		cfg.Theme.ListBGColor = tcell.GetColor(bg)
		cfg.Theme.ListFGColor = tcell.GetColor(fg)
	}
	if !fstat && bg != "" && fg != "" {
		cfg.Theme.StatusBGColor = tcell.GetColor(bg)
		cfg.Theme.StatusFGColor = tcell.GetColor(fg)
	}
	if !ftitle && bg != "" && fg != "" {
		cfg.Theme.TitleBGColor = tcell.GetColor(bg)
		cfg.Theme.TitleFGColor = tcell.GetColor(fg)
	}
	if !ftree && bg != "" && fg != "" {
		cfg.Theme.TreeBGColor = tcell.GetColor(bg)
		cfg.Theme.TreeFGColor = tcell.GetColor(fg)
	}

	return errors
}

func parseChildTheme(name string, dir *scfg.Directive) (string, string, map[error]bool) {
	errors := make(map[error]bool)

	var bg, fg string
	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "background-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				bg = d.Params[0]
			}
		case "foreground-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				fg = d.Params[0]
			}
		default:
			errors[fmt.Errorf("unknown directive within %s %q", name, n)] = false
		}
	}

	if bg == "" {
		errors[fmt.Errorf("background-color is a required directive for %s", name)] = false
	}

	if fg == "" {
		errors[fmt.Errorf("foreground-color is a required directive for %s", name)] = false
	}

	return bg, fg, errors
}