Skip to content

Commit c5b1be4

Browse files
authored
Merge pull request NixOS#11406 from kstrafe/master
nix repl: Print which variables are just loaded
2 parents 1022598 + 13e3704 commit c5b1be4

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/libcmd/repl.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct NixRepl
6969

7070
const static int envSize = 32768;
7171
std::shared_ptr<StaticEnv> staticEnv;
72+
Value lastLoaded;
7273
Env * env;
7374
int displ;
7475
StringSet varNames;
@@ -95,6 +96,7 @@ struct NixRepl
9596
void loadFiles();
9697
void loadFlakes();
9798
void reloadFilesAndFlakes();
99+
void showLastLoaded();
98100
void addAttrsToScope(Value & attrs);
99101
void addVarToScope(const Symbol name, Value & v);
100102
Expr * parseString(std::string s);
@@ -372,6 +374,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
372374
<< " current profile\n"
373375
<< " :l, :load <path> Load Nix expression and add it to scope\n"
374376
<< " :lf, :load-flake <ref> Load Nix flake and add it to scope\n"
377+
<< " :ll, :last-loaded Show most recently loaded variables added to scope\n"
375378
<< " :p, :print <expr> Evaluate and print expression recursively\n"
376379
<< " Strings are printed directly, without escaping.\n"
377380
<< " :q, :quit Exit nix-repl\n"
@@ -462,6 +465,10 @@ ProcessLineResult NixRepl::processLine(std::string line)
462465
loadFlake(arg);
463466
}
464467

468+
else if (command == ":ll" || command == ":last-loaded") {
469+
showLastLoaded();
470+
}
471+
465472
else if (command == ":r" || command == ":reload") {
466473
state->resetFileCache();
467474
reloadFilesAndFlakes();
@@ -754,6 +761,16 @@ void NixRepl::initEnv()
754761
varNames.emplace(state->symbols[i.first]);
755762
}
756763

764+
void NixRepl::showLastLoaded()
765+
{
766+
RunPager pager;
767+
768+
for (auto & i : *lastLoaded.attrs()) {
769+
std::string_view name = state->symbols[i.name];
770+
logger->cout(name);
771+
}
772+
}
773+
757774

758775
void NixRepl::reloadFilesAndFlakes()
759776
{
@@ -807,6 +824,27 @@ void NixRepl::addAttrsToScope(Value & attrs)
807824
staticEnv->sort();
808825
staticEnv->deduplicate();
809826
notice("Added %1% variables.", attrs.attrs()->size());
827+
828+
lastLoaded = attrs;
829+
830+
const int max_print = 20;
831+
int counter = 0;
832+
std::ostringstream loaded;
833+
for (auto & i : attrs.attrs()->lexicographicOrder(state->symbols)) {
834+
if (counter >= max_print)
835+
break;
836+
837+
if (counter > 0)
838+
loaded << ", ";
839+
840+
printIdentifier(loaded, state->symbols[i->name]);
841+
counter += 1;
842+
}
843+
844+
notice("%1%", loaded.str());
845+
846+
if (attrs.attrs()->size() > max_print)
847+
notice("... and %1% more; view with :ll", attrs.attrs()->size() - max_print);
810848
}
811849

812850

tests/functional/repl.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ foo + baz
157157
' "3" \
158158
./flake ./flake\#bar --experimental-features 'flakes'
159159

160+
testReplResponse $'
161+
:a { a = 1; b = 2; longerName = 3; "with spaces" = 4; }
162+
' 'Added 4 variables.
163+
a, b, longerName, "with spaces"
164+
'
165+
166+
cat <<EOF > attribute-set.nix
167+
{
168+
a = 1;
169+
b = 2;
170+
longerName = 3;
171+
"with spaces" = 4;
172+
}
173+
EOF
174+
testReplResponse '
175+
:l ./attribute-set.nix
176+
' 'Added 4 variables.
177+
a, b, longerName, "with spaces"
178+
'
179+
180+
testReplResponseNoRegex $'
181+
:a builtins.foldl\' (x: y: x // y) {} (map (x: { ${builtins.toString x} = x; }) (builtins.genList (x: x) 23))
182+
' 'Added 23 variables.
183+
"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "21", "22", "3", "4", "5", "6"
184+
... and 3 more; view with :ll'
185+
160186
# Test the `:reload` mechansim with flakes:
161187
# - Eval `./flake#changingThing`
162188
# - Modify the flake
@@ -328,7 +354,8 @@ runRepl () {
328354
-e "s@$testDir@/path/to/tests/functional@g" \
329355
-e "s@$testDirNoUnderscores@/path/to/tests/functional@g" \
330356
-e "s@$nixVersion@<nix version>@g" \
331-
-e "s@Added [0-9]* variables@Added <number omitted> variables@g" \
357+
-e "/Added [0-9]* variables/{s@ [0-9]* @ <number omitted> @;n;d}" \
358+
-e '/\.\.\. and [0-9]* more; view with :ll/d' \
332359
| grep -vF $'warning: you don\'t have Internet access; disabling some network-dependent features' \
333360
;
334361
}

0 commit comments

Comments
 (0)