summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2011-07-22 12:08:35 +0300
committerTimo Teräs <timo.teras@iki.fi>2011-07-22 12:08:35 +0300
commit6b24f3c3998a66b0e5baa4de89ed66f2b8688404 (patch)
treede0f574bf6d111e237867f5433e03728a241419f
parent384eada8aff3e9aca3cb1c289e194aee85cbd6c2 (diff)
downloadapk-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
-rw-r--r--src/apk.c31
-rw-r--r--src/apk_defines.h1
-rw-r--r--src/apk_print.h2
-rw-r--r--src/print.c25
-rw-r--r--src/state.c2
5 files changed, 41 insertions, 20 deletions
diff --git a/src/apk.c b/src/apk.c
index 3ec241e..fdd31a8 100644
--- a/src/apk.c
+++ b/src/apk.c
@@ -13,13 +13,13 @@
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
+#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/stat.h>
-#include <sys/ioctl.h>
#include <openssl/crypto.h>
#ifndef OPENSSL_NO_ENGINE
@@ -130,8 +130,6 @@ static void print_options(int num_opts, struct apk_option *opts)
static int usage(struct apk_applet *applet)
{
- struct apk_indent indent = { 0, 2 };
-
version();
if (applet == NULL) {
struct apk_applet **a;
@@ -141,23 +139,27 @@ static int usage(struct apk_applet *applet)
printf("\nThe following commands are available:\n");
for (a = &__start_apkapplets; a < &__stop_apkapplets; a++) {
- struct apk_indent sub_indent = { 20, 26 };
+ struct apk_indent indent = { 0, 26 };
- printf(" %-*s", sub_indent.indent - 3, (*a)->name);
- apk_print_indented_words(&sub_indent, (*a)->help);
+ indent.x = printf(" %-*s", indent.indent - 3, (*a)->name);
+ apk_print_indented_words(&indent, (*a)->help);
printf("\n");
}
} else {
+ struct apk_indent indent = { 0, 2 };
+
print_usage(applet->name, applet->arguments,
applet->num_options, applet->options);
- printf("\ndescription:\n%*s", indent.indent - 1, "");
+ printf("\nDescription:\n%*s", indent.indent - 1, "");
+ indent.x = indent.indent - 1;
apk_print_indented_words(&indent, applet->help);
+ printf("\n");
}
printf("\nGeneric options:\n");
print_options(ARRAY_SIZE(generic_options), generic_options);
if (applet != NULL && applet->num_options > 0) {
- printf("\noptions for %s command:\n", applet->name);
+ printf("\nOptions for %s command:\n", applet->name);
print_options(applet->num_options, applet->options);
}
printf("\nThis apk has coffee making abilities.\n");
@@ -247,18 +249,17 @@ static void init_openssl(void)
#endif
}
-static void setup_terminal(void)
+static void on_sigwinch(int s)
{
- struct winsize w;
+ apk_reset_screen_width();
+}
+static void setup_terminal(void)
+{
setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
- if (ioctl(STDERR_FILENO,TIOCGWINSZ, &w) == 0)
- apk_screen_width = w.ws_col;
- if (apk_screen_width == 0)
- apk_screen_width = 70;
if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) && isatty(STDIN_FILENO))
apk_flags |= APK_PROGRESS;
-
+ signal(SIGWINCH, on_sigwinch);
}
int main(int argc, char **argv)
diff --git a/src/apk_defines.h b/src/apk_defines.h
index b5ad12e..b11d181 100644
--- a/src/apk_defines.h
+++ b/src/apk_defines.h
@@ -51,7 +51,6 @@ extern int apk_verbosity;
extern unsigned int apk_flags;
extern const char *apk_arch;
extern char **apk_argv;
-extern int apk_screen_width;
#define APK_FORCE 0x0001
#define APK_SIMULATE 0x0002
diff --git a/src/apk_print.h b/src/apk_print.h
index 3dfb13c..a88d873 100644
--- a/src/apk_print.h
+++ b/src/apk_print.h
@@ -21,6 +21,8 @@
void apk_log(const char *prefix, const char *format, ...);
const char *apk_error_str(int error);
+void apk_reset_screen_width(void);
+int apk_get_screen_width(void);
int apk_print_indented(struct apk_indent *i, apk_blob_t blob);
void apk_print_indented_words(struct apk_indent *i, const char *text);
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, "");
}
diff --git a/src/state.c b/src/state.c
index fc7ecb5..3889162 100644
--- a/src/state.c
+++ b/src/state.c
@@ -723,7 +723,7 @@ static void apk_count_change(struct apk_change *change, struct apk_stats *stats)
static void apk_draw_progress(int percent)
{
- const int bar_width = (apk_screen_width - 7);
+ const int bar_width = apk_get_screen_width() - 7;
int i;
fprintf(stderr, "\e7%3i%% [", percent);