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

View file

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