Fix finder options (#590)

This commit is contained in:
Denis Isidoro 2021-08-07 10:31:53 -03:00 committed by GitHub
parent 3f3005f818
commit fe37cd9107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 63 deletions

View file

@ -5,4 +5,7 @@ uninstall:
scripts/make uninstall
fix:
scripts/make fix
scripts/make fix
test:
scripts/test

9
scripts/test Executable file
View file

@ -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"

View file

@ -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();

View file

@ -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");
}

View file

@ -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<String>,
pub column: Option<u8>,
pub map: Option<String>,
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<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)
}
}

View file

@ -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()

View file

@ -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"))?;
@ -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);
}
}

View file

@ -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, _| {

View file

@ -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"