From 41d50f1a71a00d122e5bc1b40da05538d75f4d31 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 2 Jan 2026 13:40:54 +0100 Subject: [PATCH] reader handle_completions(): don't duplicate pager prefix allocation While at it, use Cow I guess? --- src/pager.rs | 22 ++++++++++++++-------- src/reader/reader.rs | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/pager.rs b/src/pager.rs index a7a64cc49..c6ba73b29 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -1,5 +1,6 @@ //! Pager support. +use std::borrow::Cow; use std::collections::HashMap; use std::collections::hash_map::Entry; @@ -108,7 +109,7 @@ pub struct Pager { // then we definitely need to re-render. have_unrendered_completions: bool, - prefix: WString, + prefix: Cow<'static, wstr>, highlight_prefix: bool, // The text of the search field. @@ -390,8 +391,12 @@ fn completion_info_passes_filter(&self, info: &PagerComp) -> bool { // Match against the completion strings. for candidate in &info.comp { - if string_fuzzy_match_string(needle, &(self.prefix.clone() + &candidate[..]), false) - .is_some() + if string_fuzzy_match_string( + needle, + &(self.prefix.clone().into_owned() + &candidate[..]), + false, + ) + .is_some() { return true; } @@ -639,7 +644,7 @@ pub fn set_completions(&mut self, raw_completions: &[Completion], enable_refilte self.unfiltered_completion_infos = process_completions_into_infos(raw_completions); // Maybe join them. - if self.prefix == "-" { + if *self.prefix == "-" { join_completions(&mut self.unfiltered_completion_infos); } @@ -656,8 +661,8 @@ pub fn set_completions(&mut self, raw_completions: &[Completion], enable_refilte } // Sets the prefix. - pub fn set_prefix(&mut self, pref: &wstr, highlight: bool /* = true */) { - self.prefix = pref.to_owned(); + pub fn set_prefix(&mut self, prefix: Cow<'static, wstr>, highlight: bool /* = true */) { + self.prefix = prefix; self.highlight_prefix = highlight; } @@ -995,7 +1000,7 @@ pub fn is_empty(&self) -> bool { pub fn clear(&mut self) { self.unfiltered_completion_infos.clear(); self.completion_infos.clear(); - self.prefix.clear(); + self.prefix = Cow::Borrowed(L!("")); self.highlight_prefix = false; self.selected_completion_idx = None; self.fully_disclosed = false; @@ -1283,6 +1288,7 @@ mod tests { use crate::termsize::Termsize; use crate::tests::prelude::*; use crate::wcstringutil::StringFuzzyMatch; + use std::borrow::Cow; use std::num::NonZeroU16; #[test] @@ -1484,7 +1490,7 @@ macro_rules! validate { StringFuzzyMatch::exact_match(), CompleteFlags::default(), )]; - pager.set_prefix(L!("{\\\n"), false); // } + pager.set_prefix(Cow::Borrowed(L!("{\\\n")), false); // } pager.set_completions(&c4s, true); validate!(&mut pager, 30, L!("{\\␊Hello")); // } } diff --git a/src/reader/reader.rs b/src/reader/reader.rs index ed453f2f3..94bce010a 100644 --- a/src/reader/reader.rs +++ b/src/reader/reader.rs @@ -3297,7 +3297,7 @@ fn handle_readline_command(&mut self, c: ReadlineCmd) { self.history_pager = Some(0..1); // Update the pager data. self.pager.set_search_field_shown(true); - self.pager.set_prefix(L!("► "), false); + self.pager.set_prefix(Cow::Borrowed(L!("► ")), false); // Update the search field, which triggers the actual history search. let search_string = if !self.history_search.active() || self.history_search.search_string().is_empty() @@ -6842,7 +6842,7 @@ fn best>( } // Update the pager data. - self.pager.set_prefix(&prefix, true); + self.pager.set_prefix(Cow::Owned(prefix), true); self.pager.set_completions(&surviving_completions, true); // Modify the command line to reflect the new pager. self.pager_selection_changed();