Group subcommands by category

This commit is contained in:
Eelco Dolstra 2021-01-25 18:19:32 +01:00
parent 3ba98ba8f0
commit 807d963ee8
3 changed files with 32 additions and 6 deletions

View file

@ -13,12 +13,27 @@ let
+ showSynopsis { inherit command; args = def.args; } + showSynopsis { inherit command; args = def.args; }
+ (if def.commands or {} != {} + (if def.commands or {} != {}
then then
let
categories = sort (x: y: x.id < y.id) (unique (map (cmd: cmd.category) (attrValues def.commands)));
listCommands = cmds:
concatStrings (map (name:
"* [`${command} ${name}`](./${appendName filename name}.md) - ${cmds.${name}.description}\n")
(attrNames cmds));
in
"where *subcommand* is one of the following:\n\n" "where *subcommand* is one of the following:\n\n"
# FIXME: group by category # FIXME: group by category
+ concatStrings (map (name: + (if length categories > 1
"* [`${command} ${name}`](./${appendName filename name}.md) - ${def.commands.${name}.description}\n") then
(attrNames def.commands)) concatStrings (map
+ "\n" (cat:
"**${toString cat.description}:**\n\n"
+ listCommands (filterAttrs (n: v: v.category == cat) def.commands)
+ "\n"
) categories)
+ "\n"
else
listCommands def.commands
+ "\n")
else "") else "")
+ (if def ? doc + (if def ? doc
then def.doc + "\n\n" then def.doc + "\n\n"

View file

@ -1,7 +1,15 @@
with builtins; with builtins;
{ rec {
splitLines = s: filter (x: !isList x) (split "\n" s); splitLines = s: filter (x: !isList x) (split "\n" s);
concatStrings = concatStringsSep ""; concatStrings = concatStringsSep "";
# FIXME: O(n^2)
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [];
nameValuePair = name: value: { inherit name value; };
filterAttrs = pred: set:
listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set));
} }

View file

@ -341,7 +341,10 @@ nlohmann::json MultiCommand::toJSON()
for (auto & [name, commandFun] : commands) { for (auto & [name, commandFun] : commands) {
auto command = commandFun(); auto command = commandFun();
auto j = command->toJSON(); auto j = command->toJSON();
j["category"] = categories[command->category()]; auto cat = nlohmann::json::object();
cat["id"] = command->category();
cat["description"] = categories[command->category()];
j["category"] = std::move(cat);
cmds[name] = std::move(j); cmds[name] = std::move(j);
} }