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

WIP mem widget

This commit is contained in:
Caleb Bassi 2019-07-28 10:49:01 -07:00
parent 266e62c26d
commit 43e9cc0b47

View File

@ -1,14 +1,27 @@
use futures::try_join;
use heim::memory;
use num_rational::Ratio; use num_rational::Ratio;
use tui::buffer::Buffer; use tui::buffer::Buffer;
use tui::layout::Rect; use tui::layout::Rect;
use tui::widgets::Widget; use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, Marker, Widget};
use super::block; use super::block;
#[derive(Default)]
struct MemData {
total: u64,
used: u64,
percents: Vec<(f64, f64)>,
}
pub struct MemWidget { pub struct MemWidget {
title: String, title: String,
pub update_interval: Ratio<u64>, pub update_interval: Ratio<u64>,
update_count: f64, update_count: f64,
main: MemData,
swap: MemData,
} }
impl MemWidget { impl MemWidget {
@ -17,16 +30,53 @@ impl MemWidget {
title: " Memory Usage ".to_string(), title: " Memory Usage ".to_string(),
update_interval, update_interval,
update_count: 0.0, update_count: 0.0,
main: MemData::default(),
swap: MemData::default(),
} }
} }
pub async fn update(&mut self) { pub async fn update(&mut self) {
self.update_count += 1.0; self.update_count += 1.0;
let main = memory::memory();
let swap = memory::swap();
let (main, swap) = try_join!(main, swap).unwrap();
self.main.total = main.total().get();
self.main.used = self.main.total - main.available().get();
self.main.percents.push((
self.update_count,
self.main.used as f64 / self.main.total as f64,
));
self.swap.total = swap.total().get();
self.swap.used = swap.used().get();
self.swap.percents.push((
self.update_count,
self.swap.used as f64 / self.swap.total as f64,
));
} }
} }
impl Widget for MemWidget { impl Widget for MemWidget {
fn draw(&mut self, area: Rect, buf: &mut Buffer) { fn draw(&mut self, area: Rect, buf: &mut Buffer) {
block::new().title(&self.title).draw(area, buf); Chart::<String, String>::default()
.block(block::new().title(&self.title))
.x_axis(Axis::default().bounds([self.update_count - 100.0, self.update_count + 1.0]))
.y_axis(Axis::default().bounds([0.0, 100.0]))
.datasets(&[
Dataset::default()
.name("Main")
.marker(Marker::Braille)
.style(Style::default().fg(Color::Yellow))
.data(&self.main.percents),
Dataset::default()
.name("Swap")
.marker(Marker::Braille)
.style(Style::default().fg(Color::Blue))
.data(&self.swap.percents),
])
.draw(area, buf);
} }
} }