13 : out_(std::move(out)) {}
17 const int timeout_threshold = 50;
18 if (timeout_ < timeout_threshold) {
22 if (!pending_.empty()) {
34unsigned char TerminalInputParser::Current() {
35 return pending_[position_];
38bool TerminalInputParser::Eat() {
40 return position_ < (int)pending_.size();
43void TerminalInputParser::Send(TerminalInputParser::Output output) {
44 switch (output.type) {
53 out_->Send(Event::Character(std::move(pending_)));
62 if (pending_ ==
"\r") {
71 out_->Send(Event::Mouse(std::move(pending_), output.mouse));
75 case CURSOR_REPORTING:
76 out_->Send(Event::CursorReporting(std::move(pending_),
85TerminalInputParser::Output TerminalInputParser::Parse() {
101 if (Current() < 32) {
105 if (Current() == 127) {
128TerminalInputParser::Output TerminalInputParser::ParseUTF8() {
129 auto head =
static_cast<unsigned char>(Current());
130 unsigned char selector = 0b1000'0000;
133 unsigned char mask = selector;
136 unsigned int first_zero = 8;
137 for (
unsigned int i = 0; i < 8; ++i) {
139 if (!(head & selector)) {
147 auto value = uint32_t(head & ~mask);
150 const unsigned int max_utf8_bytes = 5;
151 if (first_zero == 1 || first_zero >= max_utf8_bytes) {
156 for (
unsigned int i = 2; i <= first_zero; ++i) {
162 head =
static_cast<unsigned char>(Current());
163 if ((head & 0b1100'0000) != 0b1000'0000) {
167 value += head & 0b0011'1111;
172 if (value <= 0b000'0000'0111'1111) {
174 }
else if (value <= 0b000'0111'1111'1111) {
176 }
else if (value <= 0b1111'1111'1111'1111) {
178 }
else if (value <= 0b1'0000'1111'1111'1111'1111) {
184 if (extra_byte != position_) {
191TerminalInputParser::Output TerminalInputParser::ParseESC() {
211TerminalInputParser::Output TerminalInputParser::ParseDCS() {
218 if (Current() !=
'\x1B') {
226 if (Current() !=
'\\') {
234TerminalInputParser::Output TerminalInputParser::ParseCSI() {
235 bool altered =
false;
237 std::vector<int> arguments;
243 if (Current() ==
'<') {
248 if (Current() >=
'0' && Current() <=
'9') {
250 argument += int(Current() -
'0');
254 if (Current() ==
';') {
255 arguments.push_back(argument);
260 if (Current() >=
' ' && Current() <=
'~' && Current() !=
'<') {
261 arguments.push_back(argument);
265 return ParseMouse(altered,
true, std::move(arguments));
267 return ParseMouse(altered,
false, std::move(arguments));
269 return ParseCursorReporting(std::move(arguments));
276 if (Current() ==
'\x1B') {
282TerminalInputParser::Output TerminalInputParser::ParseOSC() {
288 if (Current() !=
'\x1B') {
294 if (Current() !=
'\\') {
301TerminalInputParser::Output TerminalInputParser::ParseMouse(
304 std::vector<int> arguments) {
305 if (arguments.size() != 3) {
311 Output output(MOUSE);
313 ((arguments[0] & 64) >> 4));
315 output.mouse.shift = bool(arguments[0] & 4);
316 output.mouse.meta = bool(arguments[0] & 8);
317 output.mouse.x = arguments[1];
318 output.mouse.y = arguments[2];
323TerminalInputParser::Output TerminalInputParser::ParseCursorReporting(
324 std::vector<int> arguments) {
325 if (arguments.size() != 2) {
328 Output output(CURSOR_REPORTING);
329 output.cursor.y = arguments[0];
330 output.cursor.x = arguments[1];
std::unique_ptr< SenderImpl< T > > Sender
static Event Special(std::string)