From e74854a37c809fb4b0327289feebfbd5fa937588 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Tue, 8 Sep 2015 20:44:54 +0200 Subject: some very first (flickering) running progress bars --- src/CMakeLists.txt | 2 +- src/progressbar.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/progressbar.h | 10 +++++++++ tests/CMakeLists.txt | 2 +- 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 65e457e..793c742 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,4 +10,4 @@ ADD_GENGETOPT_FILES(SRC options.ggo) add_executable(cssh ${SRC}) -target_link_libraries(cssh ssh ssh_threads) +target_link_libraries(cssh ssh curses) 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 #include #include +#include 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; } diff --git a/src/progressbar.h b/src/progressbar.h index 3bcc65b..eb47516 100644 --- a/src/progressbar.h +++ b/src/progressbar.h @@ -8,17 +8,27 @@ #include #include +#define DEFAULT_COLS 80 +#define TERMBUF_SIZE 2048 + +struct cssh_progressbar_pool_t; + typedef struct cssh_progressbar_t { uint64_t min_value; uint64_t max_value; char *label; uint64_t value; + unsigned int total_steps; + unsigned int current_step; + struct cssh_progressbar_pool_t *pool; } cssh_progressbar_t; typedef struct cssh_progressbar_pool_t { size_t capacity; size_t N; cssh_progressbar_t **progressbar; + char *termbuf; + unsigned char cols; } cssh_progressbar_pool_t; int create_progressbar( cssh_progressbar_t *progressbar, uint64_t min_value, uint64_t max_value, size_t size, const char *label_fmt, ... ); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fb9e6c2..4572b47 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,6 @@ include_directories(../src) add_executable(progressbartest progressbartest.c ../src/progressbar.c ../src/port.c) -target_link_libraries(progressbartest) +target_link_libraries(progressbartest curses) add_test(ProgressBarPool progressbartest) -- cgit v1.2.3-54-g00ecf