aboutsummaryrefslogtreecommitdiff
path: root/doc/manual/utils.nix
diff options
context:
space:
mode:
authorValentin Gagarin <valentin.gagarin@tweag.io>2022-08-26 23:09:19 +0200
committerValentin Gagarin <valentin.gagarin@tweag.io>2022-09-30 01:43:53 +0200
commita85df04fcb2876591ac39d55e920c7cd15411431 (patch)
tree581a9297079544ac3f320a9ca338b68d2834f2d5 /doc/manual/utils.nix
parent4655563470b59e0ef50a33af003058c2b54db778 (diff)
refactor showOptions
it was quite a pain to manipulate strings just with `builtins`
Diffstat (limited to 'doc/manual/utils.nix')
-rw-r--r--doc/manual/utils.nix26
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/manual/utils.nix b/doc/manual/utils.nix
index d4b18472f..d0643ef46 100644
--- a/doc/manual/utils.nix
+++ b/doc/manual/utils.nix
@@ -5,6 +5,32 @@ rec {
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)
unique = foldl' (acc: e: if elem e acc then acc else acc ++ [ e ]) [];