pay-respects/src/modes.rs

127 lines
3 KiB
Rust
Raw Normal View History

2024-12-06 17:35:48 +01:00
use crate::shell::Data;
2024-11-23 22:18:33 +01:00
use crate::style::highlight_difference;
2024-12-06 17:35:48 +01:00
use crate::suggestions::{best_match_path, suggest_command};
use crate::system;
2024-11-23 22:51:43 +01:00
use crate::{shell, suggestions};
2024-11-23 22:18:33 +01:00
use colored::Colorize;
use inquire::*;
2024-11-23 22:18:33 +01:00
2024-12-06 17:35:48 +01:00
pub fn suggestion(data: &mut Data) {
let shell = data.shell.clone();
2024-12-06 21:36:08 +01:00
let mut last_command ;
2024-11-23 22:18:33 +01:00
loop {
2024-12-06 21:36:08 +01:00
last_command = data.command.clone();
2024-11-23 22:18:33 +01:00
let suggestion = {
2024-12-06 19:12:40 +01:00
let command = suggest_command(data);
2024-11-23 22:18:33 +01:00
if command.is_none() {
break;
};
let mut command = command.unwrap();
shell::shell_syntax(&shell, &mut command);
command
};
2024-12-06 17:35:48 +01:00
data.update_suggest(&suggestion);
data.expand_suggest();
2024-11-23 22:18:33 +01:00
let highlighted_suggestion = {
let difference = highlight_difference(&shell, &suggestion, &last_command);
if difference.is_none() {
break;
};
difference.unwrap()
};
2024-12-06 19:12:40 +01:00
let execution = suggestions::confirm_suggestion(data, &highlighted_suggestion);
2024-11-23 22:18:33 +01:00
if execution.is_ok() {
return;
} else {
2024-12-06 17:35:48 +01:00
data.update_command(&suggestion);
2024-12-06 19:12:40 +01:00
let msg = Some(
execution
.err()
.unwrap()
.split_whitespace()
.collect::<Vec<&str>>()
.join(" "),
);
2024-12-06 17:35:48 +01:00
data.update_error(msg);
2024-11-23 22:18:33 +01:00
let retry_message = format!("{}...", t!("retry"));
eprintln!("\n{}\n", retry_message.cyan().bold());
}
}
eprintln!("{}: {}\n", t!("no-suggestion"), last_command.red());
eprintln!(
"{}\n{}",
t!("contribute"),
option_env!("CARGO_PKG_REPOSITORY").unwrap_or("https://github.com/iffse/pay-respects/")
);
}
2024-12-06 17:35:48 +01:00
pub fn cnf(data: &mut Data) {
let shell = data.shell.clone();
let last_command = data.command.clone();
let mut split_command = data.split.clone();
2024-11-23 22:18:33 +01:00
2024-12-06 17:35:48 +01:00
let executable = split_command[0].as_str();
2024-11-23 22:18:33 +01:00
let best_match = best_match_path(executable);
if best_match.is_some() {
let best_match = best_match.unwrap();
2024-12-06 17:35:48 +01:00
split_command[0] = best_match;
2024-12-06 21:36:08 +01:00
let suggest = split_command.join(" ");
data.update_suggest(&suggest);
2024-12-06 17:35:48 +01:00
data.expand_suggest();
let highlighted_suggestion =
2024-12-06 21:36:08 +01:00
highlight_difference(&shell, &suggest, &last_command).unwrap();
let status = suggestions::confirm_suggestion(data, &highlighted_suggestion);
if status.is_err() {
data.update_command(&suggest);
let msg = Some(
status
.err()
.unwrap()
.split_whitespace()
.collect::<Vec<&str>>()
.join(" "),
);
data.update_error(msg);
}
suggestion(data);
} else {
let package_manager = match system::get_package_manager(&shell) {
Some(package_manager) => package_manager,
None => {
eprintln!("no package manager found");
return;
}
};
let packages = match system::get_packages(&shell, &package_manager, executable) {
Some(packages) => packages,
None => {
eprintln!("no package found");
return;
}
};
let ans = Select::new("Select a package to install", packages).prompt();
let package = match ans {
Ok(package) => package,
Err(_) => {
eprintln!("no package selected");
return;
}
};
// retry after installing package
if system::install_package(&shell, &package_manager, &package) {
2024-12-06 19:12:40 +01:00
let _ = suggestions::confirm_suggestion(data, &last_command);
2024-11-23 22:18:33 +01:00
}
}
}