FTXUI 6.1.9
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 <functional> // for std::function
9#include <map>
10#include <memory> // for unique_ptr, allocator
11#include <utility> // for move
12#include <vector>
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のターミナルは、returnキーに異なる改行文字を使用します。
21 // これは、Linuxでも`bind`コマンドで発生します。
22 // 詳細: https://github.com/ArthurSonzogni/FTXUI/issues/337
23 // ここでは、改行文字を`\n`に統一します。
24 {"\r", "\n"},
25
26 // 詳細: https://github.com/ArthurSonzogni/FTXUI/issues/508
27 {std::string({8}), std::string({127})},
28
29 // 詳細: https://github.com/ArthurSonzogni/FTXUI/issues/626
30 //
31 // カーソルキーモード (DECCKM) に応じて、ターミナルは異なるエスケープシーケンスを送信します。
32 //
33 // キー 通常 アプリケーション
34 // ----- -------- -----------
35 // 上 ESC [ A ESC O A
36 // 下 ESC [ B ESC O B
37 // 右 ESC [ C ESC O C
38 // 左 ESC [ D ESC O D
39 // Home ESC [ H ESC O H
40 // End ESC [ F ESC O F
41 //
42 {"\x1BOA", "\x1B[A"}, // 上
43 {"\x1BOB", "\x1B[B"}, // 下
44 {"\x1BOC", "\x1B[C"}, // 右
45 {"\x1BOD", "\x1B[D"}, // 左
46 {"\x1BOH", "\x1B[H"}, // HOME
47 {"\x1BOF", "\x1B[F"}, // END
48
49 // FNキーのバリエーション。
50 // 内部的には以下を使用しています。
51 // vt220, xterm-vt200, xterm-xf86-v44, xterm-new, mgt, screen
52 // 詳細: https://invisible-island.net/xterm/xterm-function-keys.html
53
54 // どの標準にも属さないLinux OSコンソール (CTRL+ALT+FN) の場合。
55 // 詳細: https://github.com/ArthurSonzogni/FTXUI/issues/685
56 {"\x1B[[A", "\x1BOP"}, // F1
57 {"\x1B[[B", "\x1BOQ"}, // F2
58 {"\x1B[[C", "\x1BOR"}, // F3
59 {"\x1B[[D", "\x1BOS"}, // F4
60 {"\x1B[[E", "\x1B[15~"}, // F5
61
62 // xterm-r5, xterm-r6, rxvt
63 {"\x1B[11~", "\x1BOP"}, // F1
64 {"\x1B[12~", "\x1BOQ"}, // F2
65 {"\x1B[13~", "\x1BOR"}, // F3
66 {"\x1B[14~", "\x1BOS"}, // F4
67
68 // vt100
69 {"\x1BOt", "\x1B[15~"}, // F5
70 {"\x1BOu", "\x1B[17~"}, // F6
71 {"\x1BOv", "\x1B[18~"}, // F7
72 {"\x1BOl", "\x1B[19~"}, // F8
73 {"\x1BOw", "\x1B[20~"}, // F9
74 {"\x1BOx", "\x1B[21~"}, // F10
75
76 // scoansi
77 {"\x1B[M", "\x1BOP"}, // F1
78 {"\x1B[N", "\x1BOQ"}, // F2
79 {"\x1B[O", "\x1BOR"}, // F3
80 {"\x1B[P", "\x1BOS"}, // F4
81 {"\x1B[Q", "\x1B[15~"}, // F5
82 {"\x1B[R", "\x1B[17~"}, // F6
83 {"\x1B[S", "\x1B[18~"}, // F7
84 {"\x1B[T", "\x1B[19~"}, // F8
85 {"\x1B[U", "\x1B[20~"}, // F9
86 {"\x1B[V", "\x1B[21~"}, // F10
87 {"\x1B[W", "\x1B[23~"}, // F11
88 {"\x1B[X", "\x1B[24~"}, // F12
89};
90
92 : out_(std::move(out)) {}
93
95 timeout_ += time;
96 const int timeout_threshold = 50;
97 if (timeout_ < timeout_threshold) {
98 return;
99 }
100 timeout_ = 0;
101 if (!pending_.empty()) {
102 Send(SPECIAL);
103 }
104}
105
107 pending_ += c;
108 timeout_ = 0;
109 position_ = -1;
110 Send(Parse());
111}
112
113unsigned char TerminalInputParser::Current() {
114 return pending_[position_];
115}
116
117bool TerminalInputParser::Eat() {
118 position_++;
119 return position_ < static_cast<int>(pending_.size());
120}
121
122void TerminalInputParser::Send(TerminalInputParser::Output output) {
123 switch (output.type) {
124 case UNCOMPLETED:
125 return;
126
127 case DROP:
128 pending_.clear();
129 return;
130
131 case CHARACTER:
132 out_(Event::Character(std::move(pending_)));
133 pending_.clear();
134 return;
135
136 case SPECIAL: {
137 auto it = g_uniformize.find(pending_);
138 if (it != g_uniformize.end()) {
139 pending_ = it->second;
140 }
141 out_(Event::Special(std::move(pending_)));
142 pending_.clear();
143 }
144 return;
145
146 case MOUSE:
147 out_(Event::Mouse(std::move(pending_), output.mouse)); // NOLINT
148 pending_.clear();
149 return;
150
151 case CURSOR_POSITION:
152 out_(Event::CursorPosition(std::move(pending_), // NOLINT
153 output.cursor.x, // NOLINT
154 output.cursor.y)); // NOLINT
155 pending_.clear();
156 return;
157
158 case CURSOR_SHAPE:
159 out_(Event::CursorShape(std::move(pending_), output.cursor_shape));
160 pending_.clear();
161 return;
162 }
163 // NOT_REACHED().
164}
165
166TerminalInputParser::Output TerminalInputParser::Parse() {
167 if (!Eat()) {
168 return UNCOMPLETED;
169 }
170
171 if (Current() == '\x1B') {
172 return ParseESC();
173 }
174
175 if (Current() < 32) { // C0 NOLINT
176 return SPECIAL;
177 }
178
179 if (Current() == 127) { // Delete // NOLINT
180 return SPECIAL;
181 }
182
183 return ParseUTF8();
184}
185
186// コードポイント <-> UTF-8変換
187//
188// ┏━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
189// ┃Byte 1 ┃Byte 2 ┃Byte 3 ┃Byte 4 ┃
190// ┡━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩
191// │0xxxxxxx│ │ │ │
192// ├────────┼────────┼────────┼────────┤
193// │110xxxxx│10xxxxxx│ │ │
194// ├────────┼────────┼────────┼────────┤
195// │1110xxxx│10xxxxxx│10xxxxxx│ │
196// ├────────┼────────┼────────┼────────┤
197// │11110xxx│10xxxxxx│10xxxxxx│10xxxxxx│
198// └────────┴────────┴────────┴────────┘
199//
200// 同じコードポイントのより短い表現が存在する場合、一部のシーケンスは不正になります。
201TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
202 auto head = Current();
203 unsigned char selector = 0b1000'0000; // NOLINT
204
205 // 最初のバイトのコードポイント以外の部分。
206 unsigned char mask = selector;
207
208 // 最初のバイトで最初のゼロを見つける。
209 unsigned int first_zero = 8; // NOLINT
210 for (unsigned int i = 0; i < 8; ++i) { // NOLINT
211 mask |= selector;
212 if (!(head & selector)) {
213 first_zero = i;
214 break;
215 }
216 selector >>= 1U;
217 }
218
219 // 最初のバイトの値を累積する。
220 auto value = uint32_t(head & ~mask); // NOLINT
221
222 // 5バイトを超える無効なUTF8。
223 const unsigned int max_utf8_bytes = 5;
224 if (first_zero == 1 || first_zero >= max_utf8_bytes) {
225 return DROP;
226 }
227
228 // マルチバイトUTF-8。
229 for (unsigned int i = 2; i <= first_zero; ++i) {
230 if (!Eat()) {
231 return UNCOMPLETED;
232 }
233
234 // 無効な継続バイト。
235 head = Current();
236 if ((head & 0b1100'0000) != 0b1000'0000) { // NOLINT
237 return DROP;
238 }
239 value <<= 6; // NOLINT
240 value += head & 0b0011'1111; // NOLINT
241 }
242
243 // オーバーロングUTF8エンコーディングのチェック。
244 int extra_byte = 0;
245 if (value <= 0b000'0000'0111'1111) { // NOLINT
246 extra_byte = 0; // NOLINT
247 } else if (value <= 0b000'0111'1111'1111) { // NOLINT
248 extra_byte = 1; // NOLINT
249 } else if (value <= 0b1111'1111'1111'1111) { // NOLINT
250 extra_byte = 2; // NOLINT
251 } else if (value <= 0b1'0000'1111'1111'1111'1111) { // NOLINT
252 extra_byte = 3; // NOLINT
253 } else { // NOLINT
254 return DROP;
255 }
256
257 if (extra_byte != position_) {
258 return DROP;
259 }
260
261 return CHARACTER;
262}
263
264TerminalInputParser::Output TerminalInputParser::ParseESC() {
265 if (!Eat()) {
266 return UNCOMPLETED;
267 }
268 switch (Current()) {
269 case 'P':
270 return ParseDCS();
271 case '[':
272 return ParseCSI();
273 case ']':
274 return ParseOSC();
275
276 // 2文字を期待しています。
277 case ' ':
278 case '#':
279 case '%':
280 case '(':
281 case ')':
282 case '*':
283 case '+':
284 case 'O':
285 case 'N': {
286 if (!Eat()) {
287 return UNCOMPLETED;
288 }
289 return SPECIAL;
290 }
291 // 1文字を期待しています:
292 default:
293 return SPECIAL;
294 }
295}
296
297// ESC P ... ESC BACKSLASH
298TerminalInputParser::Output TerminalInputParser::ParseDCS() {
299 // 文字列終端記号STまで解析します。
300 while (true) {
301 if (!Eat()) {
302 return UNCOMPLETED;
303 }
304
305 if (Current() != '\x1B') {
306 continue;
307 }
308
309 if (!Eat()) {
310 return UNCOMPLETED;
311 }
312
313 if (Current() != '\\') {
314 continue;
315 }
316
317 if (pending_.size() == 10 && //
318 pending_[2] == '1' && //
319 pending_[3] == '$' && //
320 pending_[4] == 'r' && //
321 true) {
322 Output output(CURSOR_SHAPE);
323 output.cursor_shape = pending_[5] - '0';
324 return output;
325 }
326
327 return SPECIAL;
328 }
329}
330
331TerminalInputParser::Output TerminalInputParser::ParseCSI() {
332 bool altered = false;
333 int argument = 0;
334 std::vector<int> arguments;
335 while (true) {
336 if (!Eat()) {
337 return UNCOMPLETED;
338 }
339
340 if (Current() == '<') {
341 altered = true;
342 continue;
343 }
344
345 if (Current() >= '0' && Current() <= '9') {
346 argument *= 10; // NOLINT
347 argument += Current() - '0';
348 continue;
349 }
350
351 if (Current() == ';') {
352 arguments.push_back(argument);
353 argument = 0;
354 continue;
355 }
356
357 // CSIは0x40-0x7Eの範囲の文字で終了します。
358 // (ASCII @A–Z[\\\]^_`a–z{|}~),
359 if (Current() >= '@' && Current() <= '~' &&
360 // 注: '<'を除外する理由を覚えていません。
361 Current() != '<' &&
362 // F1-F4を処理するために'['を除外します。
363 Current() != '[') {
364 arguments.push_back(argument);
365 argument = 0; // NOLINT
366
367 switch (Current()) {
368 case 'M':
369 return ParseMouse(altered, true, std::move(arguments));
370 case 'm':
371 return ParseMouse(altered, false, std::move(arguments));
372 case 'R':
373 return ParseCursorPosition(std::move(arguments));
374 default:
375 return SPECIAL;
376 }
377 }
378
379 // CSI内の無効なESC。
380 if (Current() == '\x1B') {
381 return SPECIAL;
382 }
383 }
384}
385
386TerminalInputParser::Output TerminalInputParser::ParseOSC() {
387 // 文字列終端記号STまで解析します。
388 while (true) {
389 if (!Eat()) {
390 return UNCOMPLETED;
391 }
392 if (Current() != '\x1B') {
393 continue;
394 }
395 if (!Eat()) {
396 return UNCOMPLETED;
397 }
398 if (Current() != '\\') {
399 continue;
400 }
401 return SPECIAL;
402 }
403}
404
405TerminalInputParser::Output TerminalInputParser::ParseMouse( // NOLINT
406 bool altered,
407 bool pressed,
408 std::vector<int> arguments) {
409 if (arguments.size() != 3) {
410 return SPECIAL;
411 }
412
413 (void)altered;
414
415 Output output(MOUSE);
416 output.mouse.motion = Mouse::Motion(pressed); // NOLINT
417
418 // ビット値 修飾子 コメント
419 // ---- ----- ------- ---------
420 // 0 1 1 2 ボタン 0 = 左, 1 = 中, 2 = 右, 3 = 離す
421 // 2 4 Shift
422 // 3 8 Meta
423 // 4 16 Control
424 // 5 32 Move
425 // 6 64 Wheel
426
427 // clang-format off
428 const int button = arguments[0] & (1 + 2); // NOLINT
429 const bool is_shift = arguments[0] & 4; // NOLINT
430 const bool is_meta = arguments[0] & 8; // NOLINT
431 const bool is_control = arguments[0] & 16; // NOLINT
432 const bool is_move = arguments[0] & 32; // NOLINT
433 const bool is_wheel = arguments[0] & 64; // NOLINT
434 // clang-format on
435
436 output.mouse.motion = is_move ? Mouse::Moved : Mouse::Motion(pressed);
437 output.mouse.button = is_wheel ? Mouse::Button(Mouse::WheelUp + button) //
438 : Mouse::Button(button);
439 output.mouse.shift = is_shift;
440 output.mouse.meta = is_meta;
441 output.mouse.control = is_control;
442 output.mouse.x = arguments[1]; // NOLINT
443 output.mouse.y = arguments[2]; // NOLINT
444
445 // 移動イベント。
446 return output;
447}
448
449// NOLINTNEXTLINE
450TerminalInputParser::Output TerminalInputParser::ParseCursorPosition(
451 std::vector<int> arguments) {
452 if (arguments.size() != 2) {
453 return SPECIAL;
454 }
455 Output output(CURSOR_POSITION);
456 output.cursor.y = arguments[0]; // NOLINT
457 output.cursor.x = arguments[1]; // NOLINT
458 return output;
459}
460
461} // namespace ftxui
TerminalInputParser(std::function< void(Event)> out)
static Event Special(std::string)
ライブラリのユーザーによって意味が定義されるカスタムイベント。
Definition event.cpp:73
イベントを表します。キープレスイベント、ターミナルのリサイズなど、さまざまなイベントがあります。
Definition event.hpp:28
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
const std::map< std::string, std::string > g_uniformize