2023-12-12 23:57:36 +02:00
|
|
|
#pragma once
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Options for printing Nix values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-03-10 04:28:04 +02:00
|
|
|
/**
|
|
|
|
* How errors should be handled when printing values.
|
|
|
|
*/
|
|
|
|
enum class ErrorPrintBehavior {
|
|
|
|
/**
|
|
|
|
* Print the first line of the error in brackets: `«error: oh no!»`
|
|
|
|
*/
|
|
|
|
Print,
|
|
|
|
/**
|
|
|
|
* Throw the error to the code that attempted to print the value, instead
|
|
|
|
* of suppressing it it.
|
|
|
|
*/
|
|
|
|
Throw,
|
|
|
|
/**
|
|
|
|
* Only throw the error if encountered at the top level of the expression.
|
|
|
|
*
|
|
|
|
* This will cause expressions like `builtins.throw "uh oh!"` to throw
|
|
|
|
* errors, but will print attribute sets and other nested structures
|
|
|
|
* containing values that error (like `nixpkgs`) normally.
|
|
|
|
*/
|
|
|
|
ThrowTopLevel,
|
|
|
|
};
|
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* Options for printing Nix values.
|
|
|
|
*/
|
|
|
|
struct PrintOptions
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* If true, output ANSI color sequences.
|
|
|
|
*/
|
|
|
|
bool ansiColors = false;
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* If true, force values.
|
|
|
|
*/
|
|
|
|
bool force = false;
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* If true and `force` is set, print derivations as
|
|
|
|
* `«derivation /nix/store/...»` instead of as attribute sets.
|
|
|
|
*/
|
|
|
|
bool derivationPaths = false;
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* If true, track which values have been printed and skip them on
|
|
|
|
* subsequent encounters. Useful for self-referential values.
|
|
|
|
*/
|
|
|
|
bool trackRepeated = true;
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* Maximum depth to evaluate to.
|
|
|
|
*/
|
|
|
|
size_t maxDepth = std::numeric_limits<size_t>::max();
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
2023-12-06 22:42:53 +02:00
|
|
|
* Maximum number of attributes in attribute sets to print.
|
|
|
|
*
|
|
|
|
* Note that this is a limit for the entire print invocation, not for each
|
|
|
|
* attribute set encountered.
|
2023-12-12 23:57:36 +02:00
|
|
|
*/
|
|
|
|
size_t maxAttrs = std::numeric_limits<size_t>::max();
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* Maximum number of list items to print.
|
2023-12-06 22:42:53 +02:00
|
|
|
*
|
|
|
|
* Note that this is a limit for the entire print invocation, not for each
|
|
|
|
* list encountered.
|
2023-12-12 23:57:36 +02:00
|
|
|
*/
|
|
|
|
size_t maxListItems = std::numeric_limits<size_t>::max();
|
2024-02-04 10:40:30 +02:00
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
/**
|
|
|
|
* Maximum string length to print.
|
|
|
|
*/
|
|
|
|
size_t maxStringLength = std::numeric_limits<size_t>::max();
|
2024-02-04 10:40:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indentation width for pretty-printing.
|
|
|
|
*
|
|
|
|
* If set to 0 (the default), values are not pretty-printed.
|
|
|
|
*/
|
|
|
|
size_t prettyIndent = 0;
|
|
|
|
|
2024-03-10 04:28:04 +02:00
|
|
|
/**
|
|
|
|
* How to handle errors encountered while printing values.
|
|
|
|
*/
|
|
|
|
ErrorPrintBehavior errors = ErrorPrintBehavior::Print;
|
|
|
|
|
2024-02-04 10:40:30 +02:00
|
|
|
/**
|
|
|
|
* True if pretty-printing is enabled.
|
|
|
|
*/
|
2024-02-08 20:17:20 +02:00
|
|
|
inline bool shouldPrettyPrint()
|
2024-02-04 10:40:30 +02:00
|
|
|
{
|
|
|
|
return prettyIndent > 0;
|
|
|
|
}
|
2023-12-12 23:57:36 +02:00
|
|
|
};
|
|
|
|
|
2023-12-07 02:03:01 +02: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,
|
2024-03-10 04:28:04 +02:00
|
|
|
.maxStringLength = 1024,
|
2023-12-07 02:03:01 +02:00
|
|
|
};
|
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
}
|