Skip to content

Commit

Permalink
feat: add help wrapping and fix interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Jan 17, 2024
1 parent 685be77 commit 7a9e2df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion modules/commands.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ lib, config, pkgs, options, ... }:
let
inherit (import ../nix/commands/lib.nix { inherit pkgs options; })
inherit (import ../nix/commands/lib.nix { inherit pkgs options config; })
commandsType
commandToPackage
devshellMenuCommandName
Expand Down
53 changes: 34 additions & 19 deletions nix/commands/devshell.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{ system ? builtins.currentSystem
, pkgs ? import ../nixpkgs.nix { inherit system; }
, config ? { }
}:
let
lib = builtins // pkgs.lib;
Expand Down Expand Up @@ -111,27 +112,41 @@ rec {
opCmd = { name, help, interpolate, ... }:
let
len = maxCommandLength - (lib.stringLength name);

nameWidth = toString maxCommandLength;
helpWidth = toString (config.devshell.menu.width - (maxCommandLength + 5));
helpHeight = toString 100;

processHelp = x:
if (if interpolate != null then interpolate else menuConfig.interpolate)
then ''\'' + "\n" +
''
"$(
cat << EOF
${x}
EOF
)"
''
else lib.escapeShellArg x;

highlyUnlikelyName = "ABDH_OKKD_VOAP_DOEE_PJGD";

command = ''
${highlyUnlikelyName}=${
if help == null || help == ""
then ""
else processHelp help
}
${lib.getExe pkgs.perl} ${./scripts/formatCommand.pl} '${toString nameWidth}' '${helpWidth}' '${helpHeight}' '${name}' "''$${highlyUnlikelyName}"'';
in
if help == null || help == "" then
"printf ' ${name}'"
else
"printf ' ${pad name len} - '\n" +
(
let
highlyUnlikelyName = "ABDH_OKKD_VOAP_DOEE_PJGD";
quotedName = (
x:
if (if interpolate != null then interpolate else menuConfig.interpolate)
then ''${x}''
else "'${x}'"
)
highlyUnlikelyName;
in
"cat <<${quotedName}\n${help}\n${highlyUnlikelyName}\n"
);
command;
commandsColumns = lib.concatMapStringsSep "\n" opCmd cmd;
in
''printf '\n${ansi.bold}[${category}]${ansi.reset}\n\n''
+ "'\n\n" + lib.concatStringsSep "\n" (map opCmd cmd);
''
printf '\n${ansi.bold}[${category}]${ansi.reset}\n\n'
${commandsColumns}
'';
in
lib.concatStringsSep "\n" (map opCat commandByCategoriesSorted) + "\n";
}
3 changes: 2 additions & 1 deletion nix/commands/lib.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{ system ? builtins.currentSystem
, pkgs ? import ../nixpkgs.nix { inherit system; }
, options ? { }
, config ? { }
}:
(import ./types.nix { inherit pkgs options; }) //
(import ./devshell.nix { inherit pkgs; }) //
(import ./devshell.nix { inherit pkgs config; }) //
(import ./commandsType.nix { inherit pkgs options; })

0 comments on commit 7a9e2df

Please sign in to comment.