summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSilvan Mosberger <contact@infinisil.com>2020-09-17 17:58:04 +0200
committerSilvan Mosberger <contact@infinisil.com>2020-09-17 18:20:31 +0200
commit073e9b2aed0f855ce26bd3a26dab044c0c17c817 (patch)
tree6b0f3bae170b862c336d7820f4b3b2e38ca5f235 /lib
parent47f2eb89c16a74881c6e1a3b18290bf3d0f94a60 (diff)
lib/generators.toPretty: Improved string printing, handling newlines
Diffstat (limited to 'lib')
-rw-r--r--lib/generators.nix14
-rw-r--r--lib/tests/misc.nix27
2 files changed, 40 insertions, 1 deletions
diff --git a/lib/generators.nix b/lib/generators.nix
index 23a74c757f8a..81bedd13d80a 100644
--- a/lib/generators.nix
+++ b/lib/generators.nix
@@ -213,7 +213,19 @@ rec {
outroSpace = if multiline then "\n${indent}" else " ";
in if isInt v then toString v
else if isFloat v then "~${toString v}"
- else if isString v then ''"${libStr.escape [''"''] v}"''
+ else if isString v then
+ let
+ # Separate a string into its lines
+ newlineSplits = filter (v: ! isList v) (builtins.split "\n" v);
+ # For a '' string terminated by a \n, which happens when the closing '' is on a new line
+ multilineResult = "''" + introSpace + concatStringsSep introSpace (lib.init newlineSplits) + outroSpace + "''";
+ # For a '' string not terminated by a \n, which happens when the closing '' is not on a new line
+ multilineResult' = "''" + introSpace + concatStringsSep introSpace newlineSplits + "''";
+ # For single lines, replace all newlines with their escaped representation
+ singlelineResult = "\"" + libStr.escape [ "\"" ] (concatStringsSep "\\n" newlineSplits) + "\"";
+ in if multiline && length newlineSplits > 1 then
+ if lib.last newlineSplits == "" then multilineResult else multilineResult'
+ else singlelineResult
else if true == v then "true"
else if false == v then "false"
else if null == v then "null"
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 4fed8ef01c94..813a8e7f815f 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -450,7 +450,9 @@ runTests {
int = 42;
float = 0.1337;
bool = true;
+ emptystring = "";
string = ''fno"rd'';
+ newlinestring = "\n";
path = /. + "/foo";
null_ = null;
function = x: x;
@@ -463,7 +465,9 @@ runTests {
int = "42";
float = "~0.133700";
bool = "true";
+ emptystring = ''""'';
string = ''"fno\"rd"'';
+ newlinestring = "\"\\n\"";
path = "/foo";
null_ = "null";
function = "<λ>";
@@ -478,6 +482,16 @@ runTests {
expr = mapAttrs (const (generators.toPretty { })) rec {
list = [ 3 4 [ false ] ];
attrs = { foo = null; bar.foo = "baz"; };
+ newlinestring = "\n";
+ multilinestring = ''
+ hello
+ there
+ test
+ '';
+ multilinestring' = ''
+ hello
+ there
+ test'';
};
expected = rec {
list = ''
@@ -495,6 +509,19 @@ runTests {
};
foo = null;
}'';
+ newlinestring = "''\n \n''";
+ multilinestring = ''
+ '''
+ hello
+ there
+ test
+ ''''';
+ multilinestring' = ''
+ '''
+ hello
+ there
+ test''''';
+
};
};