From dc0ac63519a256af7aa4915d5eed4b3012b83569 Mon Sep 17 00:00:00 2001 From: kronberger-droid Date: Wed, 19 Aug 2026 23:40:21 +0200 Subject: [PATCH] refactor(menu): read display widths through a checked helper `display_widths` is built from the same `values` in `CompletionDisplay::new`, so the two indexings in `format_plain` and `format_ansi` held. `display_width(index)` answers with 0 out of range, so a slipped invariant paints an unpadded row instead of aborting mid-repaint. --- src/menu/columnar_menu.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/menu/columnar_menu.rs b/src/menu/columnar_menu.rs index 292df212..184dbc20 100644 --- a/src/menu/columnar_menu.rs +++ b/src/menu/columnar_menu.rs @@ -355,6 +355,19 @@ impl ColumnarMenu { self.working_details.columns.max(1) } + /// Width of the suggestion at `index`, 0 when out of range. + /// + /// `display_widths` is built from the same `values` in + /// [`CompletionDisplay::new`], so the index holds today; 0 keeps a + /// mismatch painting (unpadded) instead of aborting mid-repaint. + fn display_width(&self, index: usize) -> usize { + self.completions + .display_widths + .get(index) + .copied() + .unwrap_or(0) + } + /// Creates default string that represents one suggestion from the menu pub fn create_string( &self, @@ -386,7 +399,7 @@ impl ColumnarMenu { let display_value = suggestion.display_value(); // Calculate the remaining space after the suggestion text - let empty_space = terminal_width.saturating_sub(self.completions.display_widths[index]); + let empty_space = terminal_width.saturating_sub(self.display_width(index)); let marker = if is_selected { ">" } else { "" }; let description = suggestion.description.as_deref().unwrap_or(""); @@ -424,7 +437,7 @@ impl ColumnarMenu { let is_selected = index == self.index(); let terminal_width = self.get_width(); let display_value = suggestion.display_value(); - let display_width = self.completions.display_widths[index]; + let display_width = self.display_width(index); let color_settings = &self.settings.color; // TODO(ysthakur): let the user strip quotes, rather than doing it here