doc: showOptions: Simplify code with builtins.groupBy

This makes grouping options by category much nicer. No behavior should
be changed.
This commit is contained in:
John Ericson 2023-09-23 00:28:16 -04:00
parent 694810ba34
commit 9c640c1229

View file

@ -1,8 +1,8 @@
let let
inherit (builtins) inherit (builtins)
attrNames attrValues fromJSON listToAttrs mapAttrs attrNames attrValues fromJSON listToAttrs mapAttrs groupBy
concatStringsSep concatMap length lessThan replaceStrings sort; concatStringsSep concatMap length lessThan replaceStrings sort;
inherit (import ./utils.nix) concatStrings optionalString filterAttrs trim squash unique showSettings; inherit (import ./utils.nix) attrsToList concatStrings optionalString filterAttrs trim squash unique showSettings;
in in
commandDump: commandDump:
@ -87,12 +87,11 @@ let
showOptions = allOptions: showOptions = allOptions:
let let
showCategory = cat: '' showCategory = cat: opts: ''
${optionalString (cat != "") "**${cat}:**"} ${optionalString (cat != "") "**${cat}:**"}
${listOptions (filterAttrs (n: v: v.category == cat) allOptions)} ${concatStringsSep "\n" (attrValues (mapAttrs showOption opts))}
''; '';
listOptions = opts: concatStringsSep "\n" (attrValues (mapAttrs showOption opts));
showOption = name: option: showOption = name: option:
let let
shortName = optionalString shortName = optionalString
@ -106,8 +105,12 @@ let
${option.description} ${option.description}
''; '';
categories = sort lessThan (unique (map (cmd: cmd.category) (attrValues allOptions))); categories = mapAttrs
in concatStrings (map showCategory categories); (_: listToAttrs)
(groupBy
(cmd: cmd.value.category)
(attrsToList allOptions));
in concatStrings (attrValues (mapAttrs showCategory categories));
in squash result; in squash result;
appendName = filename: name: (if filename == "nix" then "nix3" else filename) + "-" + name; appendName = filename: name: (if filename == "nix" then "nix3" else filename) + "-" + name;