-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclitl.hpp
533 lines (476 loc) · 16.9 KB
/
clitl.hpp
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifndef __CLITL_HPP__
#define __CLITL_HPP__
#include <cstddef>
#include <cstdio>
#include <locale>
#include <ostream>
#include <string>
#include <utility>
#ifdef UNIX
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#elif WIN32
#include <conio.h>
#include <Windows.h>
#endif
namespace clitl {
/* Color class */
enum class color {
#ifdef UNIX
DEFAULT = 0,
BLACK = 0,
RED = 1,
GREEN = 2,
BROWN = 3,
BLUE = 4,
MAGENTA = 5,
CYAN = 6,
WHITE = 7,
#elif WIN32
DEFAULT = -0x1,
BLACK = 0x0,
RED = 0x4,
GREEN = 0x2,
BROWN = 0x7, // Worthless
BLUE = 0x1,
MAGENTA = 0x7, // Worthless
CYAN = 0x9,
WHITE = 0x7,
#endif
};
/* Basic definitions and containers */
typedef int coord_t;
//typedef int colornum_t;
template <typename coordT, typename charT,
typename traits = std::char_traits<charT>, typename Alloc = std::allocator<charT> >
class basic_cli_object {
std::pair<coordT, coordT> origin;
std::pair<coordT, coordT> endpoint;
std::basic_string<charT, traits, Alloc> str;
color foreground;
color background;
public:
explicit basic_cli_object(
const std::pair<coordT, coordT>& origin = std::pair<coordT, coordT>(0, 0),
const std::pair<coordT, coordT>& endpoint = std::pair<coordT, coordT>(0, 0),
const std::basic_string<charT, traits, Alloc>& str
= std::basic_string<charT, traits, Alloc>(),
const color& foreground = color::DEFAULT,
const color& background = color::DEFAULT)
: origin(origin), endpoint(endpoint), str(str),
foreground(foreground), background(background) {}
const basic_cli_object<coordT, charT, traits, Alloc>&
set_origin(const std::pair<coordT, coordT>& coord)
{
origin = coord;
return *this;
}
const basic_cli_object<coordT, charT, traits, Alloc>&
set_endpoint(const std::pair<coordT, coordT>& coord)
{
endpoint = coord;
return *this;
}
const basic_cli_object<coordT, charT, traits, Alloc>&
set_string(const std::basic_string<charT, traits, Alloc>& stri)
{
str = stri;
return *this;
}
const basic_cli_object<coordT, charT, traits, Alloc>&
set_foreground(const color& foreg)
{
foreground = foreg;
return *this;
}
const basic_cli_object<coordT, charT, traits, Alloc>&
set_background(const color& backg)
{
background = backg;
return *this;
}
const std::pair<coordT, coordT>& get_origin() const
{
return origin;
}
const std::pair<coordT, coordT>& get_endpoint() const
{
return endpoint;
}
const std::basic_string<charT, traits, Alloc>& get_string() const
{
return str;
}
const color& get_foreground() const
{
return foreground;
}
const color& get_background() const
{
return background;
}
bool check_cover
(const basic_cli_object<coordT, charT, traits, Alloc>& bco) const
{
if (this->origin.first <= bco.origin.first
&& this->endpoint.first >= bco.endpoint.first
&& this->origin.second <= bco.origin.second
&& this->endpoint.second >= bco.endpoint.second)
return true;
return false;
}
bool operator&&
(const basic_cli_object<coordT, charT, traits, Alloc>& bco) const
{
if (this->origin.first <= bco.endpoint.first
&& this->endpoint.first >= bco.origin.first
&& this->origin.second <= bco.endpoint.second
&& this->endpoint.second >= bco.origin.second)
return true;
return false;
}
basic_cli_object<coordT, charT, traits, Alloc> operator+
(const basic_cli_object<coordT, charT, traits, Alloc>& bco) const
{
return basic_cli_object<coordT, charT, traits, Alloc>
(std::pair<coordT, coordT>
(this->origin + bco.origin, this->endpoint + bco.endpoint));
}
};
template <typename coordT = coord_t, typename charT = char,
typename traits = std::char_traits<charT>, typename Alloc = std::allocator<charT> >
class rect : public basic_cli_object<coordT, charT, traits, Alloc> {
public:
explicit rect(
const std::pair<coordT, coordT>& origin = std::pair<coordT, coordT>(1, 1),
const std::pair<coordT, coordT>& endpoint = std::pair<coordT, coordT>(1, 1),
const color& background = color::WHITE)
: basic_cli_object<coordT, charT, traits, Alloc>(origin, endpoint,
std::basic_string<charT, traits, Alloc>(" "), color::DEFAULT, background) {}
};
template <typename coordT = coord_t, typename charT = char,
typename traits = std::char_traits<charT>, typename Alloc = std::allocator<charT> >
class coloredstring : public basic_cli_object<coordT, charT, traits, Alloc> {
public:
explicit coloredstring(
const std::pair<coordT, coordT>& origin = std::pair<coordT, coordT>(1, 1),
const std::basic_string<charT, traits, Alloc>& str
= std::basic_string<charT, traits, Alloc>(),
const color& foreground = clitl::color::DEFAULT,
const color& background = clitl::color::DEFAULT)
: basic_cli_object<coordT, charT, traits, Alloc>(
origin, std::pair<coordT, coordT>(origin.first + str.length() - 1, origin.second),
str, foreground, background) {}
explicit coloredstring(
const std::basic_string<charT, traits, Alloc>& str
= std::basic_string<charT, traits, Alloc>(),
const color& foreground = clitl::color::DEFAULT,
const color& background = clitl::color::DEFAULT)
: basic_cli_object<coordT, charT, traits, Alloc>(
std::pair<coordT, coordT>(1, 1), std::pair<coordT, coordT>(1 + str.length() - 1, 1),
str, foreground, background) {}
};
/* Stream buffer */
template <typename charT, typename traits = std::char_traits<charT> >
class basic_streambuf : public std::basic_streambuf<charT, traits> {
protected:
static const int buffer_size = 2;
char buffer[buffer_size];
public:
basic_streambuf()
{
this->setg(buffer, buffer, buffer);
}
protected:
typename traits::int_type overflow(typename traits::int_type c)
{
if (std::putchar(c) == traits::eof()) {
return traits::eof();
}
return traits::not_eof(c);
}
typename traits::int_type underflow()
{
static const int buffer_size_allocated = 2;
if (this->gptr() < this->egptr()) {
return traits::to_int_type(*(this->gptr()));
}
#ifdef UNIX
buffer[0] = std::getchar();
#elif WIN32
if (_kbhit()) {
buffer[0] = static_cast<char>(_getch());
}
else {
buffer[0] = traits::eof();
}
#endif
if (buffer[1] != '\n') {
buffer[1] = '\n';
}
this->setg(buffer, buffer, buffer + buffer_size_allocated);
return traits::to_int_type(*(this->gptr()));
}
};
typedef basic_streambuf<char> streambuf;
typedef basic_streambuf<wchar_t> wstreambuf;
/* Output stream */
template <typename charT,
typename traits = std::char_traits<charT>, typename coordT = coord_t >
class basic_ostream : public std::basic_ostream<charT, traits> {
public:
#ifdef UNIX
struct winsize wsize;
struct termios regulartset = { 0, };
struct termios newtset = { 0, };
#endif
#ifdef WIN32
HANDLE termout_handle;
CONSOLE_CURSOR_INFO termout_curinfo;
CONSOLE_SCREEN_BUFFER_INFO termout_sbufinfo;
CONSOLE_SCREEN_BUFFER_INFO termout_initial_sbufinfo;
#endif
explicit basic_ostream(basic_streambuf<charT, traits>* sb)
: std::basic_ostream<charT, traits>(sb)
{
#ifdef UNIX
wsize = { 0 };
#elif WIN32
termout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(termout_handle, &termout_initial_sbufinfo);
#endif
}
basic_ostream<charT, traits, coordT>& movecursor(coordT xpos, coordT ypos)
{
#ifdef UNIX
*this << "\033[" << ypos << ";" << xpos << "f";
#elif WIN32
COORD pos = { static_cast<SHORT>(xpos) - 1, static_cast<SHORT>(ypos) - 1 };
SetConsoleCursorPosition(termout_handle, pos);
#endif
return *this;
}
basic_ostream<charT, traits, coordT>& movecursor(const std::pair<coordT, coordT>& coord)
{
movecursor(coord.first, coord.second);
return *this;
}
std::pair<coordT, coordT> screensize()
{
static coordT column;
static coordT row;
#ifdef UNIX
column = wsize.ws_col;
row = wsize.ws_row;
#elif WIN32
/* Real code
GetConsoleScreenBufferInfo(termout_handle, &termout_sbufinfo);
column = static_cast<coordT>(termout_sbufinfo.dwSize.X);
row = static_cast<coordT>(termout_sbufinfo.dwSize.Y);
*/
column = 110;
row = 28;
#endif
return std::pair<coordT, coordT>(column, row);
}
basic_ostream<charT, traits, coordT>& paintmode(color fgd, color bgd)
{
#ifdef UNIX
*this << "\033["
<< 30 + static_cast<int>(fgd)
<< ";"
<< 40 + static_cast<int>(bgd)
<< "m";
#elif WIN32
if (color::DEFAULT == fgd) {
fgd = static_cast<color>(termout_initial_sbufinfo.wAttributes % 0x10);
}
if (color::DEFAULT == bgd) {
bgd = static_cast<color>((termout_initial_sbufinfo.wAttributes / 0x10) % 0x10);
}
SetConsoleTextAttribute(this->termout_handle,
static_cast<WORD>(static_cast<int>(fgd)
+ 0x10 * static_cast<int>(bgd)));
#endif
return *this;
}
basic_ostream<charT, traits, coordT>& paintmode(nullptr_t nullp)
{
// Recovery mode
#ifdef UNIX
*this << "\033[0m";
#elif WIN32
SetConsoleTextAttribute(termout_handle, termout_initial_sbufinfo.wAttributes);
#endif
return *this;
}
basic_ostream<charT, traits, coordT>& draw
(const basic_cli_object<coordT, charT>& bco)
{
this->movecursor(bco.get_origin());
this->paintmode(bco.get_foreground(), bco.get_background());
*this << bco.get_string().c_str();
return *this;
}
basic_ostream<charT, traits, coordT>& operator<<
(basic_ostream<charT, traits, coordT>& (*op)(basic_ostream<charT, traits, coordT>&))
{
return (*op)(*this);
}
};
typedef basic_ostream<char> ostream;
typedef basic_ostream<wchar_t> wostream;
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
alternative_system_screenbuffer(basic_ostream<charT, traits, coordT>& os)
{
#if UNIX
os << "\033[?1049h"; // Use alternate screen buffer
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
clear(basic_ostream<charT, traits, coordT>& os)
{
#ifdef UNIX
os << "\033[2J";
#elif WIN32
COORD startpoint = { 0, 0 };
DWORD dw;
GetConsoleScreenBufferInfo(os.termout_handle, &os.termout_sbufinfo);
FillConsoleOutputCharacterA(os.termout_handle, ' ',
os.termout_sbufinfo.dwSize.X * os.termout_sbufinfo.dwSize.Y,
startpoint, &dw);
FillConsoleOutputAttribute(os.termout_handle,
os.termout_initial_sbufinfo.wAttributes,
os.termout_sbufinfo.dwSize.X * os.termout_sbufinfo.dwSize.Y,
startpoint, &dw);
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
disable_echo(basic_ostream<charT, traits, coordT>& os)
{
#ifdef UNIX
tcgetattr(0, ®ulartset); // Get current attribution
newtset = regulartset; // Substitute
newtset.c_lflag &= ~ICANON; // Set noncanonical mode
newtset.c_lflag &= ~ECHO; // Turn off the echo
newtset.c_cc[VTIME] = 0; // Zero delay time
newtset.c_cc[VMIN] = 0; // Don't need any buffer delay
tcsetattr(0, TCSANOW, &newtset); // Apply new setting
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
enable_echo(basic_ostream<charT, traits, coordT>& os)
{
#ifdef UNIX
tcsetattr(0, TCSANOW, ®ulartset); // Apply the original setting
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
hide_cursor(basic_ostream<charT, traits, coordT>& os)
{
#ifdef UNIX
os << "\033[?25l"; // Hide cursor
#elif WIN32
GetConsoleCursorInfo(os.termout_handle, &os.termout_curinfo);
os.termout_curinfo.bVisible = 0;
SetConsoleCursorInfo(os.termout_handle, &os.termout_curinfo);
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
normal_system_screenbuffer(basic_ostream<charT, traits, coordT>& os)
{
#if UNIX
os << "\033[?1049l"; // Use normal screen buffer
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
refresh(basic_ostream<charT, traits, coordT>& os)
{
os << std::flush;
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
show_cursor(basic_ostream<charT, traits, coordT>& os)
{
#if UNIX
os << "\033[?25h"; // Show cursor
#elif WIN32
GetConsoleCursorInfo(os.termout_handle, &os.termout_curinfo);
os.termout_curinfo.bVisible = 1;
SetConsoleCursorInfo(os.termout_handle, &os.termout_curinfo);
#endif
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
pre_process(basic_ostream<charT, traits, coordT>& os)
{
os << alternative_system_screenbuffer;
os << hide_cursor;
os << disable_echo;
os << clear;
return os;
}
template <typename charT, typename traits, typename coordT>
basic_ostream<charT, traits, coordT>&
post_process(basic_ostream<charT, traits, coordT>& os)
{
os << clear;
os << show_cursor;
os << enable_echo;
os.movecursor(1, 1);
os.paintmode(nullptr);
os << normal_system_screenbuffer;
return os;
}
template <typename coordT, typename charT, typename traits, typename Alloc>
basic_ostream<charT, traits, coordT>& operator<<
(basic_ostream<charT, traits, coordT>& os,
const coloredstring<coordT, charT, traits, Alloc>& cs)
{
os.draw(cs);
return os;
}
template <typename coordT, typename charT, typename traits, typename Alloc>
basic_ostream<charT, traits, coordT>& operator<<
(basic_ostream<charT, traits, coordT>& os, const basic_cli_object<coordT, charT, traits, Alloc>& re)
{
static basic_cli_object<coordT, charT, traits, Alloc> temprec;
for (int i = re.get_origin().first; i <= re.get_endpoint().first; ++i) {
for (int j = re.get_origin().second; j <= re.get_endpoint().second; ++j) {
temprec = re;
temprec.set_origin(std::pair<coordT, coordT>(i, j));
os.draw(temprec);
}
}
return os;
}
/* Input stream */
template <typename charT,
typename traits = std::char_traits<charT>, typename coordT = coord_t >
class basic_istream : public std::basic_istream<charT, traits> {
public:
explicit basic_istream(basic_streambuf<charT, traits>* sb)
: std::basic_istream<charT, traits>(sb)
{
}
};
typedef basic_istream<char> istream;
typedef basic_istream<wchar_t> wistream;
}
#endif