1
0
mirror of https://github.com/danog/ytop.git synced 2024-11-26 20:15:03 +01:00

WIP net widget

This commit is contained in:
Caleb Bassi 2020-01-11 17:05:37 -08:00
parent 03136753a4
commit 4b9958df1b

View File

@ -1,7 +1,9 @@
use num_rational::Ratio;
use psutil::network;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::widgets::Widget;
use tui::style::{Color, Style};
use tui::widgets::{Sparkline, Widget};
use crate::update::UpdatableWidget;
use crate::widgets::block;
@ -11,6 +13,14 @@ pub struct NetWidget {
update_interval: Ratio<u64>,
interfaces: String,
bytes_recv: Vec<u64>,
bytes_sent: Vec<u64>,
total_bytes_recv: u64,
total_bytes_sent: u64,
collector: network::NetIoCountersCollector,
}
impl NetWidget {
@ -20,12 +30,35 @@ impl NetWidget {
update_interval: Ratio::from_integer(1),
interfaces,
bytes_recv: Vec::new(),
bytes_sent: Vec::new(),
total_bytes_recv: 0,
total_bytes_sent: 0,
collector: network::NetIoCountersCollector::default(),
}
}
}
impl UpdatableWidget for NetWidget {
fn update(&mut self) {}
fn update(&mut self) {
let io_counters = self.collector.net_io_counters().unwrap();
if self.total_bytes_recv == 0 {
self.bytes_recv.push(0);
self.bytes_sent.push(0);
} else {
self.bytes_recv
.push(io_counters.bytes_recv() - self.total_bytes_recv);
self.bytes_sent
.push(io_counters.bytes_sent() - self.total_bytes_sent);
}
self.total_bytes_recv = io_counters.bytes_recv();
self.total_bytes_sent = io_counters.bytes_sent();
}
fn get_update_interval(&self) -> Ratio<u64> {
self.update_interval
@ -35,5 +68,36 @@ impl UpdatableWidget for NetWidget {
impl Widget for NetWidget {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
block::new().title(&self.title).draw(area, buf);
let x = area.x + 1;
let y = area.y + 1;
let height = area.height - 2;
let width = area.width - 2;
let top_half = Rect {
x,
y,
width,
height: height / 2,
};
let bottom_half = Rect {
x,
y: y + 1 + (height / 2),
width,
height: height / 2,
};
Sparkline::default()
.data(&self.bytes_recv)
.max(*self.bytes_recv.iter().max().unwrap())
.style(Style::default().fg(Color::Red))
.draw(top_half, buf);
Sparkline::default()
.data(&self.bytes_sent)
.max(*self.bytes_sent.iter().max().unwrap())
.style(Style::default().fg(Color::Red))
.draw(bottom_half, buf);
}
}