-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncurses_utils.cpp
More file actions
186 lines (149 loc) · 3.47 KB
/
ncurses_utils.cpp
File metadata and controls
186 lines (149 loc) · 3.47 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
// Author: Benjamin Johnson
// Date: July 31, 2017
// Purpose: Defines useful functions for handling the interface
#include <iostream>
#include <map>
#include <sstream>
#include <ncurses.h>
#include <panel.h>
#include "ncurses_utils.h"
using namespace std;
void NcursesUtils::init() {
initscr();
raw();
keypad(stdscr, TRUE);
start_color();
init_pair(1, COLOR_BLACK, COLOR_WHITE);
noecho();
refresh();
}
void NcursesUtils::addWin(string name, int h, int w, int y, int x, BoxType b) {
Window nwin;
// make window
nwin.win = newwin(h, w, y, x);
nwin.b = b;
// add to panel
nwin.panel = new_panel(nwin.win);
hide_panel(nwin.panel);
wins.insert(make_pair(name, nwin));
// box it
boxWin(name, b);
}
void NcursesUtils::addLayout(string name, vector<string> winlist, bool owb, bool csb) {
Layout nlay;
// make layout
nlay.l = winlist;
nlay.overwrite = owb;
nlay.clearScreen = csb;
// insert
layouts.insert(make_pair(name, nlay));
visible.insert(make_pair(name, false));
}
void NcursesUtils::showLayout(string name) {
map<string, Layout>::iterator lit, slit;
lit = layouts.find(name);
// clear screen
// TODO: restructure probably
if (lit->second.clearScreen) {
for (slit = layouts.begin(); slit != layouts.end(); slit++) {
if (slit->second.overwrite && visible.find(slit->first)->second == true) {
hideLayout(slit->first);
}
}
}
for (int i = 0; i < lit->second.l.size(); i++) {
show_panel(wins.find(lit->second.l[i])->second.panel);
}
// set showing bool
visible[name] = true;
// update screen
update_panels();
doupdate();
}
void NcursesUtils::hideLayout(string name) {
map<string, Layout>::iterator lit;
lit = layouts.find(name);
for (int i = 0; i < lit->second.l.size(); i++) {
hide_panel(wins.find(lit->second.l[i])->second.panel);
}
// set showing bool
visible.find(name)->second = false;
// update screen
update_panels();
doupdate();
}
bool NcursesUtils::showing(string name) {
return visible.find(name)->second;
}
void NcursesUtils::printWin(string name, int h, int w, string s) {
mvwprintw(getWin(name), h, w, s.c_str());
wrefresh(getWin(name));
}
void NcursesUtils::clearWin(string name) {
Window w = wins.find(name)->second;
werase(w.win);
boxWin(name, w.b);
wrefresh(w.win);
}
void NcursesUtils::boxWin(string name, BoxType b) {
// TODO: ADD BOX TYPE CHANGE STAY
switch (b) {
case NO_BOX:
break;
case BOX:
box(getWin(name), 0 , 0);
break;
case INPUT_BOX:
wborder(getWin(name), ' ', ' ', ' ', 0, ' ', ' ', 0, 0);
break;
default: break;
}
}
// lol doesn't work for some reason
void NcursesUtils::clrPrintWin(string name, int h, int w, string s) {
clearWin(name);
printWin(name, h, w, s);
}
// go-getters -------------------------------------------------------
WINDOW* NcursesUtils::getWin(string name) {
return wins.find(name)->second.win;
}
string NcursesUtils::getInput(string name) {
int cc = 2;
char ch;
string s;
stringstream ss;
// setup
//echo();
cbreak();
curs_set(1);
wmove(getWin(name), 1, 2);
//wrefresh(getWin(name));
// get it
// wgetstr(getWin(name), s);
while (1) {
ch = wgetch(getWin(name));
switch (ch) {
case 10: // enter
clearWin(name);
curs_set(0);
return s;
case 27: // esc
clearWin(name);
curs_set(0);
return "";
case 127: // backspace
if (s.size() > 0) {
s = s.substr(0, s.size() - 1);
cc--;
}
break;
default:
s = s + ch;
}
// display
cc++;
clearWin(name);
printWin(name, 1, 2, s);
}
}