Skip to content

Commit 4859a38

Browse files
author
David Arnold
authored
Add command categories (#32)
1 parent 2503db5 commit 4859a38

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

devshell.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ command = "echo hello"
4747
help = "used to format Nix code"
4848
name = "nixpkgs-fmt"
4949
package = "nixpkgs-fmt"
50+
category = "formatters"
5051

5152
[[commands]]
5253
help = "github utility"
5354
name = "hub"
5455
package = "gitAndTools.hub"
56+
category = "utilites"

docs/devshell.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ command = "echo hello"
4747
help = "used to format Nix code"
4848
name = "nixpkgs-fmt"
4949
package = "nixpkgs-fmt"
50+
category = "formatters"
5051

5152
[[commands]]
5253
help = "github utility"
5354
name = "hub"
5455
package = "gitAndTools.hub"
56+
category = "utilites"

docs/devshell.toml.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ command = "echo hello"
5555
help = "used to format Nix code"
5656
name = "nixpkgs-fmt"
5757
package = "nixpkgs-fmt"
58+
category = "formatters"
5859

5960
[[commands]]
6061
help = "github utility"

mkDevShell/options.nix

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ let
2020

2121
commandsToMenu = commands:
2222
let
23-
commandsSorted = builtins.sort (a: b: a.name < b.name) commands;
24-
2523
commandLengths =
26-
map ({ name, ... }: builtins.stringLength name) commandsSorted;
24+
map ({ name, ... }: builtins.stringLength name) commands;
2725

2826
maxCommandLength =
2927
builtins.foldl'
@@ -32,18 +30,36 @@ let
3230
commandLengths
3331
;
3432

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 }:
3650
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}";
3859
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);
4561
in
46-
builtins.concatStringsSep "\n" (map op commandsSorted)
62+
builtins.concatStringsSep "\n" (map opCat commandByCategoriesSorted)
4763
;
4864

4965
# Because we want to be able to push pure JSON-like data into the
@@ -61,6 +77,15 @@ let
6177
'';
6278
};
6379

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+
6489
help = mkOption {
6590
type = types.nullOr types.str;
6691
default = null;
@@ -193,7 +218,6 @@ in
193218
help = "prints this menu";
194219
name = "menu";
195220
command = ''
196-
echo "[commands]"
197221
cat <<'DEVSHELL_MENU'
198222
${commandsToMenu config.commands}
199223
DEVSHELL_MENU

0 commit comments

Comments
 (0)