diff options
author | Denis Isidoro <denisidoro@users.noreply.github.com> | 2021-04-15 09:07:01 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-15 09:07:01 -0300 |
commit | 5acdb2a4b21f2cb8b286301d0c052f22f3d318ed (patch) | |
tree | afb611b04948df56ecfe6ee044ca0ab9aa05a3b3 | |
parent | 1e6dfc1ec931ca87a46e83bcc0dce9a051cefab1 (diff) |
Add navi fn map::expand
-rw-r--r-- | docs/cheatsheet_syntax.md | 2 | ||||
-rw-r--r-- | src/cmds/func.rs | 15 | ||||
-rw-r--r-- | src/structures/config.rs | 49 | ||||
-rw-r--r-- | src/writer/terminal.rs | 8 | ||||
-rw-r--r-- | tests/cheats/more_cases.cheat | 4 |
5 files changed, 66 insertions, 12 deletions
diff --git a/docs/cheatsheet_syntax.md b/docs/cheatsheet_syntax.md index 829e0f0..0bc3ce0 100644 --- a/docs/cheatsheet_syntax.md +++ b/docs/cheatsheet_syntax.md @@ -137,5 +137,5 @@ true \ # This will result into: cat "file1.json" "file2.json" cat <jsons> -$ jsons: find . -iname '*.json' -type f -print --- --multi --map "sed -e 's/^.*$/\"&\"/' | tr '\n' ' ' +$ jsons: find . -iname '*.json' -type f -print --- --multi --map "navi fn map::expand" ```
\ No newline at end of file diff --git a/src/cmds/func.rs b/src/cmds/func.rs index 92a8eb7..fad38db 100644 --- a/src/cmds/func.rs +++ b/src/cmds/func.rs @@ -1,14 +1,17 @@ use crate::handler; +use crate::shell::BashSpawnError; use crate::structures::config; use crate::url; use anyhow::Error; use std::io::{self, Read}; +use std::process::Command; #[derive(Debug)] pub enum Func { UrlOpen, Welcome, WidgetLastCommand, + MapExpand, } pub fn main(func: &Func, args: Vec<String>) -> Result<(), Error> { @@ -18,9 +21,21 @@ pub fn main(func: &Func, args: Vec<String>) -> Result<(), Error> { "navi --path /tmp/navi/irrelevant".split(' ').collect(), )), Func::WidgetLastCommand => widget_last_command(), + Func::MapExpand => map_expand(), } } +fn map_expand() -> Result<(), Error> { + let cmd = r#"sed -e 's/^.*$/"&"/' | tr '\n' ' '"#; + Command::new("bash") + .arg("-c") + .arg(cmd) + .spawn() + .map_err(|e| BashSpawnError::new(cmd, e))? + .wait()?; + Ok(()) +} + fn widget_last_command() -> Result<(), Error> { let mut text = String::new(); io::stdin().read_to_string(&mut text)?; diff --git a/src/structures/config.rs b/src/structures/config.rs index 7ddd40a..44524e3 100644 --- a/src/structures/config.rs +++ b/src/structures/config.rs @@ -6,6 +6,11 @@ use crate::shell::Shell; use clap::{crate_version, AppSettings, Clap}; use std::str::FromStr; +const FINDER_POSSIBLE_VALUES: &[&str] = &[&"fzf", &"skim"]; +const SHELL_POSSIBLE_VALUES: &[&str] = &[&"bash", &"zsh", &"fish"]; +const FUNC_POSSIBLE_VALUES: &[&str] = &[&"url::open", &"welcome", &"widget::last_command", &"map::expand"]; +const INFO_POSSIBLE_VALUES: &[&str] = &[&"cheats-path"]; + impl FromStr for FinderChoice { type Err = &'static str; @@ -39,6 +44,7 @@ impl FromStr for Func { "url::open" => Ok(Func::UrlOpen), "welcome" => Ok(Func::Welcome), "widget::last_command" => Ok(Func::WidgetLastCommand), + "map::expand" => Ok(Func::MapExpand), _ => Err("no match"), } } @@ -122,7 +128,7 @@ pub struct Config { pub fzf_overrides_var: Option<String>, /// which finder application to use - #[clap(long, env = env_var::FINDER, default_value = "fzf", possible_values = &["fzf", "skim"], case_insensitive = true)] + #[clap(long, env = env_var::FINDER, default_value = "fzf", possible_values = FINDER_POSSIBLE_VALUES, case_insensitive = true)] pub finder: FinderChoice, #[clap(subcommand)] @@ -145,10 +151,10 @@ pub enum Command { /// List of arguments (example: "mybranch" "remote") args: Vec<String>, }, - /// Performs ad-hoc functions provided by navi + /// [Experimental] Performs ad-hoc, internal functions provided by navi Fn { /// Function name (example: "url::open") - #[clap(possible_values = &["welcome", "url::open", "widget::last_command"], case_insensitive = true)] + #[clap(possible_values = FUNC_POSSIBLE_VALUES, case_insensitive = true)] func: Func, /// List of arguments (example: "https://google.com") args: Vec<String>, @@ -176,12 +182,12 @@ pub enum Command { }, /// Outputs shell widget source code Widget { - #[clap(possible_values = &["bash", "zsh", "fish"], case_insensitive = true, default_value = "bash")] + #[clap(possible_values = SHELL_POSSIBLE_VALUES, case_insensitive = true, default_value = "bash")] shell: Shell, }, /// Shows info Info { - #[clap(possible_values = &["cheats-path"], case_insensitive = true)] + #[clap(possible_values = INFO_POSSIBLE_VALUES, case_insensitive = true)] info: Info, }, /// Helper command for Alfred integration @@ -272,3 +278,36 @@ pub fn config_from_env() -> Config { pub fn config_from_iter(args: Vec<&str>) -> Config { Config::parse_from(args) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_shell_possible_values() { + for v in SHELL_POSSIBLE_VALUES { + assert_eq!(true, Shell::from_str(v).is_ok()) + } + } + + #[test] + fn test_info_possible_values() { + for v in INFO_POSSIBLE_VALUES { + assert_eq!(true, Info::from_str(v).is_ok()) + } + } + + #[test] + fn test_func_possible_values() { + for v in FUNC_POSSIBLE_VALUES { + assert_eq!(true, Func::from_str(v).is_ok()) + } + } + + #[test] + fn test_finder_possible_values() { + for v in FINDER_POSSIBLE_VALUES { + assert_eq!(true, FinderChoice::from_str(v).is_ok()) + } + } +} diff --git a/src/writer/terminal.rs b/src/writer/terminal.rs index f5509b9..aa01555 100644 --- a/src/writer/terminal.rs +++ b/src/writer/terminal.rs @@ -22,8 +22,8 @@ lazy_static! { pub static ref TAG_COLOR: Color = parse_ansi(env_var::TAG_COLOR, Color::Cyan); pub static ref COMMENT_COLOR: Color = parse_ansi(env_var::COMMENT_COLOR, Color::Blue); pub static ref SNIPPET_COLOR: Color = parse_ansi(env_var::SNIPPET_COLOR, Color::White); - pub static ref TAG_WIDTH_PERCENTAGE: u16 = env_var::parse(env_var::TAG_WIDTH).unwrap_or(20); - pub static ref COMMENT_WIDTH_PERCENTAGE: u16 = env_var::parse(env_var::COMMENT_WIDTH).unwrap_or(40); + pub static ref TAG_WIDTH_PERCENTAGE: u16 = env_var::parse(env_var::TAG_WIDTH).unwrap_or(27); + pub static ref COMMENT_WIDTH_PERCENTAGE: u16 = env_var::parse(env_var::COMMENT_WIDTH).unwrap_or(43); } pub fn preview(comment: &str, tags: &str, snippet: &str) { @@ -123,8 +123,8 @@ fn limit_str(text: &str, length: usize) -> String { fn get_widths() -> (usize, usize) { let width = terminal::width(); - let tag_width = max(4, width * *TAG_WIDTH_PERCENTAGE / 100); - let comment_width = max(4, width * *COMMENT_WIDTH_PERCENTAGE / 100); + let tag_width = max(20, width * *TAG_WIDTH_PERCENTAGE / 100); + let comment_width = max(60, width * *COMMENT_WIDTH_PERCENTAGE / 100); (usize::from(tag_width), usize::from(comment_width)) } diff --git a/tests/cheats/more_cases.cheat b/tests/cheats/more_cases.cheat index d9f542a..762a6d0 100644 --- a/tests/cheats/more_cases.cheat +++ b/tests/cheats/more_cases.cheat @@ -54,7 +54,7 @@ echo description blank echo description one character # map can be used to expand into multiple arguments -echo <phrases> +for l in <phrases>; do echo "line: $l"; done # Concatenate pdf files files=($(echo "<files>")) @@ -72,7 +72,7 @@ $ mapped: echo 'true false' | tr ' ' '\n' --- --map "grep -q t && echo 1 || echo $ examples: echo -e 'foo bar\nlorem ipsum\ndolor sit' --- --multi $ multiword: echo -e 'foo bar\nlorem ipsum\ndolor sit\nbaz'i $ file: ls . --- --preview 'cat {}' --preview-window 'right:50%' -$ phrases: echo -e "foo bar\nlorem ipsum\ndolor sit" --- --multi --map "sed -e 's/^.*$/\"&\"/' | tr '\n' ' '" +$ phrases: echo -e "foo bar\nlorem ipsum\ndolor sit" --- --multi --map "navi fn map::expand" # this should be displayed echo hi |