From 428ae0e4a972be86b9eca74fcdc5f406cc163a61 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 12 Sep 2015 10:44:28 +0200 Subject: some progress bar fixes --- src/progressbar.c | 49 +++++++++++++++++++++++++++++-------------------- src/progressbar.h | 1 + 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/progressbar.c b/src/progressbar.c index c83450b..54c7639 100644 --- a/src/progressbar.c +++ b/src/progressbar.c @@ -24,6 +24,7 @@ int create_progressbar( cssh_progressbar_t *progressbar, uint64_t min_value, uin progressbar->value = progressbar->min_value; progressbar->total_steps = 0; progressbar->current_step = 0; + progressbar->buf = NULL; va_end( va ); @@ -32,7 +33,13 @@ int create_progressbar( cssh_progressbar_t *progressbar, uint64_t min_value, uin void free_progressbar( cssh_progressbar_t *progressbar ) { + free( progressbar->buf ); + progressbar->buf = NULL; free( progressbar->label ); + progressbar->label = NULL; + progressbar->value = progressbar->min_value; + progressbar->total_steps = 0; + progressbar->current_step = 0; } void set_value_of_progressbar( cssh_progressbar_t *progressbar, uint64_t value ) @@ -40,17 +47,27 @@ void set_value_of_progressbar( cssh_progressbar_t *progressbar, uint64_t value ) progressbar->value = value; } -static void compute_progressbar_steps( cssh_progressbar_t *progressbar ) +static int compute_progressbar_steps( cssh_progressbar_t *progressbar ) { cssh_progressbar_pool_t *pool = progressbar->pool; - progressbar->total_steps = pool->cols - strlen( progressbar->label ) - 2; + if( strlen( progressbar->label ) + 2 < pool->cols ) { + progressbar->total_steps = pool->cols - strlen( progressbar->label ) - 2; + } else { + progressbar->total_steps = 0; + } + progressbar->buf = (char *)malloc( progressbar->total_steps + 1 ); + if( progressbar->buf == NULL ) { + return -1; + } + + return 0; } void redraw_progressbar( cssh_progressbar_t *progressbar ) { if( progressbar->total_steps == 0 ) { - compute_progressbar_steps( progressbar ); + (void)compute_progressbar_steps( progressbar ); } unsigned int new_step = progressbar->value * ( (double)progressbar->total_steps / ( progressbar->max_value - progressbar->min_value ) ); @@ -58,20 +75,12 @@ void redraw_progressbar( cssh_progressbar_t *progressbar ) printf( "\n" ); return; } - - char *buf = (char *)malloc( progressbar->total_steps ); - //~ if( buf == NULL ) { - //~ printf( "\33[1E\n" ); - //~ return; - //~ } - - buf[progressbar->total_steps] = '\0'; - memset( buf, (int)' ', progressbar->total_steps ); - memset( buf, (int)'#', progressbar->current_step ); - - fprintf( stderr, "%s [%s]\n", progressbar->label, buf ); + + progressbar->buf[progressbar->total_steps] = '\0'; + memset( progressbar->buf, (int)' ', progressbar->total_steps ); + memset( progressbar->buf, (int)'#', progressbar->current_step ); - free( buf ); + fprintf( stderr, "%s [%s]\n", progressbar->label, progressbar->buf ); progressbar->current_step = new_step; } @@ -138,12 +147,12 @@ int remove_progressbar_from_pool( cssh_progressbar_pool_t *pool, cssh_progressba } for( size_t i = 0; i < pool->N; i++ ) { - if( pool->progressbar[i] ) { - if( i < pool->N - 1 ) { - memmove( &pool->progressbar[i], &pool->progressbar[i+1], - ( pool->N - i ) * sizeof( cssh_progressbar_t * ) ); + if( pool->progressbar[i] == progressbar ) { + for( size_t j = i; j < pool->N - 1; j++ ) { + pool->progressbar[j] = pool->progressbar[j+1]; } pool->N--; + pool->progressbar[pool->N] = NULL; return 0; } } diff --git a/src/progressbar.h b/src/progressbar.h index eb47516..53d8524 100644 --- a/src/progressbar.h +++ b/src/progressbar.h @@ -21,6 +21,7 @@ typedef struct cssh_progressbar_t { unsigned int total_steps; unsigned int current_step; struct cssh_progressbar_pool_t *pool; + char *buf; } cssh_progressbar_t; typedef struct cssh_progressbar_pool_t { -- cgit v1.2.3-54-g00ecf