2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021-2022 Alfonso Sabato Siciliano
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
37 #include "bsddialog.h"
38 #include "bsddialog_progressview.h"
39 #include "bsddialog_theme.h"
44 #define MINBARWIDTH (2 + 2 * BARPADDING + MINBARLEN)
45 #define MINMGBARLEN 18
46 #define MINMGBARWIDTH (2 + 2 * BARPADDING + MINMGBARLEN)
48 bool bsddialog_interruptprogview;
49 bool bsddialog_abortprogview;
50 int bsddialog_total_progview;
53 draw_bar(WINDOW *win, int y, int x, int barlen, int perc, bool withlabel,
56 int i, blue_x, color, stringlen;
59 blue_x = perc > 0 ? (perc * barlen) / 100 : -1;
62 for (i = 0; i < barlen; i++) {
63 color = (i <= blue_x) ? t.bar.f_color : t.bar.color;
70 sprintf(labelstr, "%d", label);
72 sprintf(labelstr, "%3d%%", perc);
73 stringlen = (int)strlen(labelstr); /* number, always 1-byte-ch string */
74 wmove(win, y, x + barlen/2 - stringlen/2);
75 for (i = 0; i < stringlen; i++) {
76 color = (blue_x + 1 <= barlen/2 - stringlen/2 + i ) ?
77 t.bar.color : t.bar.f_color;
79 waddch(win, labelstr[i]);
85 bar_autosize(struct bsddialog_conf *conf, int rows, int cols, int *h, int *w,
86 const char *text, struct buttons *bs)
90 if (cols == BSDDIALOG_AUTOSIZE || rows == BSDDIALOG_AUTOSIZE) {
91 if (text_size(conf, rows, cols, text, bs, 3, MINBARWIDTH,
93 return (BSDDIALOG_ERROR);
96 if (cols == BSDDIALOG_AUTOSIZE)
97 *w = widget_min_width(conf, wtext, MINBARWIDTH, bs);
99 if (rows == BSDDIALOG_AUTOSIZE)
100 *h = widget_min_height(conf, htext, 3 /* bar */, bs != NULL);
106 bar_checksize(int rows, int cols, struct buttons *bs)
108 int minheight, minwidth;
111 if (bs != NULL) /* gauge has not buttons */
112 minwidth = buttons_min_width(*bs);
114 minwidth = MAX(minwidth, MINBARWIDTH);
115 minwidth += VBORDERS;
118 RETURN_ERROR("Few cols to draw bar and/or buttons");
120 minheight = HBORDERS + 3;
123 if (rows < minheight)
124 RETURN_ERROR("Few rows to draw bar");
130 bsddialog_gauge(struct bsddialog_conf *conf, const char *text, int rows,
131 int cols, unsigned int perc, int fd, const char *sep)
136 WINDOW *widget, *textpad, *bar, *shadow;
137 char inputbuf[2048], ntext[2048], *pntext;
139 if (set_widget_size(conf, rows, cols, &h, &w) != 0)
140 return (BSDDIALOG_ERROR);
141 if (bar_autosize(conf, rows, cols, &h, &w, text, NULL) != 0)
142 return (BSDDIALOG_ERROR);
143 if (bar_checksize(h, w, NULL) != 0)
144 return (BSDDIALOG_ERROR);
145 if (set_widget_position(conf, &y, &x, h, w) != 0)
146 return (BSDDIALOG_ERROR);
148 if (new_dialog(conf, &shadow, &widget, y, x, h, w, &textpad, text, NULL,
150 return (BSDDIALOG_ERROR);
152 bar = new_boxed_window(conf, y+h-4, x+3, 3, w-6, RAISED);
157 if ((input = fdopen(fd2, "r")) == NULL)
158 RETURN_ERROR("Cannot build FILE* from fd");
164 prefresh(textpad, 0, 0, y+1, x+1+TEXTHMARGIN, y+h-4,
166 draw_borders(conf, bar, 3, w-6, RAISED);
167 draw_bar(bar, 1, 1, w-8, perc, false, -1 /*unused*/);
169 if (input == NULL) /* that is fd < 0 */
173 fscanf(input, "%s", inputbuf);
174 if (strcmp(inputbuf,"EOF") == 0) {
178 if (strcmp(inputbuf, sep) == 0)
181 if (mainloop == false)
183 fscanf(input, "%d", &perc);
184 perc = perc > 100 ? 100 : perc;
188 fscanf(input, "%s", inputbuf);
189 if (strcmp(inputbuf,"EOF") == 0) {
193 if (strcmp(inputbuf, sep) == 0)
195 strcpy(pntext, inputbuf);
196 pntext += strlen(inputbuf); /* end string, no strlen */
201 if (update_dialog(conf, shadow, widget, y, x, h, w, textpad,
202 ntext, NULL, false) != 0)
203 return (BSDDIALOG_ERROR);
209 end_dialog(conf, shadow, widget, textpad);
211 return (BSDDIALOG_OK);
216 do_mixedgauge(struct bsddialog_conf *conf, const char *text, int rows, int cols,
217 unsigned int mainperc, unsigned int nminibars, const char **minilabels,
218 int *minipercs, bool color)
220 int i, retval, miniperc, y, x, h, w, ypad, max_minbarlen;
221 int htextpad, htext, wtext;
222 int colorperc, red, green;
223 WINDOW *widget, *textpad, *bar, *shadow;
224 char states[12][14] = {
225 " Succeeded ", /* -1 */
228 " Completed ", /* -4 */
229 " Checked ", /* -5 */
231 " Skipped ", /* -7 */
232 " In Progress ", /* -8 */
235 " Pending ", /* -11 */
236 " UNKNOWN ", /* < -11, no API */
239 red = bsddialog_color(BSDDIALOG_WHITE,BSDDIALOG_RED, BSDDIALOG_BOLD);
240 green = bsddialog_color(BSDDIALOG_WHITE,BSDDIALOG_GREEN,BSDDIALOG_BOLD);
243 for (i = 0; i < (int)nminibars; i++)
244 max_minbarlen = MAX(max_minbarlen, (int)strcols(minilabels[i]));
245 max_minbarlen += 3 + 16; /* seps + [...] */
246 max_minbarlen = MAX(max_minbarlen, MINMGBARWIDTH); /* mainbar */
248 if (set_widget_size(conf, rows, cols, &h, &w) != 0)
249 return (BSDDIALOG_ERROR);
251 /* mixedgauge autosize */
252 if (cols == BSDDIALOG_AUTOSIZE || rows == BSDDIALOG_AUTOSIZE) {
253 if (text_size(conf, rows, cols, text, NULL, nminibars + 3,
254 max_minbarlen, &htext, &wtext) != 0)
255 return (BSDDIALOG_ERROR);
257 if (cols == BSDDIALOG_AUTOSIZE)
258 w = widget_min_width(conf, wtext, max_minbarlen, NULL);
259 if (rows == BSDDIALOG_AUTOSIZE)
260 h = widget_min_height(conf, htext, nminibars + 3, false);
262 /* mixedgauge checksize */
263 if (w < max_minbarlen + 2)
264 RETURN_ERROR("Few cols for this mixedgauge");
265 if (h < 5 + (int)nminibars)
266 RETURN_ERROR("Few rows for this mixedgauge");
268 if (set_widget_position(conf, &y, &x, h, w) != 0)
269 return (BSDDIALOG_ERROR);
271 retval = new_dialog(conf, &shadow, &widget, y, x, h, w, &textpad, text,
273 if (retval == BSDDIALOG_ERROR)
277 for (i = 0; i < (int)nminibars; i++) {
278 miniperc = minipercs[i];
279 if (miniperc == BSDDIALOG_MG_BLANK)
282 if (color && (miniperc >= 0))
283 wattron(widget, A_BOLD);
284 mvwaddstr(widget, i+1, 2, minilabels[i]);
285 if (color && (miniperc >= 0))
286 wattroff(widget, A_BOLD);
289 mvwaddstr(widget, i+1, w-2-15, states[11]);
290 else if (miniperc < 0) {
291 mvwaddstr(widget, i+1, w-2-15, "[ ]");
293 if (color && miniperc == BSDDIALOG_MG_FAILED)
295 if (color && miniperc == BSDDIALOG_MG_DONE)
298 wattron(widget, colorperc);
299 miniperc = abs(miniperc + 1);
300 mvwaddstr(widget, i+1, 1+w-2-15, states[miniperc]);
302 wattroff(widget, colorperc);
304 else { /* miniperc >= 0 */
307 mvwaddstr(widget, i+1, w-2-15, "[ ]");
308 draw_bar(widget, i+1, 1+w-2-15, 13, miniperc, false,
314 getmaxyx(textpad, htextpad, i /* unused */);
315 ypad = y + h - 4 - htextpad;
316 ypad = ypad < y+(int)nminibars ? y+(int)nminibars : ypad;
317 prefresh(textpad, 0, 0, ypad, x+2, y+h-4, x+w-2);
320 bar = new_boxed_window(conf, y+h -4, x+3, 3, w-6, RAISED);
322 draw_bar(bar, 1, 1, w-8, mainperc, false, -1 /*unused*/);
324 wattron(bar, t.bar.color);
325 mvwaddstr(bar, 0, 2, "Overall Progress");
326 wattroff(bar, t.bar.color);
330 /* getch(); alternate mode (port devel/ncurses) shows nothing */
333 end_dialog(conf, shadow, widget, textpad);
335 return (BSDDIALOG_OK);
339 bsddialog_mixedgauge(struct bsddialog_conf *conf, const char *text, int rows,
340 int cols, unsigned int mainperc, unsigned int nminibars,
341 const char **minilabels, int *minipercs)
345 retval = do_mixedgauge(conf, text, rows, cols, mainperc, nminibars,
346 minilabels, minipercs, false);
352 bsddialog_progressview (struct bsddialog_conf *conf, const char *text, int rows,
353 int cols, struct bsddialog_progviewconf *pvconf, unsigned int nminibar,
354 struct bsddialog_fileminibar *minibar)
357 int perc, retval, *minipercs;
358 unsigned int i, mainperc, totaltodo;
360 const char **minilabels;
361 time_t tstart, told, tnew, refresh;
363 if ((minilabels = calloc(nminibar, sizeof(char*))) == NULL)
364 RETURN_ERROR("Cannot allocate memory for minilabels");
365 if ((minipercs = calloc(nminibar, sizeof(int))) == NULL)
366 RETURN_ERROR("Cannot allocate memory for minipercs");
369 for (i = 0; i < nminibar; i++) {
370 totaltodo += minibar[i].size;
371 minilabels[i] = minibar[i].label;
372 minipercs[i] = minibar[i].status;
375 refresh = pvconf->refresh == 0 ? 0 : pvconf->refresh - 1;
376 retval = BSDDIALOG_OK;
381 while (!(bsddialog_interruptprogview || bsddialog_abortprogview)) {
382 if (bsddialog_total_progview == 0 || totaltodo == 0)
385 mainperc = (bsddialog_total_progview * 100) / totaltodo;
388 if (update || tnew > told + refresh) {
389 retval = do_mixedgauge(conf, text, rows, cols, mainperc,
390 nminibar, minilabels, minipercs, true);
391 if (retval == BSDDIALOG_ERROR)
392 return (BSDDIALOG_ERROR);
394 move(SCREENLINES - 1, 2);
396 readforsec = ((tnew - tstart) == 0) ? 0 :
397 bsddialog_total_progview / (float)(tnew - tstart);
398 printw(pvconf->fmtbottomstr, bsddialog_total_progview,
408 if (minibar[i].status == BSDDIALOG_MG_FAILED)
411 perc = pvconf->callback(&minibar[i]);
413 if (minibar[i].status == BSDDIALOG_MG_DONE) { /*||perc >= 100)*/
414 minipercs[i] = BSDDIALOG_MG_DONE;
417 } else if (minibar[i].status == BSDDIALOG_MG_FAILED || perc < 0) {
418 minipercs[i] = BSDDIALOG_MG_FAILED;
420 } else /* perc >= 0 */
430 bsddialog_rangebox(struct bsddialog_conf *conf, const char *text, int rows,
431 int cols, int min, int max, int *value)
433 bool loop, buttupdate, barupdate;
435 int currvalue, retval, sizebar, bigchange, positions;
438 WINDOW *widget, *textpad, *bar, *shadow;
442 RETURN_ERROR("*value cannot be NULL");
445 RETURN_ERROR("min >= max");
448 positions = max - min + 1;
450 get_buttons(conf, &bs, BUTTON_OK_LABEL, BUTTON_CANCEL_LABEL);
452 if (set_widget_size(conf, rows, cols, &h, &w) != 0)
453 return (BSDDIALOG_ERROR);
454 if (bar_autosize(conf, rows, cols, &h, &w, text, &bs) != 0)
455 return (BSDDIALOG_ERROR);
456 if (bar_checksize(h, w, &bs) != 0)
457 return (BSDDIALOG_ERROR);
458 if (set_widget_position(conf, &y, &x, h, w) != 0)
459 return (BSDDIALOG_ERROR);
461 if (new_dialog(conf, &shadow, &widget, y, x, h, w, &textpad, text, &bs,
463 return (BSDDIALOG_ERROR);
467 prefresh(textpad, 0, 0, y+1, x+1+TEXTHMARGIN, y+h-7, x+w-1-TEXTHMARGIN);
469 sizebar = w - HBORDERS - (2 * BARPADDING) - 2;
470 bigchange = MAX(1, sizebar/10);
472 bar = new_boxed_window(conf, y + h - 6, x + 1 + BARPADDING, 3,
473 sizebar + 2, RAISED);
475 loop = buttupdate = barupdate = true;
478 draw_buttons(widget, bs, true);
483 perc = ((float)(currvalue - min)*100) / (positions-1);
484 draw_bar(bar, 1, 1, sizebar, perc, true, currvalue);
489 if (get_wch(&input) == ERR)
494 retval = bs.value[bs.curr];
499 if (conf->key.enable_esc) {
500 retval = BSDDIALOG_ESC;
505 bs.curr = (bs.curr + 1) % bs.nbuttons;
515 if (bs.curr < (int) bs.nbuttons - 1) {
529 currvalue -= bigchange;
535 currvalue += bigchange;
541 if (currvalue < max) {
547 if (currvalue > min) {
553 if (conf->key.f1_file == NULL &&
554 conf->key.f1_message == NULL)
556 if (f1help(conf) != 0)
557 return (BSDDIALOG_ERROR);
558 /* No break, screen size can change */
560 /* Important for decreasing screen */
561 hide_widget(y, x, h, w, conf->shadow);
564 if (set_widget_size(conf, rows, cols, &h, &w) != 0)
565 return (BSDDIALOG_ERROR);
566 if (bar_autosize(conf, rows, cols, &h, &w, text,
568 return (BSDDIALOG_ERROR);
569 if (bar_checksize(h, w, &bs) != 0)
570 return (BSDDIALOG_ERROR);
571 if (set_widget_position(conf, &y, &x, h, w) != 0)
572 return (BSDDIALOG_ERROR);
574 if (update_dialog(conf, shadow, widget,y, x, h, w,
575 textpad, text, &bs, true) != 0)
576 return (BSDDIALOG_ERROR);
580 sizebar = w - HBORDERS - (2 * BARPADDING) - 2;
581 bigchange = MAX(1, sizebar/10);
583 mvwin(bar, y + h - 6, x + 1 + BARPADDING);
584 wresize(bar, 3, sizebar + 2);
585 draw_borders(conf, bar, 3, sizebar+2, RAISED);
587 prefresh(textpad, 0, 0, y+1, x+1+TEXTHMARGIN, y+h-7,
593 if (shortcut_buttons(input, &bs)) {
594 retval = bs.value[bs.curr];
601 end_dialog(conf, shadow, widget, textpad);
607 bsddialog_pause(struct bsddialog_conf *conf, const char *text, int rows,
608 int cols, unsigned int sec)
610 bool loop, buttupdate, barupdate;
611 int retval, y, x, h, w, tout, sizebar;
614 WINDOW *widget, *textpad, *bar, *shadow;
617 get_buttons(conf, &bs, BUTTON_OK_LABEL, BUTTON_CANCEL_LABEL);
619 if (set_widget_size(conf, rows, cols, &h, &w) != 0)
620 return (BSDDIALOG_ERROR);
621 if (bar_autosize(conf, rows, cols, &h, &w, text, &bs) != 0)
622 return (BSDDIALOG_ERROR);
623 if (bar_checksize(h, w, &bs) != 0)
624 return (BSDDIALOG_ERROR);
625 if (set_widget_position(conf, &y, &x, h, w) != 0)
626 return (BSDDIALOG_ERROR);
628 if (new_dialog(conf, &shadow, &widget, y, x, h, w, &textpad, text, &bs,
630 return (BSDDIALOG_ERROR);
634 prefresh(textpad, 0, 0, y+1, x+1+TEXTHMARGIN, y+h-7, x+w-1-TEXTHMARGIN);
636 sizebar = w - HBORDERS - (2 * BARPADDING) - 2;
637 bar = new_boxed_window(conf, y + h - 6, x + 1 + BARPADDING, 3,
638 sizebar + 2, RAISED);
641 nodelay(stdscr, TRUE);
643 loop = buttupdate = barupdate = true;
646 perc = (float)tout * 100 / sec;
647 draw_bar(bar, 1, 1, sizebar, perc, true, tout);
653 draw_buttons(widget, bs, true);
658 if (get_wch(&input) == ERR) { /* timeout */
661 retval = BSDDIALOG_TIMEOUT;
672 retval = bs.value[bs.curr];
676 if (conf->key.enable_esc) {
677 retval = BSDDIALOG_ESC;
682 bs.curr = (bs.curr + 1) % bs.nbuttons;
692 if (bs.curr < (int) bs.nbuttons - 1) {
698 if (conf->key.f1_file == NULL &&
699 conf->key.f1_message == NULL)
701 if (f1help(conf) != 0)
702 return (BSDDIALOG_ERROR);
703 /* No break, screen size can change */
705 /* Important for decreasing screen */
706 hide_widget(y, x, h, w, conf->shadow);
709 if (set_widget_size(conf, rows, cols, &h, &w) != 0)
710 return (BSDDIALOG_ERROR);
711 if (bar_autosize(conf, rows, cols, &h, &w, text,
713 return (BSDDIALOG_ERROR);
714 if (bar_checksize(h, w, &bs) != 0)
715 return (BSDDIALOG_ERROR);
716 if (set_widget_position(conf, &y, &x, h, w) != 0)
717 return (BSDDIALOG_ERROR);
719 if (update_dialog(conf, shadow, widget,y, x, h, w,
720 textpad, text, &bs, true) != 0)
721 return (BSDDIALOG_ERROR);
725 sizebar = w - HBORDERS - (2 * BARPADDING) - 2;
727 mvwin(bar, y + h - 6, x + 1 + BARPADDING);
728 wresize(bar, 3, sizebar + 2);
729 draw_borders(conf, bar, 3, sizebar+2, LOWERED);
731 prefresh(textpad, 0, 0, y+1, x+1+TEXTHMARGIN, y+h-7,
737 if (shortcut_buttons(input, &bs)) {
738 retval = bs.value[bs.curr];
744 nodelay(stdscr, FALSE);
747 end_dialog(conf, shadow, widget, textpad);