57 {
"\x1B[[A",
"\x1BOP"},
58 {
"\x1B[[B",
"\x1BOQ"},
59 {
"\x1B[[C",
"\x1BOR"},
60 {
"\x1B[[D",
"\x1BOS"},
61 {
"\x1B[[E",
"\x1B[15~"},
64 {
"\x1B[11~",
"\x1BOP"},
65 {
"\x1B[12~",
"\x1BOQ"},
66 {
"\x1B[13~",
"\x1BOR"},
67 {
"\x1B[14~",
"\x1BOS"},
70 {
"\x1BOt",
"\x1B[15~"},
71 {
"\x1BOu",
"\x1B[17~"},
72 {
"\x1BOv",
"\x1B[18~"},
73 {
"\x1BOl",
"\x1B[19~"},
74 {
"\x1BOw",
"\x1B[20~"},
75 {
"\x1BOx",
"\x1B[21~"},
82 {
"\x1B[Q",
"\x1B[15~"},
83 {
"\x1B[R",
"\x1B[17~"},
84 {
"\x1B[S",
"\x1B[18~"},
85 {
"\x1B[T",
"\x1B[19~"},
86 {
"\x1B[U",
"\x1B[20~"},
87 {
"\x1B[V",
"\x1B[21~"},
88 {
"\x1B[W",
"\x1B[23~"},
89 {
"\x1B[X",
"\x1B[24~"},
93 : out_(std::move(out)) {}
97 const int timeout_threshold = 50;
98 if (timeout_ < timeout_threshold) {
102 if (!pending_.empty()) {
114unsigned char TerminalInputParser::Current() {
115 return pending_[position_];
118bool TerminalInputParser::Eat() {
120 return position_ < static_cast<int>(pending_.size());
123void TerminalInputParser::Send(TerminalInputParser::Output output) {
124 switch (output.type) {
133 out_(Event::Character(std::move(pending_)));
140 pending_ = it->second;
148 out_(Event::Mouse(std::move(pending_), output.mouse));
152 case CURSOR_POSITION:
153 out_(Event::CursorPosition(std::move(pending_),
160 out_(Event::CursorShape(std::move(pending_), output.cursor_shape));
167TerminalInputParser::Output TerminalInputParser::Parse() {
172 if (Current() ==
'\x1B') {
176 if (Current() < 32) {
180 if (Current() == 127) {
202TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
203 auto head = Current();
204 unsigned char selector = 0b1000'0000;
207 unsigned char mask = selector;
210 unsigned int first_zero = 8;
211 for (
unsigned int i = 0; i < 8; ++i) {
213 if (!(head & selector)) {
221 auto value = uint32_t(head & ~mask);
224 const unsigned int max_utf8_bytes = 5;
225 if (first_zero == 1 || first_zero >= max_utf8_bytes) {
230 for (
unsigned int i = 2; i <= first_zero; ++i) {
237 if ((head & 0b1100'0000) != 0b1000'0000) {
241 value += head & 0b0011'1111;
246 if (value <= 0b000'0000'0111'1111) {
248 }
else if (value <= 0b000'0111'1111'1111) {
250 }
else if (value <= 0b1111'1111'1111'1111) {
252 }
else if (value <= 0b1'0000'1111'1111'1111'1111) {
258 if (extra_byte != position_) {
265TerminalInputParser::Output TerminalInputParser::ParseESC() {
299TerminalInputParser::Output TerminalInputParser::ParseDCS() {
306 if (Current() !=
'\x1B') {
314 if (Current() !=
'\\') {
318 if (pending_.size() == 10 &&
319 pending_[2] ==
'1' &&
320 pending_[3] ==
'$' &&
321 pending_[4] ==
'r' &&
323 Output output(CURSOR_SHAPE);
324 output.cursor_shape = pending_[5] -
'0';
332TerminalInputParser::Output TerminalInputParser::ParseCSI() {
333 bool altered =
false;
335 std::vector<int> arguments;
341 if (Current() ==
'<') {
346 if (Current() >=
'0' && Current() <=
'9') {
348 argument += Current() -
'0';
352 if (Current() ==
';') {
353 arguments.push_back(argument);
360 if (Current() >=
'@' && Current() <=
'~' &&
365 arguments.push_back(argument);
370 return ParseMouse(altered,
true, std::move(arguments));
372 return ParseMouse(altered,
false, std::move(arguments));
374 return ParseCursorPosition(std::move(arguments));
381 if (Current() ==
'\x1B') {
387TerminalInputParser::Output TerminalInputParser::ParseOSC() {
393 if (Current() !=
'\x1B') {
399 if (Current() !=
'\\') {
406TerminalInputParser::Output TerminalInputParser::ParseMouse(
409 std::vector<int> arguments) {
410 if (arguments.size() != 3) {
416 Output output(MOUSE);
429 const int button = arguments[0] & (1 + 2);
430 const bool is_shift = arguments[0] & 4;
431 const bool is_meta = arguments[0] & 8;
432 const bool is_control = arguments[0] & 16;
433 const bool is_move = arguments[0] & 32;
434 const bool is_wheel = arguments[0] & 64;
439 : Mouse::Button(button);
440 output.mouse.shift = is_shift;
441 output.mouse.meta = is_meta;
442 output.mouse.control = is_control;
443 output.mouse.x = arguments[1];
444 output.mouse.y = arguments[2];
451TerminalInputParser::Output TerminalInputParser::ParseCursorPosition(
452 std::vector<int> arguments) {
453 if (arguments.size() != 2) {
456 Output output(CURSOR_POSITION);
457 output.cursor.y = arguments[0];
458 output.cursor.x = arguments[1];
static Event Special(std::string)
一个自定义事件,其含义由库用户定义。
代表一个事件。它可以是按键事件、终端大小调整等等...
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
const std::map< std::string, std::string > g_uniformize