diff options
author | Timo Teräs <timo.teras@iki.fi> | 2013-06-17 16:47:49 +0300 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2013-06-17 16:47:49 +0300 |
commit | 0a131418899436b58a163978176d99c08cbddb0c (patch) | |
tree | abd0fe5c52ee39fca31ba562c934dbd6d0584705 /src/print.c | |
parent | 92764088a2b09d4e322d21e769ecb51bb5f3b17c (diff) | |
download | apk-tools-0a131418899436b58a163978176d99c08cbddb0c.tar.gz apk-tools-0a131418899436b58a163978176d99c08cbddb0c.tar.bz2 apk-tools-0a131418899436b58a163978176d99c08cbddb0c.tar.xz apk-tools-0a131418899436b58a163978176d99c08cbddb0c.zip |
print: move progress printing to common functions
Diffstat (limited to 'src/print.c')
-rw-r--r-- | src/print.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/print.c b/src/print.c index aceef38..df53be8 100644 --- a/src/print.c +++ b/src/print.c @@ -19,6 +19,7 @@ #include "apk_defines.h" #include "apk_print.h" +int apk_progress_fd; static int apk_screen_width = 0; void apk_reset_screen_width(void) @@ -40,6 +41,37 @@ int apk_get_screen_width(void) return apk_screen_width; } +void apk_print_progress(int percent_flags) +{ + static int last_written = 0; + const int bar_width = apk_get_screen_width() - 7; + char buf[8]; + int i, percent; + + percent = percent_flags & APK_PRINT_PROGRESS_MASK; + + if (last_written == percent && !(percent_flags & APK_PRINT_PROGRESS_FORCE)) + return; + + last_written = percent; + + if (apk_flags & APK_PROGRESS) { + fprintf(stderr, "\e7%3i%% [", percent); + for (i = 0; i < bar_width * percent / 100; i++) + fputc('#', stderr); + for (; i < bar_width; i++) + fputc(' ', stderr); + fputc(']', stderr); + fflush(stderr); + fputs("\e8\e[0K", stderr); + } + + if (apk_progress_fd != 0) { + i = snprintf(buf, sizeof(buf), "%zu\n", percent); + write(apk_progress_fd, buf, i); + } +} + int apk_print_indented(struct apk_indent *i, apk_blob_t blob) { if (i->x + blob.len + 1 >= apk_get_screen_width()) |