-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursesHandler.cpp
More file actions
311 lines (239 loc) · 8.79 KB
/
CursesHandler.cpp
File metadata and controls
311 lines (239 loc) · 8.79 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* CursesHandler.cpp
* by Dillon Bostwick
* see CursesHandler.h for details
*/
#include <string>
#include <iostream>
#include <stdlib.h>
#include <ncurses.h>
#include "CursesHandler.h"
#include "Job.h"
using namespace std;
//Calls to initiate I/O environment
CursesHandler::CursesHandler() {
initscr(); //startup ncurses and initialize the stdscr (terminal window object)
cbreak(); //disables line buffering
timeout(1); //set getch to non-blocking, allowing for "asynchronous" loop breaking
curs_set(0); //make the cursor invisible (makes everything look better)
echo(); //echo user input to the current location of the cursor
//Force the user to expand the terminal size until it is at MIN_WIDTH x MIN_HEIGHT
do {
getmaxyx(stdscr, consoleHeight, consoleWidth);
mvprintw(0, 0, "INCREASE TERMINAL SIZE (must be at least %d x %d)",
MIN_WIDTH, MIN_HEIGHT);
refresh();
} while (consoleHeight < MIN_HEIGHT || consoleWidth < MIN_WIDTH);
noecho(); //echo prints user input to the current location of the cursor. We want to
//start with noecho() by default for the main menu but can turn it on later
currentFeedRow = FEED_ROW;
refresh(); //Syncs the buffer with the stdscr window
}
//Returns the terminal to it's original state (if program terminates and
//there are some messy text wrapping issues in your terminal, it's probably because this
//destructor was not called at the right time)
CursesHandler::~CursesHandler() {
printw("\n"); //puts the command line cursor beneath where we were working
curs_set(1); //make cursor visible again
endwin(); //terminate NCureses mode and the stdscr object
cerr << "Sucessfully exited\n";
}
//////////////////////////////////////////////////////////////////////////////////////////
//Ensures input echoing is displayed in menu bar
void CursesHandler::CursesHandler::keep_cursor_in_menu(int num) {
move(MENU_ROW, 44 + num * 3);
}
//sets NCurses to take "asynchronous" I/O. If off, getch returns ERR if no key has been
//pressed, allowing it to be checked after each iteration of in an infinite loop
void CursesHandler::CursesHandler::blocking_off() {
timeout(1); //turn off input blocking (back to asynchronous)
noecho();
cbreak(); //returns characters one at a time
}
//If blocking is on, an input function will pause and wait until the user does something.
//I also want input to be echoed and the user presses enter to submit
void CursesHandler::CursesHandler::blocking_on() {
nodelay(stdscr, false); //turn on input blocking
echo();
nocbreak(); //waits for enter before a string of characters or integers is returned
}
//returns an integer from the user (make sure blocking is on)
int CursesHandler::get_int_input() {
char input[10];
getstr(input);
return atoi(input);
}
//returns whether or not the user pressed the 'y' character (make sure blocking is on)
bool CursesHandler::get_y_n() {
char yesno;
cbreak();
yesno = getch();
nocbreak();
echo();
return (yesno == 'y');
}
//print the main menu to the menu bar
void CursesHandler::main_menu() {
menu_bar("p = toggle pause. a = add job. f = add jobs from file. l = lookup. "
"k = kill. e = end");
}
//Menu bar////////////////////////////////////////////////////////////////////////////////
//Always takes a string.
void CursesHandler::CursesHandler::menu_bar(string str) {
move(MENU_ROW, 0);
clrtoeol(); //these lines clear the bar from its current state
mvprintw(MENU_ROW, COL_LOCATION, str.c_str()); //convert the std::string to a C string
refresh(); //Sync the buffer
}
//Console bar/////////////////////////////////////////////////////////////////////////////
//When printing to the console, you can optionally specify a line number to begin, and an
//integer at the end if you are printing a printf() style %d (if you pass a str with a
//printf() style break, mvprintw can interpret that str with the break)
void CursesHandler::CursesHandler::console_bar(int line, string str) {
move(CONSOLE_ROW + line, 0);
clrtoeol();
mvprintw(CONSOLE_ROW + line, COL_LOCATION, str.c_str());
refresh();
}
void CursesHandler::console_bar(int line, string str, int num) {
move(CONSOLE_ROW + line, 0);
clrtoeol();
mvprintw(CONSOLE_ROW + line, COL_LOCATION, str.c_str(), num);
refresh();
}
//Passed a vector of jobs, the console will print inline the list of PIDs up to 10 PIDs
void CursesHandler::console_bar(int line, const Job::JobList *list) {
move(CONSOLE_ROW + line, 0);
clrtoeol();
//if list is empty, just print "N/A"
if (list->empty()) {
mvprintw(CONSOLE_ROW + line, COL_LOCATION, "N/A");
refresh();
return;
}
//iterate and print until second to last element with commas
//NOTE: WHEN PRINTING INTEGERS, THE ROW IS THE LEFTMOST DIGIT, REGARDLESS OF # DIGITS
for (unsigned i = 0; i < list->size(); i++) {
printw("%d", list->at(i)->get_pid());
//stop printing after 10 elements
if (i == 10) {
printw("......(%d more jobs)", list->size() - 10);
break;
} else if (i != list->size() - 1) {
printw(", ");
}
}
refresh();
}
//Compatibility with a C style string -- always prints the string str first and then the
//char name[] immediately afterwards
void CursesHandler::console_bar(string str, char name[]) {
move(CONSOLE_ROW, 0);
clrtoeol();
mvprintw(CONSOLE_ROW, COL_LOCATION, str.c_str());
addstr(name); //prints a C style string to the current location of the cursor
}
void CursesHandler::console_bar(string str) {
console_bar(0, str);
}
void CursesHandler::console_bar(string str, int num) {
console_bar(0, str, num);
}
//Wipe all console lines away -- useful if some lower level lines linger and were not
//removed when an inline print function is called
void CursesHandler::clear_console() {
for (int i = CONSOLE_ROW; i < CONSOLE_ROW_MAX; i++) {
move(i, 0);
clrtoeol();
}
refresh();
}
//Status bar//////////////////////////////////////////////////////////////////////////////
void CursesHandler::status_bar( int row, string str) {
mvprintw(STATUS_ROW, row, str.c_str());
refresh();
}
void CursesHandler::status_bar(int row, string str, int num) {
mvprintw(STATUS_ROW, row, str.c_str(), num);
refresh();
}
void CursesHandler::status_bar(int line, int row, string str, int num) {
mvprintw(STATUS_ROW + line, row, str.c_str(), num);
refresh();
}
void CursesHandler::clear_status_bar() {
for (int i = STATUS_ROW; i < STATUS_ROW_MAX; i++) {
move(i, 0);
clrtoeol();
}
refresh();
}
//Paused and mode/////////////////////////////////////////////////////////////////////////
//The paused bar and mode bar are technically within the status bar area, but they have
//their own functions. Mode bar simply specifies whether quanta mode and/or weighting
//mode are selected, and the paused bar specifies whether the program is currently
//paused or running
void CursesHandler::paused_bar(bool paused) {
move(PAUSED_ROW, 0);
clrtoeol();
if (paused) {
mvprintw(PAUSED_ROW, COL_LOCATION, "~~Paused~~");
} else {
mvprintw(PAUSED_ROW, COL_LOCATION, "~~Running~~");
}
refresh();
}
void CursesHandler::mode_bar(bool varyQuanta, bool chainWeighting) {
move(MODE_ROW, 0);
clrtoeol();
if (varyQuanta) {
mvprintw(MODE_ROW, COL_LOCATION, "~~Quanta Mode~~");
}
if (chainWeighting) {
mvprintw(MODE_ROW, COL_LOCATION + 17, "~~Weighting Mode~~");
}
refresh();
}
//Core bar////////////////////////////////////////////////////////////////////////////////
//Print lines to the core bar, must specify a line when printing
void CursesHandler::core_bar(int line, string str, int num) {
move(CORE_ROW + line, 0);
clrtoeol();
mvprintw(CORE_ROW + line, COL_LOCATION, str.c_str(), num);
refresh();
}
//When the core bar clears, it always says "N/A"
void CursesHandler::clear_core_bar() {
for (int i = CORE_ROW; i < CORE_ROW_MAX; i++) {
move(i, 0);
clrtoeol();
}
mvprintw(CORE_ROW, 0, "N/A");
refresh();
}
//Feed bar////////////////////////////////////////////////////////////////////////////////
//The feed bar prints each line in place and iterates until the end of the bar, in which
//case it wraps back to the top. The "^^^" bar is always beneath the most recently printed
//line. In the future I hope use an array to cycle through where the most recent line
//is always at the top (e.g. a Facebook news feed)
void CursesHandler::feed_bar(string str, int num) {
if (currentFeedRow == FEED_ROW_MAX) {
currentFeedRow = FEED_ROW;
move(FEED_ROW_MAX, 0);
clrtoeol();
}
move(currentFeedRow, 0);
clrtoeol();
mvprintw(currentFeedRow, COL_LOCATION, str.c_str(), num);
currentFeedRow++;
mvprintw(currentFeedRow, COL_LOCATION, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
"^^^^^^^^^^");
refresh();
}
//Statitistcs bar/////////////////////////////////////////////////////////////////////////
void CursesHandler::CursesHandler::stats_bar(int line, string str, double num) {
move(STATS_ROW + line, 0);
clrtoeol();
mvprintw(STATS_ROW + line, COL_LOCATION, str.c_str(), num);
refresh();
}