2023-08-19 19:56:36 +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.
|
2020-10-25 07:57:56 +08:00
|
|
|
#ifndef FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
|
|
|
|
#define FTXUI_COMPONENT_TERMINAL_INPUT_PARSER
|
|
|
|
|
2025-07-02 21:23:01 +08:00
|
|
|
#include <functional>
|
2021-05-02 02:40:35 +08:00
|
|
|
#include <string> // for string
|
|
|
|
#include <vector> // for vector
|
2020-10-25 07:57:56 +08:00
|
|
|
|
2025-07-02 21:23:01 +08:00
|
|
|
#include "ftxui/component/mouse.hpp" // for Mouse
|
2020-10-25 07:57:56 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
2021-05-15 04:00:49 +08:00
|
|
|
struct Event;
|
2020-10-25 07:57:56 +08:00
|
|
|
|
|
|
|
// Parse a sequence of |char| accross |time|. Produces |Event|.
|
|
|
|
class TerminalInputParser {
|
|
|
|
public:
|
2025-07-02 21:23:01 +08:00
|
|
|
explicit TerminalInputParser(std::function<void(Event)> out);
|
2020-10-25 07:57:56 +08:00
|
|
|
void Timeout(int time);
|
|
|
|
void Add(char c);
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned char Current();
|
|
|
|
bool Eat();
|
|
|
|
|
|
|
|
enum Type {
|
2021-04-19 00:32:38 +08:00
|
|
|
UNCOMPLETED,
|
|
|
|
DROP,
|
|
|
|
CHARACTER,
|
2021-04-25 21:22:38 +08:00
|
|
|
MOUSE,
|
2023-12-17 17:24:33 +08:00
|
|
|
CURSOR_POSITION,
|
|
|
|
CURSOR_SHAPE,
|
|
|
|
SPECIAL,
|
2020-10-25 07:57:56 +08:00
|
|
|
};
|
2021-04-19 00:32:38 +08:00
|
|
|
|
2023-12-17 17:24:33 +08:00
|
|
|
struct CursorPosition {
|
2021-04-19 00:32:38 +08:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Output {
|
|
|
|
Type type;
|
|
|
|
union {
|
|
|
|
Mouse mouse;
|
2024-05-01 17:40:49 +08:00
|
|
|
CursorPosition cursor{};
|
2023-12-17 17:24:33 +08:00
|
|
|
int cursor_shape;
|
2021-04-19 00:32:38 +08:00
|
|
|
};
|
|
|
|
|
2024-05-01 17:40:49 +08:00
|
|
|
Output(Type t) // NOLINT
|
|
|
|
: type(t) {}
|
2021-04-19 00:32:38 +08:00
|
|
|
};
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
void Send(Output output);
|
2021-04-19 00:32:38 +08:00
|
|
|
Output Parse();
|
|
|
|
Output ParseUTF8();
|
|
|
|
Output ParseESC();
|
|
|
|
Output ParseDCS();
|
|
|
|
Output ParseCSI();
|
|
|
|
Output ParseOSC();
|
2021-04-25 21:22:38 +08:00
|
|
|
Output ParseMouse(bool altered, bool pressed, std::vector<int> arguments);
|
2023-12-17 17:24:33 +08:00
|
|
|
Output ParseCursorPosition(std::vector<int> arguments);
|
2020-10-25 07:57:56 +08:00
|
|
|
|
2025-07-02 21:23:01 +08:00
|
|
|
std::function<void(Event)> out_;
|
2020-10-25 07:57:56 +08:00
|
|
|
int position_ = -1;
|
|
|
|
int timeout_ = 0;
|
|
|
|
std::string pending_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ftxui
|
|
|
|
|
|
|
|
#endif /* end of include guard: FTXUI_COMPONENT_TERMINAL_INPUT_PARSER */
|