diff options
Diffstat (limited to 'src/progressbar.c')
-rw-r--r-- | src/progressbar.c | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/src/progressbar.c b/src/progressbar.c index e8cd54b..c83450b 100644 --- a/src/progressbar.c +++ b/src/progressbar.c @@ -4,6 +4,7 @@ #include <stdio.h> #include <stdarg.h> #include <string.h> +#include <termcap.h> int create_progressbar( cssh_progressbar_t *progressbar, uint64_t min_value, uint64_t max_value, size_t size, const char *label_fmt, ... ) { @@ -21,6 +22,8 @@ int create_progressbar( cssh_progressbar_t *progressbar, uint64_t min_value, uin (void)vsnprintf( progressbar->label, size, label_fmt, va ); progressbar->value = progressbar->min_value; + progressbar->total_steps = 0; + progressbar->current_step = 0; va_end( va ); @@ -37,9 +40,40 @@ void set_value_of_progressbar( cssh_progressbar_t *progressbar, uint64_t value ) progressbar->value = value; } +static void compute_progressbar_steps( cssh_progressbar_t *progressbar ) +{ + cssh_progressbar_pool_t *pool = progressbar->pool; + + progressbar->total_steps = pool->cols - strlen( progressbar->label ) - 2; +} + void redraw_progressbar( cssh_progressbar_t *progressbar ) { - fprintf( stderr, "%s %"PRIu64" \n", progressbar->label, progressbar->value ); + if( progressbar->total_steps == 0 ) { + compute_progressbar_steps( progressbar ); + } + + unsigned int new_step = progressbar->value * ( (double)progressbar->total_steps / ( progressbar->max_value - progressbar->min_value ) ); + if( new_step == progressbar->current_step ) { + 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 ); + + free( buf ); + + progressbar->current_step = new_step; } int create_progressbar_pool( cssh_progressbar_pool_t *pool, size_t initial_size ) @@ -50,6 +84,25 @@ int create_progressbar_pool( cssh_progressbar_pool_t *pool, size_t initial_size if( pool->progressbar == NULL ) { return -1; } + + pool->termbuf = (char *)malloc( TERMBUF_SIZE ); + if( pool->termbuf == NULL ) { + free( pool->progressbar ); + return -1; + } + + char *termtype = getenv( "TERM" ); + if( termtype == NULL ) { + pool->cols = DEFAULT_COLS; + } + + int rc = tgetent( pool->termbuf, termtype ); + + if( rc < 0 ) { + pool->cols = DEFAULT_COLS; + } else { + pool->cols = tgetnum( "co" ) - 2; + } return 0; } @@ -61,6 +114,7 @@ void free_progressbar_pool( cssh_progressbar_pool_t *pool ) } free( pool->progressbar ); + free( pool->termbuf ); } int append_progressbar_to_pool( cssh_progressbar_pool_t *pool, cssh_progressbar_t *progressbar ) @@ -72,6 +126,8 @@ int append_progressbar_to_pool( cssh_progressbar_pool_t *pool, cssh_progressbar_ pool->progressbar[pool->N] = progressbar; pool->N++; + progressbar->pool = pool; + return 0; } |