-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwelcome2.cpp
More file actions
240 lines (203 loc) · 5.3 KB
/
welcome2.cpp
File metadata and controls
240 lines (203 loc) · 5.3 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
/*TODO : curs_border(3X) may be used later*/
#ifndef WELCOME2_CPP
#define WELCOME2_CPP
#include "RawTerm.hpp"
extern time_printing timeObj;
extern log_class logObj;
extern int flag;
extern class_mem memory;
const int TEXT_WIDTH = 78;
WINDOW* win;
vector<string> lines_buffer;
/********************-CLASS-**********************/
class conversion {
private:
protected:
public:
char* str;
char *partition[20], *head[20];
int len, div_len;
conversion();
conversion(const string& s);
void call_partition();
void display(int start_col);
};
/******************-FUNCTIONS-*********************/
conversion::conversion() { // Default constructor
str = new char;
len = 0;
}
conversion::conversion(const string& s) { // copy constructor
len = s.size();
div_len = len / TEXT_WIDTH;
str = new char[len + 1];
for (int i = 0; i < 20; i++) {
partition[i] = new char[TEXT_WIDTH];
head[i] = partition[i];
}
for (int i = 0; i < len; i++) {
str[i] = s[i];
}
str[len] = '\0'; // C strings must end with a '\0'
call_partition();
}
void conversion::call_partition() { // actual partition happens here
for (int i = 0; i < div_len + 1; i++) {
partition[i] = head[i];
for (int j = 0; j < TEXT_WIDTH; j++) {
// if (timeObj.get_enable_status() && j == 0)
// *(partition[i]++) = str[(TEXT_WIDTH - 15) * i + j];
// else
*(partition[i]++) = str[TEXT_WIDTH * i + j];
}
}
}
void conversion::display(
int start_col) { // displays the string in partitioned format
string final_line = "";
if (timeObj.get_enable_status()) {
final_line += print_time() + string(4, ' ');
} else {
final_line += string(16, ' ');
}
for (int i = 0; i <= div_len; i++) {
partition[i] = head[i];
final_line += partition[i];
lines_buffer.push_back(final_line);
if (logObj.get_log_status()) {
logObj.generate_log(final_line.c_str());
}
mvwprintw(win, start_col + i, 0, final_line.c_str());
final_line = string(16, ' ');
}
refresh();
wrefresh(win);
}
void init_displayer() { // initializes the ncurses window variables
initscr();
start_color();
cbreak();
noecho();
win = newwin(51, 200, 1, 1);
keypad(win, TRUE);
init_pair(1, COLOR_YELLOW, COLOR_BLACK);
}
void trans_rec_win() { // 1st function called in this file
init_displayer();
displayer();
input_display();
}
void displayer() { // draws the display window
mvwvline(win, 1, 95, '|', 44);
wattron(win, COLOR_PAIR(1));
wattron(win, A_BOLD);
wattron(win, A_STANDOUT);
mvwprintw(win, 0, 45, "TX");
mvwprintw(win, 0, 140, "RX");
wattroff(win, A_STANDOUT);
wattroff(win, COLOR_PAIR(1));
mvwhline(win, 45, 1, '-', 180);
wmove(win, 46, 0);
wclrtoeol(win);
clear_lines();
curs_set(1);
mvwprintw(win, 46, 5, "Enter here -->");
wattroff(win, A_BOLD);
if (flag) {
lines_buffer.clear();
}
for (size_t i = 0; i < lines_buffer.size(); i++) {
// (i + 1) to account for the RX and TX headers
mvwprintw(win, i + 1, 0, lines_buffer[i].c_str());
}
refresh();
wrefresh(win);
wmove(win, 46, 20);
}
void displayer(int y, int x, const string& lin) {
displayer();
mvwprintw(win, 46, 20, lin.c_str());
refresh();
wrefresh(win);
wmove(win, y, x);
}
void input_display() { // program stays here for most of the time, waiting for
// user input
string line, time;
int i = 1, col = 0;
int ch;
lines_buffer.clear();
while (ch = wgetch(win)) {
if (ch == '\n') {
memory.devObj.write(line.c_str());
if (flag) i = 1;
if (line.size() < TEXT_WIDTH) {
string final_line;
if (timeObj.get_enable_status()) {
final_line = print_time() + string(4, ' ') + line;
} else {
final_line = string(16, ' ') + line;
}
if (logObj.get_log_status()) {
logObj.generate_log(final_line.c_str());
}
lines_buffer.push_back(final_line);
mvwprintw(win, i, 0, final_line.c_str()); // line.substr(2,6));
refresh();
wrefresh(win);
flag = 0;
} else {
i = truncater(line, i);
}
i++;
clear_lines();
line.clear();
col = 0;
wmove(win, 46, 20);
} else if (ch == KEY_BACKSPACE) {
if (!col) continue;
line.pop_back();
clear_lines();
mvwprintw(win, 46, 20, line.c_str());
wmove(win, 46, 20 + --col);
} else if (ch == KEY_F(1)) {
int x, y;
getyx(win, y, x);
wclear(win);
wrefresh(win);
menu_call();
displayer(y, x, line);
refresh();
wrefresh(win);
} else {
line.push_back(ch);
mvwprintw(win, 46, 20, line.c_str());
wmove(win, 46, 20 + ++col);
}
}
endwin();
}
void clear_lines(void) { // clears unnecessary line
wmove(win, 46, 20);
wclrtoeol(win);
wmove(win, 47, 0);
wclrtoeol(win);
wmove(win, 48, 0);
wclrtoeol(win);
wmove(win, 49, 0);
wclrtoeol(win);
wmove(win, 50, 0);
wclrtoeol(win);
}
int truncater(const string& line,
int start_row) { // makes a class object and passes the string
// over there, to be partitioned
int inc;
conversion classy(line);
classy.display(start_row);
inc = (line.size() / TEXT_WIDTH) + 1;
start_row += inc;
lines_buffer.push_back(string());
return start_row;
}
#endif