Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
1
/*
2
* File: termmanip.h
3
* Purpose: To have fun messing with people's terminals, and making
4
* lots of colorful and interesting programs.
5
*
6
* This provides a nice little interface to the ANSI escape
7
* sequence set.
8
*/
9
#ifndef TERMMANIP_H
10
#define TERMMANIP_H
11
#include <iostream>
12
#include <string>
13
#include <sstream>
14
15
/*
16
* Graphic Mode Modifiers
17
*/
18
inline std::ostream& normal(std::ostream &os) {
19
return os << "\033[0m";
20
}
21
22
23
inline std::ostream& bold(std::ostream &os) {
24
return os << "\033[1m";
25
}
26
27
28
inline std::ostream& underline(std::ostream &os) {
29
return os << "\033[4m";
30
}
31
32
33
inline std::ostream& blink(std::ostream &os) {
34
return os << "\033[5m";
35
}
36
37
38
inline std::ostream& reverseVideo(std::ostream &os) {
39
return os << "\033[7m";
40
}
41
42
43
44
/*
45
* Foreground Color Graphic Mode Modifiers
46
*/
47
inline std::ostream& black(std::ostream &os) {
48
return os << "\033[30m";
49
}
50
51
52
inline std::ostream& red(std::ostream &os) {
53
return os << "\033[31m";
54
}
55
56
57
inline std::ostream& green(std::ostream &os) {
58
return os << "\033[32m";
59
}
60
61
62
inline std::ostream& yellow(std::ostream &os) {
63
return os << "\033[33m";
64
}
65
66
67
inline std::ostream& blue(std::ostream &os) {
68
return os << "\033[34m";
69
}
70
71
72
inline std::ostream& magenta(std::ostream &os) {
73
return os << "\033[35m";
74
}
75
76
77
inline std::ostream& cyan(std::ostream &os) {
78
return os << "\033[36m";
79
}
80
81
82
inline std::ostream& white(std::ostream &os) {
83
return os << "\033[37m";
84
}
85
86
/*
87
* Foreground Color Graphic Mode Modifiers
88
*/
89
inline std::ostream& blackBackground(std::ostream &os) {
90
return os << "\033[40m";
91
}
92
93
94
inline std::ostream& redBackground(std::ostream &os) {
95
return os << "\033[41m";
96
}
97
98
99
inline std::ostream& greenBackground(std::ostream &os) {
100
return os << "\033[42m";
101
}
102
103
104
inline std::ostream& yellowBackground(std::ostream &os) {
105
return os << "\033[43m";
106
}
107
108
109
inline std::ostream& blueBackground(std::ostream &os) {
110
return os << "\033[44m";
111
}
112
113
114
inline std::ostream& magentaBackground(std::ostream &os) {
115
return os << "\033[45m";
116
}
117
118
119
inline std::ostream& cyanBackground(std::ostream &os) {
120
return os << "\033[46m";
121
}
122
123
124
inline std::ostream& whiteBackground(std::ostream &os) {
125
return os << "\033[47m";
126
}
127
128
129
/*
130
* Simple terminal commands
131
*/
132
inline std::ostream& clearScreen(std::ostream &os) {
133
return os << "\033[2J";
134
}
135
136
137
inline std::ostream& clearLine(std::ostream &os) {
138
return os << "\033[K";
139
}
140
141
142
inline std::ostream& saveCursor(std::ostream &os) {
143
return os << "\033[s";
144
}
145
146
147
inline std::ostream& restoreCursor(std::ostream &os) {
148
return os << "\033[u";
149
}
150
151
152
/*
153
* This class is used by commands requiring arguments
154
*/
155
class ArgSequence {
156
public:
157
ArgSequence(std::string seq) {
158
this->seq = seq;
159
}
160
161
std::ostream& operator()(std::ostream &os) const {
162
return os << seq;
163
}
164
165
private:
166
std::string seq;
167
};
168
169
170
//overload the << operator so ostream can use the sequence!
171
inline std::ostream& operator<<(std::ostream& os, const ArgSequence& aSeq) {
172
return aSeq(os);
173
}
174
175
176
/*
177
* Commands requiring arguments
178
*/
179
inline ArgSequence cursorUp(int value) {
180
//compute the arg sequence
181
std::ostringstream os;
182
os << "\033[" << value << "A";
183
184
//return the result
185
return ArgSequence(os.str());
186
}
187
188
189
inline ArgSequence cursorDown(int value) {
190
//compute the arg sequence
191
std::ostringstream os;
192
os << "\033[" << value << "B";
193
194
//return the result
195
return ArgSequence(os.str());
196
}
197
198
199
inline ArgSequence cursorForward(int value) {
200
//compute the arg sequence
201
std::ostringstream os;
202
os << "\033[" << value << "C";
203
204
//return the result
205
return ArgSequence(os.str());
206
}
207
208
209
inline ArgSequence cursorBackward(int value) {
210
//compute the arg sequence
211
std::ostringstream os;
212
os << "\033[" << value << "D";
213
214
//return the result
215
return ArgSequence(os.str());
216
}
217
218
219
inline ArgSequence cursorPosition(int x, int y) {
220
//compute the arg sequence
221
std::ostringstream os;
222
os << "\033[" << y << ";" << x << "H";
223
224
//return the result
225
return ArgSequence(os.str());
226
}
227
228
229
inline std::ostream& cursorOff(std::ostream &os) {
230
return os << "\033[?25l";
231
}
232
233
234
inline std::ostream& cursorOn(std::ostream &os) {
235
return os << "\033[?25h";
236
}
237
238
#endif
239
240