Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions devshell.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ command = "echo hello"
help = "used to format Nix code"
name = "nixpkgs-fmt"
package = "nixpkgs-fmt"
category = "formatters"

[[commands]]
help = "github utility"
name = "hub"
package = "gitAndTools.hub"
category = "utilites"
2 changes: 2 additions & 0 deletions docs/devshell.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ command = "echo hello"
help = "used to format Nix code"
name = "nixpkgs-fmt"
package = "nixpkgs-fmt"
category = "formatters"

[[commands]]
help = "github utility"
name = "hub"
package = "gitAndTools.hub"
category = "utilites"
1 change: 1 addition & 0 deletions docs/devshell.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ command = "echo hello"
help = "used to format Nix code"
name = "nixpkgs-fmt"
package = "nixpkgs-fmt"
category = "formatters"

[[commands]]
help = "github utility"
Expand Down
50 changes: 37 additions & 13 deletions mkDevShell/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ let

commandsToMenu = commands:
let
commandsSorted = builtins.sort (a: b: a.name < b.name) commands;

commandLengths =
map ({ name, ... }: builtins.stringLength name) commandsSorted;
map ({ name, ... }: builtins.stringLength name) commands;

maxCommandLength =
builtins.foldl'
Expand All @@ -32,18 +30,36 @@ let
commandLengths
;

op = { name, help, ... }:
commandCategories = lib.unique (
(zipAttrsWithNames [ "category" ] (name: vs: vs) commands).category
);

commandByCategoriesSorted =
builtins.attrValues (lib.genAttrs
commandCategories
(category: lib.nameValuePair category (builtins.sort
(a: b: a.name < b.name)
(builtins.filter
(x: x.category == category)
commands
)
))
);

opCat = { name, value }:
let
len = maxCommandLength - (builtins.stringLength name);
opCmd = { name, help, ...}:
let
len = maxCommandLength - (builtins.stringLength name);
in
if help == null || help == "" then
name
else
"${pad name len} - ${help}";
in
if help == null || help == "" then
name
else
"${pad name len} - ${help}"
;

"\n[${name}]\n" + builtins.concatStringsSep "\n" (map opCmd value);
in
builtins.concatStringsSep "\n" (map op commandsSorted)
builtins.concatStringsSep "\n" (map opCat commandByCategoriesSorted)
;

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

category = mkOption {
type = types.str;
default = "general commands";
description = ''
Set a free text category under which this command is grouped
and shown in the help menu.
'';
};

help = mkOption {
type = types.nullOr types.str;
default = null;
Expand Down Expand Up @@ -193,7 +218,6 @@ in
help = "prints this menu";
name = "menu";
command = ''
echo "[commands]"
cat <<'DEVSHELL_MENU'
${commandsToMenu config.commands}
DEVSHELL_MENU
Expand Down