bubbletea_widgets/list/defaultitem.rs
1//! Default item implementation and delegate for list components.
2//!
3//! This module provides the standard item type and delegate implementation for the list component.
4//! The `DefaultItem` is a simple item with a title and description, while `DefaultDelegate` handles
5//! the rendering and interaction logic for these items.
6//!
7//! ## Filter Highlighting Architecture
8//!
9//! This module implements a sophisticated filter highlighting system that avoids common ANSI
10//! rendering issues. The key insight is that lipgloss styles with padding/borders cause spacing
11//! problems when applied to individual text segments.
12//!
13//! ### The Problem
14//! When highlighting individual characters in filtered text, naively applying styles with padding
15//! results in extra spaces between segments:
16//! - Input: "Nutella" with 'N' highlighted
17//! - Broken: "│ N utella" (space between N and utella)
18//! - Correct: "│ Nutella" (seamless text)
19//!
20//! ### The Solution
21//! 1. **Segment-Based Highlighting**: Group consecutive match indices to minimize ANSI sequences
22//! 2. **Clean Styles**: Use styles without padding/borders for `apply_character_highlighting`
23//! 3. **Manual Layout**: Apply borders and padding manually after highlighting is complete
24//!
25//! ### Visual States
26//! - **Selected Items**: Left border (│) + 1 space + highlighted text
27//! - **Unselected Items**: 2 spaces + highlighted text (no border)
28//! - **Dimmed Items**: Faded colors when filter input is empty
29//!
30//! This approach ensures perfect text rendering while maintaining the visual hierarchy.
31//!
32//! ## Default Item Structure
33//!
34//! The `DefaultItem` represents a basic list item with:
35//! - A title (main text)
36//! - A description (secondary text, optional display)
37//!
38//! ## Default Delegate
39//!
40//! The `DefaultDelegate` handles:
41//! - Rendering items with different visual states (normal, selected, dimmed)
42//! - Managing item height and spacing
43//! - Complex filter highlighting with seamless text rendering
44//!
45//! ## Styling
46//!
47//! The `DefaultItemStyles` provides comprehensive styling options:
48//! - Normal state styles for title and description
49//! - Selected state styles with borders and highlighting
50//! - Dimmed state styles for filtered-out items
51//! - Filter match highlighting styles
52//!
53//! ## Example
54//!
55//! ```rust
56//! use bubbletea_widgets::list::{DefaultItem, DefaultDelegate};
57//!
58//! let item = DefaultItem::new("Task 1", "Complete the documentation");
59//! let delegate = DefaultDelegate::new();
60//! ```
61
62use super::{Item, ItemDelegate, Model};
63use bubbletea_rs::{Cmd, Msg};
64use lipgloss_extras::prelude::*;
65
66/// Applies segment-based highlighting to a string based on match indices.
67///
68/// This function is the core of the filter highlighting system. It takes a string and character
69/// indices that should be highlighted, then applies styles to create highlighted and non-highlighted
70/// segments. The key insight is to group consecutive match indices into contiguous segments rather
71/// than styling individual characters.
72///
73/// ## Why Segment-Based Highlighting?
74///
75/// Character-by-character highlighting would create excessive ANSI escape sequences:
76/// - "Nutella" with matches [0,1,2] would become: `[style]N[reset][style]u[reset][style]t[reset]ella`
77/// - Segment-based approach creates: `[style]Nut[reset]ella` (much more efficient)
78///
79/// ## Critical Implementation Detail
80///
81/// The styles passed to this function MUST NOT contain padding or borders, as lipgloss will
82/// apply padding to each individual segment, causing extra spaces between segments. For example:
83/// - ❌ With padding: "N utella" (space inserted between segments)
84/// - ✅ Without padding: "Nutella" (segments render seamlessly)
85///
86/// Border and padding should be applied manually AFTER this function returns.
87///
88/// # Arguments
89/// * `text` - The text to apply highlighting to
90/// * `matches` - Vector of character indices that should be highlighted (from fuzzy matching)
91/// * `highlight_style` - Style to apply to matched segments (should have NO padding/borders)
92/// * `normal_style` - Style to apply to non-matched segments (should have NO padding/borders)
93///
94/// # Returns
95/// A styled string with highlighting applied to contiguous segments, ready for border/padding application
96pub(super) fn apply_character_highlighting(
97 text: &str,
98 matches: &[usize],
99 highlight_style: &Style,
100 normal_style: &Style,
101) -> String {
102 if matches.is_empty() {
103 return normal_style.render(text);
104 }
105
106 let chars: Vec<char> = text.chars().collect();
107 let mut result = String::new();
108
109 // Sort match indices and remove duplicates
110 let mut sorted_matches = matches.to_vec();
111 sorted_matches.sort_unstable();
112 sorted_matches.dedup();
113
114 // Filter out invalid indices
115 let valid_matches: Vec<usize> = sorted_matches
116 .into_iter()
117 .filter(|&idx| idx < chars.len())
118 .collect();
119
120 if valid_matches.is_empty() {
121 return normal_style.render(text);
122 }
123
124 // Group consecutive indices into segments
125 let mut segments: Vec<(usize, usize, bool)> = Vec::new(); // (start, end, is_highlighted)
126 let mut current_pos = 0;
127 let mut i = 0;
128
129 while i < valid_matches.len() {
130 let match_start = valid_matches[i];
131
132 // Add normal segment before this match if needed
133 if current_pos < match_start {
134 segments.push((current_pos, match_start, false));
135 }
136
137 // Find the end of consecutive matches
138 let mut match_end = match_start + 1;
139 while i + 1 < valid_matches.len() && valid_matches[i + 1] == valid_matches[i] + 1 {
140 i += 1;
141 match_end = valid_matches[i] + 1;
142 }
143
144 // Add highlighted segment
145 segments.push((match_start, match_end, true));
146 current_pos = match_end;
147 i += 1;
148 }
149
150 // Add final normal segment if needed
151 if current_pos < chars.len() {
152 segments.push((current_pos, chars.len(), false));
153 }
154
155 // Render each segment with appropriate styling
156 for (start, end, is_highlighted) in segments {
157 let segment: String = chars[start..end].iter().collect();
158 if !segment.is_empty() {
159 if is_highlighted {
160 result.push_str(&highlight_style.render(&segment));
161 } else {
162 result.push_str(&normal_style.render(&segment));
163 }
164 }
165 }
166
167 result
168}
169
170/// Styling configuration for default list items in various visual states.
171///
172/// This struct provides comprehensive styling options for rendering list items
173/// in different states: normal, selected, and dimmed. Each state can have
174/// different styles for both the title and description text.
175///
176/// The styling system uses adaptive colors that automatically adjust to the
177/// terminal's light or dark theme, ensuring optimal readability in any environment.
178///
179/// # Visual States
180///
181/// - **Normal**: Default appearance for unselected items
182/// - **Selected**: Highlighted appearance with left border for the current selection
183/// - **Dimmed**: Faded appearance used during filtering when filter input is empty
184///
185/// # Theme Adaptation
186///
187/// All colors use `AdaptiveColor` which automatically switches between light and
188/// dark variants based on the terminal's background color detection.
189///
190/// # Examples
191///
192/// ```rust
193/// use bubbletea_widgets::list::DefaultItemStyles;
194/// use lipgloss_extras::prelude::*;
195///
196/// // Use default styles with adaptive colors
197/// let styles = DefaultItemStyles::default();
198///
199/// // Customize specific styles
200/// let mut custom_styles = DefaultItemStyles::default();
201/// custom_styles.normal_title = Style::new()
202/// .foreground(AdaptiveColor { Light: "#333333", Dark: "#FFFFFF" })
203/// .bold(true);
204/// ```
205#[derive(Debug, Clone)]
206pub struct DefaultItemStyles {
207 /// Title style in normal (unselected) state.
208 pub normal_title: Style,
209 /// Description style in normal (unselected) state.
210 pub normal_desc: Style,
211 /// Title style when the item is selected.
212 pub selected_title: Style,
213 /// Description style when the item is selected.
214 pub selected_desc: Style,
215 /// Title style when the item is dimmed (e.g., during filtering).
216 pub dimmed_title: Style,
217 /// Description style when the item is dimmed.
218 pub dimmed_desc: Style,
219 /// Style used to highlight filter matches.
220 pub filter_match: Style,
221}
222
223impl Default for DefaultItemStyles {
224 /// Creates default styling that matches the Go bubbles library appearance.
225 ///
226 /// The default styles provide a clean, professional appearance with:
227 /// - Adaptive colors that work in both light and dark terminals
228 /// - Left border highlighting for selected items
229 /// - Consistent padding and typography
230 /// - Subtle dimming for filtered states
231 ///
232 /// # Theme Colors
233 ///
234 /// - **Normal text**: Dark text on light backgrounds, light text on dark backgrounds
235 /// - **Selected items**: Purple accent with left border
236 /// - **Dimmed items**: Muted colors during filtering
237 ///
238 /// # Examples
239 ///
240 /// ```rust
241 /// use bubbletea_widgets::list::DefaultItemStyles;
242 ///
243 /// let styles = DefaultItemStyles::default();
244 /// // Styles are now ready to use with adaptive colors
245 /// ```
246 fn default() -> Self {
247 let normal_title = Style::new()
248 .foreground(AdaptiveColor {
249 Light: "#1a1a1a",
250 Dark: "#dddddd",
251 })
252 .padding(0, 0, 0, 2);
253 let normal_desc = Style::new()
254 .foreground(AdaptiveColor {
255 Light: "#A49FA5",
256 Dark: "#777777",
257 })
258 .padding(0, 0, 0, 2);
259 let selected_title = Style::new()
260 .border_style(normal_border())
261 .border_top(false)
262 .border_right(false)
263 .border_bottom(false)
264 .border_left(true)
265 .border_left_foreground(AdaptiveColor {
266 Light: "#F793FF",
267 Dark: "#AD58B4",
268 })
269 .foreground(AdaptiveColor {
270 Light: "#EE6FF8",
271 Dark: "#EE6FF8",
272 })
273 .padding(0, 0, 0, 1);
274 let selected_desc = selected_title.clone().foreground(AdaptiveColor {
275 Light: "#F793FF",
276 Dark: "#AD58B4",
277 });
278 let dimmed_title = Style::new()
279 .foreground(AdaptiveColor {
280 Light: "#A49FA5",
281 Dark: "#777777",
282 })
283 .padding(0, 0, 0, 2);
284 let dimmed_desc = Style::new()
285 .foreground(AdaptiveColor {
286 Light: "#C2B8C2",
287 Dark: "#4D4D4D",
288 })
289 .padding(0, 0, 0, 2);
290 let filter_match = Style::new().underline(true);
291 Self {
292 normal_title,
293 normal_desc,
294 selected_title,
295 selected_desc,
296 dimmed_title,
297 dimmed_desc,
298 filter_match,
299 }
300 }
301}
302
303/// A simple list item with title and description text.
304///
305/// This struct represents a basic list item that can be used with the `DefaultDelegate`
306/// for rendering in list components. It provides a straightforward implementation of the
307/// `Item` trait with built-in support for filtering and display formatting.
308///
309/// # Structure
310///
311/// Each `DefaultItem` contains:
312/// - A **title**: The primary text displayed prominently
313/// - A **description**: Secondary text shown below the title (when enabled)
314///
315/// Both fields are always present but the description display can be controlled
316/// by the delegate's `show_description` setting.
317///
318/// # Usage
319///
320/// `DefaultItem` is designed to work seamlessly with `DefaultDelegate` and provides
321/// sensible defaults for most list use cases. For more complex item types with
322/// custom data, implement the `Item` trait directly.
323///
324/// # Examples
325///
326/// ```rust
327/// use bubbletea_widgets::list::DefaultItem;
328///
329/// // Create a simple item
330/// let item = DefaultItem::new("Task 1", "Complete the documentation");
331/// println!("{}", item); // Displays: "Task 1"
332///
333/// // Create items for a todo list
334/// let todos = vec![
335/// DefaultItem::new("Buy groceries", "Milk, bread, eggs"),
336/// DefaultItem::new("Write code", "Implement the new feature"),
337/// DefaultItem::new("Review PRs", "Check team submissions"),
338/// ];
339/// ```
340#[derive(Debug, Clone)]
341pub struct DefaultItem {
342 /// Main item text.
343 pub title: String,
344 /// Secondary item text (optional display).
345 pub desc: String,
346}
347
348impl DefaultItem {
349 /// Creates a new default item with the specified title and description.
350 ///
351 /// This constructor creates a new `DefaultItem` with the provided title and description
352 /// text. Both parameters are converted to owned `String` values for storage.
353 ///
354 /// # Arguments
355 ///
356 /// * `title` - The primary text to display for this item. This will be shown
357 /// prominently and is used for filtering operations.
358 /// * `desc` - The secondary descriptive text. This provides additional context
359 /// and is displayed below the title when `show_description` is enabled.
360 ///
361 /// # Returns
362 ///
363 /// A new `DefaultItem` instance with the specified title and description.
364 ///
365 /// # Examples
366 ///
367 /// ```rust
368 /// use bubbletea_widgets::list::DefaultItem;
369 ///
370 /// // Create a task item
371 /// let task = DefaultItem::new("Review code", "Check the pull request from yesterday");
372 ///
373 /// // Create a menu item
374 /// let menu_item = DefaultItem::new("Settings", "Configure application preferences");
375 ///
376 /// // Create an item with empty description
377 /// let simple_item = DefaultItem::new("Simple Item", "");
378 /// ```
379 pub fn new(title: &str, desc: &str) -> Self {
380 Self {
381 title: title.to_string(),
382 desc: desc.to_string(),
383 }
384 }
385}
386
387impl std::fmt::Display for DefaultItem {
388 /// Formats the item for display, showing only the title.
389 ///
390 /// This implementation provides a string representation of the item
391 /// using only the title field. The description is not included in
392 /// the display output, following the pattern where descriptions are
393 /// shown separately in list rendering.
394 ///
395 /// # Examples
396 ///
397 /// ```rust
398 /// use bubbletea_widgets::list::DefaultItem;
399 ///
400 /// let item = DefaultItem::new("My Task", "This is a description");
401 /// assert_eq!(format!("{}", item), "My Task");
402 /// ```
403 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
404 write!(f, "{}", self.title)
405 }
406}
407
408impl Item for DefaultItem {
409 /// Returns the text used for filtering this item.
410 ///
411 /// This implementation returns the item's title, which means that
412 /// filtering operations will search and match against the title text.
413 /// The description is not included in filtering to keep the search
414 /// focused on the primary item identifier.
415 ///
416 /// # Returns
417 ///
418 /// A clone of the item's title string that will be used for fuzzy
419 /// matching during filtering operations.
420 ///
421 /// # Examples
422 ///
423 /// ```rust
424 /// use bubbletea_widgets::list::{DefaultItem, Item};
425 ///
426 /// let item = DefaultItem::new("Buy groceries", "Milk, bread, eggs");
427 /// assert_eq!(item.filter_value(), "Buy groceries");
428 ///
429 /// // The filter will match against "Buy groceries", not the description
430 /// ```
431 fn filter_value(&self) -> String {
432 self.title.clone()
433 }
434}
435
436/// A delegate for rendering `DefaultItem` instances in list components.
437///
438/// This delegate provides the standard rendering logic for `DefaultItem` objects,
439/// handling different visual states, filtering highlights, and layout options.
440/// It implements the `ItemDelegate` trait to integrate seamlessly with the list
441/// component system.
442///
443/// # Features
444///
445/// - **Adaptive styling**: Automatically adjusts colors for light/dark terminals
446/// - **State rendering**: Handles normal, selected, and dimmed visual states
447/// - **Filter highlighting**: Character-level highlighting of search matches
448/// - **Flexible layout**: Configurable description display and item spacing
449/// - **Responsive design**: Adjusts rendering based on available width
450///
451/// # Configuration
452///
453/// The delegate can be customized through its public fields:
454/// - `show_description`: Controls whether descriptions are rendered below titles
455/// - `styles`: Complete styling configuration for all visual states
456///
457/// # Usage with List
458///
459/// The delegate is designed to work with the `Model<DefaultItem>` list component
460/// and handles all the rendering complexity automatically.
461///
462/// # Examples
463///
464/// ```rust
465/// use bubbletea_widgets::list::{DefaultDelegate, DefaultItem, Model};
466///
467/// // Create a delegate with default settings
468/// let delegate = DefaultDelegate::new();
469///
470/// // Customize the delegate
471/// let mut custom_delegate = DefaultDelegate::new();
472/// custom_delegate.show_description = false; // Hide descriptions
473///
474/// // Use with a list
475/// let items = vec![
476/// DefaultItem::new("Task 1", "First task description"),
477/// DefaultItem::new("Task 2", "Second task description"),
478/// ];
479/// let list = Model::new(items, delegate, 80, 24);
480/// ```
481#[derive(Debug, Clone)]
482pub struct DefaultDelegate {
483 /// Whether to show the description beneath the title.
484 pub show_description: bool,
485 /// Styling used for different visual states.
486 pub styles: DefaultItemStyles,
487 height: usize,
488 spacing: usize,
489}
490
491impl Default for DefaultDelegate {
492 /// Creates a new delegate with default configuration.
493 ///
494 /// The default delegate is configured with:
495 /// - Description display enabled
496 /// - Standard adaptive styling
497 /// - Height of 2 lines (title + description)
498 /// - 1 line spacing between items
499 ///
500 /// This configuration provides a standard list appearance that matches
501 /// the Go bubbles library defaults.
502 ///
503 /// # Examples
504 ///
505 /// ```rust
506 /// use bubbletea_widgets::list::DefaultDelegate;
507 ///
508 /// let delegate = DefaultDelegate::default();
509 /// assert_eq!(delegate.show_description, true);
510 /// ```
511 fn default() -> Self {
512 Self {
513 show_description: true,
514 styles: Default::default(),
515 height: 2,
516 spacing: 1,
517 }
518 }
519}
520impl DefaultDelegate {
521 /// Creates a new delegate with default styles and layout.
522 ///
523 /// This is equivalent to `DefaultDelegate::default()` and provides a convenient
524 /// constructor for creating a new delegate with standard settings.
525 ///
526 /// # Returns
527 ///
528 /// A new `DefaultDelegate` configured with default settings suitable for
529 /// most list use cases.
530 ///
531 /// # Examples
532 ///
533 /// ```rust
534 /// use bubbletea_widgets::list::{DefaultDelegate, DefaultItem, Model};
535 ///
536 /// let delegate = DefaultDelegate::new();
537 /// let items = vec![DefaultItem::new("Item 1", "Description 1")];
538 /// let list = Model::new(items, delegate, 80, 24);
539 /// ```
540 pub fn new() -> Self {
541 Self::default()
542 }
543}
544
545impl<I: Item + 'static> ItemDelegate<I> for DefaultDelegate {
546 /// Renders an item as a styled string for display in the list.
547 ///
548 /// This method implements the complete rendering pipeline for list items, with special
549 /// handling for filter highlighting that avoids common ANSI spacing issues.
550 ///
551 /// ## Rendering Pipeline
552 ///
553 /// 1. **State Detection**: Determine if item is selected, dimmed, or has filter matches
554 /// 2. **Filter Highlighting**: Apply character-level highlighting using segment-based approach
555 /// 3. **Style Application**: Apply colors, borders, and padding based on item state
556 /// 4. **Layout Formatting**: Combine title and description if enabled
557 ///
558 /// ## Filter Highlighting Implementation
559 ///
560 /// The filter highlighting system is complex due to lipgloss rendering behavior:
561 ///
562 /// ### Problem
563 /// Lipgloss styles with padding add spaces when applied to individual text segments.
564 /// If we pass styles with padding directly to `apply_character_highlighting`, we get:
565 /// - Input: "Nutella" with matches \[0\] (highlighting 'N')
566 /// - Expected: "│ Nutella"
567 /// - Actual: "│ N utella" (extra space between 'N' and 'utella')
568 ///
569 /// ### Solution
570 /// 1. Create "base" styles WITHOUT padding/borders for segment highlighting
571 /// 2. Apply highlighting using these clean styles via `apply_character_highlighting`
572 /// 3. Manually apply border and padding AFTER highlighting is complete
573 ///
574 /// ### Selected vs Unselected Items
575 /// - **Selected**: Left border (│) + 1 space padding + colored text
576 /// - **Unselected**: No border + 2 spaces padding + normal text color
577 ///
578 /// This approach ensures seamless text rendering while preserving visual hierarchy.
579 ///
580 /// # Arguments
581 ///
582 /// * `m` - The list model containing state and filter information
583 /// * `index` - The index of this item in the current list view
584 /// * `item` - The item to render
585 ///
586 /// # Returns
587 ///
588 /// A formatted string with ANSI styling codes. Returns empty string if list width is 0.
589 ///
590 /// # Visual States
591 ///
592 /// - **Normal**: Standard appearance with left padding
593 /// - **Selected**: Highlighted with left border and accent colors
594 /// - **Dimmed**: Faded appearance when filter input is empty
595 /// - **Filtered**: Normal or selected appearance with character-level match highlighting
596 fn render(&self, m: &Model<I>, index: usize, item: &I) -> String {
597 let title = item.to_string();
598 let desc = if let Some(di) = (item as &dyn std::any::Any).downcast_ref::<DefaultItem>() {
599 di.desc.clone()
600 } else {
601 String::new()
602 };
603
604 if m.width == 0 {
605 return String::new();
606 }
607
608 let s = &self.styles;
609 let is_selected = index == m.cursor;
610
611 // Check if we're in the special "empty filter" dimmed state
612 // This happens when user has pressed '/' to filter but hasn't typed anything yet
613 let empty_filter =
614 m.filter_state == super::FilterState::Filtering && m.filter_input.value().is_empty();
615
616 // Determine if any kind of filtering is active (typing or applied)
617 let is_filtered = matches!(
618 m.filter_state,
619 super::FilterState::Filtering | super::FilterState::FilterApplied
620 );
621
622 // Extract character match indices from the fuzzy search results
623 // These indices tell us which characters in the text should be highlighted
624 // Note: matches are only available for items that passed the filter
625 let matches = if is_filtered && index < m.filtered_items.len() {
626 Some(&m.filtered_items[index].matches)
627 } else {
628 None
629 };
630
631 let mut title_out = title.clone();
632 let mut desc_out = desc.clone();
633
634 // RENDERING BRANCH 1: Empty Filter State (Dimmed)
635 // When user presses '/' but hasn't typed anything, show all items in dimmed colors
636 if empty_filter {
637 title_out = s.dimmed_title.clone().render(&title_out);
638 desc_out = s.dimmed_desc.clone().render(&desc_out);
639 // RENDERING BRANCH 2: Selected Item (with potential highlighting)
640 // Selected items get a left border and accent colors, plus highlighting if filtered
641 } else if is_selected && m.filter_state != super::FilterState::Filtering {
642 if let Some(match_indices) = matches {
643 // SELECTED ITEM WITH FILTER HIGHLIGHTING
644 //
645 // Problem: The default selected_title style has padding(0,0,0,1) and a border.
646 // If we pass this directly to apply_character_highlighting, lipgloss applies
647 // the padding to each text segment individually, creating spaces between them:
648 // "Nutella" with 'N' highlighted becomes "│ N utella" instead of "│ Nutella"
649 //
650 // Solution: Use clean styles without padding/borders for highlighting, then
651 // manually add the border and padding afterward.
652
653 // Step 1: Create clean base styles (colors only, no padding/borders)
654 let selected_base_style = Style::new().foreground(AdaptiveColor {
655 Light: "#EE6FF8", // Selected item text color
656 Dark: "#EE6FF8",
657 });
658 let selected_desc_base_style = Style::new().foreground(AdaptiveColor {
659 Light: "#F793FF", // Selected description color
660 Dark: "#AD58B4",
661 });
662
663 // Step 2: Apply character-level highlighting using clean styles
664 let highlight_style = selected_base_style.clone().inherit(s.filter_match.clone());
665 title_out = apply_character_highlighting(
666 &title,
667 match_indices,
668 &highlight_style, // Highlighted segments: selected color + underline/bold
669 &selected_base_style, // Normal segments: just selected color
670 );
671 if !desc.is_empty() {
672 let desc_highlight_style = selected_desc_base_style
673 .clone()
674 .inherit(s.filter_match.clone());
675 desc_out = apply_character_highlighting(
676 &desc,
677 match_indices,
678 &desc_highlight_style,
679 &selected_desc_base_style,
680 );
681 }
682
683 // Step 3: Manually add border and padding to avoid lipgloss segment spacing
684 // This ensures "│ Nutella" instead of "│ N utella"
685 let border_char = "│";
686 let padding = " "; // 1 space after border for selected items
687 title_out = format!(
688 "{}{}{}",
689 Style::new()
690 .foreground(AdaptiveColor {
691 Light: "#F793FF",
692 Dark: "#AD58B4",
693 })
694 .render(border_char), // Colored border character
695 padding, // Manual spacing
696 title_out // Pre-highlighted text
697 );
698 if !desc.is_empty() {
699 desc_out = format!(
700 "{}{}{}",
701 Style::new()
702 .foreground(AdaptiveColor {
703 Light: "#F793FF",
704 Dark: "#AD58B4",
705 })
706 .render(border_char),
707 padding,
708 desc_out
709 );
710 }
711 } else {
712 // SELECTED ITEM WITHOUT FILTER HIGHLIGHTING
713 // No filter matches, so use the standard selected styles (with border and padding)
714 title_out = s.selected_title.clone().render(&title_out);
715 desc_out = s.selected_desc.clone().render(&desc_out);
716 }
717 } else {
718 // RENDERING BRANCH 3: Unselected/Normal Item (with potential highlighting)
719 // Unselected items have no border, just left padding for alignment
720 if let Some(match_indices) = matches {
721 // UNSELECTED ITEM WITH FILTER HIGHLIGHTING
722 //
723 // Same issue as selected items: normal_title style has padding(0,0,0,2).
724 // Applying this to individual segments creates: "Li n ux" instead of "Linux"
725 //
726 // Solution: Use the same approach - clean styles for highlighting,
727 // then manual padding.
728
729 // Step 1: Create clean base styles (colors only, no padding)
730 let normal_base_style = Style::new().foreground(AdaptiveColor {
731 Light: "#1a1a1a", // Normal item text color (dark on light, light on dark)
732 Dark: "#dddddd",
733 });
734 let normal_desc_base_style = Style::new().foreground(AdaptiveColor {
735 Light: "#A49FA5", // Normal description color (muted)
736 Dark: "#777777",
737 });
738
739 // Step 2: Apply character-level highlighting using clean styles
740 let highlight_style = normal_base_style.clone().inherit(s.filter_match.clone());
741 title_out = apply_character_highlighting(
742 &title,
743 match_indices,
744 &highlight_style, // Highlighted segments: normal color + underline/bold
745 &normal_base_style, // Normal segments: just normal color
746 );
747 if !desc.is_empty() {
748 let desc_highlight_style = normal_desc_base_style
749 .clone()
750 .inherit(s.filter_match.clone());
751 desc_out = apply_character_highlighting(
752 &desc,
753 match_indices,
754 &desc_highlight_style,
755 &normal_desc_base_style,
756 );
757 }
758
759 // Step 3: Apply padding manually (no border for unselected items)
760 // This ensures " Linux" instead of " Li n ux"
761 let padding = " "; // 2 spaces for normal items (to align with selected items' border+space)
762 title_out = format!("{}{}", padding, title_out);
763 if !desc.is_empty() {
764 desc_out = format!("{}{}", padding, desc_out);
765 }
766 } else {
767 // UNSELECTED ITEM WITHOUT FILTER HIGHLIGHTING
768 // No filter matches, so use the standard normal styles (with padding)
769 title_out = s.normal_title.clone().render(&title_out);
770 desc_out = s.normal_desc.clone().render(&desc_out);
771 }
772 }
773
774 // FINAL LAYOUT: Combine title and description based on delegate settings
775 // At this point, title_out and desc_out are fully styled with proper spacing:
776 // - Selected items: "│ Nutella" (border + space + text)
777 // - Unselected items: " Linux" (2 spaces + text)
778 // - Filter highlighting is seamlessly integrated without spacing artifacts
779 if self.show_description && !desc_out.is_empty() {
780 format!("{}\n{}", title_out, desc_out)
781 } else {
782 title_out
783 }
784 }
785 /// Returns the height in lines that each item occupies.
786 ///
787 /// The height depends on whether descriptions are enabled:
788 /// - With descriptions: Returns the configured height (default 2 lines)
789 /// - Without descriptions: Always returns 1 line
790 ///
791 /// This height is used by the list component for layout calculations,
792 /// viewport sizing, and scroll positioning.
793 ///
794 /// # Returns
795 ///
796 /// The number of terminal lines each item will occupy when rendered.
797 fn height(&self) -> usize {
798 if self.show_description {
799 self.height
800 } else {
801 1
802 }
803 }
804 /// Returns the number of blank lines between items.
805 ///
806 /// This spacing is added between each item in the list to improve
807 /// readability and visual separation. The default spacing is 1 line.
808 ///
809 /// # Returns
810 ///
811 /// The number of blank lines to insert between rendered items.
812 fn spacing(&self) -> usize {
813 self.spacing
814 }
815 /// Handles update messages for the delegate.
816 ///
817 /// The default delegate implementation does not require any message handling,
818 /// so this method always returns `None`. Override this method in custom
819 /// delegates that need to respond to keyboard input, timer events, or
820 /// other application messages.
821 ///
822 /// # Arguments
823 ///
824 /// * `_msg` - The message to handle (unused in default implementation)
825 /// * `_m` - Mutable reference to the list model (unused in default implementation)
826 ///
827 /// # Returns
828 ///
829 /// Always returns `None` as the default delegate requires no update commands.
830 fn update(&self, _msg: &Msg, _m: &mut Model<I>) -> Option<Cmd> {
831 None
832 }
833}