feat: noconfirm mode

This commit is contained in:
iff 2025-04-05 18:42:45 +02:00
parent 885dc081e1
commit 5f521650c8
4 changed files with 48 additions and 0 deletions

View file

@ -55,6 +55,7 @@ fn main() -> Result<(), std::io::Error> {
match data.mode { match data.mode {
Suggestion => modes::suggestion(&mut data), Suggestion => modes::suggestion(&mut data),
Echo => modes::echo(&mut data), Echo => modes::echo(&mut data),
NoConfirm => modes::noconfirm(&mut data),
Cnf => modes::cnf(&mut data), Cnf => modes::cnf(&mut data),
} }

View file

@ -2,6 +2,7 @@ use crate::shell::Data;
use crate::suggestions; use crate::suggestions;
use crate::suggestions::suggest_candidates; use crate::suggestions::suggest_candidates;
use crate::system; use crate::system;
use crate::style::highlight_difference;
use colored::Colorize; use colored::Colorize;
use inquire::*; use inquire::*;
use pay_respects_utils::evals::best_matches_path; use pay_respects_utils::evals::best_matches_path;
@ -50,6 +51,43 @@ pub fn echo(data: &mut Data) {
println!("{}", data.candidates.join("<PR_BR>\n")); println!("{}", data.candidates.join("<PR_BR>\n"));
} }
pub fn noconfirm(data: &mut Data) {
let mut last_command;
loop {
last_command = data.command.clone();
suggest_candidates(data);
if data.candidates.is_empty() {
break;
};
let candidate = data.candidates[0].clone();
eprintln!("{}",
highlight_difference(data, &candidate).unwrap()
);
data.update_suggest(&candidate);
data.candidates.clear();
let execution = suggestions::confirm_suggestion(data);
if execution.is_ok() {
return;
} else {
data.update_command(&data.suggest.clone().unwrap());
let msg = Some(execution.err().unwrap());
data.update_error(msg);
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(data: &mut Data) { pub fn cnf(data: &mut Data) {
let shell = data.shell.clone(); let shell = data.shell.clone();
let mut split_command = data.split.clone(); let mut split_command = data.split.clone();

View file

@ -20,6 +20,7 @@ pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"];
pub enum Mode { pub enum Mode {
Suggestion, Suggestion,
Echo, Echo,
NoConfirm,
Cnf, Cnf,
} }
pub struct Init { pub struct Init {
@ -391,6 +392,7 @@ pub fn run_mode() -> Mode {
Ok(mode) => match mode.as_str() { Ok(mode) => match mode.as_str() {
"suggestion" => Mode::Suggestion, "suggestion" => Mode::Suggestion,
"cnf" => Mode::Cnf, "cnf" => Mode::Cnf,
"noconfirm" => Mode::NoConfirm,
"echo" => Mode::Echo, "echo" => Mode::Echo,
_ => { _ => {
eprintln!("Invalid mode: {}", mode); eprintln!("Invalid mode: {}", mode);

View file

@ -24,6 +24,13 @@ extern crate rust_i18n;
i18n!("i18n", fallback = "en", minify_key = true); i18n!("i18n", fallback = "en", minify_key = true);
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
let mode = std::env::var("_PR_MODE");
if let Ok(mode) = mode {
if mode.as_str() == "noconfirm" {
return Ok(());
}
}
let command = std::env::var("_PR_LAST_COMMAND").expect("_PR_LAST_COMMAND not set"); let command = std::env::var("_PR_LAST_COMMAND").expect("_PR_LAST_COMMAND not set");
let error = std::env::var("_PR_ERROR_MSG").expect("_PR_ERROR_MSG not set"); let error = std::env::var("_PR_ERROR_MSG").expect("_PR_ERROR_MSG not set");
colored::control::set_override(true); colored::control::set_override(true);