diff --git a/src/config.rs b/src/config.rs index b22e9190f6..343f4ca11d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,14 +3,29 @@ 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 => { + return OsString::from("fzf --multi --preview 'just --unstable --color always --show {}'"); + } + } +} + #[derive(Debug, PartialEq)] #[allow(clippy::struct_excessive_bools)] pub(crate) struct Config { diff --git a/src/subcommand.rs b/src/subcommand.rs index 99dacec470..f5cf3648ed 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -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 diff --git a/tests/choose.rs b/tests/choose.rs index 940fab0e61..9fc7a35625 100644 --- a/tests/choose.rs +++ b/tests/choose.rs @@ -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)