-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlolcat.cc
261 lines (217 loc) · 6.87 KB
/
lolcat.cc
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
#include <algorithm>
#include <exception>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <cstdint>
#include <vector>
#include <cerrno>
#include <cstring>
#include <sys/time.h>
class UnknownArgumentException : public std::exception {
public:
UnknownArgumentException(std::string argument) {
std::stringstream ss;
ss << "unknown argument: " << argument;
this->m_message = ss.str();
}
const char * what () const throw () {
return this->m_message.c_str();
}
private:
std::string m_message;
};
namespace flag {
enum FlagSetElementType {
Boolean,
Double,
String,
};
struct FlagSetElement {
std::shared_ptr<void> Value;
// TODO use the default value
std::shared_ptr<void> DefaultValue;
std::string Usage;
FlagSetElementType Type;
bool HasArg;
};
// Each 'type' has a way to cleanup, a way to create one, and a case block in
// SetArg
class FlagSet {
public:
FlagSet() {}
~FlagSet() {}
std::shared_ptr<std::string> String(std::string name, std::string value = "", std::string usage = "") {
auto fv = this->Argument<std::string>(name, value, usage, FlagSetElementType::String);
this->m_arguments[name].HasArg = true;
return fv;
}
std::shared_ptr<double> Double(std::string name, double value = 0.0, std::string usage = "") {
auto fv = this->Argument<double>(name, value, usage, FlagSetElementType::Double);
this->m_arguments[name].HasArg = true;
return fv;
}
std::shared_ptr<bool> Boolean(std::string name, bool value = false, std::string usage = "") {
auto fv = this->Argument<bool>(name, value, usage, FlagSetElementType::Boolean);
this->m_arguments[name].HasArg = false;
return fv;
}
template<typename T>
std::shared_ptr<T> Argument(std::string name, T value, std::string usage, FlagSetElementType type) {
FlagSetElement element;
std::shared_ptr<T> flagValue = std::make_shared<T>();
std::shared_ptr<T> defaultValue = std::make_shared<T>(value);
element.Value = std::static_pointer_cast<void>(flagValue);
element.DefaultValue = std::static_pointer_cast<void>(defaultValue);
element.Type = type;
element.Usage = usage;
this->m_arguments[name] = element;
return flagValue;
}
std::vector<std::string> Convert(std::size_t argc, char** argv) {
std::vector<std::string> arguments(argc - 1);
for (std::size_t i = 0; i < argc - 1; i++) {
arguments[i] = std::string(argv[i+1]);
}
return arguments;
}
void Parse(std::vector<std::string> arguments) {
for (std::size_t i = 0; i < arguments.size(); i++) {
std::string arg = arguments[i];
if (arg[0] == '-' && arg[1] == '-') {
std::string name = arg.substr(2);
if (this->m_arguments.count(name) == 0) {
std::cout << "name has no pre: " << name << std::endl;
throw UnknownArgumentException(arg);
}
if (this->m_arguments[name].HasArg) {
this->SetArg(name, arguments[i+1]);
i++;
} else {
this->RunArg(name);
}
} else {
throw UnknownArgumentException(arg);
}
}
}
private:
void RunArg(std::string name) {
switch (this->m_arguments[name].Type) {
case FlagSetElementType::Boolean:
{
std::shared_ptr<bool> target = std::static_pointer_cast<bool>(this->m_arguments[name].Value);
*target = true;
}
break;
default:
// TODO change this to a better exception
throw std::logic_error("Unknown FlagSetElementType");
}
}
void SetArg(std::string name, std::string value) {
switch (this->m_arguments[name].Type) {
case FlagSetElementType::String:
{
std::shared_ptr<std::string> target = std::static_pointer_cast<std::string>(
this->m_arguments[name].Value);
*target = value;
}
break;
case FlagSetElementType::Double:
{
std::shared_ptr<double> target = std::static_pointer_cast<double>(
this->m_arguments[name].Value);
*target = std::stod(value);
}
break;
default:
// TODO change this to a better exception
throw std::logic_error("Unknown FlagSetElementType");
}
}
private:
std::map<std::string, FlagSetElement> m_arguments;
};
}
struct Options {
std::shared_ptr<double> horizontal;
std::shared_ptr<double> vertical;
std::shared_ptr<bool> force;
};
Options ParseArgs(int argc, char **argv) {
auto fs = flag::FlagSet();
Options options = {
.horizontal = fs.Double("horizontal"),
.vertical = fs.Double("vertical"),
.force = fs.Boolean("force"),
};
auto arguments = fs.Convert(argc, argv);
fs.Parse(arguments);
return options;
}
// TODO: C++ify
void find_escape_sequences(int 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;
}
}
#define ARRAY_SIZE(foo) (sizeof(foo)/sizeof(foo[0]))
const uint8_t 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
};
int main(int argc, char **argv) {
Options options;
try {
options = ParseArgs(argc, argv);
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
// Enter 'c'
double freq_h = 0.23, freq_v = 0.1;
struct timeval tv;
gettimeofday(&tv, NULL);
double offx = (tv.tv_sec % 300) / 300.0;
FILE *f = stdin;
int escape_state =0;
int i = 0;
int l = 0;
wint_t c = 0;
int cc = -1;
while ((c = fgetwc(f)) != WEOF) {
find_escape_sequences(c, &escape_state);
if (escape_state == 0) {
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) {
printf("\033[38;5;%hhum", codes[(cc = ncc) % ARRAY_SIZE(codes)]);
}
}
}
printf("%lc", c);
if (escape_state == 2) {
printf("\033[38;5;%hhum", codes[cc % ARRAY_SIZE(codes)]);
}
}
printf("\033[0m");
cc = -1;
fclose(f);
if (ferror(f)) {
std::cerr << "Error reading stdin: " << std::strerror(errno) << std::endl;
return 2;
}
return 0;
}