diff --git a/Cargo.lock b/Cargo.lock index 48d10926..e64897a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,7 +196,7 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" [[package]] name = "navi" -version = "2.12.0" +version = "2.12.1" dependencies = [ "anyhow", "directories-next", diff --git a/Cargo.toml b/Cargo.toml index 7ccc7c73..c55b0100 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "navi" -version = "2.12.0" +version = "2.12.1" authors = ["Denis Isidoro "] edition = "2018" description = "An interactive cheatsheet tool for the command-line" diff --git a/src/cmds/core.rs b/src/cmds/core.rs index d846ee8b..4c6ffeed 100644 --- a/src/cmds/core.rs +++ b/src/cmds/core.rs @@ -1,6 +1,6 @@ use crate::cheatsh; use crate::common::clipboard; -use crate::common::shell::BashSpawnError; +use crate::common::shell::{BashSpawnError, IS_FISH}; use crate::display; use crate::env_vars; use crate::fetcher::Fetcher; @@ -27,7 +27,7 @@ fn gen_core_finder_opts(config: &Config) -> Result { } else { Some(format!("{} preview {{}}", filesystem::exe_string()?)) }, - autoselect: !config.get_no_autoselect(), + autoselect: config.autoselect(), overrides: config.fzf_overrides.clone(), suggestion_type: SuggestionType::SnippetSelection, query: if config.get_best_match() { None } else { config.get_query() }, @@ -95,18 +95,20 @@ fn prompt_finder(variable_name: &str, config: &Config, suggestion: Option<&Sugge }; let mut opts = FinderOpts { - autoselect: !config.get_no_autoselect(), + autoselect: config.autoselect(), overrides: config.fzf_overrides_var.clone(), preview: Some(format!( - r#"navi preview-var "$(cat < usize { let mut vars = results.to_owned(); - vars.sort(); + vars.sort_unstable(); vars.dedup(); vars.len() } diff --git a/src/common/shell.rs b/src/common/shell.rs index 6071ae45..84d34f1f 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -1,6 +1,11 @@ +use std::env; use std::fmt::Debug; use thiserror::Error; +lazy_static! { + pub static ref IS_FISH: bool = env::var("SHELL").unwrap_or_else(|_| "".to_string()).contains(&"fish"); +} + #[derive(Debug)] pub enum Shell { Bash, diff --git a/src/fetcher/mod.rs b/src/fetcher/mod.rs index b07b26ea..36b77f2b 100644 --- a/src/fetcher/mod.rs +++ b/src/fetcher/mod.rs @@ -5,5 +5,5 @@ use crate::structures::cheat::VariableMap; use anyhow::Error; pub trait Fetcher { - fn fetch(self: &Self, stdin: &mut std::process::ChildStdin, writer: &mut dyn Writer) -> Result, Error>; + fn fetch(&self, stdin: &mut std::process::ChildStdin, writer: &mut dyn Writer) -> Result, Error>; } diff --git a/src/parser.rs b/src/parser.rs index bb4e6e16..c0be31fd 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -114,6 +114,14 @@ fn write_cmd(tags: &str, comment: &str, snippet: &str, writer: &mut dyn Writer, } } +fn without_prefix(line: &str) -> String { + if line.len() > 2 { + String::from(line[2..].trim()) + } else { + String::from("") + } +} + pub fn read_lines( lines: impl Iterator>, id: &str, @@ -145,11 +153,11 @@ pub fn read_lines( should_break = true } snippet = String::from(""); - tags = if line.len() > 2 { String::from(&line[2..]) } else { String::from("") }; + tags = without_prefix(&line); } // dependency else if line.starts_with('@') { - let tags_dependency = if line.len() > 2 { String::from(&line[2..]) } else { String::from("") }; + let tags_dependency = without_prefix(&line); variables.insert_dependency(&tags, &tags_dependency); } // metacomment @@ -161,7 +169,7 @@ pub fn read_lines( should_break = true } snippet = String::from(""); - comment = if line.len() > 2 { String::from(&line[2..]) } else { String::from("") }; + comment = without_prefix(&line); } // variable else if line.starts_with('$') { diff --git a/src/structures/cheat.rs b/src/structures/cheat.rs index bbddd015..0d4885b0 100644 --- a/src/structures/cheat.rs +++ b/src/structures/cheat.rs @@ -43,18 +43,25 @@ impl VariableMap { pub fn get_suggestion(&self, tags: &str, variable: &str) -> Option<&Suggestion> { let k = fnv(&tags); - let res = self.variables.get(&k)?.get(variable); - if res.is_some() { - return res; + + if let Some(vm) = self.variables.get(&k) { + let res = vm.get(variable); + if res.is_some() { + return res; + } } + if let Some(dependency_keys) = self.dependencies.get(&k) { for dependency_key in dependency_keys { - let res = self.variables.get(&dependency_key)?.get(variable); - if res.is_some() { - return res; + if let Some(vm) = self.variables.get(&dependency_key) { + let res = vm.get(variable); + if res.is_some() { + return res; + } } } } + None } } diff --git a/src/structures/config.rs b/src/structures/config.rs index a3321523..a6a1ecef 100644 --- a/src/structures/config.rs +++ b/src/structures/config.rs @@ -290,12 +290,12 @@ impl Config { } } - pub fn get_no_autoselect(&self) -> bool { + pub fn autoselect(&self) -> bool { if self.no_autoselect { deprecated("--no-autoselect"); - true - } else { false + } else { + true } } } diff --git a/src/tldr.rs b/src/tldr.rs index 3cd191ff..f5801b50 100644 --- a/src/tldr.rs +++ b/src/tldr.rs @@ -13,6 +13,9 @@ lazy_static! { pub static ref NON_VAR_CHARS_REGEX: Regex = Regex::new(r"[^\da-zA-Z_]").expect("Invalid regex"); } +static VERSION_DISCLAIMER: &str = "The tldr client written in C (the default one in Homebrew) doesn't support markdown files, so navi can't use it. +The client written in Rust is recommended. The one available in npm works, too."; + fn convert_tldr_vars(line: &str) -> String { let caps = VAR_TLDR_REGEX.find_iter(&line); let mut new_line: String = line.to_string(); @@ -87,7 +90,12 @@ pub fn fetch(query: &str) -> Result { eprintln!( "navi was unable to call tldr. Make sure tldr is correctly installed. -Refer to https://github.com/tldr-pages/tldr for more info." +Refer to https://github.com/tldr-pages/tldr for more info. + +Note: +{} +", + VERSION_DISCLAIMER ); process::exit(34) } @@ -107,15 +115,15 @@ Output: Error: {} -Note: -The tldr client written in C (the default one in Homebrew) doesn't support markdown files, so navi can't use it. -Please make sure you're using a version that supports the --markdown flag. -The client written in Rust is recommended. The one available in npm works, too. -If you are already using a supported version you can ignore this message. +Note: +Please make sure you're using a version that supports the --markdown flag. +If you are already using a supported version you can ignore this message. +{} ", args.join(" "), String::from_utf8(out.stdout).unwrap_or_else(|_e| "Unable to get output message".to_string()), - String::from_utf8(out.stderr).unwrap_or_else(|_e| "Unable to get error message".to_string()) + String::from_utf8(out.stderr).unwrap_or_else(|_e| "Unable to get error message".to_string()), + VERSION_DISCLAIMER ); process::exit(35) } diff --git a/tests/run b/tests/run index 5f8a7451..be29f86e 100755 --- a/tests/run +++ b/tests/run @@ -109,7 +109,7 @@ _integration() { echoerr "Running snippet..." tmux send-key -t ci "pwd" - sleep 1 + sleep 1 tmux send-key -t ci "Enter" sleep 2