24 {std::string({8}), std::string({127})},
50 : out_(std::move(out)) {}
54 const int timeout_threshold = 50;
55 if (timeout_ < timeout_threshold) {
59 if (!pending_.empty()) {
71unsigned char TerminalInputParser::Current() {
72 return pending_[position_];
75bool TerminalInputParser::Eat() {
77 return position_ < (int)pending_.size();
80void TerminalInputParser::Send(TerminalInputParser::Output output) {
81 switch (output.type) {
90 out_->Send(Event::Character(std::move(pending_)));
97 pending_ = it->second;
105 out_->Send(Event::Mouse(std::move(pending_), output.mouse));
109 case CURSOR_REPORTING:
110 out_->Send(Event::CursorReporting(std::move(pending_),
119TerminalInputParser::Output TerminalInputParser::Parse() {
135 if (Current() < 32) {
139 if (Current() == 127) {
162TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
163 auto head =
static_cast<unsigned char>(Current());
164 unsigned char selector = 0b1000'0000;
167 unsigned char mask = selector;
170 unsigned int first_zero = 8;
171 for (
unsigned int i = 0; i < 8; ++i) {
173 if (!(head & selector)) {
181 auto value = uint32_t(head & ~mask);
184 const unsigned int max_utf8_bytes = 5;
185 if (first_zero == 1 || first_zero >= max_utf8_bytes) {
190 for (
unsigned int i = 2; i <= first_zero; ++i) {
196 head =
static_cast<unsigned char>(Current());
197 if ((head & 0b1100'0000) != 0b1000'0000) {
201 value += head & 0b0011'1111;
206 if (value <= 0b000'0000'0111'1111) {
208 }
else if (value <= 0b000'0111'1111'1111) {
210 }
else if (value <= 0b1111'1111'1111'1111) {
212 }
else if (value <= 0b1'0000'1111'1111'1111'1111) {
218 if (extra_byte != position_) {
225TerminalInputParser::Output TerminalInputParser::ParseESC() {
245TerminalInputParser::Output TerminalInputParser::ParseDCS() {
252 if (Current() !=
'\x1B') {
260 if (Current() !=
'\\') {
268TerminalInputParser::Output TerminalInputParser::ParseCSI() {
269 bool altered =
false;
271 std::vector<int> arguments;
277 if (Current() ==
'<') {
282 if (Current() >=
'0' && Current() <=
'9') {
284 argument += int(Current() -
'0');
288 if (Current() ==
';') {
289 arguments.push_back(argument);
294 if (Current() >=
' ' && Current() <=
'~' && Current() !=
'<') {
295 arguments.push_back(argument);
299 return ParseMouse(altered,
true, std::move(arguments));
301 return ParseMouse(altered,
false, std::move(arguments));
303 return ParseCursorReporting(std::move(arguments));
310 if (Current() ==
'\x1B') {
316TerminalInputParser::Output TerminalInputParser::ParseOSC() {
322 if (Current() !=
'\x1B') {
328 if (Current() !=
'\\') {
335TerminalInputParser::Output TerminalInputParser::ParseMouse(
338 std::vector<int> arguments) {
339 if (arguments.size() != 3) {
345 Output output(MOUSE);
347 ((arguments[0] & 64) >> 4));
349 output.mouse.shift = bool(arguments[0] & 4);
350 output.mouse.meta = bool(arguments[0] & 8);
351 output.mouse.x = arguments[1];
352 output.mouse.y = arguments[2];
357TerminalInputParser::Output TerminalInputParser::ParseCursorReporting(
358 std::vector<int> arguments) {
359 if (arguments.size() != 2) {
362 Output output(CURSOR_REPORTING);
363 output.cursor.y = arguments[0];
364 output.cursor.x = arguments[1];
const std::map< std::string, std::string > g_uniformize
std::unique_ptr< SenderImpl< T > > Sender
static Event Special(std::string)