test: The CGI script showing POST DATA sent to it.
[elinks.git] / src / dialogs / progress.c
blobf54aa8f3a1f3f8f8643c56fa3472023e74591d8f
1 /* Display of downloads progression stuff. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include "elinks.h"
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"
19 unsigned char *
20 get_progress_msg(struct progress *progress, struct terminal *term,
21 int wide, int full, unsigned char *separator)
23 struct string msg;
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);
44 if (wide) {
45 /* Do the following only if there is room */
47 add_to_string(&msg,
48 _(full ? (newlines ? N_("Average speed")
49 : N_("average speed"))
50 : N_("avg"),
51 term));
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, ", ");
57 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")
66 : N_("elapsed time"))
67 : N_("ETT"),
68 term));
69 add_char_to_string(&msg, ' ');
70 add_timeval_to_string(&msg, &progress->elapsed);
72 } else {
73 add_to_string(&msg, _(newlines ? N_("Speed") : N_("speed"),
74 term));
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")
84 : N_("ETA"),
85 term));
86 add_char_to_string(&msg, ' ');
87 add_timeval_to_string(&msg, &progress->estimated_time);
90 return msg.source;
93 void
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. */
99 int percent = 0;
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) {
107 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. */
118 if (text) {
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)) {
127 s[0] = '?';
128 slen = 1;
131 s[slen++] = '%';
133 /* Draw the percentage centered in the progress meter */
134 x += (1 + width - slen) / 2;
136 assert(slen <= width);
137 width = slen;
138 text = s;
141 draw_text(term, x, y, text, width, 0, NULL);