/
theme.cpp
352 lines (295 loc) · 9.03 KB
/
theme.cpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <array>
#include <exception>
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#if __cplusplus >= 201703L
#include <filesystem>
#else
#include <dirent.h>
#include <sys/types.h>
#endif
#define JSON_DIAGNOSTICS 1
#include "json.hpp"
#include "optional.hpp"
#include "garglk.h"
using json = nlohmann::json;
template<size_t...Is, typename T>
std::array<T, sizeof...(Is)> make_array(const T &value, std::index_sequence<Is...>) {
return {(static_cast<void>(Is), value)...};
}
template<size_t N, typename T>
std::array<T, N> make_array(const T &value) {
return make_array(value, std::make_index_sequence<N>());
}
template<typename K, typename V>
static std::set<K> map_keys(const std::map<K, V> &map)
{
std::set<K> keys;
for (const auto &pair : map)
keys.insert(pair.first);
return keys;
}
Color gli_parse_color(const std::string &str)
{
static const std::regex color_re(R"(#?[a-fA-F0-9]{6})");
std::string r, g, b;
if (!std::regex_match(str, color_re))
throw std::runtime_error("invalid color: " + str);
int pos = str[0] == '#' ? 1 : 0;
r = str.substr(pos + 0, 2);
g = str.substr(pos + 2, 2);
b = str.substr(pos + 4, 2);
return Color(std::stoul(r, nullptr, 16),
std::stoul(g, nullptr, 16),
std::stoul(b, nullptr, 16));
}
static const Color black = Color(0x00, 0x00, 0x00);
static const Color white = Color(0xff, 0xff, 0xff);
static const Color gray = Color(0x60, 0x60, 0x60);
struct ColorPair {
Color fg;
Color bg;
};
template <typename Iterable, typename DType>
std::string join(const Iterable &values, const DType &delim)
{
auto it = values.begin();
std::stringstream result;
if (it != values.end())
result << *it++;
for (; it != values.end(); ++it)
{
result << delim;
result << *it;
}
return result.str();
}
struct ThemeStyles {
std::array<ColorPair, style_NUMSTYLES> colors;
void to(std::array<style_t, style_NUMSTYLES> &styles) const {
for (int i = 0; i < style_NUMSTYLES; i++)
{
styles[i].fg = colors[i].fg;
styles[i].bg = colors[i].bg;
}
}
};
struct Theme {
std::string name;
Color windowcolor;
Color bordercolor;
Color caretcolor;
Color linkcolor;
Color morecolor;
ThemeStyles tstyles;
ThemeStyles gstyles;
void apply() const {
gli_window_color = windowcolor;
gli_window_save = windowcolor;
gli_border_color = bordercolor;
gli_border_save = bordercolor;
gli_caret_color = caretcolor;
gli_caret_save = caretcolor;
gli_link_color = linkcolor;
gli_link_save = linkcolor;
gli_more_color = morecolor;
gli_more_save = morecolor;
tstyles.to(gli_tstyles);
gstyles.to(gli_gstyles);
}
static Theme from_file(const std::string &filename) {
std::ifstream f(filename);
if (!f.is_open())
throw std::runtime_error("unable to open file");
json j;
f >> j;
auto window = gli_parse_color(j.at("window"));
auto border = gli_parse_color(j.at("border"));
auto caret = gli_parse_color(j.at("caret"));
auto link = gli_parse_color(j.at("link"));
auto more = gli_parse_color(j.at("more"));
auto text_buffer = get_user_styles(j, "text_buffer");
auto text_grid = get_user_styles(j, "text_grid");
return {
j.at("name"),
window,
border,
caret,
link,
more,
text_buffer,
text_grid,
};
}
private:
static ThemeStyles get_user_styles(const json &j, const std::string &color)
{
std::array<nonstd::optional<ColorPair>, style_NUMSTYLES> possible_colors;
std::map<std::string, json> styles = j.at(color);
static std::map<std::string, int> stylemap = {
{"normal", 0},
{"emphasized", 1},
{"preformatted", 2},
{"header", 3},
{"subheader", 4},
{"alert", 5},
{"note", 6},
{"blockquote", 7},
{"input", 8},
{"user1", 9},
{"user2", 10},
};
auto parse_colors = [&possible_colors](const json &style, int i) {
auto fg = gli_parse_color(style.at("fg"));
auto bg = gli_parse_color(style.at("bg"));
possible_colors[i] = ColorPair{fg, bg};
};
if (styles.find("default") != styles.end())
for (int i = 0; i < style_NUMSTYLES; i++)
parse_colors(styles["default"], i);
styles.erase("default");
for (const auto &style : styles)
{
if (stylemap.find(style.first) == stylemap.end())
throw std::runtime_error("invalid style in " + color + ": " + style.first);
parse_colors(style.second, stylemap[style.first]);
}
auto colors = make_array<style_NUMSTYLES>(ColorPair{white, black});
std::vector<std::string> missing;
for (const auto &s : stylemap)
{
if (possible_colors[s.second].has_value())
colors[s.second] = possible_colors[s.second].value();
else
missing.push_back(s.first);
}
if (!missing.empty())
throw std::runtime_error(color + " is missing the following styles: " + join(missing, ", "));
return {colors};
}
};
static Theme light{
"light",
white,
black,
black,
Color(0x00, 0x00, 0x60),
Color(0x00, 0x60, 0x00),
ThemeStyles{
ColorPair{black, white}, ColorPair{black, white},
ColorPair{black, white}, ColorPair{black, white},
ColorPair{black, white}, ColorPair{black, white},
ColorPair{black, white}, ColorPair{black, white},
ColorPair{Color(0x00, 0x60, 0x00), white}, ColorPair{black, white},
ColorPair{black, white},
},
ThemeStyles{make_array<style_NUMSTYLES>(ColorPair{gray, white})},
};
static const Color darkfg = Color(0xe7, 0xe8, 0xe9);
static const Color darkbg = Color(0x31, 0x36, 0x3b);
static Theme dark{
"dark",
Color(0x31, 0x36, 0x3b),
black,
Color(0xe7, 0xe8, 0xe9),
Color(0x1d, 0x99, 0xf3),
Color(0x00, 0xcc, 0x00),
ThemeStyles{make_array<style_NUMSTYLES>(ColorPair{darkfg, darkbg})},
ThemeStyles{make_array<style_NUMSTYLES>(ColorPair{darkfg, darkbg})},
};
static std::map<std::string, Theme> themes = {
{light.name, light},
{dark.name, dark},
};
std::vector<std::string> garglk::theme::paths()
{
std::vector<std::string> theme_paths;
for (const auto &path : garglk::winappdata())
theme_paths.push_back(path + "/themes");
return theme_paths;
}
#if __cplusplus >= 201703
static std::vector<std::string> directory_entries(const std::string &dir)
{
std::vector<std::string> entries;
try
{
for (const auto &entry : std::filesystem::directory_iterator(dir))
entries.push_back(entry.path().string());
}
catch (const std::filesystem::filesystem_error &)
{
}
return entries;
}
#else
static std::vector<std::string> directory_entries(const std::string &dir)
{
auto d = garglk::unique(opendir(dir.c_str()), closedir);
if (d == nullptr)
return {};
std::vector<std::string> entries;
dirent *de;
while ((de = readdir(d.get())) != nullptr)
entries.push_back(dir + "/" + de->d_name);
return entries;
}
#endif
void garglk::theme::init()
{
for (const auto &themedir : garglk::theme::paths())
{
for (const auto &filename : directory_entries(themedir))
{
auto dot = filename.find_last_of('.');
if (dot != std::string::npos && filename.substr(dot) == ".json")
{
try
{
auto theme = Theme::from_file(filename);
// C++17: use insert_or_assign()
const auto result = themes.insert({theme.name, theme});
if (!result.second)
result.first->second = theme;
}
catch (const std::exception &e)
{
std::cerr << "garglk: error parsing theme " << filename << ": " << e.what() << std::endl;
}
}
}
}
}
static std::string system_theme_name()
{
return windark() ? dark.name : light.name;
}
void garglk::theme::set(std::string name)
{
if (name == "system")
name = system_theme_name();
try
{
themes.at(name).apply();
}
catch (const std::out_of_range &)
{
std::cerr << "garglk: unknown theme: " << name << std::endl;
}
}
std::vector<std::string> garglk::theme::names()
{
std::vector<std::string> theme_names;
for (const auto &theme : themes)
theme_names.push_back(theme.first);
theme_names.push_back(std::string("system (") + system_theme_name() + ")");
return theme_names;
}