Skip to content

Commit

Permalink
Chooser: Pass justfile path to preview command for default chooser
Browse files Browse the repository at this point in the history
The default chooser is fzf, and we pass it a command to generate the
preview with "just --show" from the recipe name. This has been working
well as long as we rely on the default justfile available in the
directory, given that the preview command can find it too. However,
passing "--chose" alongside "--justfile" to select a specific file
results in the preview command not being able to find the right justfile
to process.

To address this issue, we turn the const string defining the preview
command into a function that takes the internal representation of the
justfile as an argument, and updates the preview command with the right
file.

Fixes: 5f9ac39 ("Use `just --show` in default chooser (casey#1539)")
Suggested-by: Casey Rodarmor <casey@rodarmor.com>
  • Loading branch information
Qeole committed Dec 12, 2023
1 parent e6e1823 commit c648b0d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ use {
clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, ArgSettings},
};

// These three strings should be kept in sync:
pub(crate) const CHOOSER_DEFAULT: &str =
"fzf --multi --preview 'just --unstable --color always --show {}'";
// These two strings, and the two in function chooser_default below should be kept in sync:
pub(crate) const CHOOSER_ENVIRONMENT_KEY: &str = "JUST_CHOOSER";
pub(crate) const CHOOSE_HELP: &str = "Select one or more recipes to run using a binary. If \
`--chooser` is not passed the chooser defaults to the value \
of $JUST_CHOOSER, falling back to `fzf`";

// Return the string for the default chooser. This is a function and not a const, because we want
// to edit the preview command and pass it the right justfile path for the target.
pub(crate) fn chooser_default(justfile: &Justfile) -> OsString {
match justfile.default {
Some(ref default) => {
let mut chooser = OsString::new();
chooser.push("fzf --multi --preview 'just --unstable --color always --justfile ");
chooser.push(default.name.path);
chooser.push(" --show {}'");
chooser
}
None => OsString::from("fzf --multi --preview 'just --unstable --color always --show {}'"),
}
}

#[derive(Debug, PartialEq)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct Config {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Subcommand {
let chooser = chooser
.map(OsString::from)
.or_else(|| env::var_os(config::CHOOSER_ENVIRONMENT_KEY))
.unwrap_or_else(|| OsString::from(config::CHOOSER_DEFAULT));
.unwrap_or_else(|| config::chooser_default(justfile));

let result = justfile
.settings
Expand Down
2 changes: 1 addition & 1 deletion tests/choose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn invoke_error_function() {
",
)
.stderr_regex(
r"error: Chooser `/ -cu fzf --multi --preview 'just --unstable --color always --show \{\}'` invocation failed: .*\n",
r"error: Chooser `/ -cu fzf --multi --preview 'just --unstable --color always --justfile justfile --show \{\}'` invocation failed: .*\n",
)
.status(EXIT_FAILURE)
.shell(false)
Expand Down

0 comments on commit c648b0d

Please sign in to comment.