]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/dialog/mixedgauge.c
Upgrade to OpenSSH 7.9p1.
[FreeBSD/FreeBSD.git] / contrib / dialog / mixedgauge.c
1 /*
2  *  $Id: mixedgauge.c,v 1.34 2018/06/18 22:09:31 tom Exp $
3  *
4  *  mixedgauge.c -- implements the mixedgauge dialog
5  *
6  *  Copyright 2007-2012,2018    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 y = MARGIN;
114     int item;
115     int cells = dlg->len_text - 2;
116     int lm = limit_x - dlg->len_text - 1;
117     int bm = limit_y;           /* bottom margin */
118     int last_y = 0, last_x = 0;
119     int j, xxx;
120     float percent;
121     const char *status = "";
122     char *freeMe = 0;
123
124     bm -= (2 * MARGIN);
125     getyx(win, last_y, last_x);
126     for (item = 0; item < dlg->item_no; ++item) {
127         chtype attr = A_NORMAL;
128
129         y = item + MARGIN + 1;
130         if (y > bm)
131             break;
132
133         status = status_string(dlg->list[item].text, &freeMe);
134         if (status == 0 || *status == 0)
135             continue;
136
137         (void) wmove(win, y, 2 * MARGIN);
138         dlg_attrset(win, dialog_attr);
139         dlg_print_text(win, dlg->list[item].name, lm, &attr);
140
141         (void) wmove(win, y, lm);
142         (void) waddch(win, '[');
143         (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
144         if (freeMe) {
145             (void) wmove(win, y, lm + 1);
146             dlg_attrset(win, title_attr);
147             for (j = 0; j < cells; j++)
148                 (void) waddch(win, ' ');
149
150             (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
151             (void) waddstr(win, status);
152
153             if ((title_attr & A_REVERSE) != 0) {
154                 dlg_attroff(win, A_REVERSE);
155             } else {
156                 dlg_attrset(win, A_REVERSE);
157             }
158             (void) wmove(win, y, lm + 1);
159
160             if (sscanf(status, "%f%%", &percent) != 1)
161                 percent = 0.0;
162             xxx = (int) ((cells * (percent + 0.5)) / 100.0);
163             for (j = 0; j < xxx; j++) {
164                 chtype ch1 = winch(win);
165                 if (title_attr & A_REVERSE) {
166                     ch1 &= ~A_REVERSE;
167                 }
168                 (void) waddch(win, ch1);
169             }
170             free(freeMe);
171
172         } else {
173             (void) wmove(win, y, lm + (cells - (int) strlen(status)) / 2);
174             (void) waddstr(win, status);
175         }
176         (void) wmove(win, y, limit_x - 3);
177         dlg_attrset(win, dialog_attr);
178         (void) waddch(win, ']');
179         (void) wnoutrefresh(win);
180     }
181     if (win != 0)
182         wmove(win, last_y, last_x);
183 }
184
185 static void
186 mydraw_mixed_box(WINDOW *win, int y, int x, int height, int width,
187                  chtype boxchar, chtype borderchar)
188 {
189     dlg_draw_box(win, y, x, height, width, boxchar, borderchar);
190     {
191         chtype attr = A_NORMAL;
192         const char *message = _("Overall Progress");
193         chtype save2 = dlg_get_attrs(win);
194         dlg_attrset(win, title_attr);
195         (void) wmove(win, y, x + 2);
196         dlg_print_text(win, message, width, &attr);
197         dlg_attrset(win, save2);
198     }
199 }
200
201 static char *
202 clean_copy(const char *string)
203 {
204     char *result = dlg_strclone(string);
205
206     dlg_trim_string(result);
207     dlg_tab_correct_str(result);
208     return result;
209 }
210
211 /*
212  * Update mixed-gauge dialog (may be from pipe, may be via direct calls).
213  */
214 static void
215 dlg_update_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int percent)
216 {
217     int i, x;
218
219     /*
220      * Clear the area for the progress bar by filling it with spaces
221      * in the title-attribute, and write the percentage with that
222      * attribute.
223      */
224     (void) wmove(dlg->dialog, dlg->height - 3, 4);
225     dlg_attrset(dlg->dialog, gauge_attr);
226
227     for (i = 0; i < (dlg->width - 2 * (3 + MARGIN)); i++)
228         (void) waddch(dlg->dialog, ' ');
229
230     (void) wmove(dlg->dialog, dlg->height - 3, (dlg->width / 2) - 2);
231     (void) wprintw(dlg->dialog, "%3d%%", percent);
232
233     /*
234      * Now draw a bar in reverse, relative to the background.
235      * The window attribute was useful for painting the background,
236      * but requires some tweaks to reverse it.
237      */
238     x = (percent * (dlg->width - 2 * (3 + MARGIN))) / 100;
239     if ((title_attr & A_REVERSE) != 0) {
240         dlg_attroff(dlg->dialog, A_REVERSE);
241     } else {
242         dlg_attrset(dlg->dialog, A_REVERSE);
243     }
244     (void) wmove(dlg->dialog, dlg->height - 3, 4);
245     for (i = 0; i < x; i++) {
246         chtype ch = winch(dlg->dialog);
247         if (title_attr & A_REVERSE) {
248             ch &= ~A_REVERSE;
249         }
250         (void) waddch(dlg->dialog, ch);
251     }
252     myprint_status(dlg);
253     dlg_trace_win(dlg->dialog);
254 }
255
256 /*
257  * Setup dialog.
258  */
259 static void
260 dlg_begin_mixedgauge(DIALOG_MIXEDGAUGE * dlg,
261                      int *began,
262                      const char *aTitle,
263                      const char *aPrompt,
264                      int aHeight,
265                      int aWidth,
266                      int aItemNo,
267                      char **items)
268 {
269     int n, y, x;
270
271     if (!*began) {
272         curs_set(0);
273
274         memset(dlg, 0, sizeof(*dlg));
275         dlg->title = aTitle;
276         dlg->prompt = clean_copy(aPrompt);
277         dlg->height = dlg->old_height = aHeight;
278         dlg->width = dlg->old_width = aWidth;
279         dlg->item_no = aItemNo;
280
281         dlg->list = dlg_calloc(DIALOG_LISTITEM, (size_t) aItemNo);
282         assert_ptr(dlg->list, "dialog_mixedgauge");
283
284         dlg->len_name = 0;
285         dlg->len_text = 15;
286
287         for (n = 0; n < aItemNo; ++n) {
288             int thisWidth = (int) strlen(ItemName(n));
289             if (dlg->len_name < thisWidth)
290                 dlg->len_name = thisWidth;
291             dlg->list[n].name = ItemName(n);
292             dlg->list[n].text = ItemText(n);
293         }
294
295         dlg->min_height = MIN_HIGH + aItemNo;
296         dlg->min_width = MIN_WIDE + dlg->len_name + GUTTER + dlg->len_text;
297
298         if (dlg->prompt != 0 && *(dlg->prompt) != 0)
299             dlg->min_height += (2 * MARGIN);
300 #ifdef KEY_RESIZE
301         nodelay(stdscr, TRUE);
302 #endif
303     }
304 #ifdef KEY_RESIZE
305     else {
306         dlg_del_window(dlg->dialog);
307         dlg->height = dlg->old_height;
308         dlg->width = dlg->old_width;
309     }
310 #endif
311
312     dlg_auto_size(dlg->title, dlg->prompt,
313                   &(dlg->height),
314                   &(dlg->width),
315                   dlg->min_height,
316                   dlg->min_width);
317     dlg_print_size(dlg->height, dlg->width);
318     dlg_ctl_size(dlg->height, dlg->width);
319
320     /* center dialog box on screen */
321     x = dlg_box_x_ordinate(dlg->width);
322     y = dlg_box_y_ordinate(dlg->height);
323
324     dlg->dialog = dlg_new_window(dlg->height, dlg->width, y, x);
325
326     (void) werase(dlg->dialog);
327     dlg_draw_box2(dlg->dialog,
328                   0, 0,
329                   dlg->height,
330                   dlg->width,
331                   dialog_attr, border_attr, border2_attr);
332
333     dlg_draw_title(dlg->dialog, dlg->title);
334     dlg_draw_helpline(dlg->dialog, FALSE);
335
336     if ((dlg->prompt != 0 && *(dlg->prompt) != 0)
337         && wmove(dlg->dialog, dlg->item_no, 0) != ERR) {
338         dlg->caption = dlg_sub_window(dlg->dialog,
339                                       dlg->height - dlg->item_no - (2 * MARGIN),
340                                       dlg->width,
341                                       y + dlg->item_no + (2 * MARGIN),
342                                       x);
343         dlg_attrset(dlg->caption, dialog_attr);
344         dlg_print_autowrap(dlg->caption, dlg->prompt, dlg->height, dlg->width);
345     }
346
347     mydraw_mixed_box(dlg->dialog,
348                      dlg->height - 4,
349                      2 + MARGIN,
350                      2 + MARGIN,
351                      dlg->width - 2 * (2 + MARGIN),
352                      dialog_attr,
353                      border_attr);
354
355     *began += 1;
356 }
357
358 /*
359  * Discard the mixed-gauge dialog.
360  */
361 static int
362 dlg_finish_mixedgauge(DIALOG_MIXEDGAUGE * dlg, int status)
363 {
364     (void) wrefresh(dlg->dialog);
365 #ifdef KEY_RESIZE
366     nodelay(stdscr, FALSE);
367 #endif
368     curs_set(1);
369     dlg_del_window(dlg->dialog);
370     return status;
371 }
372
373 /*
374  * Setup dialog, read mixed-gauge data from pipe.
375  */
376 int
377 dialog_mixedgauge(const char *title,
378                   const char *cprompt,
379                   int height,
380                   int width,
381                   int percent,
382                   int item_no,
383                   char **items)
384 {
385     DIALOG_MIXEDGAUGE dlg;
386     int began = 0;
387
388     DLG_TRACE(("# mixedgauge args:\n"));
389     DLG_TRACE2S("title", title);
390     DLG_TRACE2S("message", cprompt);
391     DLG_TRACE2N("height", height);
392     DLG_TRACE2N("width", width);
393     DLG_TRACE2N("percent", percent);
394     DLG_TRACE2N("llength", item_no);
395     /* FIXME dump the items[][] too */
396
397     dlg_begin_mixedgauge(&dlg, &began, title, cprompt, height,
398                          width, item_no, items);
399
400     dlg_update_mixedgauge(&dlg, percent);
401
402     return dlg_finish_mixedgauge(&dlg, DLG_EXIT_OK);
403 }