-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.linux.cpp
executable file
·267 lines (218 loc) · 5.43 KB
/
console.linux.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
//Linux
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <ncursesw/ncurses.h>
#include <sys/ioctl.h>
#include <string>
#include <string.h>
#include "console.h"
//Define constructor
console::constructor console::cons;
winsize console::w;
color_t console::_activeColor;
bool console::ready;
bool console::useRefresh;
console::constructor::constructor() {
useRefresh = true;
initscr();
curs_set(0);
set_escdelay(0);
setlocale(LC_ALL, "");
start_color();
if (can_change_color()) {
int t = 500;
if (COLORS < 256)
t = 1000;
init_color(COLOR_BLACK, 0,0,0);
init_color(COLOR_RED,t,0,0);
init_color(COLOR_GREEN,0,t,0);
init_color(COLOR_YELLOW,t,t,0);
init_color(COLOR_BLUE,0,0,t);
init_color(COLOR_MAGENTA,t,0,t);
init_color(COLOR_CYAN,0,t,t);
init_color(COLOR_WHITE,t,t,t);
}
auto map_color = [](int i) {
if (COLORS > 255)
;//return i;
switch (i) {
case COLOR_BLUE: return COLOR_RED;
case COLOR_RED: return COLOR_BLUE;
case 0b1000 | COLOR_BLUE: return 0b1000 | COLOR_RED;
case 0b1000 | COLOR_RED: return 0b1000 | COLOR_BLUE;
default: return i;
}
};
if (COLORS > 255) {
for (int i = 0; i < 256; i++) {
init_pair(i + 1, map_color(i % 16), map_color((i / 16) % 16));
}
} else {
for (int i = 0; i < 64; i++) {
init_pair(i + 1, map_color(i % 8), map_color((i / 8) % 8));
}
}
//nocbreak(); //Disable ESC sequence checking. Might use timeout(0) exclusively for input
//nocbreak and raw used simultaniously
//otherwise use cbreak exclusively
//raw();
//Do not use raw, it is shit
cbreak();
noecho();
keypad(stdscr, true);
console::_activeColor = -1;
ready = true;
}
console::constructor::~constructor() {
endwin();
ready = false;
}
int console::getImage() {
return IMAGE_LINUX;
}
int console::readKey() {
timeout(-1);
// wint_t c;
// get_wch(&c);
return getch();
// return c;
}
int console::readKeyAsync() {
timeout(0);
int key = getch();
if (key == -1)
key = 0;
return key;
}
void console::sleep(int millis) {
usleep(millis * 1000);
}
void console::_refreshSize() {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
}
int console::getConsoleWidth() {
console::_refreshSize();
return int(w.ws_col);
}
int console::getConsoleHeight() {
console::_refreshSize();
return int(w.ws_row);
}
void console::setConsoleColor(color_t color) {
const auto getCursesColor = [](color_t i) {
short fg = i & 0b00000111, bg = i & 0b01110000;
int c = 0;
bg >>= 4;
c = bg * 8;
c += fg;
return c + 1;
};
const auto truncateColor = [](color_t i) {
int fg = i & 0b00000111, bg = i & 0b01110000;
return (bg >> 1) | fg;
};
if (COLORS < 256)
color = truncateColor(color);
if (console::_activeColor == color)
return;
//if (console::_activeColor != -1)
//attroff(COLOR_PAIR(getCursesColor(color)));
//attroff(COLOR_PAIR((color & 0b00001111) + ((color & 0b11110000)) + 1));
//attroff(COLOR_PAIR((console::_activeColor & 0b00001111) + ((console::_activeColor & 0b11110000)) + 1));
attroff(COLOR_PAIR(console::_activeColor + 1));
//attron(COLOR_PAIR((color & 0b00001111) + ((color & 0b11110000)) + 1));
attron(COLOR_PAIR(color + 1));
//attron(COLOR_PAIR(getCursesColor(color)));
console::_activeColor = color;
}
void console::clear() {
wclear(getwin(stdout));
refresh();
}
void console::setCursorLeft(int x) {}
void console::setCursorTop(int y) {}
void console::setCursorPosition(int x, int y) {
move(y,x);
}
void console::write(int x, int y, std::string& str) {
int w = console::getConsoleWidth();
int h = console::getConsoleHeight();
if (y >= h || x >= w)
return;
for (int i = 0; i < str.length() && i + x < w; i++) {
mvaddch(y, x + i, str[i]);
}
if (useRefresh)
refresh();
}
void console::write(int x, int y, const char* str) {
console::write(x, y, str, _activeColor);
}
void console::write(int x, int y, const char* str, color_t c) {
int w = console::getConsoleWidth();
int h = console::getConsoleHeight();
int l = strlen(str);
if (y >= h || x >= w)
return;
for (int i = 0; i < l && i + x < w; i++) {
setConsoleColor(c);
mvaddch(y, x + i, str[i]);
}
if (useRefresh)
refresh();
}
void console::write(int x, int y, std::string& str, color_t c) {
int w = console::getConsoleWidth();
int h = console::getConsoleHeight();
if (y >= h || x >= w)
return;
for (int i = 0; i < str.length() && i + x < w; i++) {
setConsoleColor(c);
mvaddch(y, x + i, str[i]);
}
if (useRefresh)
refresh();
}
void console::write(char* fb, color_t* cb, int length) {
int i = 0;
int w = console::getConsoleWidth();
int h = console::getConsoleHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
console::setConsoleColor(cb[i]);
mvaddch(y, x, fb[i++]);
}
}
if (useRefresh)
refresh();
}
void console::write(wchar_t* fb, color_t* cb, int length) {
int i = 0;
int w = getConsoleWidth();
int h = getConsoleHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
setConsoleColor(cb[i]);
mvaddnwstr(y, x, &fb[i++], 1);
}
}
if (useRefresh)
refresh();
}
void console::write(int x, int y, wchar_t character) {
mvaddnwstr(y, x, &character, 1);
refresh();
}
void console::write(int x, int y, wchar_t character, color_t color) {
console::setConsoleColor(color);
console::write(x, y, character);
}
void console::write(int x, int y, char character) {
mvaddch(y, x, character);
refresh();
}
void console::write(int x, int y, char character, color_t color) {
console::setConsoleColor(color);
console::write(x, y, character);
}