diff options
author | Timo Teräs <timo.teras@iki.fi> | 2011-07-22 12:08:35 +0300 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2011-07-22 12:08:35 +0300 |
commit | 6b24f3c3998a66b0e5baa4de89ed66f2b8688404 (patch) | |
tree | de0f574bf6d111e237867f5433e03728a241419f /src/print.c | |
parent | 384eada8aff3e9aca3cb1c289e194aee85cbd6c2 (diff) | |
download | apk-tools-6b24f3c3998a66b0e5baa4de89ed66f2b8688404.tar.gz apk-tools-6b24f3c3998a66b0e5baa4de89ed66f2b8688404.tar.bz2 apk-tools-6b24f3c3998a66b0e5baa4de89ed66f2b8688404.tar.xz apk-tools-6b24f3c3998a66b0e5baa4de89ed66f2b8688404.zip |
apk: improve indented printing
* fixup the help messages to align up properly
* refresh screen width on SIGWINCH
Diffstat (limited to 'src/print.c')
-rw-r--r-- | src/print.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/print.c b/src/print.c index 260809e..e673f12 100644 --- a/src/print.c +++ b/src/print.c @@ -14,15 +14,34 @@ #include <unistd.h> #include <malloc.h> #include <errno.h> +#include <sys/ioctl.h> #include "apk_defines.h" #include "apk_print.h" -int apk_print_indented(struct apk_indent *i, apk_blob_t blob) +static int apk_screen_width = 0; + +void apk_reset_screen_width(void) +{ + apk_screen_width = 0; +} + +int apk_get_screen_width(void) { - static const int wrap_length = 80; + struct winsize w; - if (i->x + blob.len + 1 >= wrap_length) { + if (apk_screen_width == 0) { + apk_screen_width = 70; + if (ioctl(STDERR_FILENO,TIOCGWINSZ, &w) == 0) + apk_screen_width = w.ws_col; + } + + return apk_screen_width; +} + +int apk_print_indented(struct apk_indent *i, apk_blob_t blob) +{ + if (i->x + blob.len + 1 >= apk_get_screen_width()) { i->x = i->indent; printf("\n%*s", i->indent - 1, ""); } |