-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-screen.h
176 lines (120 loc) · 3.81 KB
/
text-screen.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
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
/**
* \author: Rafal Banas
* Module provides tools for convenient rich text printing in terminal
*/
#ifndef TELNET_SERVER_TEXT_SCREEN_H
#define TELNET_SERVER_TEXT_SCREEN_H
#include "byte-stream.h"
#include "terminal.h"
#include <string>
#include <memory>
namespace terminal {
/**
* Namespace contains definitions of some terminal control sequences.
*/
namespace control {
const ByteStream::series_t &ClearScreenSeq();
const ByteStream::series_t &HideCursorSeq();
const ByteStream::series_t &ShowCursorSeq();
/**
* Interface for terminal control sequence.
*/
class TerminalSequence {
public:
/**
* Returns appropriate bytes sequence
*/
virtual ByteStream::series_t toBytes() = 0;
};
/**
* Moves cursor left by 'shift' columns
*/
class MoveHorizontally: public TerminalSequence {
int shift;
public:
ByteStream::series_t toBytes() override;
explicit MoveHorizontally(int shift): shift(shift) {}
};
/**
* Moves cursor down by 'shift' lines
*/
class MoveVertically: public TerminalSequence {
int shift;
public:
ByteStream::series_t toBytes() override;
explicit MoveVertically(int shift): shift(shift) {}
};
class ClearLine: public TerminalSequence {
public:
ByteStream::series_t toBytes() override;
};
class SaveCursorPosition: public TerminalSequence {
public:
ByteStream::series_t toBytes() override;
};
class RestoreCursorPosition: public TerminalSequence {
public:
ByteStream::series_t toBytes() override;
};
class SetStyle: public TerminalSequence {
int style;
public:
static const int REVERSED = 7;
static const int NORMAL = 0;
ByteStream::series_t toBytes() override;
explicit SetStyle(int style): style(style) {}
};
/**
* Regular text
*/
class Text: public TerminalSequence {
std::string text;
public:
ByteStream::series_t toBytes() override;
explicit Text(const std::string &text): text(text) {}
};
}
/**
* Class representing terminal screen.
*/
class TextScreen {
public:
using control_seq_t = std::shared_ptr<control::TerminalSequence>;
using line_t = std::vector<control_seq_t>;
using lines_sequence_t = std::vector<line_t>;
private:
lines_sequence_t lines_sequence;
public:
/**
* Constructs text screen on given number of lines
* @param lines_count
*/
explicit TextScreen(unsigned long lines_count): lines_sequence(lines_count) {};
/**
* Clear given line
*/
void clearLine(unsigned long line);
/**
* Clears whole reserved screen
*/
void clearScreen();
/**
* Write text in given place
*/
void writeAt(unsigned long line, int column, const std::string &text);
/**
* Write text in given place with reversed colors
*/
void writeReversedStyleAt(unsigned long line, int column, const std::string &text);
/**
* Render to byte message that should be printed.
* Clears all reserved lines and writes whole content in every bytes message.
*/
ByteStream::series_t renderToBytes();
/**
* Bytes that should be printed before working on text screen.
*/
ByteStream::series_t initialBytes();
};
}
#endif //TELNET_SERVER_TEXT_SCREEN_H