Add navi fn map::expand

This commit is contained in:
Denis Isidoro 2021-04-15 09:07:01 -03:00 committed by GitHub
parent 1e6dfc1ec9
commit 5acdb2a4b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 12 deletions

View file

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

View file

@ -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)?;

View file

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

View file

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

View file

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