diff --git a/src/bin/q/main.rs b/src/bin/q/main.rs index 237a248..3f83714 100644 --- a/src/bin/q/main.rs +++ b/src/bin/q/main.rs @@ -21,7 +21,7 @@ fn try_main(args:&Args) -> Result<()>{ if args.set{ match &args.target { Some(t) => commands::save_alias(alias, t), - None => Err(anyhow!("Missing target value")) + None => commands::suggest_save_alias(alias) } }else{ if let Some(t) = &args.target{ diff --git a/src/bin/qali/args.rs b/src/bin/qali/args.rs index c1282dc..867291e 100644 --- a/src/bin/qali/args.rs +++ b/src/bin/qali/args.rs @@ -30,5 +30,10 @@ pub enum Commands { command: String }, /// Select and execute an alias - Select + Select, + /// Set an alias by suggestion + Add{ + #[clap(value_parser)] + command: String + } } \ No newline at end of file diff --git a/src/bin/qali/main.rs b/src/bin/qali/main.rs index 6fb961d..cc108d0 100644 --- a/src/bin/qali/main.rs +++ b/src/bin/qali/main.rs @@ -22,6 +22,7 @@ fn try_main(args: &Args) -> Result<()>{ List => db::interface::ls(), Remove {alias} => db::remove_alias(alias), Set {alias, command} => commands::save_alias(alias, command), - Select => commands::select_and_execute_alias() + Select => commands::select_and_execute_alias(), + Add { command } => commands::suggest_save_alias(command) } } \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b238b70..88776c8 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,10 +1,12 @@ use anyhow::Result; use dialoguer::{theme::ColorfulTheme, Input}; +use colored::Colorize; use python::Python; use cmd::Cmd; use shell::Shell; use uri::Uri; +use crate::suggest; use crate::db; @@ -40,6 +42,29 @@ pub fn save_alias(alias: &String, command: &String) -> Result<()> { db::save(alias, &store) } +pub fn suggest_save_alias(command: &String) -> Result<()>{ + let alias: String = { + let suggestion = suggest::suggest_alias(command); + match suggestion{ + Ok(s) => { + println!("{}", "Suggestion found".blue()); + Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Enter alias:") + .default(s) + .interact()? + }, + Err(_) => { + Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Enter alias:") + .interact()? + } + } + }; + let action = parse_command(command)?; + let store = action.export()?; + db::save(&alias, &store) +} + pub fn execute_alias(alias: &String, args: Option<&String>) -> Result<()> { let command = db::read(alias)?; let cmd = parse_command(&command)?; diff --git a/src/db/mod.rs b/src/db/mod.rs index de398b5..496bd15 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -7,7 +7,9 @@ use std::ffi::OsStr; use directories::ProjectDirs; use colored::*; use anyhow::{Result, Context}; -use dialoguer::{Confirm, theme::ColorfulTheme}; +use dialoguer::{Confirm, theme::ColorfulTheme, Input}; + +use crate::suggest; mod tst; pub mod interface; @@ -45,6 +47,7 @@ pub fn save(alias: &String, command: &String) -> Result<()>{ Ok(()) } + pub fn read(alias: &String) -> Result{ fs::read_to_string(get_path(alias)). with_context(|| format!("Alias {} not found", alias)) diff --git a/src/lib.rs b/src/lib.rs index 083d2c5..927ccf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod db; pub mod outputils; -pub mod commands; \ No newline at end of file +pub mod commands; +pub mod suggest; \ No newline at end of file diff --git a/src/suggest/mod.rs b/src/suggest/mod.rs new file mode 100644 index 0000000..a194de6 --- /dev/null +++ b/src/suggest/mod.rs @@ -0,0 +1,36 @@ +use crate::db; +use anyhow::{Result, Context, anyhow}; + +pub fn suggest_alias(command: &String) -> Result{ + // Suggests an alias based on the given command + // Gives an alias that doesn't exist + let mut words: Vec<&str> = command.split(&[' ', '-'][..]).collect(); + let mut alias_words = vec![String::new(); words.len()]; + let mut i = 0; + let mut empcnt = 0; // empty count + while empcnt=words.len() && !db::exists(&alias){ + return Ok(alias); + } + words[i] = &words[i][1..]; + i = (i+1)%words.len(); + } + Err(anyhow!("No suggestions found")) +} + +#[cfg(test)] +mod tests{ + use super::*; + #[test] + fn make_sug() -> Result<()>{ + let sug = suggest_alias(&"git push origin master".to_string())?; + println!("suggestion: {}", sug); + Ok(()) + } +} \ No newline at end of file