Skip to content

Commit

Permalink
Fix finder options (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro committed Aug 7, 2021
1 parent 3f3005f commit fe37cd9
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 63 deletions.
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -5,4 +5,7 @@ uninstall:
scripts/make uninstall

fix:
scripts/make fix
scripts/make fix

test:
scripts/test
9 changes: 9 additions & 0 deletions scripts/test
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail

export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
source "${NAVI_HOME}/scripts/install"

export PATH="/usr/local/Cellar/bash/5.1.8/bin/bash:${PATH}"

"${NAVI_HOME}/tests/run"
13 changes: 1 addition & 12 deletions src/actor.rs
Expand Up @@ -65,16 +65,6 @@ fn prompt_finder(
('\n'.to_string(), &None)
};

let overrides = {
let mut o = CONFIG.fzf_overrides_var();
if let Some(io) = initial_opts {
if io.overrides.is_some() {
o = io.overrides.clone()
}
}
o
};

let exe = fs::exe_string();

let subshell_prefix = if CONFIG.shell().contains("fish") { "" } else { "$" };
Expand Down Expand Up @@ -108,9 +98,8 @@ fn prompt_finder(
};

let mut opts = FinderOpts {
overrides,
preview: Some(preview),
..initial_opts.clone().unwrap_or_default()
..initial_opts.clone().unwrap_or_else(FinderOpts::var_default)
};

opts.query = env_var::get(format!("{}__query", variable_name)).ok();
Expand Down
2 changes: 1 addition & 1 deletion src/finder/mod.rs
Expand Up @@ -93,7 +93,7 @@ impl Finder for FinderChoice {
"--exact",
]);

if opts.select1 {
if !opts.prevent_select1 {
if let Self::Fzf = self {
command.arg("--select-1");
}
Expand Down
63 changes: 35 additions & 28 deletions src/finder/structures.rs
@@ -1,6 +1,5 @@
use crate::config::Config;
use crate::config::CONFIG;
use crate::filesystem;
use anyhow::Result;

#[derive(Debug, PartialEq, Clone)]
pub struct Opts {
Expand All @@ -16,7 +15,7 @@ pub struct Opts {
pub delimiter: Option<String>,
pub column: Option<u8>,
pub map: Option<String>,
pub select1: bool,
pub prevent_select1: bool,
}

impl Default for Opts {
Expand All @@ -30,11 +29,42 @@ impl Default for Opts {
header_lines: 0,
header: None,
prompt: None,
suggestion_type: SuggestionType::SingleRecommendation,
suggestion_type: SuggestionType::SingleSelection,
column: None,
delimiter: None,
map: None,
select1: true,
prevent_select1: true,
}
}
}

impl Opts {
pub fn snippet_default() -> Self {
Self {
suggestion_type: SuggestionType::SnippetSelection,
overrides: CONFIG.fzf_overrides(),
preview: Some(format!("{} preview {{}}", filesystem::exe_string())),
prevent_select1: !CONFIG.best_match(),
query: if CONFIG.best_match() {
None
} else {
CONFIG.get_query()
},
filter: if CONFIG.best_match() {
CONFIG.get_query()
} else {
None
},
..Default::default()
}
}

pub fn var_default() -> Self {
Self {
overrides: CONFIG.fzf_overrides_var(),
suggestion_type: SuggestionType::SingleRecommendation,
prevent_select1: false,
..Default::default()
}
}
}
Expand All @@ -52,26 +82,3 @@ pub enum SuggestionType {
/// initial snippet selection
SnippetSelection,
}

impl Opts {
pub fn from_config(config: &Config) -> Result<Opts> {
let opts = Opts {
preview: Some(format!("{} preview {{}}", filesystem::exe_string())),
overrides: config.fzf_overrides(),
suggestion_type: SuggestionType::SnippetSelection,
query: if config.best_match() {
None
} else {
config.get_query()
},
filter: if config.best_match() {
config.get_query()
} else {
None
},
..Default::default()
};

Ok(opts)
}
}
6 changes: 1 addition & 5 deletions src/handler/core.rs
Expand Up @@ -15,11 +15,7 @@ use anyhow::Result;

pub fn main() -> Result<()> {
let config = &CONFIG;
let opts = {
let mut o = FinderOpts::from_config(config)?;
o.select1 = false;
o
};
let opts = FinderOpts::snippet_default();

let (raw_selection, variables, files) = config
.finder()
Expand Down
18 changes: 7 additions & 11 deletions src/parser.rs
Expand Up @@ -15,7 +15,8 @@ lazy_static! {
fn parse_opts(text: &str) -> Result<FinderOpts> {
let mut multi = false;
let mut prevent_extra = false;
let mut opts = FinderOpts::default();

let mut opts = FinderOpts::var_default();

let parts = shellwords::split(text).map_err(|_| anyhow!("Given options are missing a closing quote"))?;

Expand Down Expand Up @@ -240,15 +241,10 @@ mod tests {
parse_variable_line("$ user : echo -e \"$(whoami)\\nroot\" --- --prevent-extra").unwrap();
assert_eq!(command, " echo -e \"$(whoami)\\nroot\" ");
assert_eq!(variable, "user");
assert_eq!(
command_options,
Some(FinderOpts {
header_lines: 0,
column: None,
delimiter: None,
suggestion_type: SuggestionType::SingleSelection,
..Default::default()
})
);
let opts = command_options.unwrap();
assert_eq!(opts.header_lines, 0);
assert_eq!(opts.column, None);
assert_eq!(opts.delimiter, None);
assert_eq!(opts.suggestion_type, SuggestionType::SingleSelection);
}
}
4 changes: 2 additions & 2 deletions src/welcome.rs
@@ -1,7 +1,6 @@
use crate::actor;
use crate::config::CONFIG;
use crate::extractor;
use crate::finder::structures::Opts as FinderOpts;
use crate::finder::Finder;
use crate::structures::cheat::VariableMap;
use crate::structures::item::Item;
Expand All @@ -12,7 +11,8 @@ use std::io::Write;

pub fn main() -> Result<()> {
let config = &CONFIG;
let opts = FinderOpts::from_config(config)?;
let opts = Default::default();

let (raw_selection, variables, files) = config
.finder()
.call(opts, |stdin, _| {
Expand Down
9 changes: 6 additions & 3 deletions tests/run
Expand Up @@ -18,8 +18,8 @@ _navi() {
stty sane || true
local path="${NAVI_TEST_PATH:-$TEST_CHEAT_PATH}"
path="${path//$HOME/~}"
export NAVI_TEST_PATH="$path"
RUST_BACKTRACE=1 NAVI_PATH='$NAVI_TEST_PATH' "$NAVI_EXE" "$@"
export NAVI_ENV_VAR_PATH="$path"
RUST_BACKTRACE=1 NAVI_PATH='$NAVI_ENV_VAR_PATH' "$NAVI_EXE" "$@"
}

_navi_cases() {
Expand Down Expand Up @@ -89,6 +89,7 @@ _integration() {
_kill_tmux
local -r log_file="${NAVI_HOME}/target/ci.log"
local -r cheats_path="$($NAVI_EXE info cheats-path)"
rm -rf "$cheats_path" 2>/dev/null || true
mkdir -p "$cheats_path" 2>/dev/null || true
local -r bak_cheats_path="$(mktemp -d "${cheats_path}_XXXXX")"
rm "$log_file" 2>/dev/null || true
Expand All @@ -105,7 +106,9 @@ _integration() {
_assert_tmux "$log_file"

log::note "Confirming import..."
tmux send-key -t ci "y"; tmux send-key -t ci "Enter"
tmux send-key -t ci "y"
sleep 1
tmux send-key -t ci "Enter"
sleep 6
_assert_tmux "$log_file"

Expand Down

0 comments on commit fe37cd9

Please sign in to comment.