From: Sébastien Guimmara Date: Fri, 01 May 2015 23:12:18 GMT Subject: [PATCH v2 3/3] git help: group common commands by theme Message-ID: <55440852.4000500@gmail.com> In-Reply-To: <554405D5.9080702@gmail.com> help.c - group common commands by theme Use qsort to sort the common_cmds array by group name, then print those common commands, skipping a line between each group. Signed-off-by: Sébastien Guimmara --- help.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/help.c b/help.c index 2072a87..2169a59 100644 --- a/help.c +++ b/help.c @@ -218,17 +218,38 @@ void list_commands(unsigned int colopts, } } +/* sort the command name struct by group name */ +int cmd_group_cmp(const void *elem1, const void *elem2) +{ + struct cmdname_help *cmd1 = (struct cmdname_help*) elem1; + struct cmdname_help *cmd2 = (struct cmdname_help*) elem2; + + return strcmp(cmd1->group, cmd2->group); +} + void list_common_cmds_help(void) { int i, longest = 0; + char *current_grp = NULL; for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { if (longest < strlen(common_cmds[i].name)) longest = strlen(common_cmds[i].name); } + /* sort common commands by group (i.e, beginner's relevance) */ + qsort(common_cmds, ARRAY_SIZE(common_cmds), + sizeof(struct cmdname_help), cmd_group_cmp); + puts(_("The most commonly used git commands are:")); for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { + + /* skip a line each time we encounter a new command group */ + if (current_grp != NULL && strcmp(common_cmds[i].group, current_grp)) + printf("\n"); + + current_grp = common_cmds[i].group; + printf(" %s ", common_cmds[i].name); mput_char(' ', longest - strlen(common_cmds[i].name)); puts(_(common_cmds[i].help)); -- 2.4.0