Skip to content

Commit 7a9e2df

Browse files
committed
feat: add help wrapping and fix interpolation
1 parent 685be77 commit 7a9e2df

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

modules/commands.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{ lib, config, pkgs, options, ... }:
22
let
3-
inherit (import ../nix/commands/lib.nix { inherit pkgs options; })
3+
inherit (import ../nix/commands/lib.nix { inherit pkgs options config; })
44
commandsType
55
commandToPackage
66
devshellMenuCommandName

nix/commands/devshell.nix

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{ system ? builtins.currentSystem
22
, pkgs ? import ../nixpkgs.nix { inherit system; }
3+
, config ? { }
34
}:
45
let
56
lib = builtins // pkgs.lib;
@@ -111,27 +112,41 @@ rec {
111112
opCmd = { name, help, interpolate, ... }:
112113
let
113114
len = maxCommandLength - (lib.stringLength name);
115+
116+
nameWidth = toString maxCommandLength;
117+
helpWidth = toString (config.devshell.menu.width - (maxCommandLength + 5));
118+
helpHeight = toString 100;
119+
120+
processHelp = x:
121+
if (if interpolate != null then interpolate else menuConfig.interpolate)
122+
then ''\'' + "\n" +
123+
''
124+
"$(
125+
cat << EOF
126+
${x}
127+
EOF
128+
)"
129+
''
130+
else lib.escapeShellArg x;
131+
132+
highlyUnlikelyName = "ABDH_OKKD_VOAP_DOEE_PJGD";
133+
134+
command = ''
135+
${highlyUnlikelyName}=${
136+
if help == null || help == ""
137+
then ""
138+
else processHelp help
139+
}
140+
${lib.getExe pkgs.perl} ${./scripts/formatCommand.pl} '${toString nameWidth}' '${helpWidth}' '${helpHeight}' '${name}' "''$${highlyUnlikelyName}"'';
114141
in
115-
if help == null || help == "" then
116-
"printf ' ${name}'"
117-
else
118-
"printf ' ${pad name len} - '\n" +
119-
(
120-
let
121-
highlyUnlikelyName = "ABDH_OKKD_VOAP_DOEE_PJGD";
122-
quotedName = (
123-
x:
124-
if (if interpolate != null then interpolate else menuConfig.interpolate)
125-
then ''${x}''
126-
else "'${x}'"
127-
)
128-
highlyUnlikelyName;
129-
in
130-
"cat <<${quotedName}\n${help}\n${highlyUnlikelyName}\n"
131-
);
142+
command;
143+
commandsColumns = lib.concatMapStringsSep "\n" opCmd cmd;
132144
in
133-
''printf '\n${ansi.bold}[${category}]${ansi.reset}\n\n''
134-
+ "'\n\n" + lib.concatStringsSep "\n" (map opCmd cmd);
145+
''
146+
printf '\n${ansi.bold}[${category}]${ansi.reset}\n\n'
147+
148+
${commandsColumns}
149+
'';
135150
in
136151
lib.concatStringsSep "\n" (map opCat commandByCategoriesSorted) + "\n";
137152
}

nix/commands/lib.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{ system ? builtins.currentSystem
22
, pkgs ? import ../nixpkgs.nix { inherit system; }
33
, options ? { }
4+
, config ? { }
45
}:
56
(import ./types.nix { inherit pkgs options; }) //
6-
(import ./devshell.nix { inherit pkgs; }) //
7+
(import ./devshell.nix { inherit pkgs config; }) //
78
(import ./commandsType.nix { inherit pkgs options; })

0 commit comments

Comments
 (0)