From e06e12a7c0d14f85a988a221136f9cdecd15da0f Mon Sep 17 00:00:00 2001 From: Qeole Date: Tue, 12 Dec 2023 21:18:23 +0000 Subject: [PATCH] Chooser: Pass justfile path to preview command for default chooser 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: 5f9ac39b038c ("Use `just --show` in default chooser (#1539)") Suggested-by: Casey Rodarmor --- src/config.rs | 14 +++++++++++--- src/subcommand.rs | 2 +- tests/choose.rs | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index b22e9190f6..2bf495b082 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,14 +3,22 @@ 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: &Path) -> OsString { + let mut chooser = OsString::new(); + chooser.push("fzf --multi --preview 'just --unstable --color always --justfile "); + chooser.push(justfile); + chooser.push(" --show {}'"); + chooser +} + #[derive(Debug, PartialEq)] #[allow(clippy::struct_excessive_bools)] pub(crate) struct Config { diff --git a/src/subcommand.rs b/src/subcommand.rs index 99dacec470..294b0c1395 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(&search.justfile)); let result = justfile .settings diff --git a/tests/choose.rs b/tests/choose.rs index 940fab0e61..59bc293211 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)