Skip to content

Commit

Permalink
Allow multi-word shell.command config (#644)
Browse files Browse the repository at this point in the history
Fixes #645
  • Loading branch information
denisidoro committed Nov 23, 2021
1 parent 5f80796 commit 8725654
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/config_file_example.yaml
Expand Up @@ -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
2 changes: 0 additions & 2 deletions src/config/env.rs
Expand Up @@ -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>,
Expand All @@ -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()),
Expand Down
8 changes: 8 additions & 0 deletions src/config/mod.rs
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/config/yaml.rs
Expand Up @@ -80,6 +80,7 @@ pub struct Search {
#[serde(default)]
pub struct Shell {
pub command: String,
pub finder_command: Option<String>,
}

#[derive(Deserialize, Default)]
Expand Down Expand Up @@ -177,6 +178,7 @@ impl Default for Shell {
fn default() -> Self {
Self {
command: "bash".to_string(),
finder_command: None,
}
}
}
2 changes: 0 additions & 2 deletions src/env_var.rs
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/finder/mod.rs
Expand Up @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions src/shell.rs
Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions tests/config.yaml
@@ -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 changes: 5 additions & 0 deletions tests/helpers.sh
@@ -0,0 +1,5 @@
#!/usr/local/bin/env bash

myhelperfn() {
echo "inside helper: $*"
}
3 changes: 3 additions & 0 deletions tests/no_prompt_cheats/cases.cheat
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion tests/run
Expand Up @@ -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() {
Expand Down

0 comments on commit 8725654

Please sign in to comment.