Fix support for fish shell (#421)

This commit is contained in:
Denis Isidoro 2020-10-08 17:45:01 -03:00 committed by GitHub
parent ba2efec723
commit de06161baf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 31 deletions

2
Cargo.lock generated
View file

@ -196,7 +196,7 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "navi"
version = "2.12.0"
version = "2.12.1"
dependencies = [
"anyhow",
"directories-next",

View file

@ -1,6 +1,6 @@
[package]
name = "navi"
version = "2.12.0"
version = "2.12.1"
authors = ["Denis Isidoro <denis_isidoro@live.com>"]
edition = "2018"
description = "An interactive cheatsheet tool for the command-line"

View file

@ -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<FinderOpts, Error> {
} 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 <<NAVIEOF
r#"{prefix}navi preview-var "$(cat <<NAVIEOF
{{}}
NAVIEOF
)" "$(cat <<NAVIEOF
{{q}}
NAVIEOF
)" "{}"{}"#,
variable_name,
extra_preview.clone().unwrap_or_default()
)" "{name}"; {extra}{suffix}"#,
prefix = if *IS_FISH { "bash -c '" } else { "" },
suffix = if *IS_FISH { "'" } else { "" },
name = variable_name,
extra = extra_preview.clone().unwrap_or_default()
)),
..opts.clone().unwrap_or_default()
};
@ -136,7 +138,7 @@ NAVIEOF
fn unique_result_count(results: &[&str]) -> usize {
let mut vars = results.to_owned();
vars.sort();
vars.sort_unstable();
vars.dedup();
vars.len()
}

View file

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

View file

@ -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<Option<VariableMap>, Error>;
fn fetch(&self, stdin: &mut std::process::ChildStdin, writer: &mut dyn Writer) -> Result<Option<VariableMap>, Error>;
}

View file

@ -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<Item = Result<String, Error>>,
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('$') {

View file

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

View file

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

View file

@ -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<String, Error> {
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)
}

View file

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