-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlines.cpp
377 lines (332 loc) · 7.2 KB
/
lines.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "door.h"
#include "utf8.h"
/**
* @file
* @brief Line
*/
namespace door {
#ifdef EXPERIMENTAL
BasicLine::BasicLine(std::string txt) : text{txt}, hasColor{false} {}
BasicLine::BasicLine(std::string txt, ANSIColor c)
: text{txt}, hasColor{true}, color{c} {}
bool BasicLine::hasRender(void) {
if (render)
return true;
return false;
}
void BasicLine::setText(std::string txt) { text = txt; }
void BasicLine::setColor(ANSIColor c) {
color = c;
hasColor = true;
}
void BasicLine::setRender(renderFunction rf) { render = rf; }
void BasicLine::setUpdater(updateFunction uf) { updater = uf; }
/**
* Update BasicLine, if we have an updater.
*
* If we have an updater, call it. If the text is different,
* update setText() and return true.
* Otherwise false.
*
* This doesn't detect changes (like if the render has been changed, for
* example)
*
* @return bool
*/
bool BasicLine::update(void) {
if (updater) {
std::string temp = updater();
if (temp == text)
return false;
setText(temp);
return true;
}
return false;
}
/**
* Output Line
*
* This looks for padding and paddingColor.
* This uses the render function if set.
*
* @param os std::ostream
* @param l const BasicLine &
* @return std::ostream&
*/
std::ostream &operator<<(std::ostream &os, const BasicLine &l) {
if (l.render) {
// This has a renderer. Use it.
Render r = l.render(l.text);
r.output(os);
} else {
if (l.hasColor) {
os << l.color;
};
os << l.text;
}
return os;
}
MultiLine::MultiLine(){};
void MultiLine::append(std::shared_ptr<BasicLine> bl) { lines.push_back(bl); }
bool MultiLine::update() {
bool updated = false;
for (auto line : lines) {
if (line->update())
updated = true;
}
return updated;
}
/**
* Output Line
*
* This looks for padding and paddingColor.
* This uses the render function if set.
*
* @param os std::ostream
* @param ml const MultiLine &
* @return std::ostream&
*/
std::ostream &operator<<(std::ostream &os, const MultiLine &ml) {
for (auto line : ml.lines) {
os << *line;
}
return os;
}
#endif
/**
* Construct a new Line:: Line object with
* string and total width.
*
* @param txt std::string
* @param width int
*/
Line::Line(const std::string &txt, int w) : text{txt}, width{w} {
hasColor = false;
}
Line::Line(const std::string &txt, int w, ANSIColor c)
: text{txt}, color{c}, width{w} {
hasColor = true;
}
Line::Line(const char *txt, int w, ANSIColor c)
: text{txt}, color{c}, width{w} {
hasColor = true;
}
Line::Line(const std::string &txt, int w, renderFunction rf)
: text{txt}, render{rf}, width{w} {
hasColor = false;
}
Line::Line(const char *txt, int w, renderFunction rf)
: text{txt}, render{rf}, width{w} {
hasColor = false;
}
/**
* Construct a new Line:: Line object with
* const char * and total width
*
* @param txt const char *
* @param width int
*/
Line::Line(const char *txt, int w) : text{txt}, width{w} { hasColor = false; }
/**
* Construct a new Line:: Line object from an
* existing Line
*
* @param rhs const Line&
*/
Line::Line(const Line &rhs)
: text{rhs.text}, hasColor{rhs.hasColor}, color{rhs.color},
padding{rhs.padding}, paddingColor{rhs.paddingColor} {
if (rhs.render) {
render = rhs.render;
}
if (rhs.updater) {
updater = rhs.updater;
}
width = rhs.width;
}
Line::Line(Line &&rhs)
: text{rhs.text}, hasColor{rhs.hasColor}, color{rhs.color},
padding{rhs.padding}, paddingColor{rhs.paddingColor} {
if (rhs.render)
render = rhs.render;
if (rhs.updater)
updater = rhs.updater;
width = rhs.width;
}
/**
* Has a render function been set?
*
* @return bool
*/
bool Line::hasRender(void) {
if (render) {
return true;
} else {
return false;
}
}
/**
* Return total length of Line
*
* text.length + 2 * padding length
*
* @return int
*/
int Line::length(void) {
if (!padding.empty()) {
if (unicode) {
return utf8::distance(padding.begin(), padding.end()) * 2 +
utf8::distance(text.begin(), text.end());
} else {
return padding.length() * 2 + text.length();
}
}
if (unicode) {
return utf8::distance(text.begin(), text.end());
} else {
return text.length();
}
}
/**
* Make text the given width by padding string with spaces.
*
* @param width int
*/
void Line::fit(void) {
int need;
if (door::unicode)
need = width - utf8::distance(text.begin(), text.end());
else
need = width - text.length();
need -= padding.length() * 2;
if (need > 0) {
text.append(std::string(need, ' '));
}
}
/**
* Set Line text.
* @param txt std::string
*/
void Line::setText(std::string &txt) { text = txt; }
/**
* Set Line text.
* @param txt const char *
*/
void Line::setText(const char *txt) { text = txt; }
/**
* set padding (color and text)
*
* @param padstring std::string
* @param padColor ANSIColor
*/
void Line::setPadding(std::string &padstring, ANSIColor padColor) {
padding = padstring;
paddingColor = padColor;
}
/**
* set padding (color and text)
*
* @param padstring const char *
* @param padColor ANSIColor
*/
void Line::setPadding(const char *padstring, ANSIColor padColor) {
padding = padstring;
paddingColor = padColor;
}
/**
* set color
*
* @param c ANSIColor
*/
void Line::setColor(ANSIColor c) {
color = c;
hasColor = true;
}
/**
* set render
*
* Set the renderFunction to use for this Line. This
* replaces the colorizer.
* @param rf renderFunction
*/
void Line::setRender(renderFunction rf) { render = rf; }
/**
* set updater function
*
* This can update the line text when called.
* @todo Define an updateFunction.
* @param newUpdater updateFunction
*/
void Line::setUpdater(updateFunction newUpdater) { updater = newUpdater; }
std::string Line::debug(void) {
std::string desc;
desc = "Line(";
desc += text;
desc += "): ";
if (updater) {
desc += "[U]";
}
if (render) {
desc += "[R]";
}
return desc;
}
/**
* Call updater, report if the text was actually changed.
*
* @return bool
*/
bool Line::update(void) {
if (updater) {
std::string newText = updater();
// int line_len;
int need;
if (unicode) {
// line_len = utf8::distance(text.begin(), text.end());
need = width - utf8::distance(newText.begin(), newText.end());
} else {
// line_len = text.length();
need = width - newText.length();
}
need -= padding.length() * 2;
if (need > 0) {
newText.append(std::string(need, ' '));
}
if (newText != text) {
text = newText;
return true;
}
}
return false;
}
/**
* Output Line
*
* This looks for padding and paddingColor.
* This uses the render function if set.
*
* @param os std::ostream
* @param l const Line &
* @return std::ostream&
*/
std::ostream &operator<<(std::ostream &os, const Line &l) {
// Door *d = dynamic_cast<Door *>(&os);
if (!l.padding.empty()) {
os << l.paddingColor << l.padding;
}
if (l.render) {
// This has a renderer. Use it.
Render r = l.render(l.text);
r.output(os);
} else {
if (l.hasColor) {
os << l.color;
};
os << l.text;
}
if (!l.padding.empty()) {
os << l.paddingColor << l.padding;
}
return os;
}
} // namespace door