|
20 | 20 |
|
21 | 21 | commandsToMenu = commands: |
22 | 22 | let |
23 | | - commandsSorted = builtins.sort (a: b: a.name < b.name) commands; |
24 | | - |
25 | 23 | commandLengths = |
26 | | - map ({ name, ... }: builtins.stringLength name) commandsSorted; |
| 24 | + map ({ name, ... }: builtins.stringLength name) commands; |
27 | 25 |
|
28 | 26 | maxCommandLength = |
29 | 27 | builtins.foldl' |
|
32 | 30 | commandLengths |
33 | 31 | ; |
34 | 32 |
|
35 | | - op = { name, help, ... }: |
| 33 | + commandCategories = lib.unique ( |
| 34 | + (zipAttrsWithNames [ "category" ] (name: vs: vs) commands).category |
| 35 | + ); |
| 36 | + |
| 37 | + commandByCategoriesSorted = |
| 38 | + builtins.attrValues (lib.genAttrs |
| 39 | + commandCategories |
| 40 | + (category: lib.nameValuePair category (builtins.sort |
| 41 | + (a: b: a.name < b.name) |
| 42 | + (builtins.filter |
| 43 | + (x: x.category == category) |
| 44 | + commands |
| 45 | + ) |
| 46 | + )) |
| 47 | + ); |
| 48 | + |
| 49 | + opCat = { name, value }: |
36 | 50 | let |
37 | | - len = maxCommandLength - (builtins.stringLength name); |
| 51 | + opCmd = { name, help, ...}: |
| 52 | + let |
| 53 | + len = maxCommandLength - (builtins.stringLength name); |
| 54 | + in |
| 55 | + if help == null || help == "" then |
| 56 | + name |
| 57 | + else |
| 58 | + "${pad name len} - ${help}"; |
38 | 59 | in |
39 | | - if help == null || help == "" then |
40 | | - name |
41 | | - else |
42 | | - "${pad name len} - ${help}" |
43 | | - ; |
44 | | - |
| 60 | + "\n[${name}]\n" + builtins.concatStringsSep "\n" (map opCmd value); |
45 | 61 | in |
46 | | - builtins.concatStringsSep "\n" (map op commandsSorted) |
| 62 | + builtins.concatStringsSep "\n" (map opCat commandByCategoriesSorted) |
47 | 63 | ; |
48 | 64 |
|
49 | 65 | # Because we want to be able to push pure JSON-like data into the |
|
61 | 77 | ''; |
62 | 78 | }; |
63 | 79 |
|
| 80 | + category = mkOption { |
| 81 | + type = types.str; |
| 82 | + default = "general commands"; |
| 83 | + description = '' |
| 84 | + Set a free text category under which this command is grouped |
| 85 | + and shown in the help menu. |
| 86 | + ''; |
| 87 | + }; |
| 88 | + |
64 | 89 | help = mkOption { |
65 | 90 | type = types.nullOr types.str; |
66 | 91 | default = null; |
|
193 | 218 | help = "prints this menu"; |
194 | 219 | name = "menu"; |
195 | 220 | command = '' |
196 | | - echo "[commands]" |
197 | 221 | cat <<'DEVSHELL_MENU' |
198 | 222 | ${commandsToMenu config.commands} |
199 | 223 | DEVSHELL_MENU |
|
0 commit comments