-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lolcat.cpp
332 lines (292 loc) · 8.56 KB
/
Lolcat.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
#include "Lolcat.h"
#if defined _WIN32 || defined _WIN64
WinLolcatColor::WinLolcatColor(const int& Col)
{
col = Col;
}
WinLolcatColor::WinLolcatColor(const int& foreground, const int& background)
{
col = foreground + background*16;
}
std::ostream& operator<<(std::ostream& os, const WinLolcatColor& cc)
{
if(&os == &std::cout)
{
FlushConsoleInputBuffer(hConsole);
SetConsoleTextAttribute(hConsole, cc.col);
}
return os;
}
int noColors = 9;
std::vector<WinLolcatColor> colors = {WinLolcatColor(4),
WinLolcatColor(12),
WinLolcatColor(6),
WinLolcatColor(10),
WinLolcatColor(2),
WinLolcatColor(3),
WinLolcatColor(1),
WinLolcatColor(5),
WinLolcatColor(13)};
void lolcat_powershell()
{
noColors = 7;
colors = std::vector<WinLolcatColor>{WinLolcatColor(12, 5),
WinLolcatColor(14, 5),
WinLolcatColor(10, 5),
WinLolcatColor(2, 5),
WinLolcatColor(3, 5),
WinLolcatColor(9, 5),
WinLolcatColor(13, 5)};
}
#else
const int noColors = 30;
const std::string colors[] = {"\033[38;5;39m",
"\033[38;5;38m",
"\033[38;5;44m",
"\033[38;5;43m",
"\033[38;5;49m",
"\033[38;5;48m",
"\033[38;5;84m",
"\033[38;5;83m",
"\033[38;5;119m",
"\033[38;5;118m",
"\033[38;5;154m",
"\033[38;5;148m",
"\033[38;5;184m",
"\033[38;5;178m",
"\033[38;5;214m",
"\033[38;5;208m",
"\033[38;5;209m",
"\033[38;5;203m",
"\033[38;5;204m",
"\033[38;5;198m",
"\033[38;5;199m",
"\033[38;5;163m",
"\033[38;5;164m",
"\033[38;5;128m",
"\033[38;5;129m",
"\033[38;5;93m",
"\033[38;5;99m",
"\033[38;5;63m",
"\033[38;5;69m",
"\033[38;5;33m"};
#endif
bool format = 1;
int tabWidth = 2;
int mod(const int& x, const int& m)
{
int mod_val;
if(x >= 0)
return x%m;
else
{
mod_val = (-x)%m;
if(mod_val)
return m-mod_val;
else
return 0;
}
}
double gradient = 999;
size_t width, cWidth;
bool addLineNo = 0, zigzag = 0;
int posGrad = 1;
const std::string arr = "⥤";
int lolcat(std::istream& is)
{
std::string inLine;
int color,
lineNo = 0,
r = rand()%noColors;
if(gradient < 0)
gradient = -gradient;
while(!is.eof())
{
if(!getline(is, inLine))
break;
if(addLineNo)
inLine = std::to_string(lineNo) + ": " + inLine;
if(zigzag)
{
if(int(std::floor(lineNo/noColors))%2)
color = r = (r+1)%noColors;
else
color = r = mod(r-1, noColors);
}
else
{
if(posGrad)
{
cWidth = (1-((lineNo+r)*gradient - std::floor((lineNo+r)*gradient))/1.0)*width;
color = int((lineNo+r)*gradient)%noColors;
}
else
{
cWidth = (((lineNo+r)*gradient - std::floor((lineNo+r)*gradient))/1.0)*width;
color = mod(-int((lineNo+r)*gradient), noColors);
//color = mod(noColors-int((lineNo+r)*gradient)%noColors, noColors); //alternative to above
}
}
size_t i=0;
std::cout << colors[color];
while(i < inLine.size())
{
for(size_t I=0; I<width && i<inLine.size(); ++I)
{
if(format)
{
for(size_t j=1; j<4; ++j)
{
if(j < 3 && i+width-j < inLine.size() && inLine[i+width-j] == arr[0])
{ //checks for multi-char utf characters
std::cout << colors[color] << inLine.substr(i, width+3-j);
i += width+3-j;
break;
}
else if(i+width-j < inLine.size() && (inLine[i+width-j] == '\xF0' ||
inLine[i+width-j] == '\xE2' ||
inLine[i+width-j] == '\xC2'))
{ //checks for emojis
std::cout << colors[color] << inLine.substr(i, width+4-j);
i += width+4-j;
break;
}
}
//https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
while(i < inLine.size() && ((inLine[i] == '\\' && (!i || inLine[i-1] != '\\')) ||
inLine[i] == '\x0' ||
inLine[i] == '\xF0' || // emojis
inLine[i] == '\xE2' || // emojis
inLine[i] == '\xC2' || // emojis
//inLine[i] == "\e" ||
inLine[i] == '\033' || // octag
inLine.substr(i, 2) == "^[" || // ctrl key
inLine[i] == '\x1b' || // hexadecimal
inLine[i] == '\u001b')) // unicode
{
int start = i++;
while(i < inLine.size() && inLine[i] != 'm' &&
inLine[i] != ' ' &&
inLine[i] != '\n' &&
inLine[i] != '\t')
++i;
std::cout << inLine.substr(start, i-start);
}
}
if(i < inLine.size())
std::cout << inLine[i++];
}
if(width != cWidth)
cWidth = width;
color=(color+1)%noColors;
std::cout << colors[color];
}
std::cout << std::endl;
++lineNo;
}
std::cout << c_white << std::flush;
return 0;
}
int lolfilter(std::istream &is)
{
if(width == 0)
{
#if defined _WIN32 || defined _WIN64
width = 7;
#else
width = 4;
#endif
}
if(gradient != 999)
return lolcat(is);
else if(zigzag)
return lolcat(is);
#if defined _WIN32 || defined _WIN64
#else
else if(!(rand()%3))
{
zigzag = 1;
return lolcat(is);
}
#endif
else
{
gradient = 0.6;
posGrad = rand()%2;
}
return lolcat(is);
}
int lolmain(const int& argc, const char* argv[])
{
srand(time(NULL));
std::string param;
for(int i=1; i<argc; i++)
{
param = argv[i];
if(param == "-f")
format = 1;
else if(param.substr(0, 3) == "-g=")
{
gradient = std::strtod(param.substr(3, param.size()-3).c_str(), NULL);
if(gradient < 0)
{
posGrad = 0;
gradient = -gradient;
}
}
else if(param == "-ln")
addLineNo = 1;
else if(param == "-ps")
{
#if defined _WIN32 || defined _WIN64
lolcat_powershell();
#endif
}
else if(param.substr(0, 4) == "-tw=")
tabWidth = std::atoi(param.substr(4, param.size()-4).c_str());
else if(param.substr(0, 3) == "-w=")
width = std::atoi(param.substr(3, param.size()-3).c_str());
else if(param == "-zz")
zigzag = 1;
else if(file_exists(param))
{
std::ifstream ifs(param);
int ret_val = lolfilter(ifs);
ifs.close();
return ret_val;
}
else if(param == "-h" || param == "-help")
{
std::stringstream ss;
ss << argv[0] << " is a cross-platform c++ implementation of the original lolcat,";
ss << " which writes output to the terminal with rainbow colours" << std::endl;
ss << "=>" << " usage:" << std::endl;
ss << " command | " << argv[0] << " (options)" << std::endl;
ss << " " << argv[0] << " (options) file" << std::endl;
ss << "=>" << " options:" << std::endl;
ss << " -f" << ": format" << std::endl;
ss << " -g=[d]" << ": set gradient" << std::endl;
ss << " -ln" << ": add line numbers" << std::endl;
ss << " -ps" << ": powershell colors" << std::endl;
ss << " -tw=[i]" << ": set tab width" << std::endl;
ss << " -v" << ": print version" << std::endl;
ss << " -w=[i]" << ": set width" << std::endl;
ss << " -zz" << ": +/- gradient" << std::endl;
return lolfilter(ss);
}
else if(param == "-v" || param == "-version")
{
std::stringstream ss;
ss << "v1.0.0" << std::endl;
return lolfilter(ss);
}
else
{
std::cout << "error: " << argv[0] << ": ";
std::stringstream ss;
ss << "do not recognise '" << param << "'" << std::endl;
return lolfilter(ss);
}
}
return lolfilter(std::cin);
}