-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlolz.h
103 lines (87 loc) · 2.22 KB
/
lolz.h
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
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <wchar.h>
#include <stdarg.h>
#define ARRAY_SIZE(foo) (sizeof(foo) / sizeof(foo[0]))
const unsigned char codes[] = {39, 38, 44, 43, 49, 48, 84, 83, 119, 118, 154, 148, 184, 178, 214, 208, 209, 203, 204, 198, 199, 163, 164, 128, 129, 93, 99, 63, 69, 33};
static double freq_h = 0.23, freq_v = 0.1;
static int speed = 300;
static void configLolz(double _freq_h, double _freq_v, int _speed)
{
freq_h = (_freq_h != 0.0) ? _freq_h : freq_h;
freq_v = (_freq_v != 0.0) ? _freq_v : freq_v;
speed = (_speed != 0) ? _speed : speed;
}
static void find_escape_sequences(wint_t c, int *state)
{
if (c == '\033')
{
*state = 1;
}
else if (*state == 1)
{
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
*state = 2;
}
else
{
*state = 0;
}
}
void lolcat(char *input)
{
int cc = -1, i, l = 0;
int colors = isatty(STDOUT_FILENO);
struct timeval tv;
gettimeofday(&tv, NULL);
double offx = (tv.tv_sec % speed) / (double)speed;
i = 0;
int escape_state = 0;
size_t j = 0;
while (j < strlen(input))
{
wchar_t c = input[j];
if (colors)
{
find_escape_sequences(c, &escape_state);
if (!escape_state)
{
if (c == '\n')
{
l++;
i = 0;
}
else
{
int ncc = offx * ARRAY_SIZE(codes) + (int)((i += wcwidth(c)) * freq_h + l * freq_v);
if (cc != ncc)
wprintf(L"\033[38;5;%hhum", codes[(cc = ncc) % ARRAY_SIZE(codes)]);
}
}
}
putwchar(c);
if (escape_state == 2)
wprintf(L"\033[38;5;%hhum", codes[cc % ARRAY_SIZE(codes)]);
j++;
}
if (colors)
wprintf(L"\033[0m");
cc = -1;
}
void lolz(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char *str;
vasprintf(&str, fmt, args);
lolcat(str);
free(str);
va_end(args);
}