Skip to main content

rusticity_term/
common.rs

1use chrono::{DateTime, Utc};
2use ratatui::{prelude::*, widgets::*};
3use std::collections::HashMap;
4use std::sync::OnceLock;
5
6use crate::ui::{filter_area, styles};
7
8pub type ColumnId = &'static str;
9
10static I18N: OnceLock<HashMap<String, String>> = OnceLock::new();
11
12pub fn set_i18n(map: HashMap<String, String>) {
13    I18N.set(map).ok();
14}
15
16pub fn t(key: &str) -> String {
17    I18N.get()
18        .and_then(|map| map.get(key))
19        .cloned()
20        .unwrap_or_else(|| key.to_string())
21}
22
23pub fn translate_column(key: &str, default: &str) -> String {
24    let translated = t(key);
25    if translated == key {
26        default.to_string()
27    } else {
28        translated
29    }
30}
31
32// Width for UTC timestamp format: "YYYY-MM-DD HH:MM:SS (UTC)"
33pub const UTC_TIMESTAMP_WIDTH: u16 = 27;
34
35pub fn format_timestamp(dt: &DateTime<Utc>) -> String {
36    format!("{} (UTC)", dt.format("%Y-%m-%d %H:%M:%S"))
37}
38
39pub fn format_optional_timestamp(dt: Option<DateTime<Utc>>) -> String {
40    dt.map(|t| format_timestamp(&t))
41        .unwrap_or_else(|| "-".to_string())
42}
43
44pub fn format_iso_timestamp(iso_string: &str) -> String {
45    if iso_string.is_empty() {
46        return "-".to_string();
47    }
48
49    // Parse ISO 8601 format (e.g., "2024-01-01T12:30:45.123Z")
50    if let Ok(dt) = DateTime::parse_from_rfc3339(iso_string) {
51        format_timestamp(&dt.with_timezone(&Utc))
52    } else {
53        iso_string.to_string()
54    }
55}
56
57pub fn format_unix_timestamp(unix_string: &str) -> String {
58    if unix_string.is_empty() {
59        return "-".to_string();
60    }
61
62    if let Ok(timestamp) = unix_string.parse::<i64>() {
63        if let Some(dt) = DateTime::from_timestamp(timestamp, 0) {
64            format_timestamp(&dt)
65        } else {
66            unix_string.to_string()
67        }
68    } else {
69        unix_string.to_string()
70    }
71}
72
73#[derive(Debug, Clone, Copy, PartialEq)]
74pub enum ColumnType {
75    String,
76    Number,
77    DateTime,
78    Boolean,
79}
80
81pub fn format_bytes(bytes: i64) -> String {
82    const KB: i64 = 1000;
83    const MB: i64 = KB * 1000;
84    const GB: i64 = MB * 1000;
85    const TB: i64 = GB * 1000;
86
87    if bytes >= TB {
88        format!("{:.2} TB", bytes as f64 / TB as f64)
89    } else if bytes >= GB {
90        format!("{:.2} GB", bytes as f64 / GB as f64)
91    } else if bytes >= MB {
92        format!("{:.2} MB", bytes as f64 / MB as f64)
93    } else if bytes >= KB {
94        format!("{:.2} KB", bytes as f64 / KB as f64)
95    } else {
96        format!("{} B", bytes)
97    }
98}
99
100pub fn format_memory_mb(mb: i32) -> String {
101    if mb >= 1024 {
102        format!("{} GB", mb / 1024)
103    } else {
104        format!("{} MB", mb)
105    }
106}
107
108pub fn format_duration_seconds(seconds: i32) -> String {
109    if seconds == 0 {
110        return "0s".to_string();
111    }
112
113    let days = seconds / 86400;
114    let hours = (seconds % 86400) / 3600;
115    let minutes = (seconds % 3600) / 60;
116    let secs = seconds % 60;
117
118    let mut parts = Vec::new();
119    if days > 0 {
120        parts.push(format!("{}d", days));
121    }
122    if hours > 0 {
123        parts.push(format!("{}h", hours));
124    }
125    if minutes > 0 {
126        parts.push(format!("{}m", minutes));
127    }
128    if secs > 0 {
129        parts.push(format!("{}s", secs));
130    }
131
132    parts.join(" ")
133}
134
135pub fn border_style(is_active: bool) -> Style {
136    if is_active {
137        styles::active_border()
138    } else {
139        Style::default()
140    }
141}
142
143pub fn render_scrollbar(frame: &mut Frame, area: Rect, total: usize, position: usize) {
144    if total == 0 {
145        return;
146    }
147    let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
148        .begin_symbol(Some("↑"))
149        .end_symbol(Some("↓"));
150    let mut state = ScrollbarState::new(total).position(position);
151    frame.render_stateful_widget(scrollbar, area, &mut state);
152}
153
154pub fn render_vertical_scrollbar(frame: &mut Frame, area: Rect, total: usize, position: usize) {
155    render_scrollbar(frame, area, total, position);
156}
157
158pub fn render_horizontal_scrollbar(frame: &mut Frame, area: Rect, position: usize, total: usize) {
159    let scrollbar = Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
160        .begin_symbol(Some("◀"))
161        .end_symbol(Some("▶"));
162    let mut state = ScrollbarState::new(total).position(position);
163    frame.render_stateful_widget(scrollbar, area, &mut state);
164}
165
166pub fn render_pagination(current: usize, total: usize) -> String {
167    if total == 0 {
168        return "[1]".to_string();
169    }
170    if total <= 10 {
171        return (0..total)
172            .map(|i| {
173                if i == current {
174                    format!("[{}]", i + 1)
175                } else {
176                    format!("{}", i + 1)
177                }
178            })
179            .collect::<Vec<_>>()
180            .join(" ");
181    }
182    let start = current.saturating_sub(4);
183    let end = (start + 9).min(total);
184    let start = if end == total {
185        total.saturating_sub(9)
186    } else {
187        start
188    };
189    (start..end)
190        .map(|i| {
191            if i == current {
192                format!("[{}]", i + 1)
193            } else {
194                format!("{}", i + 1)
195            }
196        })
197        .collect::<Vec<_>>()
198        .join(" ")
199}
200
201/// Renders pagination with unknown total (infinite pagination)
202/// Shows: 1 ... 6 7 8 9 [10] 11 ...
203pub fn render_infinite_pagination(current: usize) -> String {
204    let mut parts = Vec::new();
205
206    // Show 4 pages before current (or from page 1)
207    let start = current.saturating_sub(4);
208
209    // If start > 1, show page 1 and ...
210    if start > 1 {
211        parts.push("1".to_string());
212        parts.push("...".to_string());
213    }
214
215    // Show pages from start to current
216    for i in start..current {
217        parts.push(format!("{}", i + 1));
218    }
219
220    // Current page
221    parts.push(format!("[{}]", current + 1));
222
223    // Show 1 page after current
224    parts.push(format!("{}", current + 2));
225
226    // Always show ... at the end to indicate more pages
227    parts.push("...".to_string());
228
229    parts.join(" ")
230}
231
232pub fn render_pagination_text(current: usize, total: usize) -> String {
233    render_pagination(current, total)
234}
235
236pub fn render_dropdown<T: AsRef<str>>(
237    frame: &mut ratatui::Frame,
238    items: &[T],
239    selected_index: usize,
240    filter_area: ratatui::prelude::Rect,
241    controls_after_width: u16,
242) {
243    use ratatui::prelude::*;
244    use ratatui::widgets::{Block, BorderType, Borders, Clear, List, ListItem};
245
246    let max_width = items
247        .iter()
248        .map(|item| item.as_ref().len())
249        .max()
250        .unwrap_or(10) as u16
251        + 4;
252
253    let dropdown_items: Vec<ListItem> = items
254        .iter()
255        .enumerate()
256        .map(|(idx, item)| {
257            let style = if idx == selected_index {
258                Style::default().fg(Color::Yellow).bold()
259            } else {
260                Style::default().fg(Color::White)
261            };
262            ListItem::new(format!(" {} ", item.as_ref())).style(style)
263        })
264        .collect();
265
266    let dropdown_height = dropdown_items.len() as u16 + 2;
267    let dropdown_width = max_width;
268    let dropdown_x = filter_area
269        .x
270        .saturating_add(filter_area.width)
271        .saturating_sub(controls_after_width + dropdown_width);
272
273    let dropdown_area = Rect {
274        x: dropdown_x,
275        y: filter_area.y + filter_area.height,
276        width: dropdown_width,
277        height: dropdown_height.min(10),
278    };
279
280    // Clear the background first
281    frame.render_widget(Clear, dropdown_area);
282
283    frame.render_widget(
284        List::new(dropdown_items)
285            .block(
286                Block::default()
287                    .borders(Borders::ALL)
288                    .border_type(BorderType::Rounded)
289                    .border_style(Style::default().fg(Color::Yellow)),
290            )
291            .style(Style::default().bg(Color::Black)),
292        dropdown_area,
293    );
294}
295
296pub struct FilterConfig<'a> {
297    pub text: &'a str,
298    pub placeholder: &'a str,
299    pub is_active: bool,
300    pub right_content: Vec<(&'a str, &'a str)>,
301    pub area: Rect,
302}
303
304pub struct FilterAreaConfig<'a> {
305    pub filter_text: &'a str,
306    pub placeholder: &'a str,
307    pub mode: crate::keymap::Mode,
308    pub input_focus: FilterFocusType,
309    pub controls: Vec<FilterControl>,
310    pub area: Rect,
311}
312
313#[derive(Debug, Clone, Copy, PartialEq, Default)]
314pub enum SortDirection {
315    #[default]
316    Asc,
317    Desc,
318}
319
320impl SortDirection {
321    pub fn as_str(&self) -> &'static str {
322        match self {
323            SortDirection::Asc => "ASC",
324            SortDirection::Desc => "DESC",
325        }
326    }
327}
328
329#[derive(Debug, Clone, Copy, PartialEq, Default)]
330pub enum InputFocus {
331    #[default]
332    Filter,
333    Pagination,
334    Dropdown(&'static str),
335    Checkbox(&'static str),
336}
337
338impl InputFocus {
339    pub fn next(&self, controls: &[InputFocus]) -> Self {
340        if controls.is_empty() {
341            return *self;
342        }
343        let idx = controls.iter().position(|f| f == self).unwrap_or(0);
344        controls[(idx + 1) % controls.len()]
345    }
346
347    pub fn prev(&self, controls: &[InputFocus]) -> Self {
348        if controls.is_empty() {
349            return *self;
350        }
351        let idx = controls.iter().position(|f| f == self).unwrap_or(0);
352        controls[(idx + controls.len() - 1) % controls.len()]
353    }
354
355    /// Navigate to next page when pagination is focused
356    pub fn handle_page_down(
357        &self,
358        selected: &mut usize,
359        scroll_offset: &mut usize,
360        page_size: usize,
361        filtered_count: usize,
362    ) {
363        if *self == InputFocus::Pagination {
364            let max_offset = filtered_count.saturating_sub(page_size);
365            *selected = (*selected + page_size).min(max_offset);
366            *scroll_offset = *selected;
367        }
368    }
369
370    /// Navigate to previous page when pagination is focused
371    pub fn handle_page_up(
372        &self,
373        selected: &mut usize,
374        scroll_offset: &mut usize,
375        page_size: usize,
376    ) {
377        if *self == InputFocus::Pagination {
378            *selected = selected.saturating_sub(page_size);
379            *scroll_offset = *selected;
380        }
381    }
382}
383
384pub trait CyclicEnum: Copy + PartialEq + Sized + 'static {
385    const ALL: &'static [Self];
386
387    fn next(&self) -> Self {
388        let idx = Self::ALL.iter().position(|x| x == self).unwrap_or(0);
389        Self::ALL[(idx + 1) % Self::ALL.len()]
390    }
391
392    fn prev(&self) -> Self {
393        let idx = Self::ALL.iter().position(|x| x == self).unwrap_or(0);
394        Self::ALL[(idx + Self::ALL.len() - 1) % Self::ALL.len()]
395    }
396}
397
398#[derive(PartialEq)]
399pub enum FilterFocusType {
400    Input,
401    Control(usize),
402}
403
404pub struct FilterControl {
405    pub text: String,
406    pub is_focused: bool,
407    pub style: ratatui::style::Style,
408}
409
410pub fn render_filter_area(frame: &mut Frame, config: FilterAreaConfig) {
411    use crate::keymap::Mode;
412    use crate::ui::get_cursor;
413    use ratatui::prelude::*;
414
415    let cursor = get_cursor(
416        config.mode == Mode::FilterInput && config.input_focus == FilterFocusType::Input,
417    );
418    let filter_width = config.area.width.saturating_sub(4) as usize;
419
420    // Calculate controls text
421    let controls_text: String = config
422        .controls
423        .iter()
424        .map(|c| c.text.as_str())
425        .collect::<Vec<_>>()
426        .join(" ⋮ ");
427    let controls_len = controls_text.len();
428
429    let placeholder_len = config.placeholder.len();
430    let content_len =
431        if config.filter_text.is_empty() && config.mode != Mode::FilterInput {
432            placeholder_len
433        } else {
434            config.filter_text.len()
435        } + if config.mode == Mode::FilterInput && config.input_focus == FilterFocusType::Input {
436            cursor.len()
437        } else {
438            0
439        };
440
441    let available_space = filter_width.saturating_sub(controls_len + 1);
442
443    let mut line_spans = vec![];
444    if config.filter_text.is_empty() {
445        if config.mode == Mode::FilterInput {
446            line_spans.push(Span::raw(""));
447        } else {
448            line_spans.push(Span::styled(
449                config.placeholder,
450                Style::default().fg(Color::DarkGray),
451            ));
452        }
453    } else {
454        line_spans.push(Span::raw(config.filter_text));
455    }
456
457    if config.mode == Mode::FilterInput && config.input_focus == FilterFocusType::Input {
458        line_spans.push(Span::styled(cursor, Style::default().fg(Color::Yellow)));
459    }
460
461    if content_len < available_space {
462        line_spans.push(Span::raw(" ".repeat(available_space - content_len)));
463    }
464
465    if config.mode == Mode::FilterInput {
466        for control in &config.controls {
467            line_spans.push(Span::raw(" ⋮ "));
468            line_spans.push(Span::styled(&control.text, control.style));
469        }
470    } else {
471        line_spans.push(Span::styled(
472            format!(" ⋮ {}", controls_text),
473            Style::default(),
474        ));
475    }
476
477    let filter = filter_area(line_spans, config.mode == Mode::FilterInput);
478    frame.render_widget(filter, config.area);
479}
480
481#[cfg(test)]
482mod tests {
483    use super::*;
484    use chrono::TimeZone;
485
486    #[test]
487    fn test_format_timestamp() {
488        let dt = Utc.with_ymd_and_hms(2025, 11, 12, 14, 30, 45).unwrap();
489        assert_eq!(format_timestamp(&dt), "2025-11-12 14:30:45 (UTC)");
490    }
491
492    #[test]
493    fn test_format_optional_timestamp_some() {
494        let dt = Utc.with_ymd_and_hms(2025, 11, 12, 14, 30, 45).unwrap();
495        assert_eq!(
496            format_optional_timestamp(Some(dt)),
497            "2025-11-12 14:30:45 (UTC)"
498        );
499    }
500
501    #[test]
502    fn test_format_optional_timestamp_none() {
503        assert_eq!(format_optional_timestamp(None), "-");
504    }
505
506    #[test]
507    fn test_format_bytes() {
508        assert_eq!(format_bytes(500), "500 B");
509        assert_eq!(format_bytes(1500), "1.50 KB");
510        assert_eq!(format_bytes(1_500_000), "1.50 MB");
511        assert_eq!(format_bytes(1_500_000_000), "1.50 GB");
512        assert_eq!(format_bytes(1_500_000_000_000), "1.50 TB");
513    }
514
515    #[test]
516    fn test_format_duration_seconds_zero() {
517        assert_eq!(format_duration_seconds(0), "0s");
518    }
519
520    #[test]
521    fn test_render_infinite_pagination_page_1() {
522        // Page 1: [1] 2 ...
523        let result = render_infinite_pagination(0);
524        assert_eq!(result, "[1] 2 ...");
525    }
526
527    #[test]
528    fn test_render_infinite_pagination_page_5() {
529        // Page 5: 1 2 3 4 [5] 6 ...
530        let result = render_infinite_pagination(4);
531        assert_eq!(result, "1 2 3 4 [5] 6 ...");
532    }
533
534    #[test]
535    fn test_render_infinite_pagination_page_6() {
536        // Page 6: 2 3 4 5 [6] 7 ... (start=2, no need for "1 ...")
537        let result = render_infinite_pagination(5);
538        assert_eq!(result, "2 3 4 5 [6] 7 ...");
539    }
540
541    #[test]
542    fn test_render_infinite_pagination_page_7() {
543        // Page 7: 1 ... 3 4 5 6 [7] 8 ...
544        let result = render_infinite_pagination(6);
545        assert_eq!(result, "1 ... 3 4 5 6 [7] 8 ...");
546    }
547
548    #[test]
549    fn test_render_infinite_pagination_page_10() {
550        // Page 10: 1 ... 6 7 8 9 [10] 11 ...
551        let result = render_infinite_pagination(9);
552        assert_eq!(result, "1 ... 6 7 8 9 [10] 11 ...");
553    }
554
555    #[test]
556    fn test_render_infinite_pagination_page_100() {
557        // Page 100: 1 ... 96 97 98 99 [100] 101 ...
558        let result = render_infinite_pagination(99);
559        assert_eq!(result, "1 ... 96 97 98 99 [100] 101 ...");
560    }
561
562    #[test]
563    fn test_format_duration_seconds_only_seconds() {
564        assert_eq!(format_duration_seconds(30), "30s");
565    }
566
567    #[test]
568    fn test_format_duration_seconds_minutes_and_seconds() {
569        assert_eq!(format_duration_seconds(120), "2m");
570        assert_eq!(format_duration_seconds(150), "2m 30s");
571    }
572
573    #[test]
574    fn test_format_duration_seconds_hours() {
575        assert_eq!(format_duration_seconds(3630), "1h 30s");
576        assert_eq!(format_duration_seconds(10800), "3h");
577    }
578
579    #[test]
580    fn test_format_duration_seconds_days() {
581        assert_eq!(format_duration_seconds(90061), "1d 1h 1m 1s");
582        assert_eq!(format_duration_seconds(345600), "4d");
583    }
584
585    #[test]
586    fn test_format_duration_seconds_complex() {
587        assert_eq!(format_duration_seconds(1800), "30m");
588        assert_eq!(format_duration_seconds(86400), "1d");
589    }
590
591    #[test]
592    fn test_render_pagination_single_page() {
593        assert_eq!(render_pagination(0, 1), "[1]");
594    }
595
596    #[test]
597    fn test_render_pagination_two_pages() {
598        assert_eq!(render_pagination(0, 2), "[1] 2");
599        assert_eq!(render_pagination(1, 2), "1 [2]");
600    }
601
602    #[test]
603    fn test_render_pagination_ten_pages() {
604        assert_eq!(render_pagination(0, 10), "[1] 2 3 4 5 6 7 8 9 10");
605        assert_eq!(render_pagination(5, 10), "1 2 3 4 5 [6] 7 8 9 10");
606        assert_eq!(render_pagination(9, 10), "1 2 3 4 5 6 7 8 9 [10]");
607    }
608
609    #[test]
610    fn test_format_memory_mb() {
611        assert_eq!(format_memory_mb(128), "128 MB");
612        assert_eq!(format_memory_mb(512), "512 MB");
613        assert_eq!(format_memory_mb(1024), "1 GB");
614        assert_eq!(format_memory_mb(2048), "2 GB");
615    }
616
617    #[test]
618    fn test_render_pagination_many_pages() {
619        assert_eq!(render_pagination(0, 20), "[1] 2 3 4 5 6 7 8 9");
620        assert_eq!(render_pagination(5, 20), "2 3 4 5 [6] 7 8 9 10");
621        assert_eq!(render_pagination(15, 20), "12 13 14 15 [16] 17 18 19 20");
622        assert_eq!(render_pagination(19, 20), "12 13 14 15 16 17 18 19 [20]");
623    }
624
625    #[test]
626    fn test_render_pagination_zero_total() {
627        assert_eq!(render_pagination(0, 0), "[1]");
628    }
629
630    #[test]
631    fn test_render_dropdown_items_format() {
632        let items = ["us-east-1", "us-west-2", "eu-west-1"];
633        assert_eq!(items.len(), 3);
634        assert_eq!(items[0], "us-east-1");
635        assert_eq!(items[2], "eu-west-1");
636    }
637
638    #[test]
639    fn test_render_dropdown_selected_index() {
640        let items = ["item1", "item2", "item3"];
641        let selected = 1;
642        assert_eq!(items[selected], "item2");
643    }
644
645    #[test]
646    fn test_render_dropdown_controls_after_width() {
647        let pagination_len = 10;
648        let separator = 3;
649        let controls_after = pagination_len + separator;
650        assert_eq!(controls_after, 13);
651    }
652
653    #[test]
654    fn test_render_dropdown_multiple_controls_after() {
655        let view_nested_width = 15;
656        let pagination_len = 10;
657        let controls_after = view_nested_width + 3 + pagination_len + 3;
658        assert_eq!(controls_after, 31);
659    }
660
661    #[test]
662    fn test_render_dropdown_clears_background() {
663        // This test verifies that render_dropdown uses Clear widget
664        // The actual rendering is tested via integration tests
665        // Here we just verify the function can be called with valid parameters
666        use ratatui::backend::TestBackend;
667        use ratatui::Terminal;
668
669        let backend = TestBackend::new(80, 24);
670        let mut terminal = Terminal::new(backend).unwrap();
671
672        terminal
673            .draw(|frame| {
674                let area = ratatui::prelude::Rect {
675                    x: 0,
676                    y: 0,
677                    width: 80,
678                    height: 3,
679                };
680                let items = ["Running", "Stopped", "Terminated"];
681                render_dropdown(frame, &items, 0, area, 10);
682            })
683            .unwrap();
684
685        // If we get here without panic, the function works correctly
686        // The Clear widget is used internally to clear the background
687    }
688}
689
690pub fn render_filter(frame: &mut Frame, config: FilterConfig) {
691    let cursor = if config.is_active { "█" } else { "" };
692    let content = if config.text.is_empty() && !config.is_active {
693        config.placeholder
694    } else {
695        config.text
696    };
697
698    let right_text = config
699        .right_content
700        .iter()
701        .map(|(k, v)| format!("{}: {}", k, v))
702        .collect::<Vec<_>>()
703        .join(" ⋮ ");
704
705    let width = (config.area.width as usize).saturating_sub(4);
706    let right_len = right_text.len();
707    let content_len = content.len() + if config.is_active { cursor.len() } else { 0 };
708    let available = width.saturating_sub(right_len + 3);
709
710    let display = if content_len > available {
711        &content[content_len.saturating_sub(available)..]
712    } else {
713        content
714    };
715
716    let style = if config.is_active {
717        styles::yellow()
718    } else {
719        styles::placeholder()
720    };
721
722    let mut spans = vec![Span::styled(display, style)];
723    if config.is_active {
724        spans.push(Span::styled(cursor, styles::cursor()));
725    }
726
727    let padding = " ".repeat(
728        width
729            .saturating_sub(display.len())
730            .saturating_sub(if config.is_active { cursor.len() } else { 0 })
731            .saturating_sub(right_len)
732            .saturating_sub(3),
733    );
734
735    spans.push(Span::raw(padding));
736    spans.push(Span::styled(format!(" {}", right_text), styles::cyan()));
737
738    frame.render_widget(
739        Paragraph::new(Line::from(spans)).block(
740            Block::default()
741                .borders(Borders::ALL)
742                .border_style(border_style(config.is_active)),
743        ),
744        config.area,
745    );
746}
747
748#[derive(Debug, Clone, Copy, PartialEq)]
749pub enum PageSize {
750    Ten,
751    TwentyFive,
752    Fifty,
753    OneHundred,
754}
755
756/// Generic helper to filter items by a field that matches a filter string (case-insensitive contains)
757pub fn filter_by_field<'a, T, F>(items: &'a [T], filter: &str, get_field: F) -> Vec<&'a T>
758where
759    F: Fn(&T) -> &str,
760{
761    if filter.is_empty() {
762        items.iter().collect()
763    } else {
764        let filter_lower = filter.to_lowercase();
765        items
766            .iter()
767            .filter(|item| get_field(item).to_lowercase().contains(&filter_lower))
768            .collect()
769    }
770}
771
772/// Generic helper to filter items by multiple fields (case-insensitive contains on any field)
773pub fn filter_by_fields<'a, T, F>(items: &'a [T], filter: &str, get_fields: F) -> Vec<&'a T>
774where
775    F: Fn(&T) -> Vec<&str>,
776{
777    if filter.is_empty() {
778        items.iter().collect()
779    } else {
780        let filter_lower = filter.to_lowercase();
781        items
782            .iter()
783            .filter(|item| {
784                get_fields(item)
785                    .iter()
786                    .any(|field| field.to_lowercase().contains(&filter_lower))
787            })
788            .collect()
789    }
790}