Add navi info cheats-path command (#409)

This commit is contained in:
Denis Isidoro 2020-09-15 15:30:08 -03:00 committed by GitHub
parent 3067fe96cb
commit 2868d178ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 19 deletions

View file

@ -3,12 +3,15 @@ use crate::handler;
use crate::structures::config;
use anyhow::Error;
pub fn main(func: String, args: Vec<String>) -> Result<(), Error> {
match func.as_str() {
"url::open" => url::open(args),
#[derive(Debug)]
pub enum Func {
UrlOpen,
Welcome,
}
"welcome" => handler::handle_config(config::config_from_iter("navi --path /tmp/navi/irrelevant".split(' ').collect())),
_ => Err(anyhow!("Unrecognized function")),
pub fn main(func: &Func, args: Vec<String>) -> Result<(), Error> {
match func {
Func::UrlOpen => url::open(args),
Func::Welcome => handler::handle_config(config::config_from_iter("navi --path /tmp/navi/irrelevant".split(' ').collect())),
}
}

15
src/cmds/info.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::common::filesystem::pathbuf_to_string;
use crate::filesystem::default_cheat_pathbuf;
use anyhow::Error;
#[derive(Debug)]
pub enum Info {
CheatsPath,
}
pub fn main(info: &Info) -> Result<(), Error> {
match info {
Info::CheatsPath => println!("{}", pathbuf_to_string(default_cheat_pathbuf()?)?),
}
Ok(())
}

View file

@ -1,6 +1,7 @@
pub mod alfred;
pub mod core;
pub mod func;
pub mod info;
pub mod preview;
pub mod repo;
pub mod shell;

View file

@ -1,10 +1,11 @@
use crate::common::shell::Shell;
use anyhow::Error;
pub fn main(shell: &str) -> Result<(), Error> {
pub fn main(shell: &Shell) -> Result<(), Error> {
let content = match shell {
"zsh" => include_str!("../../shell/navi.plugin.zsh"),
"fish" => include_str!("../../shell/navi.plugin.fish"),
_ => include_str!("../../shell/navi.plugin.bash"),
Shell::Bash => include_str!("../../shell/navi.plugin.bash"),
Shell::Zsh => include_str!("../../shell/navi.plugin.zsh"),
Shell::Fish => include_str!("../../shell/navi.plugin.fish"),
};
println!("{}", content);

View file

@ -1,6 +1,13 @@
use std::fmt::Debug;
use thiserror::Error;
#[derive(Debug)]
pub enum Shell {
Bash,
Zsh,
Fish,
}
#[derive(Error, Debug)]
#[error("Failed to spawn child process `bash` to execute `{command}`")]
pub struct BashSpawnError {

View file

@ -1,5 +1,5 @@
use crate::cmds;
use crate::structures::config::Command::{Alfred, Fn, Preview, PreviewVar, Repo, Widget};
use crate::structures::config::Command::{Alfred, Fn, Info, Preview, PreviewVar, Repo, Widget};
use crate::structures::config::{AlfredCommand, Config, RepoCommand};
use anyhow::Context;
use anyhow::Error;
@ -13,9 +13,11 @@ pub fn handle_config(config: Config) -> Result<(), Error> {
PreviewVar { selection, query, variable } => cmds::preview::main_var(&selection, &query, &variable),
Widget { shell } => cmds::shell::main(&shell).context("Failed to print shell widget code"),
Widget { shell } => cmds::shell::main(shell).context("Failed to print shell widget code"),
Fn { func, args } => cmds::func::main(func.clone(), args.to_vec()).with_context(|| format!("Failed to execute function `{}`", func)),
Fn { func, args } => cmds::func::main(func, args.to_vec()).with_context(|| format!("Failed to execute function `{:#?}`", func)),
Info { info } => cmds::info::main(info).with_context(|| format!("Failed to fetch info `{:#?}`", info)),
Repo { cmd } => match cmd {
RepoCommand::Add { uri } => {

View file

@ -1,3 +1,6 @@
use crate::cmds::func::Func;
use crate::cmds::info::Info;
use crate::common::shell::Shell;
use crate::env_vars;
use crate::finder::FinderChoice;
use anyhow::Error;
@ -6,13 +9,36 @@ use structopt::{clap::AppSettings, StructOpt};
static mut NOTIFIED_DEPRECATION: bool = false;
fn parse_finder(src: &str) -> Result<FinderChoice, Error> {
match src {
match src.to_lowercase().as_str() {
"fzf" => Ok(FinderChoice::Fzf),
"skim" => Ok(FinderChoice::Skim),
_ => Err(Error::msg(format!("unknown finder '{}'", src))),
}
}
fn parse_shell(src: &str) -> Result<Shell, Error> {
match src.to_lowercase().as_str() {
"bash" => Ok(Shell::Bash),
"zsh" => Ok(Shell::Zsh),
"fish" => Ok(Shell::Fish),
_ => Err(Error::msg(format!("unknown shell '{}'", src))),
}
}
fn parse_func(src: &str) -> Result<Func, Error> {
match src.to_lowercase().as_str() {
"url::open" => Ok(Func::UrlOpen),
"welcome" => Ok(Func::Welcome),
_ => Err(Error::msg(format!("unknown shell '{}'", src))),
}
}
fn parse_info(src: &str) -> Result<Info, Error> {
match src.to_lowercase().as_str() {
"cheats-path" => Ok(Info::CheatsPath),
_ => Err(Error::msg(format!("unknown info '{}'", src))),
}
}
#[derive(Debug, StructOpt)]
#[structopt(after_help = r#"MORE INFO:
Please refer to https://github.com/denisidoro/navi
@ -108,7 +134,8 @@ pub enum Command {
/// Performs ad-hoc functions provided by navi
Fn {
/// Function name (example: "url::open")
func: String,
#[structopt(parse(try_from_str = parse_func))]
func: Func,
/// List of arguments (example: "https://google.com")
args: Vec<String>,
},
@ -117,13 +144,13 @@ pub enum Command {
#[structopt(subcommand)]
cmd: RepoCommand,
},
/// Used for fzf's preview window
/// Used for fzf's preview window when selecting snippets
#[structopt(setting = AppSettings::Hidden)]
Preview {
/// Selection line
line: String,
},
/// Used for fzf's preview window
/// Used for fzf's preview window when selecting variable suggestions
#[structopt(setting = AppSettings::Hidden)]
PreviewVar {
/// Selection line
@ -135,8 +162,13 @@ pub enum Command {
},
/// Shows the path for shell widget files
Widget {
/// bash, zsh or fish
shell: String,
#[structopt(default_value = "bash", parse(try_from_str = parse_shell))]
shell: Shell,
},
/// Shows info
Info {
#[structopt(parse(try_from_str = parse_info))]
info: Info,
},
/// Helper command for Alfred integration
#[structopt(setting = AppSettings::Hidden)]