From: Nguyễn Thái Ngọc Duy Date: Tue, 08 Feb 2011 15:22:19 GMT Subject: [PATCH 5/7] help: reuse struct column_layout Message-ID: <1297178541-31124-6-git-send-email-pclouds@gmail.com> In-Reply-To: <1297178541-31124-1-git-send-email-pclouds@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy --- help.c | 33 +++++++++++---------------------- 1 files changed, 11 insertions(+), 22 deletions(-) diff --git a/help.c b/help.c index 1344208..dc06f63 100644 --- a/help.c +++ b/help.c @@ -4,6 +4,7 @@ #include "levenshtein.h" #include "help.h" #include "common-cmds.h" +#include "column.h" void add_cmdname(struct cmdnames *cmds, const char *name, int len) { @@ -72,29 +73,17 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) static void pretty_print_string_list(struct cmdnames *cmds, int longest) { - int cols = 1, rows; - int space = longest + 1; /* min 1 SP between words */ - int max_cols = term_columns() - 1; /* don't print *on* the edge */ - int i, j; - - if (space < max_cols) - cols = max_cols / space; - rows = DIV_ROUND_UP(cmds->cnt, cols); + struct column_layout c; + int i; - for (i = 0; i < rows; i++) { - printf(" "); + memset(&c, 0, sizeof(c)); + c.mode = COL_COLUMN_FIRST; + c.width = term_columns(); - for (j = 0; j < cols; j++) { - int n = j * rows + i; - int size = space; - if (n >= cmds->cnt) - break; - if (j == cols-1 || n + rows >= cmds->cnt) - size = 1; - printf("%-*s", size, cmds->names[n]->name); - } - putchar('\n'); - } + for (i = 0; i < cmds->cnt; i++) + string_list_append(&c.items, cmds->names[i]->name); + display_columns(&c, 2, " "); + string_list_clear(&c.items, 0); } static int is_executable(const char *name) -- 1.7.2.2