From 9db851bb8227717b3cc2d4099c5fb43ee738e274 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Fri, 9 Oct 2020 11:27:23 -0500 Subject: [PATCH] Add command categories --- devshell.toml | 2 ++ docs/devshell.toml | 2 ++ docs/devshell.toml.md | 1 + mkDevShell/options.nix | 50 +++++++++++++++++++++++++++++++----------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/devshell.toml b/devshell.toml index 0dc0fa16..1955f9f7 100644 --- a/devshell.toml +++ b/devshell.toml @@ -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" diff --git a/docs/devshell.toml b/docs/devshell.toml index da84d794..858a3029 100644 --- a/docs/devshell.toml +++ b/docs/devshell.toml @@ -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" diff --git a/docs/devshell.toml.md b/docs/devshell.toml.md index 5e7cf2d0..a06969b3 100644 --- a/docs/devshell.toml.md +++ b/docs/devshell.toml.md @@ -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" diff --git a/mkDevShell/options.nix b/mkDevShell/options.nix index ca5d53f2..11aff60c 100644 --- a/mkDevShell/options.nix +++ b/mkDevShell/options.nix @@ -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' @@ -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 @@ -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; @@ -193,7 +218,6 @@ in help = "prints this menu"; name = "menu"; command = '' - echo "[commands]" cat <<'DEVSHELL_MENU' ${commandsToMenu config.commands} DEVSHELL_MENU