{"thread":{"id":"2871","subject":"Make \"git help\" sort git commands in columns","startedAt":"2005-12-18T20:41:10Z","lastAt":"2005-12-18T20:41:10Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"13792","messageId":"Pine.LNX.4.64.0512181229370.4827@g5.osdl.org","threadId":"2871","inReplyTo":null,"subject":"Make \"git help\" sort git commands in columns","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2005-12-18T20:41:10Z","receivedAt":"2005-12-18T20:41:10Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nThis changes \"pretty_print_string_list()\" to show the git commands \nalphabetically in column order, which is the normal one. \n\nIe instead of doing\n\n\tgit commands available in '/home/torvalds/bin'\n\t----------------------------------------------\n\t  add                am                 ...\n\t  applypatch         archimport         ...\n\t  cat-file           check-ref-format   ...\n\t...\n\nit does\n\n\tgit commands available in '/home/torvalds/bin'\n\t----------------------------------------------\n\t  add                diff-tree          ...\n\t  am                 fetch              ...\n\t  apply              fetch-pack         ...\n\t...\n\nwhere each column is sorted.\n\nThis is how \"ls\" sorts things too, and since visually the columns are much \nmore distinct than the rows, so it _looks_ more sorted.\n\nThe \"ls\" command has a \"-x\" option that lists entries by lines (the way \ngit.c used to): if somebody wants to do that, the new print-out logic \ncould be easily accomodated to that too. Matter of taste and preference, I \nguess.\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nSide note: this is independent of the other patch I just sent out. Take \neither or both.\n\n----\ndiff --git a/git.c b/git.c\nindex c26cac6..0fd95bf 100644\n--- a/git.c\n+++ b/git.c\n@@ -74,25 +85,28 @@ static int cmdname_compare(const void *a\n \n static void pretty_print_string_list(struct cmdname **cmdname, int longest)\n {\n-\tint cols = 1;\n+\tint cols = 1, rows;\n \tint space = longest + 1; /* min 1 SP between words */\n \tint max_cols = term_columns() - 1; /* don't print *on* the edge */\n-\tint i;\n+\tint i, j;\n \n \tif (space < max_cols)\n \t\tcols = max_cols / space;\n+\trows = (cmdname_cnt + cols - 1) / cols;\n \n \tqsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);\n \n-\tfor (i = 0; i < cmdname_cnt; ) {\n-\t\tint c;\n+\tfor (i = 0; i < rows; i++) {\n \t\tprintf(\"  \");\n \n-\t\tfor (c = cols; c && i < cmdname_cnt; i++) {\n-\t\t\tprintf(\"%s\", cmdname[i]->name);\n-\n-\t\t\tif (--c)\n-\t\t\t\tmput_char(' ', space - cmdname[i]->len);\n+\t\tfor (j = 0; j < cols; j++) {\n+\t\t\tint n = j * rows + i;\n+\t\t\tint size = space;\n+\t\t\tif (n >= cmdname_cnt)\n+\t\t\t\tbreak;\n+\t\t\tif (j == cols-1 || n + rows >= cmdname_cnt)\n+\t\t\t\tsize = 1;\n+\t\t\tprintf(\"%-*s\", size, cmdname[n]->name);\n \t\t}\n \t\tputchar('\\n');\n \t}\n"}]}