-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.h
110 lines (77 loc) · 2.17 KB
/
terminal.h
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
#ifndef TELNET_SERVER_TERINAL_H
#define TELNET_SERVER_TERINAL_H
#include "byte-stream.h"
#include <optional>
namespace terminal {
const uint8_t ESCAPE_BYTE = 27;
const uint8_t CR_BYTE = 13;
const uint8_t LF_BYTE = 10;
bool isPrintableByte(uint8_t byte);
enum class ActionKeyType {
ENTER,
ARROW_UP,
ARROW_DOWN,
ARROW_LEFT,
ARROW_RIGHT,
NONE, // Placeholder key
END_OF_STREAM // special key for indicating byte stream's eof
};
/**
* Union type for keyboard key.
*/
struct Key {
enum class Type {
ACTION, /// type for special keyboard keys
PRINTABLE /// type for non-action keys
};
/**
* Type of key
*/
Type type;
union {
ActionKeyType action;
char printable;
};
/**
* Constructor for action key
*/
Key(Type type, ActionKeyType action): type(type), action(action) {}
/**
* Constructor for printable key
* @param type
* @param printable
*/
Key(Type type, char printable): type(type), printable(printable) {}
bool isAction() {
return type == Type::ACTION;
}
bool isPrintable() {
return type == Type::PRINTABLE;
}
};
struct ActionKey: public Key {
explicit ActionKey(ActionKeyType action): Key(Type::ACTION, action) {}
};
struct PrintableKey: public Key {
explicit PrintableKey(char printable): Key(Type::PRINTABLE, printable) {}
};
/**
* Class for mapping byte stream to keyboard events
*/
class KeyStream {
ByteStream &byte_stream;
std::optional<Key> processCsiSequence();
std::optional<Key> processEscapeSequence();
public:
/**
* Get next key from stream
*/
Key nextKey();
/**
* Constructs key stream as mapper from given byte stream
* @param byte_stream
*/
explicit KeyStream(ByteStream &byte_stream): byte_stream(byte_stream) {}
};
}
#endif //TELNET_SERVER_TERINAL_H