summaryrefslogtreecommitdiffstats
path: root/src/display/components/total_bandwidth.rs
blob: ba78d2dd96cdfb4728826bcf34882208bd446b18 (plain)
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
use ::tui::backend::Backend;
use ::tui::layout::{Alignment, Rect};
use ::tui::style::{Color, Modifier, Style};
use ::tui::terminal::Frame;
use ::tui::widgets::{Block, Borders, Paragraph, Text, Widget};

use crate::display::{DisplayBandwidth, UIState};

pub struct TotalBandwidth<'a> {
    pub state: &'a UIState,
    pub paused: bool,
}

impl<'a> TotalBandwidth<'a> {
    pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) {
        let title_text = {
            let paused_str = if self.paused { "[PAUSED]" } else { "" };
            let color = if self.paused {
                Color::Yellow
            } else {
                Color::Green
            };

            [Text::styled(
                format!(
                    " Total Rate Up / Down: {} / {} {}",
                    DisplayBandwidth(self.state.total_bytes_uploaded as f64),
                    DisplayBandwidth(self.state.total_bytes_downloaded as f64),
                    paused_str
                ),
                Style::default().fg(color).modifier(Modifier::BOLD),
            )]
        };
        Paragraph::new(title_text.iter())
            .block(Block::default().borders(Borders::NONE))
            .alignment(Alignment::Left)
            .render(frame, rect);
    }
}