Skip to main content

adguardian/widgets/
list.rs

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