lib/generators.toPretty: Implement multiline printing
This commit is contained in:
parent
4811f54e94
commit
47f2eb89c1
|
@ -203,9 +203,14 @@ rec {
|
||||||
/* If this option is true, attrsets like { __pretty = fn; val = …; }
|
/* If this option is true, attrsets like { __pretty = fn; val = …; }
|
||||||
will use fn to convert val to a pretty printed representation.
|
will use fn to convert val to a pretty printed representation.
|
||||||
(This means fn is type Val -> String.) */
|
(This means fn is type Val -> String.) */
|
||||||
allowPrettyValues ? false
|
allowPrettyValues ? false,
|
||||||
}@args: let go = v: with builtins;
|
/* If this option is true, the output is indented with newlines for attribute sets and lists */
|
||||||
|
multiline ? true
|
||||||
|
}@args: let
|
||||||
|
go = indent: v: with builtins;
|
||||||
let isPath = v: typeOf v == "path";
|
let isPath = v: typeOf v == "path";
|
||||||
|
introSpace = if multiline then "\n${indent} " else " ";
|
||||||
|
outroSpace = if multiline then "\n${indent}" else " ";
|
||||||
in if isInt v then toString v
|
in if isInt v then toString v
|
||||||
else if isFloat v then "~${toString v}"
|
else if isFloat v then "~${toString v}"
|
||||||
else if isString v then ''"${libStr.escape [''"''] v}"''
|
else if isString v then ''"${libStr.escape [''"''] v}"''
|
||||||
|
@ -213,9 +218,9 @@ rec {
|
||||||
else if false == v then "false"
|
else if false == v then "false"
|
||||||
else if null == v then "null"
|
else if null == v then "null"
|
||||||
else if isPath v then toString v
|
else if isPath v then toString v
|
||||||
else if isList v then "[ "
|
else if isList v then "[" + introSpace
|
||||||
+ libStr.concatMapStringsSep " " go v
|
+ libStr.concatMapStringsSep introSpace (go (indent + " ")) v
|
||||||
+ " ]"
|
+ outroSpace + "]"
|
||||||
else if isAttrs v then
|
else if isAttrs v then
|
||||||
# apply pretty values if allowed
|
# apply pretty values if allowed
|
||||||
if attrNames v == [ "__pretty" "val" ] && allowPrettyValues
|
if attrNames v == [ "__pretty" "val" ] && allowPrettyValues
|
||||||
|
@ -224,11 +229,11 @@ rec {
|
||||||
else if v ? type && v.type == "derivation" then
|
else if v ? type && v.type == "derivation" then
|
||||||
"<δ:${v.name}>"
|
"<δ:${v.name}>"
|
||||||
# "<δ:${concatStringsSep "," (builtins.attrNames v)}>"
|
# "<δ:${concatStringsSep "," (builtins.attrNames v)}>"
|
||||||
else "{ "
|
else "{" + introSpace
|
||||||
+ libStr.concatStringsSep " " (libAttr.mapAttrsToList
|
+ libStr.concatStringsSep introSpace (libAttr.mapAttrsToList
|
||||||
(name: value:
|
(name: value:
|
||||||
"${libStr.escapeNixIdentifier name} = ${go value};") v)
|
"${libStr.escapeNixIdentifier name} = ${go (indent + " ") value};") v)
|
||||||
+ " }"
|
+ outroSpace + "}"
|
||||||
else if isFunction v then
|
else if isFunction v then
|
||||||
let fna = lib.functionArgs v;
|
let fna = lib.functionArgs v;
|
||||||
showFnas = concatStringsSep "," (libAttr.mapAttrsToList
|
showFnas = concatStringsSep "," (libAttr.mapAttrsToList
|
||||||
|
@ -237,7 +242,7 @@ rec {
|
||||||
in if fna == {} then "<λ>"
|
in if fna == {} then "<λ>"
|
||||||
else "<λ:{${showFnas}}>"
|
else "<λ:{${showFnas}}>"
|
||||||
else abort "generators.toPretty: should never happen (v = ${v})";
|
else abort "generators.toPretty: should never happen (v = ${v})";
|
||||||
in go;
|
in go "";
|
||||||
|
|
||||||
# PLIST handling
|
# PLIST handling
|
||||||
toPlist = {}: v: let
|
toPlist = {}: v: let
|
||||||
|
|
|
@ -446,7 +446,7 @@ runTests {
|
||||||
};
|
};
|
||||||
|
|
||||||
testToPretty = {
|
testToPretty = {
|
||||||
expr = mapAttrs (const (generators.toPretty {})) rec {
|
expr = mapAttrs (const (generators.toPretty { multiline = false; })) rec {
|
||||||
int = 42;
|
int = 42;
|
||||||
float = 0.1337;
|
float = 0.1337;
|
||||||
bool = true;
|
bool = true;
|
||||||
|
@ -474,6 +474,30 @@ runTests {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testToPrettyMultiline = {
|
||||||
|
expr = mapAttrs (const (generators.toPretty { })) rec {
|
||||||
|
list = [ 3 4 [ false ] ];
|
||||||
|
attrs = { foo = null; bar.foo = "baz"; };
|
||||||
|
};
|
||||||
|
expected = rec {
|
||||||
|
list = ''
|
||||||
|
[
|
||||||
|
3
|
||||||
|
4
|
||||||
|
[
|
||||||
|
false
|
||||||
|
]
|
||||||
|
]'';
|
||||||
|
attrs = ''
|
||||||
|
{
|
||||||
|
bar = {
|
||||||
|
foo = "baz";
|
||||||
|
};
|
||||||
|
foo = null;
|
||||||
|
}'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
testToPrettyAllowPrettyValues = {
|
testToPrettyAllowPrettyValues = {
|
||||||
expr = generators.toPretty { allowPrettyValues = true; }
|
expr = generators.toPretty { allowPrettyValues = true; }
|
||||||
{ __pretty = v: "«" + v + "»"; val = "foo"; };
|
{ __pretty = v: "«" + v + "»"; val = "foo"; };
|
||||||
|
|
Loading…
Reference in New Issue