Allow multi-word shell.command config (#644)

Fixes #645
This commit is contained in:
Denis Isidoro 2021-11-23 09:54:25 -03:00 committed by GitHub
parent 5f8079668c
commit 8725654a74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 47 additions and 10 deletions

View file

@ -29,3 +29,4 @@ finder:
shell:
command: bash # shell used for shell out. possible values: bash, zsh, dash, ...
# finder_command: bash # similar, but for fzf's internals

View file

@ -8,7 +8,6 @@ pub struct EnvConfig {
pub config_yaml: Option<String>,
pub config_path: Option<String>,
pub path: Option<String>,
pub shell: Option<String>,
pub finder: Option<FinderChoice>,
pub fzf_overrides: Option<String>,
pub fzf_overrides_var: Option<String>,
@ -20,7 +19,6 @@ impl EnvConfig {
config_yaml: env_var::get(env_var::CONFIG_YAML).ok(),
config_path: env_var::get(env_var::CONFIG).ok(),
path: env_var::get(env_var::PATH).ok(),
shell: env_var::get(env_var::SHELL).ok(),
finder: env_var::get(env_var::FINDER)
.ok()
.and_then(|x| FinderChoice::from_str(&x).ok()),

View file

@ -93,6 +93,14 @@ impl Config {
self.yaml.shell.command.clone()
}
pub fn finder_shell(&self) -> String {
self.yaml
.shell
.finder_command
.clone()
.unwrap_or_else(|| self.yaml.shell.command.clone())
}
pub fn tag_rules(&self) -> Option<String> {
self.clap
.tag_rules

View file

@ -80,6 +80,7 @@ pub struct Search {
#[serde(default)]
pub struct Shell {
pub command: String,
pub finder_command: Option<String>,
}
#[derive(Deserialize, Default)]
@ -177,6 +178,7 @@ impl Default for Shell {
fn default() -> Self {
Self {
command: "bash".to_string(),
finder_command: None,
}
}
}

View file

@ -16,8 +16,6 @@ pub const FZF_OVERRIDES: &str = "NAVI_FZF_OVERRIDES";
pub const FZF_OVERRIDES_VAR: &str = "NAVI_FZF_OVERRIDES_VAR";
pub const FINDER: &str = "NAVI_FINDER";
pub const SHELL: &str = "NAVI_SHELL";
pub const CONFIG: &str = "NAVI_CONFIG";
pub const CONFIG_YAML: &str = "NAVI_CONFIG_YAML";

View file

@ -160,7 +160,7 @@ impl Finder for FinderChoice {
}
let child = command
.env("SHELL", CONFIG.shell())
.env("SHELL", CONFIG.finder_shell())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn();

View file

@ -36,10 +36,14 @@ impl ShellSpawnError {
}
pub fn out() -> Command {
let shell = CONFIG.shell();
let mut cmd = Command::new(&shell);
let arg = if shell == "cmd.exe" { "/c" } else { "-c" };
cmd.arg(arg);
let words_str = CONFIG.shell();
let mut words_vec = shellwords::split(&words_str).expect("empty shell command");
let mut words = words_vec.iter_mut();
let first_cmd = words.next().expect("absent shell binary");
let mut cmd = Command::new(&first_cmd);
cmd.args(words);
let dash_c = if words_str.contains("cmd.exe") { "/c" } else { "-c" };
cmd.arg(dash_c);
cmd
}

18
tests/config.yaml Normal file
View file

@ -0,0 +1,18 @@
style:
tag:
color: cyan
width_percentage: 26
min_width: 20
comment:
color: yellow
width_percentage: 42
min_width: 45
snippet:
color: white
finder:
command: fzf
shell:
finder_command: bash
command: env BASH_ENV="${NAVI_HOME}/tests/helpers.sh" bash --norc --noprofile

5
tests/helpers.sh Normal file
View file

@ -0,0 +1,5 @@
#!/usr/local/bin/env bash
myhelperfn() {
echo "inside helper: $*"
}

View file

@ -61,6 +61,9 @@ echo "foo" \
# multiline variable -> "foo bar"
echo "<multilinevar>"
# helper -> "inside helper: 42"
myhelperfn 42
$ x: echo '2'
$ x2: echo "$((x+10))"
$ y: echo 'a'

View file

@ -19,7 +19,7 @@ _navi() {
local path="${NAVI_TEST_PATH:-$TEST_CHEAT_PATH}"
path="${path//$HOME/~}"
export NAVI_ENV_VAR_PATH="$path"
RUST_BACKTRACE=1 NAVI_PATH='$NAVI_ENV_VAR_PATH' "$NAVI_EXE" "$@"
RUST_BACKTRACE=1 NAVI_PATH='$NAVI_ENV_VAR_PATH' NAVI_CONFIG="${NAVI_HOME}/tests/config.yaml" "$NAVI_EXE" "$@"
}
_navi_cases() {