Unify and refactor value printing
Previously, there were two mostly-identical value printers -- one in
`libexpr/eval.cc` (which didn't force values) and one in
`libcmd/repl.cc` (which did force values and also printed ANSI color
codes).
This PR unifies both of these printers into `print.cc` and provides a
`PrintOptions` struct for controlling the output, which allows for
toggling whether values are forced, whether repeated values are tracked,
and whether ANSI color codes are displayed.
Additionally, `PrintOptions` allows tuning the maximum number of
attributes, list items, and bytes in a string that will be displayed;
this makes it ideal for contexts where printing too much output (e.g.
all of Nixpkgs) is distracting. (As requested by @roberth in
https://github.com/NixOS/nix/pull/9554#issuecomment-1845095735)
Please read the tests for example output.
Future work:
- It would be nice to provide this function as a builtin, perhaps
`builtins.toStringDebug` -- a printing function that never fails would
be useful when debugging Nix code.
- It would be nice to support customizing `PrintOptions` members on the
command line, e.g. `--option to-string-max-attrs 1000`.
(cherry picked from commit 0fa08b451682fb3311fe58112ff05c4fe5bee3a4, )
===
Restore ambiguous value printer for `nix-instantiate`
The Nix team has requested that this output format remain unchanged.
I've added a warning to the man page explaining that `nix-instantiate
--eval` output will not parse correctly in many situations.
(cherry picked from commit df84dd4d8dd3fd6381ac2ca3064432ab31a16b79)
Change-Id: I7cca6b4b53cd0642f2d49af657d5676a8554c9f8
2024-03-08 02:05:47 +00:00
|
|
|
#pragma once
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Options for printing Nix values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options for printing Nix values.
|
|
|
|
*/
|
|
|
|
struct PrintOptions
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* If true, output ANSI color sequences.
|
|
|
|
*/
|
|
|
|
bool ansiColors = false;
|
|
|
|
/**
|
|
|
|
* If true, force values.
|
|
|
|
*/
|
|
|
|
bool force = false;
|
|
|
|
/**
|
|
|
|
* If true and `force` is set, print derivations as
|
|
|
|
* `«derivation /nix/store/...»` instead of as attribute sets.
|
|
|
|
*/
|
|
|
|
bool derivationPaths = false;
|
|
|
|
/**
|
|
|
|
* If true, track which values have been printed and skip them on
|
|
|
|
* subsequent encounters. Useful for self-referential values.
|
|
|
|
*/
|
|
|
|
bool trackRepeated = true;
|
|
|
|
/**
|
|
|
|
* Maximum depth to evaluate to.
|
|
|
|
*/
|
|
|
|
size_t maxDepth = std::numeric_limits<size_t>::max();
|
|
|
|
/**
|
|
|
|
* Maximum number of attributes in an attribute set to print.
|
|
|
|
*/
|
|
|
|
size_t maxAttrs = std::numeric_limits<size_t>::max();
|
|
|
|
/**
|
|
|
|
* Maximum number of list items to print.
|
|
|
|
*/
|
|
|
|
size_t maxListItems = std::numeric_limits<size_t>::max();
|
|
|
|
/**
|
|
|
|
* Maximum string length to print.
|
|
|
|
*/
|
|
|
|
size_t maxStringLength = std::numeric_limits<size_t>::max();
|
|
|
|
};
|
|
|
|
|
2024-03-08 03:49:08 +00:00
|
|
|
/**
|
|
|
|
* `PrintOptions` for unknown and therefore potentially large values in error messages,
|
|
|
|
* to avoid printing "too much" output.
|
|
|
|
*/
|
|
|
|
static PrintOptions errorPrintOptions = PrintOptions {
|
|
|
|
.ansiColors = true,
|
|
|
|
.maxDepth = 10,
|
|
|
|
.maxAttrs = 10,
|
|
|
|
.maxListItems = 10,
|
|
|
|
.maxStringLength = 1024
|
|
|
|
};
|
|
|
|
|
Unify and refactor value printing
Previously, there were two mostly-identical value printers -- one in
`libexpr/eval.cc` (which didn't force values) and one in
`libcmd/repl.cc` (which did force values and also printed ANSI color
codes).
This PR unifies both of these printers into `print.cc` and provides a
`PrintOptions` struct for controlling the output, which allows for
toggling whether values are forced, whether repeated values are tracked,
and whether ANSI color codes are displayed.
Additionally, `PrintOptions` allows tuning the maximum number of
attributes, list items, and bytes in a string that will be displayed;
this makes it ideal for contexts where printing too much output (e.g.
all of Nixpkgs) is distracting. (As requested by @roberth in
https://github.com/NixOS/nix/pull/9554#issuecomment-1845095735)
Please read the tests for example output.
Future work:
- It would be nice to provide this function as a builtin, perhaps
`builtins.toStringDebug` -- a printing function that never fails would
be useful when debugging Nix code.
- It would be nice to support customizing `PrintOptions` members on the
command line, e.g. `--option to-string-max-attrs 1000`.
(cherry picked from commit 0fa08b451682fb3311fe58112ff05c4fe5bee3a4, )
===
Restore ambiguous value printer for `nix-instantiate`
The Nix team has requested that this output format remain unchanged.
I've added a warning to the man page explaining that `nix-instantiate
--eval` output will not parse correctly in many situations.
(cherry picked from commit df84dd4d8dd3fd6381ac2ca3064432ab31a16b79)
Change-Id: I7cca6b4b53cd0642f2d49af657d5676a8554c9f8
2024-03-08 02:05:47 +00:00
|
|
|
}
|