about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-07-28 00:10:18 +0100
committerJames Booth <boothj5@gmail.com>2015-07-28 00:10:18 +0100
commit30c1cdd3185cbbc59f79ce16305b1c099ee4044f (patch)
tree8e6d59d622a947b94c59391158f2da2a85e16f33
parent08f43bee8edd32a8193997cd96fd97da47655ced (diff)
downloadprofani-tty-30c1cdd3185cbbc59f79ce16305b1c099ee4044f.tar.gz
Refactored showing tagged command help
-rw-r--r--src/command/commands.c163
1 files changed, 65 insertions, 98 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index d42663b8..e26db08f 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -773,6 +773,68 @@ cmd_win(ProfWin *window, const char * const command, gchar **args)
     return TRUE;
 }
 
+static void
+_cmd_help_cmd_list(const char * const tag)
+{
+    cons_show("");
+    ProfWin *console = wins_get_console();
+    if (tag) {
+        win_vprint(console, '-', NULL, 0, THEME_WHITE_BOLD, "", "%s commands", tag);
+    } else {
+        win_print(console, '-', NULL, 0, THEME_WHITE_BOLD, "", "All commands");
+    }
+
+    GList *ordered_commands = NULL;
+    GHashTableIter iter;
+    gpointer key;
+    gpointer value;
+
+    g_hash_table_iter_init(&iter, commands);
+    while (g_hash_table_iter_next(&iter, &key, &value)) {
+        Command *pcmd = (Command *)value;
+        if (tag) {
+            if (cmd_has_tag(pcmd, tag)) {
+                ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
+            }
+        } else {
+            ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
+        }
+    }
+
+    int maxlen = 0;
+    GList *curr = ordered_commands;
+    while (curr) {
+        gchar *cmd = curr->data;
+        int len = strlen(cmd);
+        if (len > maxlen) maxlen = len;
+        curr = g_list_next(curr);
+    }
+
+    GString *cmds = g_string_new("");
+    curr = ordered_commands;
+    int count = 0;
+    while (curr) {
+        gchar *cmd = curr->data;
+        if (count == 5) {
+            cons_show(cmds->str);
+            g_string_free(cmds, TRUE);
+            cmds = g_string_new("");
+            count = 0;
+        }
+        g_string_append_printf(cmds, "%-*s", maxlen + 1, cmd);
+        curr = g_list_next(curr);
+        count++;
+    }
+    cons_show(cmds->str);
+    g_string_free(cmds, TRUE);
+    g_list_free(ordered_commands);
+    g_list_free(curr);
+
+    cons_show("");
+    cons_show("Use /help [command] without the leading slash, for help on a specific command");
+    cons_show("");
+}
+
 gboolean
 cmd_help(ProfWin *window, const char * const command, gchar **args)
 {
@@ -783,106 +845,11 @@ cmd_help(ProfWin *window, const char * const command, gchar **args)
         if (args[1]) {
             if (!cmd_valid_tag(args[1])) {
                 cons_bad_cmd_usage(command);
-                return TRUE;
-            }
-
-            cons_show("");
-            ProfWin *console = wins_get_console();
-            win_vprint(console, '-', NULL, 0, THEME_WHITE_BOLD, "", "%s commands", args[1]);
-
-            GList *ordered_commands = NULL;
-            GHashTableIter iter;
-            gpointer key;
-            gpointer value;
-
-            g_hash_table_iter_init(&iter, commands);
-            while (g_hash_table_iter_next(&iter, &key, &value)) {
-                Command *pcmd = (Command *)value;
-                if (cmd_has_tag(pcmd, args[1])) {
-                    ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
-                }
-            }
-
-            int maxlen = 0;
-            GList *curr = ordered_commands;
-            while (curr) {
-                gchar *cmd = curr->data;
-                int len = strlen(cmd);
-                if (len > maxlen) maxlen = len;
-                curr = g_list_next(curr);
-            }
-
-            GString *cmds = g_string_new("");
-            curr = ordered_commands;
-            int count = 0;
-            while (curr) {
-                gchar *cmd = curr->data;
-                if (count == 5) {
-                    cons_show(cmds->str);
-                    g_string_free(cmds, TRUE);
-                    cmds = g_string_new("");
-                    count = 0;
-                }
-                g_string_append_printf(cmds, "%-*s", maxlen + 1, cmd);
-                curr = g_list_next(curr);
-                count++;
+            } else {
+                _cmd_help_cmd_list(args[1]);
             }
-            cons_show(cmds->str);
-            g_string_free(cmds, TRUE);
-            g_list_free(ordered_commands);
-            g_list_free(curr);
-
-            cons_show("");
-            cons_show("Use /help [command] without the leading slash, for help on a specific command");
-            cons_show("");
         } else {
-            cons_show("");
-            ProfWin *console = wins_get_console();
-            win_print(console, '-', NULL, 0, THEME_WHITE_BOLD, "", "All commands");
-
-            GList *ordered_commands = NULL;
-            GHashTableIter iter;
-            gpointer key;
-            gpointer value;
-
-            g_hash_table_iter_init(&iter, commands);
-            while (g_hash_table_iter_next(&iter, &key, &value)) {
-                Command *pcmd = (Command *)value;
-                ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
-            }
-
-            int maxlen = 0;
-            GList *curr = ordered_commands;
-            while (curr) {
-                gchar *cmd = curr->data;
-                int len = strlen(cmd);
-                if (len > maxlen) maxlen = len;
-                curr = g_list_next(curr);
-            }
-
-            GString *cmds = g_string_new("");
-            curr = ordered_commands;
-            int count = 0;
-            while (curr) {
-                gchar *cmd = curr->data;
-                if (count == 5) {
-                    cons_show(cmds->str);
-                    g_string_free(cmds, TRUE);
-                    cmds = g_string_new("");
-                    count = 0;
-                }
-                g_string_append_printf(cmds, "%-*s", maxlen + 1, cmd);
-                curr = g_list_next(curr);
-                count++;
-            }
-            cons_show(cmds->str);
-            g_string_free(cmds, TRUE);
-            g_list_free(ordered_commands);
-            g_list_free(curr);
-
-            cons_show("");
-            cons_show("Use /help [command] without the leading slash, for help on a specific command");
-            cons_show("");
+            _cmd_help_cmd_list(NULL);
         }
     } else if (strcmp(args[0], "navigation") == 0) {
         cons_navigation_help();