-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterminal.cpp
More file actions
188 lines (162 loc) · 4.06 KB
/
terminal.cpp
File metadata and controls
188 lines (162 loc) · 4.06 KB
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
#include "terminal.hpp"
#include <sys/select.h>
#include <chrono>
#include <csignal>
#include <cstdlib>
#include "draw_warning.hpp"
std::function<void()> Terminal::onSizeChange = nullptr;
Terminal::Terminal() {
tcgetattr(STDIN_FILENO, &original_termios_);
enableAltBuffer();
mvXY(0, 0);
fflush(stdout);
if (isKittyKeyboardSupported()) {
kittyKeyboardSupported_ = true;
}
if (signal(SIGWINCH, signalHandler) == SIG_ERR) {
return;
}
if (ioctl(0, TIOCGWINSZ, &dim_) == -1) {
dim_.ws_col = -1;
dim_.ws_row = -1;
}
if (signal(SIGINT, signalHandler) == SIG_ERR) {
return;
}
return;
}
Terminal& Terminal::getInstance() {
static Terminal tty;
return tty;
}
int Terminal::getWidth() { return getInstance().dim_.ws_col; }
int Terminal::getHeight() { return getInstance().dim_.ws_row; }
void Terminal::clear() { printf("\033[2J\033[H"); }
void Terminal::signalHandler(int signal) {
Terminal& term = Terminal::getInstance();
if (signal == SIGWINCH) {
ioctl(STDIN_FILENO, TIOCGWINSZ, &term.dim_);
term.pauseIfTooSmall();
onSizeChange();
return;
}
if (signal == SIGINT) {
term.disableAltBuffer();
term.showCursor();
term.disableRawMode();
exit(1);
return;
}
}
void Terminal::enableRawMode() {
struct termios raw = original_termios_;
raw.c_lflag &= ~(ICANON | ECHO);
raw.c_iflag &= ~(ICRNL | INLCR | IGNCR | IXON | IXOFF);
raw.c_oflag &= ~(OPOST);
raw.c_cflag |= (CS8);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void Terminal::disableRawMode() {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios_);
}
void Terminal::echo(bool enable) {
struct termios attr;
tcgetattr(STDIN_FILENO, &attr);
if (enable) {
attr.c_lflag |= ECHO;
} else {
attr.c_lflag &= ~ECHO;
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr);
}
void Terminal::canonical(bool enable) {
struct termios attr;
tcgetattr(STDIN_FILENO, &attr);
if (enable) {
attr.c_lflag |= ICANON;
} else {
attr.c_lflag &= ~ICANON;
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr);
}
void Terminal::setMinSize(int minCols, int minRows) {
this->minRows_ = minRows;
this->minCols_ = minCols;
this->pauseIfTooSmall();
return;
}
void Terminal::pauseIfTooSmall() {
if (minRows_ < 0 || minCols_ < 0) {
paused_ = false;
return;
}
if (dim_.ws_row < minRows_ || dim_.ws_col < minCols_) {
paused_ = true;
clear();
drawResizeWarning();
} else {
paused_ = false;
clear();
}
fflush(stdout);
}
bool Terminal::isPaused() const { return paused_; }
bool Terminal::isBigEnough() const { return !paused_; }
Terminal::~Terminal() {
Terminal::getInstance().showCursor();
showCursor();
disableRawMode();
echo(true);
disableAltBuffer();
disableRawMode();
}
bool Terminal::isKittyKeyboardSupported(double timeoutSec) {
enableRawMode();
write(STDOUT_FILENO, "\x1b[?u\x1b[c", 7);
auto start = std::chrono::steady_clock::now();
char buf[256];
bool supported = false;
while (true) {
auto elapsed =
std::chrono::duration<double>(std::chrono::steady_clock::now() - start)
.count();
if (elapsed > timeoutSec) {
supported = false;
break;
}
fd_set set;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
timeval tv{};
tv.tv_sec = 0;
tv.tv_usec = (long)50 * 1000;
if (select(STDIN_FILENO + 1, &set, nullptr, nullptr, &tv) > 0) {
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
if (n <= 0) continue;
for (ssize_t i = 0; i + 2 < n; ++i) {
if (buf[i] == '\x1b' && buf[i + 1] == '[' && buf[i + 2] == '?') {
ssize_t p = i + 3;
bool digit = false;
while (p < n && std::isdigit(buf[p])) {
digit = true;
++p;
}
if (digit && p < n) {
if (buf[p] == 'u') {
supported = true;
goto done;
} else if (buf[p] == 'c') {
supported = false;
goto done;
}
}
}
}
}
}
done:
disableRawMode();
return supported;
}