Commit 6b24f3c3 authored by Timo Teräs's avatar Timo Teräs

apk: improve indented printing

 * fixup the help messages to align up properly
 * refresh screen width on SIGWINCH
parent 384eada8
......@@ -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)
......
......@@ -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
......
......@@ -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);
......
......@@ -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, "");
}
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment