forked from mk12/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkitty-colors.cpp
More file actions
302 lines (264 loc) · 8.82 KB
/
kitty-colors.cpp
File metadata and controls
302 lines (264 loc) · 8.82 KB
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
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
extern "C" {
#include <sys/wait.h>
}
namespace {
const char* const USAGE = R"EOS(
Usage: kitty-colors [-h] [-c FILE] [-a | -p] [-f FRAMES] [-d DELAY]
This script changes the terminal colors in kitty.
Note: If you get "Connection refused" Python errors, it's probably because
there's a stale control file in ~/.local/share/kitty.
Flags:
-h display this help messge
-p print OSC codes only (no kitty remote-control or changing colors.conf)
-a animate the transition to the new colors
Options:
-c FILE specify colors conf file (if omitted, you pick using fzf)
-f FRAMES number of animation frames (default: 50)
-d DELAY delay in seconds between frames (default: 30 ms)
)EOS";
struct Options {
bool print_only = false;
bool animate = false;
int frames = 100;
int delay = 30;
std::string target;
};
const char* PROGRAM = nullptr;
bool inside_tmux() {
static bool tmux = std::getenv("TMUX") != nullptr;
return tmux;
}
std::string base16_colors_dir() {
return std::string(std::getenv("PROJECTS")) + "/base16-kitty/colors";
}
std::string kitty_colors_conf_file() {
return std::string(std::getenv("HOME")) + "/.config/kitty/colors.conf";
}
std::string kitty_sockets_dir() {
return std::string(std::getenv("HOME")) + "/.local/share/kitty";
}
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
result.erase(result.find_last_not_of("\r\n") + 1);
return result;
}
using Key = int;
using Color = unsigned;
using ColorSet = std::map<Key, Color>;
const Key FOREGROUND = -1;
const Key BACKGROUND = -2;
ColorSet parse_color_file(const char* filename) {
ColorSet colors;
std::ifstream infile(filename);
std::string line;
while (std::getline(infile, line)) {
if (line.empty() || line[0] == '#') {
continue;
}
Key key;
std::istringstream iss(line);
std::string key_str;
iss >> key_str;
if (key_str == "foreground") {
key = FOREGROUND;
} else if (key_str == "background") {
key = BACKGROUND;
} else if (key_str.substr(0, 5) == "color") {
key = std::stoi(key_str.substr(5));
} else {
continue;
}
std::string color_str;
iss >> color_str;
if (color_str.empty() || color_str[0] != '#') {
continue;
}
Color color =
static_cast<unsigned>(std::stoi(color_str.substr(1), nullptr, 16));
colors.emplace(key, color);
}
return colors;
}
void set_colors_osc(const ColorSet& colors) {
const char* pre = inside_tmux() ? "\x1bPtmux;\x1b\x1b]" : "\x1b]";
const char* post = inside_tmux() ? "\a\x1b\\\x1b\\" : "\a";
std::ostringstream ss;
char buffer[7];
for (const auto& entry : colors) {
ss << pre;
switch (entry.first) {
case FOREGROUND:
ss << "10";
break;
case BACKGROUND:
ss << "11";
break;
default:
ss << "4;" << entry.first;
break;
}
std::snprintf(buffer, sizeof buffer, "%06x", entry.second);
ss << ";#" << buffer << post;
}
std::cout << ss.str();
std::cout.flush();
}
double bit_to_linear(unsigned bit) {
double x = static_cast<double>(bit) / 255;
if (x < 0.04045) {
return x / 12.92;
}
return std::pow((x + 0.055) / 1.055, 2.4);
}
unsigned linear_to_bit(double linear) {
double x = linear <= 0.0031308 ? linear * 12.92
: 1.055 * std::pow(linear, 1 / 2.4) - 0.055;
return static_cast<unsigned>(
std::max(0, std::min(255, static_cast<int>(std::lround(x * 255)))));
}
Color interpolate_color(Color c1, Color c2, double t) {
double r1 = bit_to_linear((c1 >> 16) & 0xff);
double g1 = bit_to_linear((c1 >> 8) & 0xff);
double b1 = bit_to_linear(c1 & 0xff);
double r2 = bit_to_linear((c2 >> 16) & 0xff);
double g2 = bit_to_linear((c2 >> 8) & 0xff);
double b2 = bit_to_linear(c2 & 0xff);
unsigned r = linear_to_bit(r1 + (r2 - r1) * t);
unsigned g = linear_to_bit(g1 + (g2 - g1) * t);
unsigned b = linear_to_bit(b1 + (b2 - b1) * t);
return (r << 16) + (g << 8) + b;
}
void animate_colors(const ColorSet& src, const ColorSet& dst, int frames,
int delay_ms) {
const std::chrono::milliseconds delay(delay_ms);
for (int i = 0; i < frames; ++i) {
ColorSet colors;
for (const auto& entry : src) {
const double t = static_cast<double>(i + 1) / frames;
Color interp =
interpolate_color(entry.second, dst.at(entry.first), t);
colors.emplace(entry.first, interp);
}
set_colors_osc(colors);
std::this_thread::sleep_for(delay);
}
}
void update_running_kitties(const std::string& colors_file) {
for (const auto& entry :
std::filesystem::directory_iterator(kitty_sockets_dir())) {
if (entry.path().extension() != ".sock") {
continue;
}
std::ostringstream ss;
ss << "kitty @ --to 'unix:" << entry.path().string()
<< "' set-colors -a -c '" << colors_file << "' &";
system(ss.str().c_str());
}
wait(nullptr);
}
void update_kitty_conf(const std::string& colors_file) {
std::ofstream file(kitty_colors_conf_file());
file << "include " << colors_file << "\n";
}
} // namespace
int main(int argc, char** argv) {
PROGRAM = argv[0];
if (std::system("command -v kitty > /dev/null 2>&1") != 0) {
std::cerr << PROGRAM << ": kitty: command not found\n";
return 1;
}
Options options;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "-h") == 0 ||
std::strcmp(argv[i], "--help") == 0) {
std::cout << USAGE;
return 0;
}
if (std::strcmp(argv[i], "-p") == 0) {
options.print_only = true;
} else if (std::strcmp(argv[i], "-a") == 0) {
options.animate = true;
} else if (std::strcmp(argv[i], "-c") == 0) {
options.target = argv[++i];
} else if (std::strcmp(argv[i], "-d") == 0) {
options.delay = std::stoi(argv[++i]);
} else if (std::strcmp(argv[i], "-f") == 0) {
options.frames = std::stoi(argv[++i]);
}
}
const auto colors_dir = base16_colors_dir();
if (options.target.empty()) {
if (!std::filesystem::is_directory(colors_dir)) {
std::cerr << PROGRAM << ": " << colors_dir
<< ": directory not found\n";
return 1;
}
std::ostringstream ss;
ss << "find " << colors_dir
<< " -name '*[^2][^5][^6].conf'"
" | sed 's|^.*/base16-||;s/.conf$//' | sort | fzf";
options.target = exec(ss.str().c_str());
if (options.target.empty()) {
return 0;
}
}
if (!std::filesystem::is_regular_file(options.target)) {
std::string original = options.target;
options.target = colors_dir + "/base16-" + options.target + ".conf";
if (!std::filesystem::is_regular_file(options.target)) {
std::cerr << PROGRAM << ": " << original
<< ": file or color scheme name not found\n";
return 1;
}
}
if (options.print_only) {
set_colors_osc(parse_color_file(options.target.c_str()));
return 0;
}
if (options.animate) {
std::ifstream config_file(kitty_colors_conf_file());
std::string token;
config_file >> token;
if (token != "include") {
std::cerr << PROGRAM << ": " << kitty_colors_conf_file()
<< ": malformed config file\n";
return 1;
}
config_file >> token;
const auto& path = token;
if (!std::filesystem::is_regular_file(path)) {
std::cerr << PROGRAM << ": " << kitty_colors_conf_file() << ": "
<< path << ": file not found\n";
return 1;
}
auto src_colors = parse_color_file(path.c_str());
auto dst_colors = parse_color_file(options.target.c_str());
animate_colors(src_colors, dst_colors, options.frames, options.delay);
}
update_running_kitties(options.target);
update_kitty_conf(options.target);
return 0;
}