-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermfuncs.cpp
More file actions
279 lines (241 loc) · 6.02 KB
/
Copy pathtermfuncs.cpp
File metadata and controls
279 lines (241 loc) · 6.02 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// -- NOTE TO COMP11 STUDENTS ----------------
//
// DO NOT EDIT OR COPY CODE FROM THIS FILE
// TO USE THESE FUNCTIONS:
// at the top of your code put: #include "termfuncs.h"
// when you compile: clang++ -Wall myprog.cpp termfuncs.cpp
//
// DO NOT DO THIS:
// #include "termfuncs.cpp"
//
//
// termfuncs.cpp -- some simple functions for using the terminal nicely
//
// char getachar() -- returns next char with no echo and no Enter needed
// char getacharnow(ds) -- only waits ds/10th seconds
// returns '\0' if no input by that time
// void screen_clear() -- clears the screen
// void screen_home() -- moves cursor to top of screen
// void screen_fg(string color)
// void screen_bg(string color)
// void screen_attr(string attr)
// void screen_bright()
// void screen_reset()
// void place_cursor(int, int); -- move the cursor to row col
// void place_char(char, int, int) -- move and place
// void hide_cursor()
// void show_cursor()
// int get_screen_cols();
// int get_screen_rows();
//
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
using namespace std;
static const string CSI = "\033[";
static string color_names[] = { "black", "red", "green", "yellow",
"navy", "pink", "blue", "white" };
static const int num_colors = 8;
static string attr_names[] = { "reset", "bright", "dim", "",
"underscore", "blink", "",
"reverse", "hidden" };
static const int num_attrs = 9;
static termios prev_tty_state;
static int prev_state_stored = 0;
//
// getachar
// returns a char using noecho and -icanon unless not a tty
//
char getachar()
{
char c;
cout << std::flush;
if (isatty(0)) {
struct termios info, orig;
// system("stty -echo -icanon");
tcgetattr(0, &info);
orig = info;
prev_tty_state = orig;
prev_state_stored = 1;
info.c_lflag &= ~ECHO;
info.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &info);
read(0, &c, 1);
// system("stty echo icanon");
tcsetattr(0, TCSANOW, &orig);
} else {
read(0, &c, 1);
}
return c;
}
//
// getacharnow
// returns a char using noecho and -icanon unless not a tty
//
char getacharnow(int decisecs)
{
char c;
cout << std::flush;
if (isatty(0)) {
struct termios info, orig;
// system("stty -echo -icanon");
tcgetattr(0, &info);
orig = info;
prev_tty_state = orig;
prev_state_stored = 1;
info.c_lflag &= ~ECHO;
info.c_lflag &= ~ICANON;
info.c_cc[VMIN] = 0;
info.c_cc[VTIME] = decisecs;
tcsetattr(0, TCSANOW, &info);
if (read(0, &c, 1) != 1)
c = '\0';
tcsetattr(0, TCSANOW, &orig);
// system("stty echo icanon");
} else {
if (read(0, &c, 1) != 1)
c = '\0';
}
return c;
}
static void restore_tty_state()
{
if (prev_state_stored)
tcsetattr(0, TCSANOW, &prev_tty_state);
}
//
// clear the terminal screen
//
void screen_clear()
{
cout << (CSI + "H");
cout << (CSI + "2J");
}
//
// Move the cursor to the home position (upper left corner)
//
void screen_home()
{
cout << (CSI + "H");
}
static void (*prev_handler)(int) = NULL;;
//
// Make the cursor invisible
//
void hide_cursor()
{
void on_sigint(int);
prev_handler = signal(SIGINT, on_sigint);
cout << (CSI + "?25l");
}
//
// make the cursor visible
//
void show_cursor()
{
cout << (CSI + "?25h");
}
//
// To do if program gets a SIGINT (e. g., if someone hits Ctrl-c
//
void on_sigint(int s)
{
cout << std::flush;
show_cursor();
if ( prev_handler ){
prev_handler(s);
}
restore_tty_state();
exit(SIGINT);
}
//
// lookup a string in an array
// args: string to find, list of strings, len of list
// rets: index of string or -1 for not found
//
static int lookup(string findme, string list[], int num)
{
int i;
for (i = 0; i < num; i++) {
if (list[i] == findme)
return i;
}
return -1;
}
//
// set screen foreground color
//
void screen_fg(string color)
{
int num = lookup(color, color_names, num_colors);
if (num >= 0) {
cout << CSI << ( 30 + num ) << "m";
}
}
//
// set screen background color
//
void screen_bg(string color)
{
int num = lookup(color, color_names, num_colors);
if (num >= 0) {
cout << CSI << ( 40 + num ) << "m";
}
}
void screen_attr(string attr)
{
int num = lookup(attr, attr_names, num_attrs);
if ( num >= 0 ){
cout << CSI << num << "m";
}
}
void screen_bright()
{
screen_attr("bright");
}
//
// Put screen back in it's normal state
//
void screen_reset()
{
screen_attr("reset");
}
//
// Put cursor at given row and column
//
void place_cursor(int row, int col)
{
cout << "\033[" << (row + 1) << ";" << (col + 1) << "H";
flush(cout);
}
//
// Place the character c at the given row and column
//
void place_char(char c, int row, int col)
{
place_cursor(row, col);
cout << c;
flush(cout);
}
#include <sys/ioctl.h>
//
// returns number of rows on the screen
//
int get_screen_rows()
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_row;
}
//
// returns number of cols on the screen
//
int get_screen_cols()
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
}