From 6b24f3c3998a66b0e5baa4de89ed66f2b8688404 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Fri, 22 Jul 2011 12:08:35 +0300 Subject: apk: improve indented printing * fixup the help messages to align up properly * refresh screen width on SIGWINCH --- src/print.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/print.c') 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 #include #include +#include #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, ""); } -- cgit v1.2.3-70-g09d2