-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.cc
74 lines (67 loc) · 1.88 KB
/
terminal.cc
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
/**
* \author: Rafal Banas
*/
#include "terminal.h"
terminal::Key terminal::KeyStream::nextKey() {
uint8_t byte;
try {
byte = byte_stream.readByte();
} catch (const Eof &eof_exception) {
return ActionKey(ActionKeyType::END_OF_STREAM);
}
if (isPrintableByte(byte)) {
return PrintableKey(byte);
} else if (byte == ESCAPE_BYTE) {
auto processed = processEscapeSequence();
if (processed) {
return *processed;
} else {
return nextKey();
}
} else if (byte == CR_BYTE) {
return ActionKey(ActionKeyType::ENTER);
} else {
return nextKey();
}
}
std::optional<terminal::Key> terminal::KeyStream::processCsiSequence() {
uint8_t byte;
try {
byte = byte_stream.readByte();
} catch (const Eof &eof_exception) {
return std::make_optional(ActionKey(ActionKeyType::END_OF_STREAM));
}
switch (byte) {
case 'A': {
return std::make_optional(ActionKey(ActionKeyType::ARROW_UP));
}
case 'B': {
return std::make_optional(ActionKey(ActionKeyType::ARROW_DOWN));
}
case 'C': {
return std::make_optional(ActionKey(ActionKeyType::ARROW_RIGHT));
}
case 'D': {
return std::make_optional(ActionKey(ActionKeyType::ARROW_LEFT));
}
default: {
return {};
}
}
}
std::optional<terminal::Key> terminal::KeyStream::processEscapeSequence() {
uint8_t byte;
try {
byte = byte_stream.readByte();
} catch (const Eof &eof_exception) {
return std::make_optional(ActionKey(ActionKeyType::END_OF_STREAM));
}
if (byte == '[') {
return processCsiSequence();
} else {
return std::make_optional(nextKey());
}
}
bool terminal::isPrintableByte(uint8_t byte) {
return byte >= 32 && byte <= 126;
}