-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncurses.c
232 lines (190 loc) · 6.29 KB
/
ncurses.c
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
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include "mfst.h"
#include "ncurses.h"
/**
* Splits a string into multiple strings that are each less than or equal to
* the given number of characters. If the string is longer than the
* maximum_line_length, this function attempts to split the line on the closest
* word boundary; otherwise, it splits the line mid-word. Additionally, strings
* with line feeds are split on the line feed characters (which are removed from
* the resulting output).
*
* @param str The input string to parse.
* @param max_line_length The maximum length of each string in the output.
* @param string_count A pointer to an int that will receive the number of
* strings in the output array.
*
* @returns A pointer to an array of null-terminated strings. Both the array
* and each of the strings in the array are allocated by this function
* using malloc() and must be free()'d once the caller is done with
* them.
*/
char **wordwrap(char *str, int max_line_length, int *string_count) {
char *work_str, **output, **output_new, *curptr, *tmpptr;
int work_str_len, cur_str_len, output_count, i;
output = NULL;
output_count = 0;
// Don't modify the original string
if(!(work_str = strdup(str))) {
return NULL;
}
work_str_len = strlen(work_str);
// Make a first pass through the string and replace any \n's with NULLs
for(i = 0; i <= work_str_len; i++) {
if(work_str[i] == '\n') {
work_str[i] = 0;
}
}
// Second pass: break strings that are longer than the maximum line length
// using spaces
for(curptr = work_str; curptr <= (work_str + work_str_len); curptr += strlen(curptr) + 1) {
cur_str_len = strlen(curptr);
if((cur_str_len = strlen(curptr)) > max_line_length) {
// Set tmpptr to the last character in the string, then work
// backwards until it finds something it can break on or it hits the
// beginning of the string
for(tmpptr = curptr + max_line_length; tmpptr >= curptr; tmpptr--) {
if(*tmpptr == ' ' || *tmpptr == '\t') {
*tmpptr = 0;
break;
}
}
}
}
// Third pass: duplicate all strings and place them in the output array.
// While we're at it, split any strings that are still longer than the
// maximum line length (e.g., those strings that contained a single word
// that was longer than the maximum line length).
for(curptr = work_str; curptr <= (work_str + work_str_len); curptr += strlen(curptr) + 1) {
cur_str_len = strlen(curptr);
if(!(output_new = realloc(output, sizeof(char *) * ++output_count))) {
if(output) {
for(i = 0; i < output_count - 1; i++) {
free(output[i]);
}
free(output);
free(work_str);
return NULL;
}
}
output = output_new;
if(cur_str_len >= max_line_length) {
cur_str_len = max_line_length;
}
if(!(output[output_count - 1] = malloc(cur_str_len + 1))) {
for(i = 0; i < output_count - 1; i++) {
free(output[i]);
}
free(output);
free(work_str);
return NULL;
}
snprintf(output[output_count - 1], cur_str_len + 1, "%s", curptr);
curptr[cur_str_len] = 0;
}
free(work_str);
*string_count = output_count;
return output;
}
WINDOW *message_window(WINDOW *parent, const char *title, char *msg, char wait) {
WINDOW *window;
int lines, len, longest, i;
char **split;
if(program_options.no_curses) {
return NULL;
}
// Split the string so that it takes up a max of 75% of the screen width.
if(!(split = wordwrap(msg, (COLS * 4) / 5, &lines))) {
return NULL;
}
// Now figure out the actual length of the longest line
longest = 0;
for(i = 0; i < lines; i++) {
len = strlen(split[i]);
if(len > longest) {
longest = len;
}
}
if(title) {
len = strlen(title);
if(len > longest) {
longest = len;
}
}
if(wait) {
// If the "Press Enter to continue" line is longer than the longest line,
// increase the length of the longest line to 23.
if(longest < 23) {
longest = 23;
}
}
// If there are more rows than there are lines on the display, abort.
if((lines + 2) > LINES) {
for(i = 0; i < lines; i++) {
free(split[i]);
}
free(split);
return NULL;
}
window = newwin(lines + 2 + (wait ? 2 : 0), longest + 4, (LINES - (lines + 2 + (wait ? 2 : 0))) / 2, (COLS - (longest + 4)) / 2);
nodelay(window, TRUE);
werase(window);
box(window, 0, 0);
if(title) {
wattron(window, A_BOLD);
mvwprintw(window, 0, ((longest + 4) - (len + 2)) / 2, " %s ", title);
wattroff(window, A_BOLD);
}
for(i = 0; i < lines; i++) {
mvwaddstr(window, i + 1, 2, split[i]);
free(split[i]);
}
free(split);
if(wait) {
wattron(window, A_BOLD);
mvwaddstr(window, lines + 2, (longest - 19) / 2, "Press Enter to continue");
wattroff(window, A_BOLD);
}
wrefresh(window);
if(wait) {
while(handle_key_inputs(window) != '\r') {
napms(100);
}
erase_and_delete_window(window);
return NULL;
} else {
return window;
}
}
int handle_key_inputs(WINDOW *curwin) {
int key, width, height;
if(curwin) {
key = wgetch(curwin);
} else {
key = getch();
}
if(key == KEY_RESIZE) {
if(curwin) {
getmaxyx(curwin, height, width);
mvwin(curwin, (LINES - height) / 2, (COLS - width) / 2);
}
clear();
redraw_screen();
if(curwin) {
touchwin(curwin);
}
refresh();
return ERR;
}
return key;
}
void erase_and_delete_window(WINDOW *window) {
if(!program_options.no_curses) {
werase(window);
touchwin(stdscr);
wrefresh(window);
delwin(window);
}
}