From dc4fe98f5611f40af9f232aaae4b3cfe6058bef5 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Thu, 5 Mar 2020 17:40:14 -0300 Subject: [PATCH] Fix best command (#210) --- src/cmds/best.rs | 2 +- src/cmds/core.rs | 12 ++++++++---- src/cmds/query.rs | 2 +- src/main.rs | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cmds/best.rs b/src/cmds/best.rs index bec3dd7..ef449d4 100644 --- a/src/cmds/best.rs +++ b/src/cmds/best.rs @@ -8,6 +8,6 @@ pub fn main(query: String, args: Vec, config: Config) -> Result<(), Box< if !args.is_empty() { cmds::aux::abort("passing arguments to 'navi best'", 201) } else { - cmds::core::main(Variant::Filter(query), config) + cmds::core::main(Variant::Filter(query), config, false) } } diff --git a/src/cmds/core.rs b/src/cmds/core.rs index 3922dba..998027d 100644 --- a/src/cmds/core.rs +++ b/src/cmds/core.rs @@ -36,9 +36,13 @@ fn gen_core_fzf_opts(variant: Variant, config: &Config) -> fzf::Opts { opts } -fn extract_from_selections(raw_output: &str) -> (&str, &str, &str) { +fn extract_from_selections(raw_output: &str, contains_key: bool) -> (&str, &str, &str) { let mut lines = raw_output.split('\n'); - let key = lines.next().unwrap(); + let key = if contains_key { + lines.next().unwrap() + } else { + "enter" + }; let mut parts = lines.next().unwrap().split('\t'); parts.next(); parts.next(); @@ -127,7 +131,7 @@ fn replace_variables_from_snippet( interpolated_snippet } -pub fn main(variant: Variant, config: Config) -> Result<(), Box> { +pub fn main(variant: Variant, config: Config, contains_key: bool) -> Result<(), Box> { let (output, variables) = fzf::call(gen_core_fzf_opts(variant, &config), |stdin| { Some(cheat::read_all(&config, stdin)) }); @@ -135,7 +139,7 @@ pub fn main(variant: Variant, config: Config) -> Result<(), Box> { match output.status.code() { Some(0) => { let raw_output = String::from_utf8(output.stdout)?; - let (key, tags, snippet) = extract_from_selections(&raw_output[..]); + let (key, tags, snippet) = extract_from_selections(&raw_output[..], contains_key); let interpolated_snippet = replace_variables_from_snippet(snippet, tags, variables.unwrap(), &config); diff --git a/src/cmds/query.rs b/src/cmds/query.rs index 12a6571..627d67b 100644 --- a/src/cmds/query.rs +++ b/src/cmds/query.rs @@ -5,5 +5,5 @@ use crate::cmds::core::Variant; use crate::option::Config; pub fn main(query: String, config: Config) -> Result<(), Box> { - cmds::core::main(Variant::Query(query), config) + cmds::core::main(Variant::Query(query), config, true) } diff --git a/src/main.rs b/src/main.rs index 036c0e9..7e9fe5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ fn main() -> Result<(), Box> { _ => { let mut config = option::parse(); match config.cmd.as_mut() { - None => cmds::core::main(Variant::Core, config), + None => cmds::core::main(Variant::Core, config, true), Some(c) => match c { Command::Query { query } => cmds::query::main(query.clone(), config), Command::Best { query, args } => {