xref: /freebsd/contrib/dialog/mixedgauge.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*
2  *  $Id: mixedgauge.c,v 1.37 2021/01/16 17:19:15 tom Exp $
3  *
4  *  mixedgauge.c -- implements the mixedgauge dialog
5  *
6  *  Copyright 2007-2020,2021	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  *
23  *  This is inspired by a patch from Kiran Cherupally
24  *  (but different interface design).
25  */
26 
27 #include <dialog.h>
28 
29 #define LLEN(n) ((n) * MIXEDGAUGE_TAGS)
30 #define ItemData(i)    &items[LLEN(i)]
31 #define ItemName(i)    items[LLEN(i)]
32 #define ItemText(i)    items[LLEN(i) + 1]
33 
34 #define MIN_HIGH (4)
35 #define MIN_WIDE (10 + 2 * (2 + MARGIN))
36 
37 typedef struct {
38     WINDOW *dialog;
39     WINDOW *caption;
40     const char *title;
41     char *prompt;
42     int height, old_height, min_height;
43     int width, old_width, min_width;
44     int len_name, len_text;
45     int item_no;
46     DIALOG_LISTITEM *list;
47 } DIALOG_MIXEDGAUGE;
48 
49 static const char *
50 status_string(char *given, char **freeMe)
51 {
52     const char *result;
53 
54     *freeMe = 0;
55     if (isdigit(UCH(*given))) {
56 	switch (*given) {
57 	case '0':
58 	    result = _("Succeeded");
59 	    break;
60 	case '1':
61 	    result = _("Failed");
62 	    break;
63 	case '2':
64 	    result = _("Passed");
65 	    break;
66 	case '3':
67 	    result = _("Completed");
68 	    break;
69 	case '4':
70 	    result = _("Checked");
71 	    break;
72 	case '5':
73 	    result = _("Done");
74 	    break;
75 	case '6':
76 	    result = _("Skipped");
77 	    break;
78 	case '7':
79 	    result = _("In Progress");
80 	    break;
81 	case '8':
82 	    result = "";
83 	    break;
84 	case '9':
85 	    result = _("N/A");
86 	    break;
87 	default:
88 	    result = "?";
89 	    break;
90 	}
91     } else if (*given == '-') {
92 	size_t need = strlen(++given) + 4;
93 	char *temp = dlg_malloc(char, need);
94 	*freeMe = temp;
95 	sprintf(temp, "%3s%%", given);
96 	result = temp;
97     } else if (!isspace(UCH(*given))) {
98 	result = given;
99     } else {
100 	result = 0;
101     }
102     return result;
103 }
104 
105 /* This function displays status messages */
106 static void
107 myprint_status(DIALOG_MIXEDGAUGE * dlg)
108 {
109     WINDOW *win = dlg->dialog;
110     int limit_y = dlg->height;
111     int limit_x = dlg->width;
112 
113     int item;
114     int cells = dlg->len_text - 2;
115     int lm = limit_x - dlg->len_text - 1;
116     int bm = limit_y;		/* bottom margin */
117     int last_y = 0, last_x = 0;
118     int j, xxx;
119     float percent;
120     char *freeMe = 0;
121 
122     bm -= (2 * MARGIN);
123     getyx(win, last_y, last_x);
124     for (item = 0; item < dlg->item_no; ++item) {
125 	const char *status = "";
126 	chtype attr = A_NORMAL;
127 	int y = item + MARGIN + 1;
128 
129 	if (y > bm)
130 	    break;
131 
132 	status = status_string(dlg->list[item].text, &freeMe);
133 	if (status == 0 || *status == 0) {
134 	    free(freeMe);
135 	    continue;
136 	}
137 
138 	(void) wmove(win, y, 2 * MARGIN);
139 	dlg_attrset(win, dialog_attr);
140 	dlg_print_text(win, dlg->list[item].name, lm, &attr);
141 
142 	(void) wmove(win, y, lm);
143 	(void) waddch(win, '[');
144 	(void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
145 	if (freeMe) {
146 	    (void) wmove(win, y, lm + 1);
147 	    dlg_attrset(win, title_attr);
148 	    for (j = 0; j < cells; j++)
149 		(void) waddch(win, ' ');
150 
151 	    (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
152 	    (void) waddstr(win, status);
153 
154 	    if ((title_attr & A_REVERSE) != 0) {
155 		dlg_attroff(win, A_REVERSE);
156 	    } else {
157 		dlg_attrset(win, A_REVERSE);
158 	    }
159 	    (void) wmove(win, y, lm + 1);
160 
161 	    if (sscanf(status, "%f%%", &percent) != 1)
162 		percent = 0.0;
163 	    xxx = (int) ((cells * (percent + 0.5)) / 100.0);
164 	    for (j = 0; j < xxx; j++) {
165 		chtype ch1 = winch(win);
166 		if (title_attr & A_REVERSE) {
167 		    ch1 &= ~A_REVERSE;
168 		}
169 		(void) waddch(win, ch1);
170 	    }
171 	    free(freeMe);
172 
173 	} else {
174 	    (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
175 	    (void) waddstr(win, status);
176 	}
177 	(void) wmove(win, y, limit_x - 3);
178 	dlg_attrset(win, dialog_attr);
179 	(void) waddch(win, ']');
180 	(void) wnoutrefresh(win);
181     }
182     if (win != 0)
183 	wmove(win, last_y, last_x);
184 }
185 
186 static void
187 mydraw_mixed_box(WINDOW *win, int y, int x, int height, int width,
188 		 chtype boxchar, chtype borderchar)
189 {
190     dlg_draw_box(win, y, x, height, width, boxchar, borderchar);
191     {
192 	chtype attr = A_NORMAL;
193 	const char *message = _("Overall Progress");
194 	chtype save2 = dlg_get_attrs(win);
195 	dlg_attrset(win, title_attr);
196 	(void) wmove(win, y, x + 2);
197 	dlg_print_text(win, message, width, &attr);
198 	dlg_attrset(win, save2);
199     }
200 }
201 
202 static char *
203 clean_copy(const char *string)
204 {
205     char *result = dlg_strclone(string);
206 
207     dlg_trim_string(result);
208     dlg_tab_correct_str(result);
209     return result;
210 }
211 
212 /*
213  * Update mixed-gauge dialog (may be from pipe, may be via direct calls).
214  */
215 static void
216 dlg_update_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int percent)
217 {
218     int i, x;
219 
220     /*
221      * Clear the area for the progress bar by filling it with spaces
222      * in the title-attribute, and write the percentage with that
223      * attribute.
224      */
225     (void) wmove(dlg->dialog, dlg->height - 3, 4);
226     dlg_attrset(dlg->dialog, gauge_attr);
227 
228     for (i = 0; i < (dlg->width - 2 * (3 + MARGIN)); i++)
229 	(void) waddch(dlg->dialog, ' ');
230 
231     (void) wmove(dlg->dialog, dlg->height - 3, (dlg->width / 2) - 2);
232     (void) wprintw(dlg->dialog, "%3d%%", percent);
233 
234     /*
235      * Now draw a bar in reverse, relative to the background.
236      * The window attribute was useful for painting the background,
237      * but requires some tweaks to reverse it.
238      */
239     x = (percent * (dlg->width - 2 * (3 + MARGIN))) / 100;
240     if ((title_attr & A_REVERSE) != 0) {
241 	dlg_attroff(dlg->dialog, A_REVERSE);
242     } else {
243 	dlg_attrset(dlg->dialog, A_REVERSE);
244     }
245     (void) wmove(dlg->dialog, dlg->height - 3, 4);
246     for (i = 0; i < x; i++) {
247 	chtype ch = winch(dlg->dialog);
248 	if (title_attr & A_REVERSE) {
249 	    ch &= ~A_REVERSE;
250 	}
251 	(void) waddch(dlg->dialog, ch);
252     }
253     myprint_status(dlg);
254     dlg_trace_win(dlg->dialog);
255 }
256 
257 /*
258  * Setup dialog.
259  */
260 static void
261 dlg_begin_mixedgauge(DIALOG_MIXEDGAUGE * dlg,
262 		     int *began,
263 		     const char *aTitle,
264 		     const char *aPrompt,
265 		     int aHeight,
266 		     int aWidth,
267 		     int aItemNo,
268 		     char **items)
269 {
270     int y, x;
271 
272     if (!*began) {
273 	int n;
274 
275 	curs_set(0);
276 
277 	memset(dlg, 0, sizeof(*dlg));
278 	dlg->title = aTitle;
279 	dlg->prompt = clean_copy(aPrompt);
280 	dlg->height = dlg->old_height = aHeight;
281 	dlg->width = dlg->old_width = aWidth;
282 	dlg->item_no = aItemNo;
283 
284 	dlg->list = dlg_calloc(DIALOG_LISTITEM, (size_t) aItemNo);
285 	assert_ptr(dlg->list, "dialog_mixedgauge");
286 
287 	dlg->len_name = 0;
288 	dlg->len_text = 15;
289 
290 	for (n = 0; n < aItemNo; ++n) {
291 	    int thisWidth = (int) strlen(ItemName(n));
292 	    if (dlg->len_name < thisWidth)
293 		dlg->len_name = thisWidth;
294 	    dlg->list[n].name = ItemName(n);
295 	    dlg->list[n].text = ItemText(n);
296 	}
297 
298 	dlg->min_height = MIN_HIGH + aItemNo;
299 	dlg->min_width = MIN_WIDE + dlg->len_name + GUTTER + dlg->len_text;
300 
301 	if (dlg->prompt != 0 && *(dlg->prompt) != 0)
302 	    dlg->min_height += (2 * MARGIN);
303 #ifdef KEY_RESIZE
304 	nodelay(stdscr, TRUE);
305 #endif
306     }
307 #ifdef KEY_RESIZE
308     else {
309 	dlg_del_window(dlg->dialog);
310 	dlg->height = dlg->old_height;
311 	dlg->width = dlg->old_width;
312     }
313 #endif
314 
315     dlg_auto_size(dlg->title, dlg->prompt,
316 		  &(dlg->height),
317 		  &(dlg->width),
318 		  dlg->min_height,
319 		  dlg->min_width);
320     dlg_print_size(dlg->height, dlg->width);
321     dlg_ctl_size(dlg->height, dlg->width);
322 
323     /* center dialog box on screen */
324     x = dlg_box_x_ordinate(dlg->width);
325     y = dlg_box_y_ordinate(dlg->height);
326 
327     dlg->dialog = dlg_new_window(dlg->height, dlg->width, y, x);
328 
329     (void) werase(dlg->dialog);
330     dlg_draw_box2(dlg->dialog,
331 		  0, 0,
332 		  dlg->height,
333 		  dlg->width,
334 		  dialog_attr, border_attr, border2_attr);
335 
336     dlg_draw_title(dlg->dialog, dlg->title);
337     dlg_draw_helpline(dlg->dialog, FALSE);
338 
339     if ((dlg->prompt != 0 && *(dlg->prompt) != 0)
340 	&& wmove(dlg->dialog, dlg->item_no, 0) != ERR) {
341 	dlg->caption = dlg_sub_window(dlg->dialog,
342 				      dlg->height - dlg->item_no - (2 * MARGIN),
343 				      dlg->width,
344 				      y + dlg->item_no + (2 * MARGIN),
345 				      x);
346 	dlg_attrset(dlg->caption, dialog_attr);
347 	dlg_print_autowrap(dlg->caption, dlg->prompt, dlg->height, dlg->width);
348     }
349 
350     mydraw_mixed_box(dlg->dialog,
351 		     dlg->height - 4,
352 		     2 + MARGIN,
353 		     2 + MARGIN,
354 		     dlg->width - 2 * (2 + MARGIN),
355 		     dialog_attr,
356 		     border_attr);
357 
358     *began += 1;
359 }
360 
361 /*
362  * Discard the mixed-gauge dialog.
363  */
364 static int
365 dlg_finish_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int status)
366 {
367     (void) wrefresh(dlg->dialog);
368 #ifdef KEY_RESIZE
369     nodelay(stdscr, FALSE);
370 #endif
371     curs_set(1);
372     dlg_del_window(dlg->dialog);
373     free(dlg->prompt);
374     free(dlg->list);
375     return status;
376 }
377 
378 /*
379  * Setup dialog, read mixed-gauge data from pipe.
380  */
381 int
382 dialog_mixedgauge(const char *title,
383 		  const char *cprompt,
384 		  int height,
385 		  int width,
386 		  int percent,
387 		  int item_no,
388 		  char **items)
389 {
390     DIALOG_MIXEDGAUGE dlg;
391     int began = 0;
392 
393     DLG_TRACE(("# mixedgauge args:\n"));
394     DLG_TRACE2S("title", title);
395     DLG_TRACE2S("message", cprompt);
396     DLG_TRACE2N("height", height);
397     DLG_TRACE2N("width", width);
398     DLG_TRACE2N("percent", percent);
399     DLG_TRACE2N("llength", item_no);
400     /* FIXME dump the items[][] too */
401 
402     dlg_begin_mixedgauge(&dlg, &began, title, cprompt, height,
403 			 width, item_no, items);
404 
405     dlg_update_mixedgauge(&dlg, percent);
406 
407     return dlg_finish_mixedgauge(&dlg, DLG_EXIT_OK);
408 }
409