项目中需要设计一个远程设备升级程序,程序优化时想在数字显示升级进度的同时用类似wget的进度条的形式显示升级进度,于是写了一个简单的数字+进度条显示升级进度的程序,以下为程序的核心部分,以达到抛砖引玉的效果。
/* The progress bar like this:
xx% [=======> ]
*/
int dlbytes_size = 1 + MAX (size_grouped_len, 11);
int progress_size = bp->width - (4 + 2 + dlbytes_size + 8 + 14);
/* The difference between the number of bytes used,
and the number of columns used. */
int bytes_cols_diff = 0;
if (progress_size < 5)
progress_size = 0;
/* "xx% " */
if (bp->total_length > 0)
{
int percentage = 100.0 * size / bp->total_length;
assert (percentage <= 100);
if (percentage < 100)
sprintf (p, "%2d%% ",