From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Subject: [PATCH v4 09/13] help: reuse print_columns() for help -a Date: Fri, 3 Feb 2012 20:34:34 +0700 Message-ID: <1328276078-27955-10-git-send-email-pclouds@gmail.com> References: <1328276078-27955-1-git-send-email-pclouds@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Fri Feb 03 14:32:01 2012 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RtJFA-0008Fq-B1 for gcvg-git-2@plane.gmane.org; Fri, 03 Feb 2012 14:32:00 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756427Ab2BCNby convert rfc822-to-quoted-printable (ORCPT ); Fri, 3 Feb 2012 08:31:54 -0500 Received: from mail-pz0-f46.google.com ([209.85.210.46]:39200 "EHLO mail-pz0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756423Ab2BCNbx (ORCPT ); Fri, 3 Feb 2012 08:31:53 -0500 Received: by dadp15 with SMTP id p15so2737801dad.19 for ; Fri, 03 Feb 2012 05:31:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=hogzf7JDu1YJEMThKMB1lEhyL/HxbsHddrw+iS4ZxYE=; b=VUJiDeWPtPiRBfZa81fcyNncAwH6kVbn+/PssFtSPoxfMwJyKiNWs/YDCda88oZDoh 64jkMkuYaL9Gp+CE5LLdE0m4vwHnBQMBDXzDftB2fw/O2TBRFdeKH35zdu8YNBHZNw2z tH7Roe8ZAsmJN8qaeiOq5Yz/kt8rxqTxdfV2c= Received: by 10.68.222.131 with SMTP id qm3mr17849723pbc.34.1328275913060; Fri, 03 Feb 2012 05:31:53 -0800 (PST) Received: from tre ([115.74.57.120]) by mx.google.com with ESMTPS id o7sm13123485pbq.8.2012.02.03.05.31.49 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 03 Feb 2012 05:31:51 -0800 (PST) Received: by tre (sSMTP sendmail emulation); Fri, 03 Feb 2012 20:36:02 +0700 X-Mailer: git-send-email 1.7.8.36.g69ee2 In-Reply-To: <1328276078-27955-1-git-send-email-pclouds@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Signed-off-by: Nguy=E1=BB=85n Th=C3=A1i Ng=E1=BB=8Dc Duy --- help.c | 47 +++++++++++++---------------------------------- 1 files changed, 13 insertions(+), 34 deletions(-) diff --git a/help.c b/help.c index 672561b..d6d2e19 100644 --- a/help.c +++ b/help.c @@ -4,6 +4,7 @@ #include "levenshtein.h" #include "help.h" #include "common-cmds.h" +#include "string-list.h" #include "column.h" =20 void add_cmdname(struct cmdnames *cmds, const char *name, int len) @@ -71,31 +72,18 @@ void exclude_cmds(struct cmdnames *cmds, struct cmd= names *excludes) cmds->cnt =3D cj; } =20 -static void pretty_print_string_list(struct cmdnames *cmds, int longes= t) +static void pretty_print_string_list(struct cmdnames *cmds) { - int cols =3D 1, rows; - int space =3D longest + 1; /* min 1 SP between words */ - int max_cols =3D term_columns() - 1; /* don't print *on* the edge */ - int i, j; - - if (space < max_cols) - cols =3D max_cols / space; - rows =3D DIV_ROUND_UP(cmds->cnt, cols); - - for (i =3D 0; i < rows; i++) { - printf(" "); + struct string_list list =3D STRING_LIST_INIT_NODUP; + struct column_options copts; + int i; =20 - for (j =3D 0; j < cols; j++) { - int n =3D j * rows + i; - int size =3D space; - if (n >=3D cmds->cnt) - break; - if (j =3D=3D cols-1 || n + rows >=3D cmds->cnt) - size =3D 1; - printf("%-*s", size, cmds->names[n]->name); - } - putchar('\n'); - } + for (i =3D 0; i < cmds->cnt; i++) + string_list_append(&list, cmds->names[i]->name); + memset(&copts, 0, sizeof(copts)); + copts.indent =3D " "; + print_columns(&list, COL_MODE_COLUMN | COL_ENABLED, &copts); + string_list_clear(&list, 0); } =20 static int is_executable(const char *name) @@ -207,22 +195,13 @@ void load_command_list(const char *prefix, void list_commands(const char *title, struct cmdnames *main_cmds, struct cmdnames *other_cmds) { - int i, longest =3D 0; - - for (i =3D 0; i < main_cmds->cnt; i++) - if (longest < main_cmds->names[i]->len) - longest =3D main_cmds->names[i]->len; - for (i =3D 0; i < other_cmds->cnt; i++) - if (longest < other_cmds->names[i]->len) - longest =3D other_cmds->names[i]->len; - if (main_cmds->cnt) { const char *exec_path =3D git_exec_path(); printf("available %s in '%s'\n", title, exec_path); printf("----------------"); mput_char('-', strlen(title) + strlen(exec_path)); putchar('\n'); - pretty_print_string_list(main_cmds, longest); + pretty_print_string_list(main_cmds); putchar('\n'); } =20 @@ -231,7 +210,7 @@ void list_commands(const char *title, struct cmdnam= es *main_cmds, printf("---------------------------------------"); mput_char('-', strlen(title)); putchar('\n'); - pretty_print_string_list(other_cmds, longest); + pretty_print_string_list(other_cmds); putchar('\n'); } } --=20 1.7.8.36.g69ee2