forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.c
241 lines (217 loc) · 6.11 KB
/
progress.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* @file
* Progress bar
*
* @authors
* Copyright (C) 2018 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page progress Progress bar
*
* Progress bar
*/
#include "config.h"
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include "mutt/mutt.h"
#include "progress.h"
#include "curs_lib.h"
#include "mutt_curses.h"
#include "mutt_logging.h"
#include "options.h"
/* These Config Variables are only used in progress.c */
short C_TimeInc; ///< Config: Frequency of progress bar updates (milliseconds)
/**
* message_bar - Draw a colourful progress bar
* @param percent %age complete
* @param fmt printf(1)-like formatting string
* @param ... Arguments to formatting string
*/
static void message_bar(int percent, const char *fmt, ...)
{
va_list ap;
char buf[256], buf2[256];
int w = percent * COLS / 100;
size_t l;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
l = mutt_strwidth(buf);
va_end(ap);
mutt_simple_format(buf2, sizeof(buf2), 0, COLS - 2, FMT_LEFT, 0, buf, sizeof(buf), 0);
move(LINES - 1, 0);
if (ColorDefs[MT_COLOR_PROGRESS] == 0)
{
addstr(buf2);
}
else
{
if (l < w)
{
/* The string fits within the colour bar */
SETCOLOR(MT_COLOR_PROGRESS);
addstr(buf2);
w -= l;
while (w-- > 0)
{
addch(' ');
}
NORMAL_COLOR;
}
else
{
/* The string is too long for the colour bar */
char ch;
int off = mutt_wstr_trunc(buf2, sizeof(buf2), w, NULL);
ch = buf2[off];
buf2[off] = '\0';
SETCOLOR(MT_COLOR_PROGRESS);
addstr(buf2);
buf2[off] = ch;
NORMAL_COLOR;
addstr(&buf2[off]);
}
}
clrtoeol();
mutt_refresh();
}
/**
* mutt_progress_init - Set up a progress bar
* @param progress Progress bar
* @param msg Message to display
* @param flags Flags, e.g. #MUTT_PROGRESS_SIZE
* @param inc Increments to display (0 disables updates)
* @param size Total size of expected file / traffic
*/
void mutt_progress_init(struct Progress *progress, const char *msg,
unsigned short flags, unsigned short inc, size_t size)
{
struct timeval tv = { 0, 0 };
if (!progress)
return;
if (OptNoCurses)
return;
memset(progress, 0, sizeof(struct Progress));
progress->inc = inc;
progress->flags = flags;
progress->msg = msg;
progress->size = size;
if (progress->size != 0)
{
if (progress->flags & MUTT_PROGRESS_SIZE)
{
mutt_str_pretty_size(progress->sizestr, sizeof(progress->sizestr),
progress->size);
}
else
snprintf(progress->sizestr, sizeof(progress->sizestr), "%zu", progress->size);
}
if (inc == 0)
{
if (size != 0)
mutt_message("%s (%s)", msg, progress->sizestr);
else
mutt_message(msg);
return;
}
if (gettimeofday(&tv, NULL) < 0)
mutt_debug(LL_DEBUG1, "gettimeofday failed: %d\n", errno);
/* if timestamp is 0 no time-based suppression is done */
if (C_TimeInc != 0)
{
progress->timestamp =
((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
}
mutt_progress_update(progress, 0, 0);
}
/**
* mutt_progress_update - Update the state of the progress bar
* @param progress Progress bar
* @param pos Position, or count
* @param percent Percentage complete
*
* If percent is -1, then the percentage will be calculated using pos and the
* size in progress.
*
* If percent is positive, it is displayed as percentage, otherwise
* percentage is calculated from progress->size and pos if progress
* was initialized with positive size, otherwise no percentage is shown
*/
void mutt_progress_update(struct Progress *progress, long pos, int percent)
{
char posstr[128];
bool update = false;
struct timeval tv = { 0, 0 };
unsigned int now = 0;
if (OptNoCurses)
return;
if (progress->inc == 0)
goto out;
/* refresh if size > inc */
if ((progress->flags & MUTT_PROGRESS_SIZE) &&
(pos >= (progress->pos + (progress->inc << 10))))
{
update = true;
}
else if (pos >= (progress->pos + progress->inc))
update = true;
/* skip refresh if not enough time has passed */
if (update && progress->timestamp && (gettimeofday(&tv, NULL) == 0))
{
now = ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
if (now && ((now - progress->timestamp) < C_TimeInc))
update = false;
}
/* always show the first update */
if (pos == 0)
update = true;
if (update)
{
if (progress->flags & MUTT_PROGRESS_SIZE)
{
pos = pos / (progress->inc << 10) * (progress->inc << 10);
mutt_str_pretty_size(posstr, sizeof(posstr), pos);
}
else
snprintf(posstr, sizeof(posstr), "%ld", pos);
mutt_debug(4, "updating progress: %s\n", posstr);
progress->pos = pos;
if (now)
progress->timestamp = now;
if (progress->size > 0)
{
message_bar(
(percent > 0) ? percent :
(int) (100.0 * (double) progress->pos / progress->size),
"%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
(percent > 0) ? percent :
(int) (100.0 * (double) progress->pos / progress->size));
}
else
{
if (percent > 0)
message_bar(percent, "%s %s (%d%%)", progress->msg, posstr, percent);
else
mutt_message("%s %s", progress->msg, posstr);
}
}
out:
if (pos >= progress->size)
mutt_clear_error();
}