refactor showOptions
it was quite a pain to manipulate strings just with `builtins`
This commit is contained in:
parent
4655563470
commit
a85df04fcb
2 changed files with 45 additions and 26 deletions
|
@ -41,32 +41,25 @@ let
|
||||||
|
|
||||||
${showOptions def.flags}
|
${showOptions def.flags}
|
||||||
'';
|
'';
|
||||||
showOptions = flags:
|
showOptions = options:
|
||||||
let
|
let
|
||||||
categories = sort builtins.lessThan (unique (map (cmd: cmd.category) (attrValues flags)));
|
showCategory = cat: ''
|
||||||
in
|
${if cat != "" then "**${cat}:**" else ""}
|
||||||
concatStrings (map
|
|
||||||
(cat:
|
${listOptions (filterAttrs (n: v: v.category == cat) options)}
|
||||||
(if cat != ""
|
'';
|
||||||
then "**${cat}:**\n\n"
|
listOptions = opts: concatStringsSep "\n" (attrValues (mapAttrs showOption opts));
|
||||||
else "")
|
showOption = name: option:
|
||||||
+ concatStrings
|
|
||||||
(map (longName:
|
|
||||||
let
|
let
|
||||||
flag = flags.${longName};
|
shortName = if option ? shortName then "/ `-${option.shortName}`" else "";
|
||||||
in
|
labels = if option ? labels then (concatStringsSep " " (map (s: "*${s}*") option.labels)) else "";
|
||||||
" - `--${longName}`"
|
in trim ''
|
||||||
+ (if flag ? shortName then " / `-${flag.shortName}`" else "")
|
- `--${name}` ${shortName} ${labels}
|
||||||
+ (if flag ? labels then " " + (concatStringsSep " " (map (s: "*${s}*") flag.labels)) else "")
|
|
||||||
+ " \n"
|
${option.description}
|
||||||
+ " " + flag.description + "\n\n"
|
'';
|
||||||
) (attrNames (filterAttrs (n: v: v.category == cat) flags))))
|
categories = sort builtins.lessThan (unique (map (cmd: cmd.category) (attrValues options)));
|
||||||
categories);
|
in concatStrings (map showCategory categories);
|
||||||
squash = string: # squash more than two repeated newlines
|
|
||||||
let
|
|
||||||
replaced = replaceStrings [ "\n\n\n" ] [ "\n\n" ] string;
|
|
||||||
in
|
|
||||||
if replaced == string then string else squash replaced;
|
|
||||||
in squash ''
|
in squash ''
|
||||||
> **Warning** \
|
> **Warning** \
|
||||||
> This program is **experimental** and its interface is subject to change.
|
> This program is **experimental** and its interface is subject to change.
|
||||||
|
|
|
@ -5,6 +5,32 @@ rec {
|
||||||
|
|
||||||
concatStrings = concatStringsSep "";
|
concatStrings = concatStringsSep "";
|
||||||
|
|
||||||
|
replaceStringsRec = from: to: string:
|
||||||
|
# recursively replace occurrences of `from` with `to` within `string`
|
||||||
|
# example:
|
||||||
|
# replaceStringRec "--" "-" "hello-----world"
|
||||||
|
# => "hello-world"
|
||||||
|
let
|
||||||
|
replaced = replaceStrings [ from ] [ to ] string;
|
||||||
|
in
|
||||||
|
if replaced == string then string else replaceStringsRec from to replaced;
|
||||||
|
|
||||||
|
squash = replaceStringsRec "\n\n\n" "\n\n";
|
||||||
|
|
||||||
|
trim = string:
|
||||||
|
# trim trailing spaces and squash non-leading spaces
|
||||||
|
let
|
||||||
|
trimLine = line:
|
||||||
|
let
|
||||||
|
# separate leading spaces from the rest
|
||||||
|
parts = split "(^ *)" line;
|
||||||
|
spaces = head (elemAt parts 1);
|
||||||
|
rest = elemAt parts 2;
|
||||||
|
# drop trailing spaces
|
||||||
|
body = head (split " *$" rest);
|
||||||
|
in spaces + replaceStringsRec " " " " body;
|
||||||
|
in concatStringsSep "\n" (map trimLine (splitLines string));
|
||||||
|
|
||||||
# FIXME: O(n^2)
|
# FIXME: O(n^2)
|
||||||
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [];
|
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue