2020-04-20 03:00:37 +08:00
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
#include "ftxui/component/event.hpp"
|
2020-03-23 05:32:44 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
#include "ftxui/screen/string.hpp"
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2020-08-09 20:53:56 +08:00
|
|
|
namespace {
|
2019-06-23 23:47:33 +08:00
|
|
|
|
2020-03-25 07:07:41 +08:00
|
|
|
void ParseUTF8(Receiver<char>& in, Sender<Event>& out, std::string& input) {
|
2020-03-25 06:26:55 +08:00
|
|
|
char c;
|
2020-03-25 09:01:31 +08:00
|
|
|
unsigned char head = static_cast<unsigned char>(input[0]);
|
|
|
|
for (int i = 0; i < 3; ++i, head <<= 1) {
|
|
|
|
if ((head & 0b11000000) != 0b11000000)
|
|
|
|
break;
|
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
2020-03-25 06:26:55 +08:00
|
|
|
}
|
|
|
|
out->Send(Event::Character(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
}
|
|
|
|
|
2020-03-25 07:07:41 +08:00
|
|
|
void ParseCSI(Receiver<char>& in, Sender<Event>& out, std::string& input) {
|
2020-03-25 06:26:55 +08:00
|
|
|
char c;
|
2019-06-23 23:47:33 +08:00
|
|
|
while (1) {
|
2020-03-25 06:26:55 +08:00
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
2019-06-23 23:47:33 +08:00
|
|
|
input += c;
|
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
if (c >= '0' && c <= '9')
|
2019-07-01 06:43:29 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c == ';')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c >= ' ' && c <= '~')
|
2020-03-25 06:26:55 +08:00
|
|
|
return out->Send(Event::Special(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
|
|
|
|
// Invalid ESC in CSI.
|
2020-02-12 04:44:55 +08:00
|
|
|
if (c == '\x1B')
|
2020-03-25 06:26:55 +08:00
|
|
|
return out->Send(Event::Special(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 07:07:41 +08:00
|
|
|
void ParseDCS(Receiver<char>& in, Sender<Event>& out, std::string& input) {
|
2020-03-25 06:26:55 +08:00
|
|
|
char c;
|
2019-06-23 23:47:33 +08:00
|
|
|
// Parse until the string terminator ST.
|
|
|
|
while (1) {
|
2020-03-25 06:26:55 +08:00
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
2020-02-12 04:44:55 +08:00
|
|
|
if (input.back() != '\x1B')
|
2019-06-23 23:47:33 +08:00
|
|
|
continue;
|
2020-03-25 06:26:55 +08:00
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
2019-06-23 23:47:33 +08:00
|
|
|
if (input.back() != '\\')
|
|
|
|
continue;
|
2020-03-25 06:26:55 +08:00
|
|
|
return out->Send(Event::Special(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 07:07:41 +08:00
|
|
|
void ParseOSC(Receiver<char>& in, Sender<Event>& out, std::string& input) {
|
2020-03-25 06:26:55 +08:00
|
|
|
char c;
|
2019-06-23 23:47:33 +08:00
|
|
|
// Parse until the string terminator ST.
|
|
|
|
while (1) {
|
2020-03-25 06:26:55 +08:00
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
2020-02-12 04:44:55 +08:00
|
|
|
if (input.back() != '\x1B')
|
2019-06-23 23:47:33 +08:00
|
|
|
continue;
|
2020-03-25 06:26:55 +08:00
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
2019-06-23 23:47:33 +08:00
|
|
|
if (input.back() != '\\')
|
|
|
|
continue;
|
2020-03-25 06:26:55 +08:00
|
|
|
return out->Send(Event::Special(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 07:07:41 +08:00
|
|
|
void ParseESC(Receiver<char>& in, Sender<Event>& out, std::string& input) {
|
2020-03-25 06:26:55 +08:00
|
|
|
char c;
|
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
|
|
|
switch (c) {
|
2020-03-23 05:32:44 +08:00
|
|
|
case 'P':
|
2020-03-25 06:26:55 +08:00
|
|
|
return ParseDCS(in, out, input);
|
2020-03-23 05:32:44 +08:00
|
|
|
case '[':
|
2020-03-25 06:26:55 +08:00
|
|
|
return ParseCSI(in, out, input);
|
2020-03-23 05:32:44 +08:00
|
|
|
case ']':
|
2020-03-25 06:26:55 +08:00
|
|
|
return ParseOSC(in, out, input);
|
2019-06-23 23:47:33 +08:00
|
|
|
default:
|
2020-03-25 06:26:55 +08:00
|
|
|
if (!in->Receive(&c))
|
|
|
|
return;
|
|
|
|
input += c;
|
|
|
|
out->Send(Event::Special(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:53:56 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
Event Event::Character(const std::string& input) {
|
|
|
|
Event event;
|
|
|
|
event.input_ = input;
|
|
|
|
event.is_character_ = true;
|
|
|
|
event.character_ = to_wstring(input)[0];
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Event Event::Character(char c) {
|
|
|
|
return Character(wchar_t(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Event Event::Character(wchar_t c) {
|
|
|
|
Event event;
|
|
|
|
event.input_ = {(char)c};
|
|
|
|
event.is_character_ = true;
|
|
|
|
event.character_ = c;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Event Event::Special(const std::string& input) {
|
|
|
|
Event event;
|
|
|
|
event.input_ = std::move(input);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
// static
|
2020-03-25 07:07:41 +08:00
|
|
|
void Event::Convert(Receiver<char>& in, Sender<Event>& out, char c) {
|
2019-06-23 23:47:33 +08:00
|
|
|
std::string input;
|
2020-03-25 06:26:55 +08:00
|
|
|
input += c;
|
2019-06-23 23:47:33 +08:00
|
|
|
|
2020-02-12 06:43:26 +08:00
|
|
|
unsigned char head = input[0];
|
|
|
|
switch (head) {
|
2020-03-25 06:26:55 +08:00
|
|
|
case 24: // CAN
|
|
|
|
case 26: // SUB
|
|
|
|
return;
|
2019-06-23 23:47:33 +08:00
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
case '\x1B':
|
2020-03-25 06:26:55 +08:00
|
|
|
return ParseESC(in, out, input);
|
2019-06-23 23:47:33 +08:00
|
|
|
}
|
|
|
|
|
2020-02-12 06:43:26 +08:00
|
|
|
if (head < 32) // C0
|
2020-03-25 06:26:55 +08:00
|
|
|
return out->Send(Event::Special(input));
|
2019-06-23 23:47:33 +08:00
|
|
|
|
2020-02-12 06:43:26 +08:00
|
|
|
if (head == 127) // Delete
|
2020-03-25 06:26:55 +08:00
|
|
|
return out->Send(Event::Special(input));
|
2019-07-03 05:09:20 +08:00
|
|
|
|
2020-03-25 06:26:55 +08:00
|
|
|
return ParseUTF8(in, out, input);
|
2018-10-19 04:58:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Arrow ---
|
2020-02-12 04:44:55 +08:00
|
|
|
Event Event::ArrowLeft = Event::Special("\x1B[D");
|
|
|
|
Event Event::ArrowRight = Event::Special("\x1B[C");
|
|
|
|
Event Event::ArrowUp = Event::Special("\x1B[A");
|
|
|
|
Event Event::ArrowDown = Event::Special("\x1B[B");
|
2019-06-23 23:47:33 +08:00
|
|
|
Event Event::Backspace = Event::Special({127});
|
2020-02-12 04:44:55 +08:00
|
|
|
Event Event::Delete = Event::Special("\x1B[3~");
|
|
|
|
Event Event::Escape = Event::Special("\x1B");
|
2020-07-22 01:04:43 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
Event Event::Return = Event::Special({13});
|
|
|
|
#else
|
2019-06-23 23:47:33 +08:00
|
|
|
Event Event::Return = Event::Special({10});
|
2020-07-22 01:04:43 +08:00
|
|
|
#endif
|
2019-11-02 02:52:41 +08:00
|
|
|
Event Event::Tab = Event::Special({9});
|
|
|
|
Event Event::TabReverse = Event::Special({27, 91, 90});
|
2020-02-12 04:44:55 +08:00
|
|
|
Event Event::F1 = Event::Special("\x1B[OP");
|
|
|
|
Event Event::F2 = Event::Special("\x1B[OQ");
|
|
|
|
Event Event::F3 = Event::Special("\x1B[OR");
|
|
|
|
Event Event::F4 = Event::Special("\x1B[OS");
|
|
|
|
Event Event::F5 = Event::Special("\x1B[15~");
|
|
|
|
Event Event::F6 = Event::Special("\x1B[17~");
|
|
|
|
Event Event::F7 = Event::Special("\x1B[18~");
|
|
|
|
Event Event::F8 = Event::Special("\x1B[19~");
|
|
|
|
Event Event::F9 = Event::Special("\x1B[20~");
|
|
|
|
Event Event::F10 = Event::Special("\x1B[21~");
|
|
|
|
Event Event::F11 = Event::Special("\x1B[21~"); // Doesn't exist
|
|
|
|
Event Event::F12 = Event::Special("\x1B[24~");
|
2019-06-23 23:47:33 +08:00
|
|
|
Event Event::Custom = Event::Special({0});
|
|
|
|
|
|
|
|
} // namespace ftxui
|