git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #155158 from ZenithalHourlyRate/command-not-found-multiple-choice-interactive

command-not-found: make NIX_AUTO_RUN work when multiple choices

commitab98adb9a698b47134645577608801581067ab9b authorBernardo Meurer <bernardo@meurer.org> dateTue 8 Feb 2022, 15:29 (5 years ago) parent91094c9d046fcf
1 file changed+25−4
nixos/modules/programs/command-not-found/command-not-found.pl+25−4
nixos/modules/programs/command-not-found/command-not-found.pl+25−4
@@ -21,9 +21,11 @@ my $res = $dbh->selectall_arrayref(
2121 "select package from Programs where system = ? and name = ?",
2222 { Slice => {} }, $system, $program);
2323
24−if (!defined $res || scalar @$res == 0) {
24+my $len = !defined $res ? 0 : scalar @$res;
25+
26+if ($len == 0) {
2527 print STDERR "$program: command not found\n";
26−} elsif (scalar @$res == 1) {
28+} elsif ($len == 1) {
2729 my $package = @$res[0]->{package};
2830 if ($ENV{"NIX_AUTO_RUN"} // "") {
2931 exec("nix-shell", "-p", $package, "--run", shell_quote("exec", @ARGV));
@@ -35,11 +37,30 @@ ephemeral shell by typing:
3537EOF
3638 }
3739} else {
38− print STDERR <<EOF;
40+ if ($ENV{"NIX_AUTO_RUN"} // "") {
41+ print STDERR "Select a package that provides '$program':\n";
42+ for my $i (0 .. $len - 1) {
43+ print STDERR " [", $i + 1, "]: @$res[$i]->{package}\n";
44+ }
45+ my $choice = 0;
46+ while (1) { # exec will break this loop
47+ no warnings "numeric";
48+ print STDERR "Your choice [1-${len}]: ";
49+ # 0 can be invalid user input like non-number string
50+ # so we start from 1
51+ $choice = <STDIN> + 0;
52+ if (1 <= $choice && $choice <= $len) {
53+ exec("nix-shell", "-p", @$res[$choice - 1]->{package},
54+ "--run", shell_quote("exec", @ARGV));
55+ }
56+ }
57+ } else {
58+ print STDERR <<EOF;
3959The program '$program' is not in your PATH. It is provided by several packages.
4060You can make it available in an ephemeral shell by typing one of the following:
4161EOF
42− print STDERR " nix-shell -p $_->{package}\n" foreach @$res;
62+ print STDERR " nix-shell -p $_->{package}\n" foreach @$res;
63+ }
4364}
4465
4566exit 127;