Skip to content

Commit

Permalink
Merge pull request #51 from MrDogeBro/task/list-quiet
Browse files Browse the repository at this point in the history
Implement -q flag for quicknav list
  • Loading branch information
MrDogeBro committed May 30, 2022
2 parents a982edd + 473e024 commit 92003f6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ quicknav init fish | source

## Docs

For more info on quicknav such as cofiguration, [head over to our docs](https://quicknav.readthedocs.io/) where you can find all of the
For more info on quicknav such as configuration, [head over to our docs](https://quicknav.readthedocs.io/) where you can find all of the
information you might need.

## License
Expand Down
78 changes: 46 additions & 32 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,60 @@ use prettytable::{format, Table};

use crate::config;

pub fn list(shortcut: Option<String>) -> Result<i32> {
if let Some(shortcut) = shortcut {
let config: config::Config = config::Config::load()?;

let mut shortcut_list = Table::new();
shortcut_list.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
shortcut_list.set_titles(row!["Name", "Shortcuts", "Shortcut Location"]);

fn get_relevant_shortcuts(
shortcut: Option<String>,
config: config::Config,
) -> Result<Vec<config::Shortcut>> {
if let Some(shortcut_name) = shortcut {
for shortcut_conf in config.shortcuts {
if shortcut_conf.name.to_lowercase() == shortcut.to_lowercase() {
let calls: String = shortcut_conf.calls.join(", ");
shortcut_list.add_row(row![shortcut_conf.name, calls, shortcut_conf.location]);
shortcut_list.printstd();
return Ok(0);
if shortcut_conf.name.to_lowercase() == shortcut_name.to_lowercase() {
return Ok(vec![shortcut_conf]);
}
}

Err(anyhow!(format!(
return Err(anyhow!(format!(
"Shortcut with name {} was not found",
shortcut
)))
} else {
let config: config::Config = config::Config::load()?;
shortcut_name
)));
}

if config.shortcuts.is_empty() {
return Err(anyhow!(
"You haven't set up any shortcuts yet, get started with quicknav add."
));
}
Ok(config.shortcuts)
}

let mut shortcut_list = Table::new();
shortcut_list.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
shortcut_list.set_titles(row!["Name", "Shortcuts", "Shortcut Location"]);
fn display_in_table(shortcut: Option<String>, config: config::Config) -> Result<i32> {
let mut shortcut_list = Table::new();
shortcut_list.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
shortcut_list.set_titles(row!["Name", "Shortcuts", "Shortcut Location"]);

for shortcut_conf in config.shortcuts {
let calls: String = shortcut_conf.calls.join(", ");
shortcut_list.add_row(row![shortcut_conf.name, calls, shortcut_conf.location]);
}
for shortcut_conf in get_relevant_shortcuts(shortcut, config)? {
let calls: String = shortcut_conf.calls.join(", ");
shortcut_list.add_row(row![shortcut_conf.name, calls, shortcut_conf.location]);
}

shortcut_list.printstd();
Ok(0)
shortcut_list.printstd();
Ok(0)
}

fn display_quiet(shortcut: Option<String>, config: config::Config) -> Result<i32> {
for shortcut_conf in get_relevant_shortcuts(shortcut, config)? {
println!("{}", shortcut_conf.location);
}

Ok(0)
}

pub fn list(shortcut: Option<String>, quiet: bool) -> Result<i32> {
let config: config::Config = config::Config::load()?;

if config.shortcuts.is_empty() {
return Err(anyhow!(
"You haven't set up any shortcuts yet, get started with quicknav add."
));
}

if quiet {
display_quiet(shortcut, config)
} else {
display_in_table(shortcut, config)
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn run() -> Result<i32> {
match Quicknav::from_args_safe() {
Ok(cmd) => match cmd {
Quicknav::Get { location, search } => commands::get(location, search),
Quicknav::List { shortcut } => commands::list(shortcut),
Quicknav::List { shortcut, quiet } => commands::list(shortcut, quiet),
Quicknav::Add {
call,
location,
Expand Down
3 changes: 3 additions & 0 deletions src/quicknav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub enum Quicknav {
List {
/// The shortcut to search for (by name)
shortcut: Option<String>,
/// Display only the shortcut path
#[structopt(short = "q", long = "quiet")]
quiet: bool,
},
/// Adds a new shortcut
#[structopt(alias = "new")]
Expand Down

0 comments on commit 92003f6

Please sign in to comment.