From fe37cd9107e1b25db34d7d3dea9cc1053563a01f Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Sat, 7 Aug 2021 10:31:53 -0300 Subject: [PATCH] Fix finder options (#590) --- Makefile | 5 +++- scripts/test | 9 ++++++ src/actor.rs | 13 +-------- src/finder/mod.rs | 2 +- src/finder/structures.rs | 63 ++++++++++++++++++++++------------------ src/handler/core.rs | 6 +--- src/parser.rs | 18 +++++------- src/welcome.rs | 4 +-- tests/run | 9 ++++-- 9 files changed, 66 insertions(+), 63 deletions(-) create mode 100755 scripts/test diff --git a/Makefile b/Makefile index a2d68d1c..cbf7b67d 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,7 @@ uninstall: scripts/make uninstall fix: - scripts/make fix \ No newline at end of file + scripts/make fix + +test: + scripts/test \ No newline at end of file diff --git a/scripts/test b/scripts/test new file mode 100755 index 00000000..9583e3fc --- /dev/null +++ b/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" \ No newline at end of file diff --git a/src/actor.rs b/src/actor.rs index cce0626d..f6ebee6f 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -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 { "$" }; @@ -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(); diff --git a/src/finder/mod.rs b/src/finder/mod.rs index 9cac882a..a710a7d7 100644 --- a/src/finder/mod.rs +++ b/src/finder/mod.rs @@ -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"); } diff --git a/src/finder/structures.rs b/src/finder/structures.rs index 2d4cd41f..e2c98db5 100644 --- a/src/finder/structures.rs +++ b/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 { @@ -16,7 +15,7 @@ pub struct Opts { pub delimiter: Option, pub column: Option, pub map: Option, - pub select1: bool, + pub prevent_select1: bool, } impl Default for Opts { @@ -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() } } } @@ -52,26 +82,3 @@ pub enum SuggestionType { /// initial snippet selection SnippetSelection, } - -impl Opts { - pub fn from_config(config: &Config) -> Result { - 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) - } -} diff --git a/src/handler/core.rs b/src/handler/core.rs index bf40361a..a76daeb8 100644 --- a/src/handler/core.rs +++ b/src/handler/core.rs @@ -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() diff --git a/src/parser.rs b/src/parser.rs index 3e2ddc19..c798c308 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -15,7 +15,8 @@ lazy_static! { fn parse_opts(text: &str) -> Result { 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"))?; @@ -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); } } diff --git a/src/welcome.rs b/src/welcome.rs index c4920f89..0bb6f754 100644 --- a/src/welcome.rs +++ b/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; @@ -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, _| { diff --git a/tests/run b/tests/run index 3083ddb4..b8e801b0 100755 --- a/tests/run +++ b/tests/run @@ -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() { @@ -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 @@ -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"