From 667a643582eb179307b472149ce4ddc60631f63e Mon Sep 17 00:00:00 2001 From: Kevin Robert Stravers Date: Mon, 2 Sep 2024 18:51:32 -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 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 0aed7eda865..dea62ac9ffc 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -72,6 +72,7 @@ struct NixRepl const static int envSize = 32768; std::shared_ptr staticEnv; + Value lastLoaded; Env * env; int displ; StringSet varNames; @@ -96,6 +97,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); @@ -470,6 +472,10 @@ ProcessLineResult NixRepl::processLine(std::string line) loadFlake(arg); } + else if (command == ":ll" || command == ":last-flake") { + showLastLoaded(); + } + else if (command == ":r" || command == ":reload") { state->resetFileCache(); reloadFiles(); @@ -750,6 +756,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() { @@ -791,12 +807,14 @@ void NixRepl::addAttrsToScope(Value & attrs) staticEnv->deduplicate(); notice("Added %1% variables.", attrs.attrs()->size()); + lastLoaded = attrs; + const int max_print = 10; int count = 0; for (auto & i : *attrs.attrs()) { count += 1; if (count > max_print) { - notice("... And %1% other variables", attrs.attrs()->size() - max_print); + notice("... And %1% other variables, use :ll to see these", attrs.attrs()->size() - max_print); break; }