refactor: shell syntax

This commit is contained in:
iff 2025-04-05 00:10:16 +02:00
parent bdc1327d29
commit e297aa3def
3 changed files with 18 additions and 20 deletions

View file

@ -1,7 +1,7 @@
use crate::shell::Data; use crate::shell::Data;
use crate::suggestions;
use crate::suggestions::suggest_candidates; use crate::suggestions::suggest_candidates;
use crate::system; use crate::system;
use crate::{shell, suggestions};
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;
@ -11,7 +11,6 @@ use ui::Color;
use std::path::Path; use std::path::Path;
pub fn suggestion(data: &mut Data) { pub fn suggestion(data: &mut Data) {
let shell = data.shell.clone();
let mut last_command; let mut last_command;
loop { loop {
@ -21,10 +20,6 @@ pub fn suggestion(data: &mut Data) {
break; break;
}; };
for candidate in &mut data.candidates {
shell::shell_syntax(&shell, candidate);
}
suggestions::select_candidate(data); suggestions::select_candidate(data);
let execution = suggestions::confirm_suggestion(data); let execution = suggestions::confirm_suggestion(data);
@ -55,15 +50,11 @@ pub fn suggestion(data: &mut Data) {
} }
pub fn echo(data: &mut Data) { pub fn echo(data: &mut Data) {
let shell = data.shell.clone();
suggest_candidates(data); suggest_candidates(data);
if data.candidates.is_empty() { if data.candidates.is_empty() {
eprintln!("No suggestions found") return;
}; };
for candidate in &mut data.candidates { println!("{}", data.candidates.join("<PR_BR>\n"));
shell::shell_syntax(&shell, candidate);
println!("{} <_PR_BR>", candidate);
}
} }
pub fn cnf(data: &mut Data) { pub fn cnf(data: &mut Data) {

View file

@ -691,13 +691,11 @@ pub fn get_shell() -> String {
} }
} }
pub fn shell_syntax(shell: &str, command: &mut String) { pub fn shell_syntax(shell: &str, command: &str) -> String {
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
match shell { match shell {
"nu" => { "nu" => command.replace("&&", ";").to_string(),
*command = command.replace(" && ", " ; "); _ => command.to_string(),
}
_ => {}
} }
} }

View file

@ -8,13 +8,16 @@ use inquire::*;
use ui::Color; use ui::Color;
use crate::rules::match_pattern; use crate::rules::match_pattern;
use crate::shell::{add_candidates_no_dup, module_output, shell_evaluated_commands, Data}; use crate::shell::{
add_candidates_no_dup, module_output, shell_evaluated_commands, shell_syntax, Data,
};
use crate::style::highlight_difference; use crate::style::highlight_difference;
pub fn suggest_candidates(data: &mut Data) { pub fn suggest_candidates(data: &mut Data) {
if data.split.is_empty() { if data.split.is_empty() {
return; return;
} }
let shell = &data.shell;
let executable = &data.split[0] let executable = &data.split[0]
.rsplit(std::path::MAIN_SEPARATOR) .rsplit(std::path::MAIN_SEPARATOR)
.next() .next()
@ -70,14 +73,20 @@ pub fn suggest_candidates(data: &mut Data) {
} }
if !final_candidates.is_empty() { if !final_candidates.is_empty() {
data.candidates = final_candidates; data.candidates = final_candidates
.iter()
.map(|s| shell_syntax(shell, s))
.collect();
return; return;
} }
for fallback in fallbacks { for fallback in fallbacks {
let candidates = module_output(data, fallback); let candidates = module_output(data, fallback);
if candidates.is_some() { if candidates.is_some() {
add_candidates_no_dup(command, &mut final_candidates, &candidates.unwrap()); add_candidates_no_dup(command, &mut final_candidates, &candidates.unwrap());
data.candidates = final_candidates; data.candidates = final_candidates
.iter()
.map(|s| shell_syntax(shell, s))
.collect();
return; return;
} }
} }