From ff1241075b526420bfc2b20987dcf70359d96d5f Mon Sep 17 00:00:00 2001 From: Kevin Robert Stravers Date: Sat, 21 Sep 2024 02:58:18 -0400 Subject: [PATCH] nix repl: Add `:ll` to show all recently loaded variables Invoking `:ll` will start a pager with all variables which have just been loaded by `:lf`, `:l`, or by a flake provided to `nix repl` as an argument. https://github.com/NixOS/nix/issues/11404 --- src/libcmd/repl.cc | 20 +++++++++++++++++++- tests/functional/repl.sh | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index da01a3105..10b048070 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -66,6 +66,7 @@ struct NixRepl const static int envSize = 32768; std::shared_ptr staticEnv; + Value lastLoaded; Env * env; int displ; StringSet varNames; @@ -90,6 +91,7 @@ struct NixRepl void loadFile(const Path & path); void loadFlake(const std::string & flakeRef); void loadFiles(); + void showLastLoaded(); void reloadFiles(); void addAttrsToScope(Value & attrs); void addVarToScope(const Symbol name, Value & v); @@ -464,6 +466,10 @@ ProcessLineResult NixRepl::processLine(std::string line) loadFlake(arg); } + else if (command == ":ll" || command == ":last-loaded") { + showLastLoaded(); + } + else if (command == ":r" || command == ":reload") { state->resetFileCache(); reloadFiles(); @@ -751,6 +757,16 @@ void NixRepl::initEnv() varNames.emplace(state->symbols[i.first]); } +void NixRepl::showLastLoaded() +{ + RunPager pager; + + for (auto & i : *lastLoaded.attrs()) { + std::string_view name = state->symbols[i.name]; + logger->cout(name); + } +} + void NixRepl::reloadFiles() { @@ -792,6 +808,8 @@ void NixRepl::addAttrsToScope(Value & attrs) staticEnv->deduplicate(); notice("Added %1% variables.", attrs.attrs()->size()); + lastLoaded = attrs; + const int max_print = 10; int counter = 0; std::string loaded; @@ -809,7 +827,7 @@ void NixRepl::addAttrsToScope(Value & attrs) notice("- %1%", loaded); if (attrs.attrs()->size() > max_print) - notice("... and %1% more", attrs.attrs()->size() - max_print); + notice("... and %1% more; view with :ll", attrs.attrs()->size() - max_print); } diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh index 0fbf07765..8d4c33db8 100755 --- a/tests/functional/repl.sh +++ b/tests/functional/repl.sh @@ -177,7 +177,7 @@ testReplResponseNoRegex $' :a builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 13)) ' 'Added 13 variables. - 0, 1, 10, 11, 12, 2, 3, 4, 5, 6 -... and 3 more' +... and 3 more; view with :ll' # Test the `:reload` mechansim with flakes: # - Eval `./flake#changingThing` @@ -329,7 +329,7 @@ runRepl () { -e "s@$nixVersion@@g" \ -e "s@Added [0-9]* variables@Added variables@g" \ -e '/^- /d' \ - -e '/\.\.\. and [0-9]* more/d' \ + -e '/\.\.\. and [0-9]* more; view with :ll/d' \ | grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \ ; }