-
Notifications
You must be signed in to change notification settings - Fork 1
/
clolcat.c
298 lines (255 loc) · 6.67 KB
/
clolcat.c
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "help.h"
#define BUFSZ 65536
int use_color = 1;
double freq = 0.1;
uint16_t seed = 0;
int invert = 0;
int ignore_file_errors = 0;
size_t posX = 0;
size_t posY = 0;
size_t pos_mod = 0;
size_t max_width = 0;
// to minimize processor usage we buffer color escape strings
char** color_map = NULL;
uint8_t* color_map_lens = NULL;
// to minimize writing overhead we buffer output
char* output_buffer = NULL;
void write_color_by_offset(int fd, char* data, size_t size);
void fputs_color_by_offset(char* str, FILE* file);
uint32_t color_by_offset(size_t i);
// not used if file won't even open, just for other I/O errors
void exit_ioerr(char* file);
int main(int argc, char** argv) {
int force = 0;
int print_help = 0;
int exit_status = EXIT_SUCCESS;
int c;
while((c = getopt(argc, argv, "F:S:iqfh")) != -1) {
switch(c) {
case 'F':
freq = atof(optarg); break;
case 'S':
seed = atoi(optarg); break;
case 'i':
invert = 1; break;
case 'q':
ignore_file_errors = 1; break;
case 'f':
force = 1; break;
// show help later, after stuff needed for color_by_offset output
// has been initialized
case 'h':
print_help = 1; break;
default:
print_help = 1; exit_status = EXIT_FAILURE; break;
}
}
// if a single line is too fat it might break down to multiple visual lines,
// but the color will be as if it was a single visual line. So we get the
// width of the terminal and every time it is reached we act as if a \n
// appeared
if(isatty(1)) {
struct winsize termsz;
ioctl(1, TIOCGWINSZ, &termsz);
max_width = termsz.ws_col;
}
// add some offset to posY for random color start, either based on
// a random number or a pnrg number seeded by user-given seed
srand(seed == 0? time(0) : seed);
posY = rand() & 0xFFFF;
if(!force && !isatty(1)) {
use_color = 0;
}
else if(!force && freq < 0.001) {
fputs("Because it is pretty useless and might consume a lot of"
"memory, without -f I'll refuse to use color frequencies less"
"than 0.001.\n", stderr);
use_color = 0;
}
else {
// we want the max amount of different colors so we can
// make a color table.
// the color repeats revery time freq * n equals 2 * pi,
// so we compute an integer that is close to i * freq == 2 * pi
// (suffices -freq / 10 < (i * freq) % (2 * pi) < freq / 10)
// and do posX % i and posY % i to keep them small.
// (tested with dmesg | clolcat, looks much better now)
double two_pi = M_PI * 2;
for(int i = 1; ; i++) {
double d = fmod(freq * i + freq / 10, two_pi);
if(d < freq / 5) {
pos_mod = i;
break;
}
}
// also, since the colors will be repeating every pos_mod characters,
// we can make a color map (with the actual color escape strings)
// and avoid calculating 3 sines for every byte from stdin
color_map = (char**)malloc(pos_mod * sizeof(char*));
color_map_lens = (uint8_t*)malloc(pos_mod * sizeof(uint8_t));
for(size_t i = 0; i < pos_mod; i++) {
color_map[i] = (char*)malloc(21);
uint32_t color = color_by_offset(i);
int esc_len;
if(!invert) {
sprintf(color_map[i],
"\033[38;2;%u;%u;%um%n",
(color & 0xff000000) >> 24,
(color & 0x00ff0000) >> 16,
(color & 0x0000ff00) >> 8,
&esc_len);
}
else {
sprintf(color_map[i],
"\033[48;2;%u;%u;%um%n",
(color & 0xff000000) >> 24,
(color & 0x00ff0000) >> 16,
(color & 0x0000ff00) >> 8,
&esc_len);
}
color_map_lens[i] = esc_len;
}
}
// BUFSZ is the maximum number of input bytes to process.
// 1 input byte generates less than 21 output bytes
// but Ill roll with this, just to be sure.
char* input_buffer = (char*)malloc(BUFSZ);
output_buffer = (char*)malloc(BUFSZ * 21);
if(print_help) {
fputs_color_by_offset(help_str, stderr);
goto epilog;
}
if(optind == argc) {
for(;;) {
ssize_t ret = read(0, input_buffer, BUFSZ);
if(ret < 0) {
exit_ioerr("stdin");
}
if(ret == 0) {
break;
}
write_color_by_offset(1, input_buffer, ret);
}
}
else {
do {
int fd = open(argv[optind], O_RDONLY);
if(fd < 0) {
if(!ignore_file_errors) {
fprintf(stderr, "Error: couldn't open %s\n", argv[optind]);
exit_status = EXIT_FAILURE;
goto epilog;
}
else {
goto next_file;
}
}
for(;;) {
ssize_t ret = read(fd, input_buffer, BUFSZ);
if(ret < 0) {
if(!ignore_file_errors) {
exit_ioerr(argv[optind]);
}
else {
goto next_file;
}
}
if(ret == 0) {
break;
}
write_color_by_offset(1, input_buffer, ret);
}
close(fd);
next_file:
optind++;
} while(optind < argc);
}
epilog:
if(isatty(1)) printf("\033[39m\033[49m");
free(input_buffer);
free(output_buffer);
return exit_status;
}
void write_color_by_offset(int fd, char* data, size_t size) {
if(!use_color) {
// just write buffer to fd
size_t written = 0;
do {
ssize_t ret = write(fd, data + written, size - written);
if(ret < 0) {
exit_ioerr(NULL);
}
written += ret;
} while(written < size);
}
else {
size_t written = 0;
off_t output_offs = 0;
do {
if(data[written] == '\n') {
posX = 0;
posY++;
}
size_t esc_len = color_map_lens[(posX + posY) % pos_mod];
memcpy(
output_buffer + output_offs,
color_map[(posX + posY) % pos_mod],
esc_len);
output_buffer[output_offs + esc_len] = data[written];
output_offs += esc_len + 1;
written++;
if(posX == max_width) {
posX = 1;
posY++;
}
else {
posX++;
}
} while(written < size);
// reuse written for how much buffer written to fd
written = 0;
do {
ssize_t ret = write(fd, output_buffer + written,
output_offs - written);
if(ret < 0) {
exit_ioerr(NULL);
}
written += ret;
} while(written < output_offs);
}
}
void fputs_color_by_offset(char* str, FILE* file) {
write_color_by_offset(fileno(file), str, strlen(str));
}
uint32_t color_by_offset(size_t i) {
return
((uint8_t)(sin(freq * i) * 127 + 128) << 24) |
((uint8_t)(sin(freq * i + M_PI / 3 * 2) * 127 + 128) << 16) |
((uint8_t)(sin(freq * i + M_PI / 3 * 4) * 127 + 128) << 8) |
((0x00) << 0);
}
void exit_ioerr(char* file) {
char* errstr = strerror(errno);
if(errstr) {
fprintf(stderr, "I/O error%s%s: %s\n", file? " on file " : "", file,
errstr);
}
else {
fprintf(stderr, "I/O error%s%s: %d\n", file? " on file " : "", file,
errno);
}
if(isatty(1)) printf("\033[39m\033[49m\n");
exit(1);
}