From 3dbbb2ed70aa7c1b0ae261fda30ff46d3b9e4ef9 Mon Sep 17 00:00:00 2001 From: Qeole Date: Thu, 14 Dec 2023 00:48:40 +0000 Subject: [PATCH] Pass justfile path to default chooser (#1759) --- completions/just.elvish | 2 +- completions/just.fish | 2 +- completions/just.powershell | 2 +- completions/just.zsh | 2 +- src/config.rs | 18 ++++++++++++------ src/subcommand.rs | 2 +- tests/choose.rs | 2 +- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/completions/just.elvish b/completions/just.elvish index 51176476..57ad502e 100644 --- a/completions/just.elvish +++ b/completions/just.elvish @@ -51,7 +51,7 @@ edit:completion:arg-completer[just] = [@words]{ cand -v 'Use verbose output' cand --verbose 'Use verbose output' cand --changelog 'Print changelog' - cand --choose '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`' + cand --choose 'Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`' cand --dump 'Print justfile' cand -e 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`' cand --edit 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`' diff --git a/completions/just.fish b/completions/just.fish index aa126bdd..aa28dbd9 100644 --- a/completions/just.fish +++ b/completions/just.fish @@ -64,7 +64,7 @@ complete -c just -n "__fish_use_subcommand" -s u -l unsorted -d 'Return list and complete -c just -n "__fish_use_subcommand" -l unstable -d 'Enable unstable features' complete -c just -n "__fish_use_subcommand" -s v -l verbose -d 'Use verbose output' complete -c just -n "__fish_use_subcommand" -l changelog -d 'Print changelog' -complete -c just -n "__fish_use_subcommand" -l choose -d '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`' +complete -c just -n "__fish_use_subcommand" -l choose -d 'Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`' complete -c just -n "__fish_use_subcommand" -l dump -d 'Print justfile' complete -c just -n "__fish_use_subcommand" -s e -l edit -d 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`' complete -c just -n "__fish_use_subcommand" -l evaluate -d 'Evaluate and print all variables. If a variable name is given as an argument, only print that variable\'s value.' diff --git a/completions/just.powershell b/completions/just.powershell index d2907e3c..92439dc2 100644 --- a/completions/just.powershell +++ b/completions/just.powershell @@ -56,7 +56,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock { [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'Use verbose output') [CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Use verbose output') [CompletionResult]::new('--changelog', 'changelog', [CompletionResultType]::ParameterName, 'Print changelog') - [CompletionResult]::new('--choose', 'choose', [CompletionResultType]::ParameterName, '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`') + [CompletionResult]::new('--choose', 'choose', [CompletionResultType]::ParameterName, 'Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`') [CompletionResult]::new('--dump', 'dump', [CompletionResultType]::ParameterName, 'Print justfile') [CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`') [CompletionResult]::new('--edit', 'edit', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`') diff --git a/completions/just.zsh b/completions/just.zsh index b2ee3d65..46f35569 100644 --- a/completions/just.zsh +++ b/completions/just.zsh @@ -52,7 +52,7 @@ _just() { '*-v[Use verbose output]' \ '*--verbose[Use verbose output]' \ '--changelog[Print changelog]' \ -'--choose[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`]' \ +'--choose[Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`]' \ '--dump[Print justfile]' \ '-e[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \ '--edit[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \ diff --git a/src/config.rs b/src/config.rs index b22e9190..ec611606 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,13 +3,19 @@ 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 {}'"; 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`"; + +pub(crate) const CHOOSE_HELP: &str = "Select one or more recipes to run using a binary chooser. \ + If `--chooser` is not passed the chooser defaults to the \ + value of $JUST_CHOOSER, falling back to `fzf`"; + +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)] diff --git a/src/subcommand.rs b/src/subcommand.rs index 99dacec4..294b0c13 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 940fab0e..892045cf 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)