Merge pull request #10208 from 9999years/print-strings-directly

`:print` strings directly in `nix repl`

(cherry picked from commit 3539172fd2f7cee639ce46423c58beca4231f2db)
Change-Id: I1972f3bf3b56312851f38288509d371d37f21677
Upstream-PR: https://github.com/NixOS/nix/pull/10208
This commit is contained in:
Robert Hensing 2024-03-11 10:02:09 +01:00 committed by Jade Lovelace
parent de20392c37
commit b995c17f0e
4 changed files with 101 additions and 3 deletions

View file

@ -412,6 +412,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
<< " :l, :load <path> Load Nix expression and add it to scope\n" << " :l, :load <path> Load Nix expression and add it to scope\n"
<< " :lf, :load-flake <ref> Load Nix flake and add it to scope\n" << " :lf, :load-flake <ref> Load Nix flake and add it to scope\n"
<< " :p, :print <expr> Evaluate and print expression recursively\n" << " :p, :print <expr> Evaluate and print expression recursively\n"
<< " Strings are printed directly, without escaping.\n"
<< " :q, :quit Exit nix-repl\n" << " :q, :quit Exit nix-repl\n"
<< " :r, :reload Reload all files\n" << " :r, :reload Reload all files\n"
<< " :sh <expr> Build dependencies of derivation, then start\n" << " :sh <expr> Build dependencies of derivation, then start\n"
@ -619,7 +620,11 @@ ProcessLineResult NixRepl::processLine(std::string line)
else if (command == ":p" || command == ":print") { else if (command == ":p" || command == ":print") {
Value v; Value v;
evalString(arg, v); evalString(arg, v);
if (v.type() == nString) {
std::cout << v.string.s;
} else {
printValue(std::cout, v); printValue(std::cout, v);
}
std::cout << std::endl; std::cout << std::endl;
} }

View file

@ -0,0 +1,92 @@
Printing a string with escapes in it will render as a string normally.
nix-repl> "meow\n\nmeowmeowmeow"
"meow\n\nmeowmeowmeow"
But with :p on the string itself it will print it literally to the output.
nix-repl> :p "meow\n\nmeowmeowmeow"
meow
meowmeowmeow
nix-repl> builtins.listToAttrs (builtins.genList (x: { name = "meow${toString x}"; value = { meow = { inherit x; s = "meowmeow\n\n${toString x}"; }; }; }) 10)
{
meow0 = { ... };
meow1 = { ... };
meow2 = { ... };
meow3 = { ... };
meow4 = { ... };
meow5 = { ... };
meow6 = { ... };
meow7 = { ... };
meow8 = { ... };
meow9 = { ... };
}
Also, :p will expand attrs, but it will leave the strings escaped as normal if
they aren't the top level item being printed.
nix-repl> :p builtins.listToAttrs (builtins.genList (x: { name = "meow${toString x}"; value = { meow = { inherit x; s = "meowmeow\n\n${toString x}"; }; }; }) 10)
{
meow0 = {
meow = {
s = "meowmeow\n\n0";
x = 0;
};
};
meow1 = {
meow = {
s = "meowmeow\n\n1";
x = 1;
};
};
meow2 = {
meow = {
s = "meowmeow\n\n2";
x = 2;
};
};
meow3 = {
meow = {
s = "meowmeow\n\n3";
x = 3;
};
};
meow4 = {
meow = {
s = "meowmeow\n\n4";
x = 4;
};
};
meow5 = {
meow = {
s = "meowmeow\n\n5";
x = 5;
};
};
meow6 = {
meow = {
s = "meowmeow\n\n6";
x = 6;
};
};
meow7 = {
meow = {
s = "meowmeow\n\n7";
x = 7;
};
};
meow8 = {
meow = {
s = "meowmeow\n\n8";
x = 8;
};
};
meow9 = {
meow = {
s = "meowmeow\n\n9";
x = 9;
};
};
}

View file

@ -185,5 +185,6 @@ REPL_TEST(regression_9917);
REPL_TEST(regression_9918); REPL_TEST(regression_9918);
REPL_TEST(regression_l145); REPL_TEST(regression_l145);
REPL_TEST(stack_vars); REPL_TEST(stack_vars);
REPL_TEST(repl_printing);
}; // namespace nix }; // namespace nix

View file

@ -71,7 +71,7 @@ namespace nix {
if (arg.type() != nString) { if (arg.type() != nString) {
return false; return false;
} }
return std::string_view(arg.string.s) == s; return std::string_view(arg.string.s) == std::string_view(s);
} }
MATCHER_P(IsIntEq, v, fmt("The string is equal to \"%1%\"", v)) { MATCHER_P(IsIntEq, v, fmt("The string is equal to \"%1%\"", v)) {
@ -106,7 +106,7 @@ namespace nix {
if (arg.type() != nPath) { if (arg.type() != nPath) {
*result_listener << "Expected a path got " << arg.type(); *result_listener << "Expected a path got " << arg.type();
return false; return false;
} else if (std::string_view(arg.string.s) != p) { } else if (std::string_view(arg._path) != p) {
*result_listener << "Expected a path that equals \"" << p << "\" but got: " << arg.string.s; *result_listener << "Expected a path that equals \"" << p << "\" but got: " << arg.string.s;
return false; return false;
} }