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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use tui::{
  style::{Color, Style, Modifier},
  text::{Span, Spans},
  widgets::{Paragraph, Wrap, Borders, Block},
};

use crate::fetch::fetch_status::StatusResponse;
use crate::fetch::fetch_stats::StatsResponse;

pub fn render_status_paragraph<'a>(status: &'a StatusResponse, stats: &'a StatsResponse) -> Paragraph<'a> {

  let block = Block::default()
        .borders(Borders::ALL)
        .title(Span::styled(
            "Status",
            Style::default().add_modifier(Modifier::BOLD),
        ));

    let get_color = |enabled: bool| {
      if enabled { Color::Green } else { Color::Red }
    };

    let value_style = Style::default().fg(Color::Blue).add_modifier(Modifier::BOLD);

    let coloured = |color: Color| {
      Style::default().fg(color).add_modifier(Modifier::BOLD)
    };
      

  let text = vec![
      Spans::from(vec![
        Span::styled("Running: ", Style::default()),
        Span::styled(
            format!("{}", status.running),
            Style::default().fg(get_color(status.running)).add_modifier(Modifier::BOLD)
        ),
    ]),
      Spans::from(vec![
        Span::styled("Protection Enabled: ", Style::default()),
        Span::styled(
            format!("{}", status.protection_enabled),
            Style::default().fg(get_color(status.protection_enabled)).add_modifier(Modifier::BOLD)
        ),
    ]),
      Spans::from(vec![
        Span::styled("DHCP Available: ", Style::default()),
        Span::styled(
            format!("{}", status.dhcp_available),
            Style::default().fg(get_color(status.dhcp_available)).add_modifier(Modifier::BOLD)
        ),
    ]),
    Spans::from(vec![
      Span::styled("Avg Processing Time: ", Style::default()),
      Span::styled(format!("{}ms", (stats.avg_processing_time * 1000.0) as i16), value_style),
    ]),
    Spans::from(vec![
      Span::styled("Version: ", Style::default()),
      Span::styled(status.version.to_string(), value_style),
    ]),
    Spans::from(vec![
        Span::styled("Ports: ", Style::default()),
        Span::styled(format!(":{} (DNS), :{} (HTTP)", status.dns_port, status.http_port), value_style),
    ]),
    Spans::from(vec![
      Span::styled("Total Queries: ", Style::default()),
      Span::styled(stats.num_dns_queries.to_string(), coloured(Color::Green)),
    ]),
    Spans::from(vec![
      Span::styled("Filtered: ", Style::default()),
      Span::styled(stats.num_blocked_filtering.to_string(), coloured(Color::Yellow)),
    ]),
    Spans::from(vec![
      Span::styled("Malware Blocked: ", Style::default()),
      Span::styled(stats.num_replaced_safebrowsing.to_string(), coloured(Color::Red)),
    ]),
    Spans::from(vec![
      Span::styled("Parental Controls: ", Style::default()),
      Span::styled(stats.num_replaced_parental.to_string(), coloured(Color::Magenta)),
    ]),
    Spans::from(vec![
      Span::styled("Safe Search: ", Style::default()),
      Span::styled(stats.num_replaced_safesearch.to_string(), coloured(Color::Cyan)),
    ]),
  ];
  Paragraph::new(text)
    .wrap(Wrap { trim: true })
    .block(block)
}