mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
feat: cnf mode
This commit is contained in:
parent
0d8786f5a1
commit
f80fbaa8d3
2 changed files with 98 additions and 58 deletions
66
src/main.rs
66
src/main.rs
|
|
@ -14,8 +14,6 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use crate::{shell::command_output, style::highlight_difference};
|
|
||||||
use colored::Colorize;
|
|
||||||
use sys_locale::get_locale;
|
use sys_locale::get_locale;
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
|
|
@ -24,6 +22,7 @@ mod rules;
|
||||||
mod shell;
|
mod shell;
|
||||||
mod style;
|
mod style;
|
||||||
mod suggestions;
|
mod suggestions;
|
||||||
|
mod modes;
|
||||||
|
|
||||||
#[cfg(feature = "runtime-rules")]
|
#[cfg(feature = "runtime-rules")]
|
||||||
mod replaces;
|
mod replaces;
|
||||||
|
|
@ -59,66 +58,17 @@ fn main() {
|
||||||
|
|
||||||
args::handle_args();
|
args::handle_args();
|
||||||
|
|
||||||
let shell = match std::env::var("_PR_SHELL") {
|
let mode = match std::env::var("_PR_MODE") {
|
||||||
Ok(shell) => shell,
|
Ok(mode) => mode,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!(
|
"suggestion".to_string()
|
||||||
"{}",
|
|
||||||
t!("no-env-setup", var = "_PR_SHELL", help = "pay-respects -h")
|
|
||||||
);
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut last_command = shell::last_command(&shell).trim().to_string();
|
match mode.as_str() {
|
||||||
last_command = shell::expand_alias(&shell, &last_command);
|
"suggestion" => modes::suggestion(),
|
||||||
let mut error_msg = command_output(&shell, &last_command);
|
"cnf" => modes::cnf(),
|
||||||
error_msg = error_msg
|
_ => {
|
||||||
.split_whitespace()
|
|
||||||
.collect::<Vec<&str>>()
|
|
||||||
.join(" ");
|
|
||||||
|
|
||||||
loop {
|
|
||||||
let suggestion = {
|
|
||||||
let command = suggestions::suggest_command(&shell, &last_command, &error_msg);
|
|
||||||
if command.is_none() {
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut command = command.unwrap();
|
|
||||||
shell::shell_syntax(&shell, &mut command);
|
|
||||||
command
|
|
||||||
};
|
|
||||||
|
|
||||||
let highlighted_suggestion = {
|
|
||||||
let difference = highlight_difference(&shell, &suggestion, &last_command);
|
|
||||||
if difference.is_none() {
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
difference.unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
let execution =
|
|
||||||
suggestions::confirm_suggestion(&shell, &suggestion, &highlighted_suggestion);
|
|
||||||
if execution.is_ok() {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
last_command = suggestion;
|
|
||||||
error_msg = execution.err().unwrap();
|
|
||||||
error_msg = error_msg
|
|
||||||
.split_whitespace()
|
|
||||||
.collect::<Vec<&str>>()
|
|
||||||
.join(" ");
|
|
||||||
|
|
||||||
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/")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
90
src/modes.rs
Normal file
90
src/modes.rs
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
use crate::shell::{command_output, get_shell, PRIVILEGE_LIST};
|
||||||
|
use crate::style::highlight_difference;
|
||||||
|
use crate::{shell, suggestions};
|
||||||
|
use crate::suggestions::{split_command, suggest_typo};
|
||||||
|
use colored::Colorize;
|
||||||
|
|
||||||
|
pub fn suggestion() {
|
||||||
|
let shell = get_shell();
|
||||||
|
let mut last_command = shell::last_command(&shell).trim().to_string();
|
||||||
|
last_command = shell::expand_alias(&shell, &last_command);
|
||||||
|
let mut error_msg = command_output(&shell, &last_command);
|
||||||
|
error_msg = error_msg
|
||||||
|
.split_whitespace()
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let suggestion = {
|
||||||
|
let command = suggestions::suggest_command(&shell, &last_command, &error_msg);
|
||||||
|
if command.is_none() {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut command = command.unwrap();
|
||||||
|
shell::shell_syntax(&shell, &mut command);
|
||||||
|
command
|
||||||
|
};
|
||||||
|
|
||||||
|
let highlighted_suggestion = {
|
||||||
|
let difference = highlight_difference(&shell, &suggestion, &last_command);
|
||||||
|
if difference.is_none() {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
difference.unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
let execution =
|
||||||
|
suggestions::confirm_suggestion(&shell, &suggestion, &highlighted_suggestion);
|
||||||
|
if execution.is_ok() {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
last_command = suggestion;
|
||||||
|
error_msg = execution.err().unwrap();
|
||||||
|
error_msg = error_msg
|
||||||
|
.split_whitespace()
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
|
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/")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cnf() {
|
||||||
|
let shell = get_shell();
|
||||||
|
let mut last_command = shell::last_command(&shell).trim().to_string();
|
||||||
|
last_command = shell::expand_alias(&shell, &last_command);
|
||||||
|
|
||||||
|
let mut split_command = split_command(&last_command);
|
||||||
|
let executable = match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
|
||||||
|
true => split_command.get(1).expect(&t!("no-command")).as_str(),
|
||||||
|
false => split_command.first().expect(&t!("no-command")).as_str(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let best_match = suggest_typo(&[executable.to_owned()], vec!["path".to_string()]);
|
||||||
|
if best_match == executable {
|
||||||
|
eprintln!("{}: no command found", shell);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
|
||||||
|
true => {
|
||||||
|
split_command[1] = best_match;
|
||||||
|
}
|
||||||
|
false => {
|
||||||
|
split_command[0] = best_match;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let suggestion = split_command.join(" ");
|
||||||
|
|
||||||
|
let highlighted_suggestion = highlight_difference(&shell, &suggestion, &last_command).unwrap();
|
||||||
|
let _ = suggestions::confirm_suggestion(&shell, &suggestion, &highlighted_suggestion);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue