mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
fix: some error handlings
This commit is contained in:
parent
1444eeab84
commit
15c1ba3370
7 changed files with 49 additions and 37 deletions
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "pay-respects"
|
||||
version = "0.4.6"
|
||||
version = "0.4.7"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ fn gen_match_rules(rules: Vec<Rule>) -> 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);
|
||||
}
|
||||
|
||||
|
|
|
|||
11
src/files.rs
11
src/files.rs
|
|
@ -38,13 +38,16 @@ pub fn get_best_match_file(input: &str) -> Option<String> {
|
|||
};
|
||||
|
||||
while let Some(exit_dir) = exit_dirs.pop() {
|
||||
let dir_files = files.filter_map(|file| {
|
||||
let dir_files = files
|
||||
.map(|file| {
|
||||
let file = file.unwrap();
|
||||
let file_name = file.file_name().into_string().unwrap();
|
||||
Some(file_name)
|
||||
}).collect::<Vec<String>>();
|
||||
|
||||
file.file_name().into_string().unwrap()
|
||||
})
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
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) {
|
||||
|
|
|
|||
20
src/main.rs
20
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;
|
||||
|
|
|
|||
17
src/shell.rs
17
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)
|
||||
sender
|
||||
.send(
|
||||
std::process::Command::new(_shell)
|
||||
.arg("-c")
|
||||
.arg(_command)
|
||||
.env("LC_ALL", "C")
|
||||
.output()
|
||||
.expect("failed to execute process"))
|
||||
.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)
|
||||
Ok(output) => String::from_utf8_lossy(&output.stderr)
|
||||
.to_string()
|
||||
.split_whitespace()
|
||||
.collect::<Vec<&str>>()
|
||||
.join(" ")
|
||||
.to_lowercase()
|
||||
}
|
||||
.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,
|
||||
|
|
|
|||
10
src/style.rs
10
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<String> {
|
||||
let replaced_newline = suggested_command.replace("\n", r" {{newline}}");
|
||||
pub fn highlight_difference(
|
||||
shell: &str,
|
||||
suggested_command: &str,
|
||||
last_command: &str,
|
||||
) -> Option<String> {
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue