Skip to content

Commit

Permalink
Fix support for fish shell (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Oct 8, 2020
1 parent ba2efec commit de06161
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "navi"
version = "2.12.0"
version = "2.12.1"
authors = ["Denis Isidoro <[email protected]>"]
edition = "2018"
description = "An interactive cheatsheet tool for the command-line"
Expand Down
18 changes: 10 additions & 8 deletions src/cmds/core.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() },
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -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()
}
Expand Down
5 changes: 5 additions & 0 deletions src/common/shell.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
14 changes: 11 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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('$') {
Expand Down
19 changes: 13 additions & 6 deletions src/structures/cheat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 3 additions & 3 deletions src/structures/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/tldr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de06161

Please sign in to comment.