chore: cleanup

This commit is contained in:
iff 2024-11-17 15:06:14 +01:00
parent f1f7ed8dbe
commit 171b3d4607
5 changed files with 26 additions and 23 deletions

View file

@ -47,17 +47,22 @@ Install from your package manager if available:
For generic x86 Linux desktop, you can get the binary from [releases](https://github.com/iffse/pay-respects/releases). For generic x86 Linux desktop, you can get the binary from [releases](https://github.com/iffse/pay-respects/releases).
```shell ```shell
# download lastest release # download latest release
curl -sL -o pay-respects.zip \ curl -sL -o pay-respects.zip \
$(curl -s https://api.github.com/repos/iffse/pay-respects/releases/latest \ $(curl -s https://api.github.com/repos/iffse/pay-respects/releases/latest \
| sed 's/[()",{}]/ /g; s/ /\n/g' \ | sed 's/[()",{}]/ /g; s/ /\n/g' \
| grep "https.*pay-respects-ubuntu-latest.zip") | grep "https.*pay-respects-ubuntu-latest.zip")
unzip -q pay-respects.zip pay-respects
rm pay-respects.zip # extract zip, e.g. one of the following
7z -x pay-respects.zip
unzip pay-respects.zip
# system-wide installation # system-wide installation
sudo chmod a+x pay-respects sudo chmod a+x pay-respects
sudo mv pay-respects /usr/local/bin/pay-respects sudo mv pay-respects /usr/local/bin/pay-respects
# delete downloaded package
rm pay-respects.zip
``` ```
If you have cargo installed, you can compile it from source (should work regardless of operating system or architecture): If you have cargo installed, you can compile it from source (should work regardless of operating system or architecture):

View file

@ -37,7 +37,7 @@ pub fn opts(suggest: &mut String, last_command: &mut String, opt_list: &mut Vec<
} }
} }
pub fn cmd_reg(suggest: &mut String, last_command: &String) { pub fn cmd_reg(suggest: &mut String, last_command: &str) {
while suggest.contains("{{cmd::") { while suggest.contains("{{cmd::") {
let (placeholder, args) = eval_placeholder(suggest, "{{cmd::", "}}"); let (placeholder, args) = eval_placeholder(suggest, "{{cmd::", "}}");
@ -59,7 +59,7 @@ pub fn err(suggest: &mut String, error_msg: &str) {
} }
} }
pub fn command(suggest: &mut String, split_command: &Vec<String>) { pub fn command(suggest: &mut String, split_command: &[String]) {
while suggest.contains("{{command") { while suggest.contains("{{command") {
let (placeholder, args) = eval_placeholder(suggest, "{{command", "}}"); let (placeholder, args) = eval_placeholder(suggest, "{{command", "}}");
@ -67,7 +67,7 @@ pub fn command(suggest: &mut String, split_command: &Vec<String>) {
if let Some((start, end)) = range.split_once(':') { if let Some((start, end)) = range.split_once(':') {
let mut start_index = start.parse::<i32>().unwrap_or(0); let mut start_index = start.parse::<i32>().unwrap_or(0);
if start_index < 0 { if start_index < 0 {
start_index = split_command.len() as i32 + start_index; start_index += split_command.len() as i32;
}; };
let mut end_index; let mut end_index;
let parsed_end = end.parse::<i32>(); let parsed_end = end.parse::<i32>();
@ -76,9 +76,9 @@ pub fn command(suggest: &mut String, split_command: &Vec<String>) {
} else { } else {
end_index = parsed_end.unwrap(); end_index = parsed_end.unwrap();
if end_index < 0 { if end_index < 0 {
end_index = split_command.len() as i32 + end_index + 1; end_index += split_command.len() as i32 + 1;
} else { } else {
end_index = end_index + 1; end_index += 1;
} }
}; };
@ -89,12 +89,12 @@ pub fn command(suggest: &mut String, split_command: &Vec<String>) {
let range = range.parse::<usize>().unwrap_or(0); let range = range.parse::<usize>().unwrap_or(0);
let command = &split_command[range]; let command = &split_command[range];
suggest.replace_range(placeholder, &command); suggest.replace_range(placeholder, command);
} }
} }
} }
pub fn typo(suggest: &mut String, split_command: &Vec<String>, shell: &str) { pub fn typo(suggest: &mut String, split_command: &[String], shell: &str) {
while suggest.contains("{{typo") { while suggest.contains("{{typo") {
let (placeholder, args) = eval_placeholder(suggest, "{{typo", "}}"); let (placeholder, args) = eval_placeholder(suggest, "{{typo", "}}");
@ -138,8 +138,7 @@ pub fn typo(suggest: &mut String, split_command: &Vec<String>, shell: &str) {
unreachable!("Typo suggestion must have a command index"); unreachable!("Typo suggestion must have a command index");
}; };
let match_list; let match_list = if suggest.contains('(') {
if suggest.contains('(') {
let split = suggest[args.to_owned()] let split = suggest[args.to_owned()]
.split_once("(") .split_once("(")
.unwrap() .unwrap()
@ -147,25 +146,24 @@ pub fn typo(suggest: &mut String, split_command: &Vec<String>, shell: &str) {
.rsplit_once(")") .rsplit_once(")")
.unwrap() .unwrap()
.0; .0;
match_list = split.split(',').collect::<Vec<&str>>(); split.split(',').collect::<Vec<&str>>()
} else { } else {
unreachable!("Typo suggestion must have a match list"); unreachable!("Typo suggestion must have a match list");
} };
let match_list = match_list let match_list = match_list
.iter() .iter()
.map(|s| s.trim().to_string()) .map(|s| s.trim().to_string())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
let command; let command = if match_list[0].starts_with("{{shell") {
if match_list[0].starts_with("{{shell") {
let function = match_list.join(","); let function = match_list.join(",");
let (_, args) = eval_placeholder(&function, "{{shell", "}}"); let (_, args) = eval_placeholder(&function, "{{shell", "}}");
let function = &function[args.to_owned()].trim_matches(|c| c == '(' || c == ')'); let function = &function[args.to_owned()].trim_matches(|c| c == '(' || c == ')');
command = suggest_typo(&split_command[index], eval_shell_command(shell, function)); suggest_typo(&split_command[index], eval_shell_command(shell, function))
} else { } else {
command = suggest_typo(&split_command[index], match_list); suggest_typo(&split_command[index], match_list)
} };
suggest.replace_range(placeholder, &command); suggest.replace_range(placeholder, &command);
} }

View file

@ -55,13 +55,14 @@ pub fn runtime_match(
file = check_dirs(xdg_data_dirs); file = check_dirs(xdg_data_dirs);
} }
#[allow(clippy::question_mark)]
if file.is_none() { if file.is_none() {
return None; return None;
} }
let file = std::fs::read_to_string(file.unwrap()).unwrap(); let file = std::fs::read_to_string(file.unwrap()).unwrap();
let rule: Rule = toml::from_str(&file).unwrap(); let rule: Rule = toml::from_str(&file).unwrap();
let split_command = split_command(&last_command); let split_command = split_command(last_command);
let mut pure_suggest; let mut pure_suggest;
@ -132,7 +133,7 @@ fn eval_condition(
shell: &str, shell: &str,
last_command: &str, last_command: &str,
error_msg: &str, error_msg: &str,
split_command: &Vec<String>, split_command: &[String],
) -> bool { ) -> bool {
match condition { match condition {
"executable" => check_executable(shell, arg), "executable" => check_executable(shell, arg),

View file

@ -192,7 +192,6 @@ pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str) {
.expect("Failed to execute process"); .expect("Failed to execute process");
let config_path = String::from_utf8_lossy(&output.stdout); let config_path = String::from_utf8_lossy(&output.stdout);
let mut file = std::fs::OpenOptions::new() let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) .append(true)
.open(config_path.trim()) .open(config_path.trim())
.expect("Failed to open config file"); .expect("Failed to open config file");

View file

@ -167,7 +167,7 @@ pub fn suggest_typo(typos: &[String], candidates: Vec<String>) -> String {
suggestions.join(" ") suggestions.join(" ")
} }
pub fn find_similar(typo: &str, candidates: &Vec<String>) -> Option<String> { pub fn find_similar(typo: &str, candidates: &[String]) -> Option<String> {
let mut min_distance = 10; let mut min_distance = 10;
let mut min_distance_index = None; let mut min_distance_index = None;
for (i, candidate) in candidates.iter().enumerate() { for (i, candidate) in candidates.iter().enumerate() {