Fix parsing of keys that are prefix of others. (#58)

The ESC key generates sequences that are prefix of others. For instance:
- ESC => [27]
- F1 =>  [27, 79, 8]

As a result, we can't generate the ESC event when receiving [27],
because it might be the start of the [27, 79, 8] sequence (or not).

Application usually applies a timeout to help detecting the ESC key.
This patch introduce a timeout. It is set to 50ms.

Bug: https://github.com/ArthurSonzogni/FTXUI/issues/55
This commit is contained in:
Arthur Sonzogni
2020-10-25 01:57:56 +02:00
committed by GitHub
parent c13621d1f9
commit 406355df8c
10 changed files with 311 additions and 209 deletions

View File

@@ -0,0 +1,48 @@
#ifndef FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
#define FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
#include "ftxui/component/event.hpp"
#include "ftxui/component/receiver.hpp"
#include <string>
namespace ftxui {
// Parse a sequence of |char| accross |time|. Produces |Event|.
class TerminalInputParser {
public:
TerminalInputParser(Sender<Event> out);
void Timeout(int time);
void Add(char c);
private:
unsigned char Current();
bool Eat();
enum Type {
UNCOMPLETED = 0,
DROP = 1,
CHARACTER = 2,
SPECIAL = 3,
};
void Send(Type type);
Type Parse();
Type ParseUTF8();
Type ParseESC();
Type ParseDCS();
Type ParseCSI();
Type ParseOSC();
Sender<Event> out_;
int position_ = -1;
int timeout_ = 0;
std::string pending_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_TERMINAL_INPUT_PARSER */
// 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.