1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
module;
#include <algorithm>
#include <boost/sml.hpp>
#include <span>
#include <string>
#include <vector>
export module ttwhy.scanners:ansi;
import :events;
namespace ttwhy::scanners::detail
{
/// Events
struct byte_received
{
char value;
};
struct timeout_expired
{
};
/// States
constexpr auto idle = boost::sml::state<class idle>;
constexpr auto escape_sequence = boost::sml::state<class escape_sequence>;
constexpr auto csi_sequence = boost::sml::state<class csi_sequence>;
constexpr auto ss3_sequence = boost::sml::state<class ss3_sequence>;
/// Actions
constexpr auto push_character = [](byte_received const & event, std::vector<input_event> & queue) {
queue.push_back(character_event{event.value});
};
constexpr auto push_backspace = [](std::vector<input_event> & queue) {
queue.push_back(control_event{control_key::backspace});
};
constexpr auto push_tab = [](std::vector<input_event> & queue) {
queue.push_back(control_event(control_key::tab));
};
constexpr auto push_enter = [](std::vector<input_event> & queue) {
queue.push_back(control_event(control_key::enter));
};
constexpr auto emit_timeout_escape = [](std::vector<input_event> & queue, std::string & buffer) {
queue.push_back(control_event{control_key::escape});
buffer.clear();
};
constexpr auto fallback_escape = [](byte_received const & event, std::vector<input_event> & queue) {
queue.push_back(control_event{control_key::escape});
if (event.value >= 0x20 && event.value <= 0x7e)
{
queue.push_back(character_event{event.value});
}
};
constexpr auto clear_csi = [](std::string & buffer) {
buffer.clear();
};
constexpr auto push_csi_parameter = [](byte_received const & event, std::string & buffer) {
buffer.push_back(event.value);
};
constexpr auto resolve_csi = [](byte_received const & event, std::string & buffer, std::vector<input_event> & queue) {
if (event.value == '~')
{
switch (buffer.at(0))
{
case '1':
case '7':
queue.push_back(navigation_event{navigation_key::home});
break;
case '2':
queue.push_back(navigation_event{navigation_key::insert_key});
break;
case '3':
queue.push_back(navigation_event{navigation_key::delete_key});
break;
case '4':
case '8':
queue.push_back(navigation_event{navigation_key::end});
break;
case '5':
queue.push_back(navigation_event{navigation_key::page_up});
break;
case '6':
queue.push_back(navigation_event{navigation_key::page_down});
break;
default:
}
}
else
{
switch (event.value)
{
case 'A':
queue.push_back(navigation_event{navigation_key::up});
break;
case 'B':
queue.push_back(navigation_event{navigation_key::down});
break;
case 'C':
queue.push_back(navigation_event{navigation_key::right});
break;
case 'D':
queue.push_back(navigation_event{navigation_key::left});
break;
case 'H':
queue.push_back(navigation_event{navigation_key::home});
break;
case 'F':
queue.push_back(navigation_event{navigation_key::end});
break;
default:
}
}
buffer.clear();
};
constexpr auto resolve_ss3 = [](byte_received const & event, std::vector<input_event> & queue) {
switch (event.value)
{
case 'A':
queue.push_back(navigation_event{navigation_key::up});
break;
case 'B':
queue.push_back(navigation_event{navigation_key::down});
break;
case 'C':
queue.push_back(navigation_event{navigation_key::right});
break;
case 'D':
queue.push_back(navigation_event{navigation_key::left});
break;
case 'H':
queue.push_back(navigation_event{navigation_key::home});
break;
case 'F':
queue.push_back(navigation_event{navigation_key::end});
break;
default:
}
};
/// Guards
constexpr auto is_backspace = [](byte_received e) {
return e.value == '\x08' || e.value == '\x7f';
};
constexpr auto is_tab = [](byte_received e) {
return e.value == '\x09';
};
constexpr auto is_enter = [](byte_received e) {
return e.value == '\x0a' || e.value == '\x0d';
};
constexpr auto is_escape = [](byte_received e) {
return e.value == '\x1b';
};
constexpr auto is_printable = [](byte_received e) {
return e.value >= 0x20 && e.value <= 0x7e;
};
constexpr auto is_csi_introducer = [](byte_received e) {
return e.value == '[';
};
constexpr auto is_ss3_introducer = [](byte_received e) {
return e.value == 'O';
};
constexpr auto is_invalid_introducer = [](byte_received e) {
return !(is_ss3_introducer(e) || is_csi_introducer(e));
};
constexpr auto is_csi_param = [](byte_received e) {
return e.value >= 0x20 && e.value <= 0x3f;
};
constexpr auto is_csi_terminator = [](byte_received e) {
return e.value >= 0x40 && e.value <= 0x7e;
};
/// Transitions
struct transition_table
{
auto operator()() const noexcept
{
using namespace boost::sml;
// clang-format off
return make_transition_table(
*idle + event<byte_received>[is_escape] = escape_sequence,
idle + event<byte_received>[is_backspace] / push_backspace = idle,
idle + event<byte_received>[is_tab] / push_tab = idle,
idle + event<byte_received>[is_enter] / push_enter = idle,
idle + event<byte_received>[is_printable] / push_character = idle,
escape_sequence + event<byte_received>[is_csi_introducer] / clear_csi = csi_sequence,
escape_sequence + event<byte_received>[is_ss3_introducer] = ss3_sequence,
escape_sequence + event<byte_received>[is_invalid_introducer] / fallback_escape = idle,
escape_sequence + event<timeout_expired> / emit_timeout_escape = idle,
csi_sequence + event<byte_received>[is_csi_param] / push_csi_parameter = csi_sequence,
csi_sequence + event<byte_received>[is_csi_terminator] / resolve_csi = idle,
csi_sequence + event<timeout_expired> / emit_timeout_escape = idle,
ss3_sequence + event<byte_received> / resolve_ss3 = idle,
ss3_sequence + event<timeout_expired> / emit_timeout_escape = idle
);
// clang-format on
}
};
} // namespace ttwhy::scanners::detail
export namespace ttwhy::scanners
{
struct ansi
{
explicit ansi(std::vector<input_event> & queue)
: m_state_machine{queue, m_csi_buffer}
{}
auto process(std::span<char const> buffer) -> void
{
std::ranges::for_each(buffer, [&](auto byte) { m_state_machine.process_event(detail::byte_received{byte}); });
}
auto timeout()
{
m_state_machine.process_event(detail::timeout_expired{});
}
[[nodiscard]] auto is_pending() const -> bool
{
return m_state_machine.is(detail::escape_sequence) || //
m_state_machine.is(detail::csi_sequence) || //
m_state_machine.is(detail::ss3_sequence);
}
private:
std::string m_csi_buffer{};
boost::sml::sm<detail::transition_table> m_state_machine;
};
} // namespace ttwhy::scanners
|