-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgBox.cpp
More file actions
193 lines (161 loc) · 5.5 KB
/
MsgBox.cpp
File metadata and controls
193 lines (161 loc) · 5.5 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
#include "MsgBox.hpp"
#include <curses.h>
#include <algorithm>
using std::remove;
#include <vector>
using std::vector;
#include <sstream>
#include <string>
using std::string;
using std::to_string;
#include <cmath>
using std::ceil;
MsgBox::MsgBox(WINDOW* msg_win, std::mutex& new_drawing_mutex)
: drawing_mutex(new_drawing_mutex){
win = msg_win;
draw();
}
vector<string> wrap(string msg, int width) {
vector<string> lines;
int chunks = ceil(msg.size() / (double)width);
for (int i = 0; i < chunks; i++) {
lines.push_back(msg.substr(i * width, width));
}
return lines;
}
// "foo bar baz boo" → "foo bar b…"
string truncate(string msg, int width){
if(width < 0) return "";
if(msg.size() > width)
return msg.substr(0,width-1) + "…";
else
return msg;
}
void MsgBox::draw() {
drawing_mutex.lock();
int width, height;
getmaxyx(win, height, width);
werase(win);
if (width > 3 && height > 3){ // Else too small to draw
box(win, 0, 0);
// Just shove to screen the last messages
int Nmessages = height - 2; // The two borders!
for(int i = 0; i < Nmessages; i++){
// Mind the two borders when computing the width
try {
string msg = scrollback_buffer.at(last_msg_idx - i);
msg = truncate(msg, width - 2); // The borders!
// Color special messages in different colors.
if (msg.substr(0,8) == "Termsim:"){
wattron(win, A_BOLD);
mvwprintw(win, height - 2 - i, 1, msg.substr(0,8).c_str());
wattroff(win, A_BOLD);
mvwprintw(win, height - 2 - i, 1+8, msg.substr(8).c_str());
}
else if (msg.substr(0,5) == "Info:"){
wattron(win, A_BOLD);
wattron(win, COLOR_PAIR(1));
mvwprintw(win, height - 2 - i, 1, msg.substr(0,5).c_str());
wattroff(win, COLOR_PAIR(1));
wattroff(win, A_BOLD);
mvwprintw(win, height - 2 - i, 1+5, msg.substr(5).c_str());
}
else if (msg.substr(0,8) == "Warning:"){
wattron(win, A_BOLD);
wattron(win, COLOR_PAIR(2));
mvwprintw(win, height - 2 - i, 1, msg.substr(0,8).c_str());
wattroff(win, COLOR_PAIR(2));
wattroff(win, A_BOLD);
mvwprintw(win, height - 2 - i, 1+8, msg.substr(8).c_str());
}
else if (msg.substr(0,6) == "Error:"){
wattron(win, A_BOLD);
wattron(win, COLOR_PAIR(3));
mvwprintw(win, height - 2 - i, 1, msg.substr(0,6).c_str());
wattroff(win, COLOR_PAIR(3));
wattroff(win, A_BOLD);
mvwprintw(win, height - 2 - i, 1+6, msg.substr(6).c_str());
}
else {
mvwprintw(win, height - 2 - i, 1, msg.c_str());
}
} catch (std::out_of_range) {
// Don't print anything, not enough lines of output yet.
}
}//for Nmessages
}//if enough big
// Draw scroll-lock and num of msgs
if(!autoscroll)
mvwprintw(win, 0, width-12, "SCROLL LOCK");
string msg_indicator =
to_string(last_msg_idx+1) +
"/" +
to_string(scrollback_buffer.size());
mvwprintw(win, 0, 1, msg_indicator.c_str());
mvwprintw(win, height-2, 0, "|"); // Last msg indicator
refresh();
wrefresh(win);
drawing_mutex.unlock();
}
void trim_whitespace(string& str){
// str.erase( std::remove( str.begin(), str.end(), '\n' ), str.end() );
auto whitespace = "\n\t\r ";
auto start = str.find_first_not_of(whitespace);
auto end = str.find_last_not_of(whitespace);
str = str.substr(start, 1+end-start);
}
std::vector<std::string> split_string(std::string str, char delim) {
std::stringstream sstr(str);
std::string segment;
std::vector<std::string> seglist;
while (std::getline(sstr, segment, delim)) {
seglist.push_back(segment);
}
return seglist;
}
// This supposes that the msg has no escape chars anywhere except at
// the end, where one can have either \n or \r. If \r and the last one
// was also an \r, overwrite it.
void MsgBox::insert_msg(std::string str){
static bool last_was_cr = false;
auto L = str.size();
// Remember to trim the special character.
if (str.back() == '\n'){
scrollback_buffer.push_back(str.substr(0,L-1));
last_was_cr = false;
}
else if (str.back() == '\r'){
if (last_was_cr)
scrollback_buffer.back() = str.substr(0,L-1);
else
scrollback_buffer.push_back(str.substr(0,L-1));
last_was_cr = true;
}
else {
scrollback_buffer.push_back(str);
last_was_cr = false;
}
if(autoscroll) last_msg_idx = scrollback_buffer.size()-1;
draw();
}
void MsgBox::scroll_down(){
if (last_msg_idx < scrollback_buffer.size()-1) ++last_msg_idx;
draw();
}
void MsgBox::scroll_up(){
if (last_msg_idx > 0) --last_msg_idx;
draw();
}
void MsgBox::go_to_beginning() {
last_msg_idx = 0;
draw();
}
void MsgBox::go_to_end() {
last_msg_idx = scrollback_buffer.size()-1;
draw();
}
void MsgBox::toggle_autoscroll() {
if(!autoscroll) go_to_end(); // Returning from it.
autoscroll = !autoscroll;
draw();
}