-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextIO.h
111 lines (94 loc) · 2.05 KB
/
TextIO.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
104
105
106
107
108
109
110
111
#ifndef TEXTIO_H_
#define TEXTIO_H_
#ifndef _MSC_VER
#include <curses.h> // https://www.linuxjournal.com/content/getting-started-ncurses
#else
#include "curses.h"
#endif
#include <string>
const int CTRL_D = 'D' - 'A' + 1;
const int CTRL_S = 'S' - 'A' + 1;
const int CTRL_L = 'L' - 'A' + 1;
const int CTRL_X = 'X' - 'A' + 1;
const int CTRL_Z = 'Z' - 'A' + 1;
class TextIO {
public:
TextIO(int fgcolor, int bgcolor, int hilite) {
initscr();
start_color();
cbreak();
noecho();
raw();
init_pair(COLOR::WHITE, fgcolor, bgcolor);
init_pair(COLOR::RED, hilite, bgcolor);
keypad(stdscr, TRUE);
refresh();
}
~TextIO() {
echo();
endwin();
}
static void clear() {
::clear();
}
enum COLOR {
WHITE = COLOR_WHITE,
RED = COLOR_RED
};
static void print(char ch, COLOR fcolor = COLOR::WHITE) {
attron(COLOR_PAIR(fcolor));
addch(ch);
}
static void print(const std::string& s, COLOR fcolor = COLOR::WHITE) {
attron(COLOR_PAIR(fcolor));
addstr(s.c_str());
}
static void refresh() {
::refresh();
}
static void move(int row, int col) {
::move(row, col);
}
/*
key code description
KEY_DOWN The four arrow keys ...
KEY_UP
KEY_LEFT
KEY_RIGHT
KEY_PPAGE Previous page
KEY_NPAGE Next page
KEY_HOME Home key
KEY_BACKSPACE Backspace
KEY_F(n) Function keys, for 0 <= n >= 63
KEY_DC Delete character
KEY_IC Insert char or enter insert mode
KEY_ENTER Enter or send
*/
static int getChar() {
int ch = 0;
ch = getch();
#ifdef _MBCS
const int kEnter = '\r';
const int kBackspace = '\b';
#else
const int kBackspace = 127;
const int kEnter = '\n';
#endif
if (ch == kBackspace)
return KEY_BACKSPACE;
if (ch == kEnter)
return KEY_ENTER;
return ch;
}
static void getString(std::string& str) {
const int kMaxFilenameLength = 1024;
char temp[kMaxFilenameLength] = "";
echo();
getnstr(temp, kMaxFilenameLength);
noecho();
str = temp;
}
private:
static const int kDefaultPair = 1;
};
#endif // TEXTIO_H_