Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions lib/debug.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,7 @@ rec {
trace: { a = { b = {…}; }; }
=> null
*/
traceSeqN = depth: x: y: with lib;
let snip = v: if isList v then noQuotes "[…]" v
else if isAttrs v then noQuotes "{…}" v
else v;
noQuotes = str: v: { __pretty = const str; val = v; };
modify = n: fn: v: if (n == 0) then fn v
else if isList v then map (modify (n - 1) fn) v
else if isAttrs v then mapAttrs
(const (modify (n - 1) fn)) v
else v;
in trace (generators.toPretty { allowPrettyValues = true; }
(modify depth snip x)) y;
traceSeqN = depth: x: y: trace (lib.generators.toPretty { recursionLimit = depth; } x) y;

/* A combination of `traceVal` and `traceSeq` that applies a
provided function to the value to be traced after `deepSeq`ing
Expand Down
21 changes: 15 additions & 6 deletions lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,16 @@ rec {
(This means fn is type Val -> String.) */
allowPrettyValues ? false,
/* If this option is true, the output is indented with newlines for attribute sets and lists */
multiline ? true
multiline ? true,
recursionLimit ? null,
}@args: let
go = indent: v: with builtins;
go = depth: v: with builtins;
let isPath = v: typeOf v == "path";
indent = lib.concatStrings (lib.genList (_: " ") depth);
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 isString v then
let
Expand All @@ -232,8 +235,12 @@ rec {
else if isPath v then toString v
else if isList v then
if v == [] then "[ ]"
else if recursionLimit != null && depth >= recursionLimit then "[ ... ]"
else "[" + introSpace
+ libStr.concatMapStringsSep introSpace (go (indent + " ")) v
+ libStr.concatStringsSep introSpace (lib.imap0 (n: value:
builtins.addErrorContext "while lib.generators.toPretty descended into list index `${toString n}'"
(go (depth + 1) value)
) v)
+ outroSpace + "]"
else if isFunction v then
let fna = lib.functionArgs v;
Expand All @@ -249,13 +256,15 @@ rec {
else if v == {} then "{ }"
else if v ? type && v.type == "derivation" then
"<derivation ${v.drvPath}>"
else if recursionLimit != null && depth >= recursionLimit then "{ ... }"
else "{" + introSpace
+ libStr.concatStringsSep introSpace (libAttr.mapAttrsToList
(name: value:
"${libStr.escapeNixIdentifier name} = ${go (indent + " ") value};") v)
builtins.addErrorContext "while lib.generators.toPretty descended into the `${name}' attribute"
"${libStr.escapeNixIdentifier name} = ${go (depth + 1) value};") v)
+ outroSpace + "}"
else abort "generators.toPretty: should never happen (v = ${v})";
in go "";
in go 0;

# PLIST handling
toPlist = {}: v: let
Expand Down
12 changes: 10 additions & 2 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ runTests {
};

testToPrettyMultiline = {
expr = mapAttrs (const (generators.toPretty { })) rec {
expr = mapAttrs (const (generators.toPretty { recursionLimit = 3; })) rec {
list = [ 3 4 [ false ] ];
attrs = { foo = null; bar.foo = "baz"; };
newlinestring = "\n";
Expand All @@ -499,6 +499,7 @@ runTests {
hello
there
test'';
recursive = let x = { inherit x; }; in x;
};
expected = rec {
list = ''
Expand Down Expand Up @@ -528,7 +529,14 @@ runTests {
hello
there
test''''';

recursive = ''
{
x = {
x = {
x = { ... };
};
};
}'';
};
};

Expand Down