mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 22:40:09 +01:00
init
This commit is contained in:
commit
334bd88b03
11 changed files with 317 additions and 0 deletions
93
src/corrections.rs
Normal file
93
src/corrections.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use rule_parser::parse_rules;
|
||||
|
||||
use crate::shell::{command_output, find_last_command, find_shell};
|
||||
use crate::style::highlight_difference;
|
||||
|
||||
pub fn correct_command() -> Option<String> {
|
||||
let shell = find_shell();
|
||||
let last_command = find_last_command(&shell);
|
||||
let command_output = command_output(&shell, &last_command);
|
||||
println!("Last command: {}", last_command);
|
||||
println!("Command output: {}", command_output);
|
||||
|
||||
let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
|
||||
let command = match split_command.first().expect("No command found.") {
|
||||
&"sudo" => split_command.get(1).expect("No command found."),
|
||||
_ => split_command.first().expect("No command found."),
|
||||
};
|
||||
|
||||
if split_command[0] != "sudo" {
|
||||
let suggest = match_pattern("sudo", &command_output);
|
||||
if let Some(suggest) = suggest {
|
||||
let suggest = eval_suggest(&suggest, &last_command);
|
||||
return Some(highlight_difference(&suggest, &last_command));
|
||||
}
|
||||
}
|
||||
|
||||
let suggest = match_pattern(command, &command_output);
|
||||
if let Some(suggest) = suggest {
|
||||
let suggest = eval_suggest(&suggest, &last_command);
|
||||
return Some(highlight_difference(&suggest, &last_command));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn match_pattern(command: &str, error_msg: &str) -> Option<String> {
|
||||
let rules = parse_rules!("rules");
|
||||
if rules.contains_key(command) {
|
||||
let suggest = rules.get(command).unwrap();
|
||||
for (pattern, suggest) in suggest {
|
||||
for pattern in pattern {
|
||||
if error_msg.contains(pattern) {
|
||||
return Some(suggest.to_owned().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_suggest(suggest: &str, last_command: &str) -> String {
|
||||
let mut suggest = suggest.to_owned();
|
||||
if suggest.contains("{{command}}") {
|
||||
suggest = suggest.replace("{{command}}", last_command);
|
||||
}
|
||||
while suggest.contains("{{command") {
|
||||
let placeholder_start = "{{command";
|
||||
let placeholder_end = "}}";
|
||||
let placeholder = suggest.find(placeholder_start).unwrap()
|
||||
..suggest.find(placeholder_end).unwrap() + placeholder_end.len();
|
||||
|
||||
let range = suggest[placeholder.to_owned()].trim_matches(|c| c == '[' || c == ']');
|
||||
if let Some((start, end)) = range.split_once(':') {
|
||||
let start = start.parse::<usize>().unwrap();
|
||||
let end = end.parse::<usize>().unwrap();
|
||||
let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
|
||||
let command = split_command[start..end].join(" ");
|
||||
suggest = suggest.replace(&suggest[placeholder], &command);
|
||||
} else {
|
||||
let range = range.parse::<usize>().unwrap();
|
||||
let split_command = last_command.split_whitespace().collect::<Vec<&str>>();
|
||||
let command = split_command[range].to_owned();
|
||||
suggest = suggest.replace(&suggest[placeholder], &command);
|
||||
}
|
||||
}
|
||||
|
||||
suggest
|
||||
}
|
||||
|
||||
pub fn confirm_correction(command: &str) {
|
||||
println!("Did you mean {}?", command);
|
||||
println!("Press enter to execute the corrected command. Or press Ctrl+C to exit.");
|
||||
std::io::stdin().read_line(&mut String::new()).unwrap();
|
||||
let shell = find_shell();
|
||||
std::process::Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.spawn()
|
||||
.expect("failed to execute process");
|
||||
}
|
||||
14
src/main.rs
Normal file
14
src/main.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
mod corrections;
|
||||
mod shell;
|
||||
mod style;
|
||||
|
||||
fn main() {
|
||||
std::env::set_var("LC_ALL", "C");
|
||||
|
||||
let corrected_command = corrections::correct_command();
|
||||
if let Some(corrected_command) = corrected_command {
|
||||
corrections::confirm_correction(&corrected_command);
|
||||
} else {
|
||||
println!("No correction found.");
|
||||
}
|
||||
}
|
||||
73
src/shell.rs
Normal file
73
src/shell.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use std::{collections::HashMap, fs::read_to_string, process::exit};
|
||||
|
||||
pub fn find_shell() -> String {
|
||||
std::env::var("SHELL")
|
||||
.unwrap_or_else(|_| String::from("bash"))
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.to_lowercase()
|
||||
}
|
||||
|
||||
pub fn find_last_command(shell: &str) -> String {
|
||||
let history_env = std::env::var("HISTFILE");
|
||||
let history_file = match history_env {
|
||||
Ok(file) => file,
|
||||
Err(_) => shell_default_history_file(shell),
|
||||
};
|
||||
|
||||
let history = read_to_string(history_file).expect("Could not read history file.");
|
||||
|
||||
match shell {
|
||||
"bash" => history.lines().rev().nth(1).unwrap().to_string(),
|
||||
"zsh" => history
|
||||
.lines()
|
||||
.rev()
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.split_once(';')
|
||||
.unwrap()
|
||||
.1
|
||||
.to_string(),
|
||||
"fish" => {
|
||||
let mut history_lines = history.lines().rev();
|
||||
let mut last_command = String::new();
|
||||
let mut skips = 0;
|
||||
while skips <= 2 {
|
||||
last_command = history_lines.next().unwrap().to_string();
|
||||
if last_command.starts_with("- cmd") {
|
||||
skips += 1;
|
||||
}
|
||||
}
|
||||
last_command.split_once(": ").unwrap().1.to_string()
|
||||
}
|
||||
_ => {
|
||||
println!("Unsupported shell.");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn command_output(shell: &str, command: &str) -> String {
|
||||
let output = std::process::Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
.to_string()
|
||||
.to_lowercase()
|
||||
}
|
||||
|
||||
fn shell_default_history_file(shell: &str) -> String {
|
||||
let shell_file_map = HashMap::from([
|
||||
("bash", String::from(".bash_history")),
|
||||
("zsh", String::from(".zsh_history")),
|
||||
("fish", String::from(".local/share/fish/fish_history")),
|
||||
]);
|
||||
|
||||
let file = shell_file_map.get(shell).expect("Unsupported shell.");
|
||||
format!("{}/{}", std::env::var("HOME").unwrap(), file)
|
||||
}
|
||||
27
src/style.rs
Normal file
27
src/style.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use colored::*;
|
||||
|
||||
pub fn highlight_difference(corrected_command: &str, last_command: &str) -> String {
|
||||
let mut highlighted_command = String::new();
|
||||
|
||||
let split_corrected_command = corrected_command.split(' ');
|
||||
let split_last_command = last_command.split(' ');
|
||||
|
||||
for new in split_corrected_command {
|
||||
let mut changed = true;
|
||||
for old in split_last_command.clone() {
|
||||
if new == old {
|
||||
changed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
highlighted_command.push_str(&new.red().bold());
|
||||
} else {
|
||||
highlighted_command.push_str(&new.green());
|
||||
}
|
||||
highlighted_command.push(' ');
|
||||
}
|
||||
|
||||
highlighted_command.pop();
|
||||
highlighted_command
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue