1 /* Display of downloads progression stuff. */
9 #include "bfu/dialog.h"
10 #include "intl/gettext/libintl.h"
11 #include "network/progress.h"
12 #include "terminal/draw.h"
13 #include "util/conv.h"
14 #include "util/error.h"
15 #include "util/memory.h"
16 #include "util/string.h"
20 get_progress_msg(struct progress
*progress
, struct terminal
*term
,
21 int wide
, int full
, unsigned char *separator
)
24 int newlines
= separator
[strlen(separator
) - 1] == '\n';
26 if (!init_string(&msg
)) return NULL
;
28 /* FIXME: The following is a PITA from the l10n standpoint. A *big*
29 * one, _("of")-like pearls are a nightmare. Format strings need to
30 * be introduced to this fuggy corner of code as well. --pasky */
32 add_to_string(&msg
, _("Received", term
));
33 add_char_to_string(&msg
, ' ');
34 add_xnum_to_string(&msg
, progress
->pos
);
35 if (progress
->size
>= 0) {
36 add_char_to_string(&msg
, ' ');
37 add_to_string(&msg
, _("of", term
));
38 add_char_to_string(&msg
, ' ');
39 add_xnum_to_string(&msg
, progress
->size
);
42 add_to_string(&msg
, separator
);
45 /* Do the following only if there is room */
48 _(full
? (newlines
? N_("Average speed")
49 : N_("average speed"))
52 add_char_to_string(&msg
, ' ');
53 add_xnum_to_string(&msg
, progress
->average_speed
);
54 add_to_string(&msg
, "/s");
56 add_to_string(&msg
, ", ");
58 _(full
? N_("current speed") : N_("cur"), term
));
59 add_char_to_string(&msg
, ' '),
60 add_xnum_to_string(&msg
, progress
->current_speed
);
61 add_to_string(&msg
, "/s");
63 add_to_string(&msg
, separator
);
65 add_to_string(&msg
, _(full
? (newlines
? N_("Elapsed time")
69 add_char_to_string(&msg
, ' ');
70 add_timeval_to_string(&msg
, &progress
->elapsed
);
73 add_to_string(&msg
, _(newlines
? N_("Speed") : N_("speed"),
76 add_char_to_string(&msg
, ' ');
77 add_xnum_to_string(&msg
, progress
->average_speed
);
78 add_to_string(&msg
, "/s");
81 if (progress
->size
>= 0 && progress
->loaded
> 0) {
82 add_to_string(&msg
, ", ");
83 add_to_string(&msg
, _(full
? N_("estimated time")
86 add_char_to_string(&msg
, ' ');
87 add_timeval_to_string(&msg
, &progress
->estimated_time
);
94 draw_progress_bar(struct progress
*progress
, struct terminal
*term
,
95 int x
, int y
, int width
,
96 unsigned char *text
, struct color_pair
*meter_color
)
98 /* Note : values > 100% are theorically possible and were seen. */
100 struct box barprogress
;
102 if (progress
->size
> 0)
103 percent
= (int) ((longlong
) 100 * progress
->pos
/ progress
->size
);
105 /* Draw the progress meter part "[### ]" */
106 if (!text
&& width
> 2) {
108 draw_text(term
, x
++, y
, "[", 1, 0, NULL
);
109 draw_text(term
, x
+ width
, y
, "]", 1, 0, NULL
);
112 if (!meter_color
) meter_color
= get_bfu_color(term
, "dialog.meter");
113 set_box(&barprogress
,
114 x
, y
, int_min(width
* percent
/ 100, width
), 1);
115 draw_box(term
, &barprogress
, ' ', 0, meter_color
);
117 /* On error, will print '?' only, should not occur. */
119 width
= int_min(width
, strlen(text
));
121 } else if (width
> 1) {
122 static unsigned char s
[] = "????"; /* Reduce or enlarge at will. */
123 unsigned int slen
= 0;
124 int max
= int_min(sizeof(s
), width
) - 1;
126 if (ulongcat(s
, &slen
, percent
, max
, 0)) {
133 /* Draw the percentage centered in the progress meter */
134 x
+= (1 + width
- slen
) / 2;
136 assert(slen
<= width
);
141 draw_text(term
, x
, y
, text
, width
, 0, NULL
);