diff --git a/Cargo.toml b/Cargo.toml index da72eb7..64b11aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pay-respects" -version = "0.4.6" +version = "0.4.7" edition = "2021" [dependencies] diff --git a/rule_parser/src/lib.rs b/rule_parser/src/lib.rs index a5ce746..df4574e 100644 --- a/rule_parser/src/lib.rs +++ b/rule_parser/src/lib.rs @@ -93,7 +93,7 @@ fn gen_match_rules(rules: Vec) -> TokenStream { suggestion_tokens.push(match_tokens); let string_patterns = pattern.join("\", \""); - let string_patterns: TokenStream2 = format!("vec![\"{}\"]", string_patterns).parse().unwrap(); + let string_patterns: TokenStream2 = format!("[\"{}\"]", string_patterns).parse().unwrap(); patterns_tokens.push(string_patterns); } diff --git a/src/files.rs b/src/files.rs index b95b70e..99c35f9 100644 --- a/src/files.rs +++ b/src/files.rs @@ -38,13 +38,16 @@ pub fn get_best_match_file(input: &str) -> Option { }; while let Some(exit_dir) = exit_dirs.pop() { - let dir_files = files.filter_map(|file| { - let file = file.unwrap(); - let file_name = file.file_name().into_string().unwrap(); - Some(file_name) - }).collect::>(); + let dir_files = files + .map(|file| { + let file = file.unwrap(); + + file.file_name().into_string().unwrap() + }) + .collect::>(); let best_match = find_similar(&exit_dir, dir_files); + best_match.as_ref()?; input = format!("{}/{}", input, best_match.unwrap()); files = match std::fs::read_dir(&input) { diff --git a/src/main.rs b/src/main.rs index b3e5a05..bd22383 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use colored::Colorize; use crate::style::highlight_difference; +use colored::Colorize; mod args; mod files; @@ -15,21 +15,27 @@ fn main() { ); let mut last_command = shell::last_command_expanded_alias(&shell); loop { - let corrected_command = suggestions::suggest_command(&shell, &last_command); if let Some(corrected_command) = corrected_command { - let command_difference = highlight_difference(&shell, &corrected_command, &last_command); + let command_difference = + highlight_difference(&shell, &corrected_command, &last_command); if let Some(highlighted_command) = command_difference { - let execution = suggestions::confirm_suggestion(&shell, &corrected_command, &highlighted_command); + let execution = suggestions::confirm_suggestion( + &shell, + &corrected_command, + &highlighted_command, + ); if execution.is_ok() { return; - } - else { - let retry_message = format!("{}", "Looking for new suggestion...".cyan().bold()); + } else { + let retry_message = + format!("{}", "Looking for new suggestion...".cyan().bold()); println!("\n{}\n", retry_message); last_command = corrected_command; } + } else { + break; } } else { break; diff --git a/src/shell.rs b/src/shell.rs index 562075b..f70f398 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,7 +1,6 @@ use std::io::prelude::*; use std::process::exit; - use std::sync::mpsc::channel; use std::thread; use std::time::Duration; @@ -9,30 +8,30 @@ use std::time::Duration; pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"]; pub fn command_output(shell: &str, command: &str) -> String { - let (sender, receiver) = channel(); let _shell = shell.to_owned(); let _command = command.to_owned(); thread::spawn(move || { - sender.send(std::process::Command::new(_shell) - .arg("-c") - .arg(_command) - .env("LC_ALL", "C") - .output() - .expect("failed to execute process")) + sender + .send( + std::process::Command::new(_shell) + .arg("-c") + .arg(_command) + .env("LC_ALL", "C") + .output() + .expect("failed to execute process"), + ) .expect("failed to send output"); }); match receiver.recv_timeout(Duration::from_secs(3)) { - Ok(output) => { - String::from_utf8_lossy(&output.stderr) - .to_string() - .split_whitespace() - .collect::>() - .join(" ") - .to_lowercase() - } + Ok(output) => String::from_utf8_lossy(&output.stderr) + .to_string() + .split_whitespace() + .collect::>() + .join(" ") + .to_lowercase(), Err(_) => { use colored::*; eprintln!("Timeout while executing command: {}", command.red()); @@ -46,7 +45,7 @@ fn last_command(shell: &str) -> String { match shell { "bash" => { let first_line = last_command.lines().next().unwrap().trim(); - first_line.split_once(" ").unwrap().1.to_string() + first_line.split_once(' ').unwrap().1.to_string() } "zsh" => last_command, "fish" => last_command, diff --git a/src/style.rs b/src/style.rs index fd05d52..e05bb91 100644 --- a/src/style.rs +++ b/src/style.rs @@ -4,8 +4,12 @@ use colored::*; // to_string() is necessary here, otherwise there won't be color in the output #[warn(clippy::unnecessary_to_owned)] -pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: &str) -> Option { - let replaced_newline = suggested_command.replace("\n", r" {{newline}}"); +pub fn highlight_difference( + shell: &str, + suggested_command: &str, + last_command: &str, +) -> Option { + let replaced_newline = suggested_command.replace('\n', r" {{newline}}"); let mut split_suggested_command = split_command(&replaced_newline); let split_last_command = split_command(last_command); @@ -31,7 +35,7 @@ pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: // let mut highlighted = suggested_command.to_string(); 'next: for entry in split_suggested_command.iter_mut() { if entry == r"{{newline}" { - continue + continue; } for old in &old_entries { if old == entry { diff --git a/src/suggestions.rs b/src/suggestions.rs index 32fbf62..da55c57 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -167,7 +167,7 @@ fn compare_string(a: &str, b: &str) -> usize { } pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), ()> { - println!{"{}\n", highlighted} + println! {"{}\n", highlighted} println!("Press enter to execute the suggestion. Or press Ctrl+C to exit."); std::io::stdin().read_line(&mut String::new()).unwrap(); @@ -201,8 +201,8 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu .expect("failed to wait on process"); if process.success() { - return Ok(()); + Ok(()) } else { - return Err(()); + Err(()) } }