FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
terminal_input_parser.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
5
6#include <cstdint> // for uint32_t
7#include <ftxui/component/mouse.hpp> // for Mouse, Mouse::Button, Mouse::Motion
8#include <ftxui/component/receiver.hpp> // for SenderImpl, Sender
9#include <map>
10#include <memory> // for unique_ptr, allocator
11#include <utility> // for move
12
13#include "ftxui/component/event.hpp" // for Event
14#include "ftxui/component/task.hpp" // for Task
15
16namespace ftxui {
17
18// NOLINTNEXTLINE
19const std::map<std::string, std::string> g_uniformize = {
20 // Microsoft's terminal uses a different new line character for the return
21 // key. This also happens with linux with the `bind` command:
22 // See https://github.com/ArthurSonzogni/FTXUI/issues/337
23 // Here, we uniformize the new line character to `\n`.
24 {"\r", "\n"},
25
26 // See: https://github.com/ArthurSonzogni/FTXUI/issues/508
27 {std::string({8}), std::string({127})},
28
29 // See: https://github.com/ArthurSonzogni/FTXUI/issues/626
30 //
31 // Depending on the Cursor Key Mode (DECCKM), the terminal sends different
32 // escape sequences:
33 //
34 // Key Normal Application
35 // ----- -------- -----------
36 // Up ESC [ A ESC O A
37 // Down ESC [ B ESC O B
38 // Right ESC [ C ESC O C
39 // Left ESC [ D ESC O D
40 // Home ESC [ H ESC O H
41 // End ESC [ F ESC O F
42 //
43 {"\x1BOA", "\x1B[A"}, // UP
44 {"\x1BOB", "\x1B[B"}, // DOWN
45 {"\x1BOC", "\x1B[C"}, // RIGHT
46 {"\x1BOD", "\x1B[D"}, // LEFT
47 {"\x1BOH", "\x1B[H"}, // HOME
48 {"\x1BOF", "\x1B[F"}, // END
49
50 // Variations around the FN keys.
51 // Internally, we are using:
52 // vt220, xterm-vt200, xterm-xf86-v44, xterm-new, mgt, screen
53 // See: https://invisible-island.net/xterm/xterm-function-keys.html
54
55 // For linux OS console (CTRL+ALT+FN), who do not belong to any
56 // real standard.
57 // See: https://github.com/ArthurSonzogni/FTXUI/issues/685
58 {"\x1B[[A", "\x1BOP"}, // F1
59 {"\x1B[[B", "\x1BOQ"}, // F2
60 {"\x1B[[C", "\x1BOR"}, // F3
61 {"\x1B[[D", "\x1BOS"}, // F4
62 {"\x1B[[E", "\x1B[15~"}, // F5
63
64 // xterm-r5, xterm-r6, rxvt
65 {"\x1B[11~", "\x1BOP"}, // F1
66 {"\x1B[12~", "\x1BOQ"}, // F2
67 {"\x1B[13~", "\x1BOR"}, // F3
68 {"\x1B[14~", "\x1BOS"}, // F4
69
70 // vt100
71 {"\x1BOt", "\x1B[15~"}, // F5
72 {"\x1BOu", "\x1B[17~"}, // F6
73 {"\x1BOv", "\x1B[18~"}, // F7
74 {"\x1BOl", "\x1B[19~"}, // F8
75 {"\x1BOw", "\x1B[20~"}, // F9
76 {"\x1BOx", "\x1B[21~"}, // F10
77
78 // scoansi
79 {"\x1B[M", "\x1BOP"}, // F1
80 {"\x1B[N", "\x1BOQ"}, // F2
81 {"\x1B[O", "\x1BOR"}, // F3
82 {"\x1B[P", "\x1BOS"}, // F4
83 {"\x1B[Q", "\x1B[15~"}, // F5
84 {"\x1B[R", "\x1B[17~"}, // F6
85 {"\x1B[S", "\x1B[18~"}, // F7
86 {"\x1B[T", "\x1B[19~"}, // F8
87 {"\x1B[U", "\x1B[20~"}, // F9
88 {"\x1B[V", "\x1B[21~"}, // F10
89 {"\x1B[W", "\x1B[23~"}, // F11
90 {"\x1B[X", "\x1B[24~"}, // F12
91};
92
95
97 timeout_ += time;
98 const int timeout_threshold = 50;
99 if (timeout_ < timeout_threshold) {
100 return;
101 }
102 timeout_ = 0;
103 if (!pending_.empty()) {
104 Send(SPECIAL);
105 }
106}
107
109 pending_ += c;
110 timeout_ = 0;
111 position_ = -1;
112 Send(Parse());
113}
114
115unsigned char TerminalInputParser::Current() {
116 return pending_[position_];
117}
118
119bool TerminalInputParser::Eat() {
120 position_++;
121 return position_ < static_cast<int>(pending_.size());
122}
123
124void TerminalInputParser::Send(TerminalInputParser::Output output) {
125 switch (output.type) {
126 case UNCOMPLETED:
127 return;
128
129 case DROP:
130 pending_.clear();
131 return;
132
133 case CHARACTER:
134 out_->Send(Event::Character(std::move(pending_)));
135 pending_.clear();
136 return;
137
138 case SPECIAL: {
139 auto it = g_uniformize.find(pending_);
140 if (it != g_uniformize.end()) {
141 pending_ = it->second;
142 }
143 out_->Send(Event::Special(std::move(pending_)));
144 pending_.clear();
145 }
146 return;
147
148 case MOUSE:
149 out_->Send(Event::Mouse(std::move(pending_), output.mouse)); // NOLINT
150 pending_.clear();
151 return;
152
153 case CURSOR_REPORTING:
154 out_->Send(Event::CursorReporting(std::move(pending_), // NOLINT
155 output.cursor.x, // NOLINT
156 output.cursor.y)); // NOLINT
157 pending_.clear();
158 return;
159 }
160 // NOT_REACHED().
161}
162
163TerminalInputParser::Output TerminalInputParser::Parse() {
164 if (!Eat()) {
165 return UNCOMPLETED;
166 }
167
168 switch (Current()) {
169 case 24: // CAN NOLINT
170 case 26: // SUB NOLINT
171 return DROP;
172
173 case '\x1B':
174 return ParseESC();
175 default:
176 break;
177 }
178
179 if (Current() < 32) { // C0 NOLINT
180 return SPECIAL;
181 }
182
183 if (Current() == 127) { // Delete // NOLINT
184 return SPECIAL;
185 }
186
187 return ParseUTF8();
188}
189
190// Code point <-> UTF-8 conversion
191//
192// ┏━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
193// ┃Byte 1 ┃Byte 2 ┃Byte 3 ┃Byte 4 ┃
194// ┡━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩
195// │0xxxxxxx│ │ │ │
196// ├────────┼────────┼────────┼────────┤
197// │110xxxxx│10xxxxxx│ │ │
198// ├────────┼────────┼────────┼────────┤
199// │1110xxxx│10xxxxxx│10xxxxxx│ │
200// ├────────┼────────┼────────┼────────┤
201// │11110xxx│10xxxxxx│10xxxxxx│10xxxxxx│
202// └────────┴────────┴────────┴────────┘
203//
204// Then some sequences are illegal if it exist a shorter representation of the
205// same codepoint.
206TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
207 auto head = Current();
208 unsigned char selector = 0b1000'0000; // NOLINT
209
210 // The non code-point part of the first byte.
211 unsigned char mask = selector;
212
213 // Find the first zero in the first byte.
214 unsigned int first_zero = 8; // NOLINT
215 for (unsigned int i = 0; i < 8; ++i) { // NOLINT
216 mask |= selector;
217 if (!(head & selector)) {
218 first_zero = i;
219 break;
220 }
221 selector >>= 1U;
222 }
223
224 // Accumulate the value of the first byte.
225 auto value = uint32_t(head & ~mask); // NOLINT
226
227 // Invalid UTF8, with more than 5 bytes.
228 const unsigned int max_utf8_bytes = 5;
229 if (first_zero == 1 || first_zero >= max_utf8_bytes) {
230 return DROP;
231 }
232
233 // Multi byte UTF-8.
234 for (unsigned int i = 2; i <= first_zero; ++i) {
235 if (!Eat()) {
236 return UNCOMPLETED;
237 }
238
239 // Invalid continuation byte.
240 head = Current();
241 if ((head & 0b1100'0000) != 0b1000'0000) { // NOLINT
242 return DROP;
243 }
244 value <<= 6; // NOLINT
245 value += head & 0b0011'1111; // NOLINT
246 }
247
248 // Check for overlong UTF8 encoding.
249 int extra_byte = 0;
250 if (value <= 0b000'0000'0111'1111) { // NOLINT
251 extra_byte = 0; // NOLINT
252 } else if (value <= 0b000'0111'1111'1111) { // NOLINT
253 extra_byte = 1; // NOLINT
254 } else if (value <= 0b1111'1111'1111'1111) { // NOLINT
255 extra_byte = 2; // NOLINT
256 } else if (value <= 0b1'0000'1111'1111'1111'1111) { // NOLINT
257 extra_byte = 3; // NOLINT
258 } else { // NOLINT
259 return DROP;
260 }
261
262 if (extra_byte != position_) {
263 return DROP;
264 }
265
266 return CHARACTER;
267}
268
269TerminalInputParser::Output TerminalInputParser::ParseESC() {
270 if (!Eat()) {
271 return UNCOMPLETED;
272 }
273 switch (Current()) {
274 case 'P':
275 return ParseDCS();
276 case '[':
277 return ParseCSI();
278 case ']':
279 return ParseOSC();
280 default:
281 if (!Eat()) {
282 return UNCOMPLETED;
283 } else {
284 return SPECIAL;
285 }
286 }
287}
288
289TerminalInputParser::Output TerminalInputParser::ParseDCS() {
290 // Parse until the string terminator ST.
291 while (true) {
292 if (!Eat()) {
293 return UNCOMPLETED;
294 }
295
296 if (Current() != '\x1B') {
297 continue;
298 }
299
300 if (!Eat()) {
301 return UNCOMPLETED;
302 }
303
304 if (Current() != '\\') {
305 continue;
306 }
307
308 return SPECIAL;
309 }
310}
311
312TerminalInputParser::Output TerminalInputParser::ParseCSI() {
313 bool altered = false;
314 int argument = 0;
315 std::vector<int> arguments;
316 while (true) {
317 if (!Eat()) {
318 return UNCOMPLETED;
319 }
320
321 if (Current() == '<') {
322 altered = true;
323 continue;
324 }
325
326 if (Current() >= '0' && Current() <= '9') {
327 argument *= 10; // NOLINT
328 argument += Current() - '0';
329 continue;
330 }
331
332 if (Current() == ';') {
333 arguments.push_back(argument);
334 argument = 0;
335 continue;
336 }
337
338 // CSI is terminated by a character in the range 0x40–0x7E
339 // (ASCII @A–Z[\]^_`a–z{|}~),
340 if (Current() >= '@' && Current() <= '~' &&
341 // Note: I don't remember why we exclude '<'
342 Current() != '<' &&
343 // To handle F1-F4, we exclude '['.
344 Current() != '[') {
345 arguments.push_back(argument);
346 argument = 0; // NOLINT
347
348 switch (Current()) {
349 case 'M':
350 return ParseMouse(altered, true, std::move(arguments));
351 case 'm':
352 return ParseMouse(altered, false, std::move(arguments));
353 case 'R':
354 return ParseCursorReporting(std::move(arguments));
355 default:
356 return SPECIAL;
357 }
358 }
359
360 // Invalid ESC in CSI.
361 if (Current() == '\x1B') {
362 return SPECIAL;
363 }
364 }
365}
366
367TerminalInputParser::Output TerminalInputParser::ParseOSC() {
368 // Parse until the string terminator ST.
369 while (true) {
370 if (!Eat()) {
371 return UNCOMPLETED;
372 }
373 if (Current() != '\x1B') {
374 continue;
375 }
376 if (!Eat()) {
377 return UNCOMPLETED;
378 }
379 if (Current() != '\\') {
380 continue;
381 }
382 return SPECIAL;
383 }
384}
385
386TerminalInputParser::Output TerminalInputParser::ParseMouse( // NOLINT
387 bool altered,
388 bool pressed,
389 std::vector<int> arguments) {
390 if (arguments.size() != 3) {
391 return SPECIAL;
392 }
393
394 (void)altered;
395
396 Output output(MOUSE);
397 output.mouse.button = Mouse::Button((arguments[0] & 3) + // NOLINT
398 ((arguments[0] & 64) >> 4)); // NOLINT
399 output.mouse.motion = Mouse::Motion(pressed); // NOLINT
400 output.mouse.shift = bool(arguments[0] & 4); // NOLINT
401 output.mouse.meta = bool(arguments[0] & 8); // NOLINT
402 output.mouse.x = arguments[1]; // NOLINT
403 output.mouse.y = arguments[2]; // NOLINT
404 return output;
405}
406
407// NOLINTNEXTLINE
408TerminalInputParser::Output TerminalInputParser::ParseCursorReporting(
409 std::vector<int> arguments) {
410 if (arguments.size() != 2) {
411 return SPECIAL;
412 }
413 Output output(CURSOR_REPORTING);
414 output.cursor.y = arguments[0]; // NOLINT
415 output.cursor.x = arguments[1]; // NOLINT
416 return output;
417}
418
419} // namespace ftxui
TerminalInputParser(Sender< Task > out)
std::unique_ptr< SenderImpl< T > > Sender
Definition receiver.hpp:47
const std::map< std::string, std::string > g_uniformize
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:339
static Event Special(std::string)
An custom event whose meaning is defined by the user of the library.
Definition event.cpp:56