adguardian/widgets/
list.rs

1
2use crate::fetch::fetch_stats::DomainData;
3
4
5use tui::{
6  text::{Span, Spans},
7  widgets::{Block, Borders, List, ListItem},
8  style::{Color, Style, Modifier},
9};
10
11fn truncate(text: &str, width: usize) -> String {
12  if text.chars().count() <= width {
13      text.to_string()
14  } else {
15      text.chars().take(width - 3).collect::<String>() + "..."
16  }
17}
18
19pub fn make_list<'a>(title: &'a str, data: &[DomainData], color: Color, width: u16) -> List<'a> {
20  let items: Vec<ListItem> = data
21      .iter()
22      .map(|data| {
23
24          let name = Span::raw(format!(" {}", truncate(&data.name, width as usize / 4 - 12)));
25          let count = Span::styled(format!(" ({})", data.count), Style::default().fg(color).add_modifier(Modifier::BOLD));
26          ListItem::new(Spans::from(vec![name, count]))
27      })
28      .collect();
29
30  List::new(items)
31      .block(Block::default().borders(Borders::ALL)
32      .title(Span::styled(
33        title,
34        Style::default().add_modifier(Modifier::BOLD),
35      )))
36      .highlight_style(Style::default().add_modifier(Modifier::BOLD))
37}
38