/
console.hh
58 lines (48 loc) · 1.31 KB
/
console.hh
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
#pragma once
#include <cstdlib>
#include <iostream>
#include <curses.h>
#include <boost/noncopyable.hpp>
class ConsoleWindow: boost::noncopyable {
public:
ConsoleWindow() {
if ((window = initscr()) == NULL) {
std::cerr << "-!- Error initializing ncurses." << std::endl;
exit(1);
}
oldcur = curs_set(0);
if (COLS < 80 || LINES < 24) {
cleanup();
std::cerr << "-!- Error with console size:" << std::endl;
std::cerr << " COLS: " << COLS << ", minimum 80" << std::endl;
std::cerr << " LINES: " << LINES << ", minimum 24" << std::endl;
exit(2);
}
cbreak(); // No line buffering
noecho(); // Turn off key echoing
keypad(window, true); // Enable the keypad for non-char keys
start_color(); // Initialize colours
if (has_colors() && COLOR_PAIRS >= 16 && COLORS >= 8) {
use_default_colors();
for (int i = 1; i < 8; i++) init_pair(i, i, -1);
} else {
cleanup();
std::cerr << "-!- Error initializing colors:" << std::endl;
std::cerr << " has_colors: " << has_colors() << std::endl;
std::cerr << " COLOR_PAIRS: " << COLOR_PAIRS << std::endl;
std::cerr << " COLORS: " << COLORS << std::endl;
exit(3);
}
}
~ConsoleWindow() {
cleanup();
}
void cleanup() {
delwin(window);
curs_set(oldcur);
endwin();
}
private:
WINDOW* window;
int oldcur;
};