Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

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