1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

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
This commit is contained in:
Kevin Robert Stravers 2024-09-02 18:51:32 -04:00
parent fa0b3b6595
commit 9db7cce5ff
No known key found for this signature in database
GPG key ID: 7C7E12DE0238D700
2 changed files with 21 additions and 3 deletions

View file

@ -72,6 +72,7 @@ struct NixRepl
const static int envSize = 32768; const static int envSize = 32768;
std::shared_ptr<StaticEnv> staticEnv; std::shared_ptr<StaticEnv> staticEnv;
Value lastLoaded;
Env * env; Env * env;
int displ; int displ;
StringSet varNames; StringSet varNames;
@ -96,6 +97,7 @@ struct NixRepl
void loadFile(const Path & path); void loadFile(const Path & path);
void loadFlake(const std::string & flakeRef); void loadFlake(const std::string & flakeRef);
void loadFiles(); void loadFiles();
void showLastLoaded();
void reloadFiles(); void reloadFiles();
void addAttrsToScope(Value & attrs); void addAttrsToScope(Value & attrs);
void addVarToScope(const Symbol name, Value & v); void addVarToScope(const Symbol name, Value & v);
@ -470,6 +472,10 @@ ProcessLineResult NixRepl::processLine(std::string line)
loadFlake(arg); loadFlake(arg);
} }
else if (command == ":ll" || command == ":last-flake") {
showLastLoaded();
}
else if (command == ":r" || command == ":reload") { else if (command == ":r" || command == ":reload") {
state->resetFileCache(); state->resetFileCache();
reloadFiles(); reloadFiles();
@ -750,6 +756,16 @@ void NixRepl::initEnv()
varNames.emplace(state->symbols[i.first]); 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() void NixRepl::reloadFiles()
{ {
@ -791,12 +807,14 @@ void NixRepl::addAttrsToScope(Value & attrs)
staticEnv->deduplicate(); staticEnv->deduplicate();
notice("Added %1% variables.", attrs.attrs()->size()); notice("Added %1% variables.", attrs.attrs()->size());
lastLoaded = attrs;
const int max_print = 10; const int max_print = 10;
int count = 0; int count = 0;
for (auto & i : *attrs.attrs()) { for (auto & i : *attrs.attrs()) {
count += 1; count += 1;
if (count > max_print) { 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; break;
} }

View file

@ -193,7 +193,7 @@ attributes = builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x
- 7 - 7
- 8 - 8
- 9 - 9
... And 3 other variables ... And 3 other variables, use :ll to see these
' '
# Test the `:reload` mechansim with flakes: # Test the `:reload` mechansim with flakes:
@ -346,7 +346,7 @@ runRepl () {
-e "s@$nixVersion@<nix version>@g" \ -e "s@$nixVersion@<nix version>@g" \
-e "s@Added [0-9]* variables@Added <number omitted> variables@g" \ -e "s@Added [0-9]* variables@Added <number omitted> variables@g" \
-e '/^- /d' \ -e '/^- /d' \
-e '/\.\.\. And [0-9]* other variables/d' \ -e '/\.\.\. And [0-9]* other variables, use :ll to see these/d' \
| grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \ | grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \
; ;
} }