Skip to content

Commit

Permalink
Add navi fn map::expand
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 15, 2021
1 parent 1e6dfc1 commit 5acdb2a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/cheatsheet_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
15 changes: 15 additions & 0 deletions src/cmds/func.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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)?;
Expand Down
49 changes: 44 additions & 5 deletions src/structures/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"),
}
}
Expand Down Expand Up @@ -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)]
Expand All @@ -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>,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
}
}
8 changes: 4 additions & 4 deletions src/writer/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}

Expand Down
4 changes: 2 additions & 2 deletions tests/cheats/more_cases.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -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>"))
Expand All @@ -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
Expand Down

0 comments on commit 5acdb2a

Please sign in to comment.