1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// filters.rs

use tui::{
  text::{Span, Spans},
  widgets::{Block, Borders, List, ListItem},
  style::{Color, Style, Modifier},
};

use crate::fetch::fetch_filters::Filter;

fn truncate(text: &str, width: usize) -> String {
  if text.chars().count() <= width {
      text.to_string()
  } else {
      text.chars().take(width - 3).collect::<String>() + "..."
  }
}

pub fn make_filters_list(filters: &[Filter], width: u16) -> List {
  let items: Vec<ListItem> = filters
    .iter()
    .map(|filter| {
        let (status_text, color) = if filter.enabled {
            ("✔", Color::Green)
        } else {
            ("✘", Color::Red)
        };
        let status = Span::styled(status_text, Style::default().fg(color));
        let rule_count = Span::styled(format!(" ({})", filter.rules_count), Style::default().fg(Color::Magenta).add_modifier(Modifier::BOLD));
        let name = Span::raw(format!(" {}", truncate(&filter.name, width as usize / 4 - 12)));
        let content = Spans::from(vec![status, name, rule_count]);
        ListItem::new(content)
    })
    .collect();

  List::new(items)
      .block(
          Block::default()
              .borders(Borders::ALL)
              .title(Span::styled(
                "Filters",
                Style::default().add_modifier(Modifier::BOLD),
              )),
      )
}