Skip to content

Commit

Permalink
add suggest alias functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
JettChenT committed Sep 15, 2022
1 parent 06fedfe commit 2a9c0e4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/bin/q/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
7 changes: 6 additions & 1 deletion src/bin/qali/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
3 changes: 2 additions & 1 deletion src/bin/qali/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
25 changes: 25 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)?;
Expand Down
5 changes: 4 additions & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +47,7 @@ pub fn save(alias: &String, command: &String) -> Result<()>{
Ok(())
}


pub fn read(alias: &String) -> Result<String>{
fs::read_to_string(get_path(alias)).
with_context(|| format!("Alias {} not found", alias))
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod db;
pub mod outputils;
pub mod commands;
pub mod commands;
pub mod suggest;
36 changes: 36 additions & 0 deletions src/suggest/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::db;
use anyhow::{Result, Context, anyhow};

pub fn suggest_alias(command: &String) -> Result<String>{
// 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(){
if words[i].is_empty(){
empcnt+=1;
continue;
}
alias_words[i].push_str(&words[i][0..1]);
let alias = alias_words.join("");
if alias.len()>=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(())
}
}

0 comments on commit 2a9c0e4

Please sign in to comment.