Skip to main content

adguardian/widgets/
gauge.rs

1use tui::{
2  style::{Color, Modifier, Style},
3  text::Span,
4  widgets::{Block, Borders, Gauge},
5};
6
7use crate::fetch::fetch_stats::StatsResponse;
8
9pub fn make_gauge(stats: &StatsResponse) -> Gauge<'_> {
10  let total_blocked = stats.num_blocked_filtering
11    + stats.num_replaced_parental
12    + stats.num_replaced_safebrowsing
13    + stats.num_replaced_safesearch;
14
15  // `max(1)` avoids a divide-by-zero, and the clamp keeps it in the 0..=100
16  // range that `Gauge::percent` requires (it panics otherwise)
17  let percent =
18    (total_blocked as f64 / stats.num_dns_queries.max(1) as f64 * 100.0).min(100.0) as u16;
19
20  let label = format!(
21    "Blocked {} out of {} ({}%)",
22    total_blocked, stats.num_dns_queries, percent
23  );
24
25  Gauge::default()
26    .block(
27      Block::default()
28        .title(Span::styled(
29          "Block Percentage",
30          Style::default().add_modifier(Modifier::BOLD),
31        ))
32        .borders(Borders::ALL),
33    )
34    .gauge_style(Style::default().fg(Color::Red).bg(Color::Green))
35    .percent(percent)
36    .label(label)
37}