From e297aa3def067a002ca36d1ff760ac7422e36277 Mon Sep 17 00:00:00 2001 From: iff Date: Sat, 5 Apr 2025 00:10:16 +0200 Subject: [PATCH] refactor: shell syntax --- core/src/modes.rs | 15 +++------------ core/src/shell.rs | 8 +++----- core/src/suggestions.rs | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/core/src/modes.rs b/core/src/modes.rs index 118a8df..981cb26 100644 --- a/core/src/modes.rs +++ b/core/src/modes.rs @@ -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("\n")); } pub fn cnf(data: &mut Data) { diff --git a/core/src/shell.rs b/core/src/shell.rs index 23f87c2..d69610c 100644 --- a/core/src/shell.rs +++ b/core/src/shell.rs @@ -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(), } } diff --git a/core/src/suggestions.rs b/core/src/suggestions.rs index db0e52b..ffbf689 100644 --- a/core/src/suggestions.rs +++ b/core/src/suggestions.rs @@ -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; } }