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

Pass colorscheme to widgets and annotate lifetimes

This commit is contained in:
Caleb Bassi 2020-01-12 15:47:49 -08:00
parent 46f6402f7b
commit def79e153b
10 changed files with 111 additions and 77 deletions

View File

@ -4,45 +4,45 @@ use crate::args::Args;
use crate::colorscheme::Colorscheme;
use crate::widgets::*;
pub struct App {
pub help_menu: HelpMenu,
pub struct App<'a, 'b> {
pub help_menu: HelpMenu<'a>,
pub statusbar: Option<Statusbar>,
pub widgets: Widgets,
pub widgets: Widgets<'a, 'b>,
}
pub struct Widgets {
pub battery: Option<BatteryWidget>,
pub cpu: CpuWidget,
pub disk: Option<DiskWidget>,
pub mem: MemWidget,
pub net: Option<NetWidget>,
pub proc: ProcWidget,
pub temp: Option<TempWidget>,
pub struct Widgets<'a, 'b> {
pub battery: Option<BatteryWidget<'a>>,
pub cpu: CpuWidget<'a>,
pub disk: Option<DiskWidget<'a>>,
pub mem: MemWidget<'a>,
pub net: Option<NetWidget<'a, 'b>>,
pub proc: ProcWidget<'a>,
pub temp: Option<TempWidget<'a>>,
}
pub fn setup_app(
args: &Args,
pub fn setup_app<'a, 'b>(
args: &'b Args,
update_ratio: Ratio<u64>,
colorscheme: &Colorscheme,
colorscheme: &'a Colorscheme,
program_name: &str,
) -> App {
let cpu = CpuWidget::new(update_ratio, args.average_cpu, args.per_cpu);
let mem = MemWidget::new(update_ratio);
let proc = ProcWidget::new();
let help_menu = HelpMenu::new();
) -> App<'a, 'b> {
let cpu = CpuWidget::new(colorscheme, update_ratio, args.average_cpu, args.per_cpu);
let mem = MemWidget::new(colorscheme, update_ratio);
let proc = ProcWidget::new(colorscheme);
let help_menu = HelpMenu::new(colorscheme);
let (battery, disk, net, temp) = if args.minimal {
(None, None, None, None)
} else {
(
if args.battery {
Some(BatteryWidget::new())
Some(BatteryWidget::new(colorscheme))
} else {
None
},
Some(DiskWidget::new()),
Some(NetWidget::new(args.interfaces.clone())),
Some(TempWidget::new(args.fahrenheit)),
Some(DiskWidget::new(colorscheme)),
Some(NetWidget::new(colorscheme, &args.interfaces)),
Some(TempWidget::new(colorscheme, args.fahrenheit)),
)
};

View File

@ -7,34 +7,35 @@ use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
pub struct BatteryWidget {
pub struct BatteryWidget<'a> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
update_count: u64,
battery_data: HashMap<String, Vec<(f64, f64)>>,
manager: Manager,
}
impl BatteryWidget {
pub fn new() -> BatteryWidget {
impl BatteryWidget<'_> {
pub fn new(colorscheme: &Colorscheme) -> BatteryWidget {
BatteryWidget {
title: " Batteries ".to_string(),
update_interval: Ratio::from_integer(60),
colorscheme,
update_count: 0,
battery_data: HashMap::new(),
manager: Manager::new().unwrap(),
}
}
}
impl UpdatableWidget for BatteryWidget {
impl UpdatableWidget for BatteryWidget<'_> {
fn update(&mut self) {
self.update_count += 1;
let mut current_batteries = Vec::new();
@ -65,7 +66,7 @@ impl UpdatableWidget for BatteryWidget {
}
}
impl Widget for BatteryWidget {
impl Widget for BatteryWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let datasets: Vec<Dataset> = self
.battery_data
@ -80,7 +81,7 @@ impl Widget for BatteryWidget {
.collect();
Chart::<String, String>::default()
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.x_axis(Axis::default().bounds([
self.update_count as f64 - 100.0,
self.update_count as f64 + 1.0,

View File

@ -1,8 +1,11 @@
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders};
pub fn new() -> Block<'static> {
use crate::colorscheme::Colorscheme;
pub fn new<'a>(colorscheme: &Colorscheme, title: &'a str) -> Block<'a> {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Cyan))
.title(title)
}

View File

@ -5,12 +5,15 @@ use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
pub struct CpuWidget {
pub struct CpuWidget<'a> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
update_count: u64,
horizontal_scale: i64,
@ -25,13 +28,20 @@ pub struct CpuWidget {
collector: cpu::CpuPercentCollector,
}
impl CpuWidget {
pub fn new(update_interval: Ratio<u64>, show_average: bool, show_percpu: bool) -> CpuWidget {
impl CpuWidget<'_> {
pub fn new(
colorscheme: &Colorscheme,
update_interval: Ratio<u64>,
show_average: bool,
show_percpu: bool,
) -> CpuWidget {
let update_count = 0;
let mut cpu_widget = CpuWidget {
title: " CPU Usage ".to_string(),
update_interval,
colorscheme,
update_count,
horizontal_scale: 100,
@ -66,7 +76,7 @@ impl CpuWidget {
}
}
impl UpdatableWidget for CpuWidget {
impl UpdatableWidget for CpuWidget<'_> {
fn update(&mut self) {
self.update_count += 1;
if self.show_average {
@ -87,7 +97,7 @@ impl UpdatableWidget for CpuWidget {
}
}
impl Widget for CpuWidget {
impl Widget for CpuWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let mut datasets = Vec::new();
if self.show_average {
@ -112,7 +122,7 @@ impl Widget for CpuWidget {
}
Chart::<String, String>::default()
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.x_axis(Axis::default().bounds([
self.update_count as f64 - 100.0,
self.update_count as f64 + 1.0,

View File

@ -8,6 +8,7 @@ use tui::layout::{Constraint, Rect};
use tui::style::{Color, Modifier, Style};
use tui::widgets::{Row, Table, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
@ -23,20 +24,22 @@ struct Partition {
bytes_free: u64,
}
pub struct DiskWidget {
pub struct DiskWidget<'a> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
partitions: HashMap<String, Partition>,
collector: disk::DiskIoCountersCollector,
}
impl DiskWidget {
pub fn new() -> DiskWidget {
impl DiskWidget<'_> {
pub fn new(colorscheme: &Colorscheme) -> DiskWidget {
DiskWidget {
title: " Disk Usage ".to_string(),
update_interval: Ratio::from_integer(1),
colorscheme,
partitions: HashMap::new(),
@ -45,7 +48,7 @@ impl DiskWidget {
}
}
impl UpdatableWidget for DiskWidget {
impl UpdatableWidget for DiskWidget<'_> {
fn update(&mut self) {
let io_counters = self.collector.disk_io_counters_perdisk().unwrap();
self.partitions = disk::partitions_physical()
@ -102,7 +105,7 @@ impl UpdatableWidget for DiskWidget {
}
}
impl Widget for DiskWidget {
impl Widget for DiskWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let row_style = Style::default().fg(Color::White);
@ -127,7 +130,7 @@ impl Widget for DiskWidget {
)
}),
)
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.header_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD))
.widths(&[
Constraint::Length(20),

View File

@ -3,7 +3,8 @@ use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::widgets::{Paragraph, Text, Widget};
use super::block;
use crate::colorscheme::Colorscheme;
use crate::widgets::block;
const TEXT: &str = r"Quit: q or <C-c>
Process navigation:
@ -41,14 +42,16 @@ lazy_static! {
.collect();
}
pub struct HelpMenu {
pub struct HelpMenu<'a> {
title: String,
colorscheme: &'a Colorscheme,
}
impl HelpMenu {
pub fn new() -> HelpMenu {
impl HelpMenu<'_> {
pub fn new(colorscheme: &Colorscheme) -> HelpMenu {
HelpMenu {
title: " Help Menu ".to_string(),
colorscheme,
}
}
@ -62,10 +65,10 @@ impl HelpMenu {
}
}
impl Widget for HelpMenu {
impl Widget for HelpMenu<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
Paragraph::new(TEXT_VEC.iter())
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.draw(area, buf);
}
}

View File

@ -6,6 +6,7 @@ use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
@ -16,17 +17,19 @@ struct MemData {
percents: Vec<(f64, f64)>,
}
pub struct MemWidget {
pub struct MemWidget<'a> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
update_count: u64,
main: MemData,
swap: MemData,
}
impl MemWidget {
pub fn new(update_interval: Ratio<u64>) -> MemWidget {
impl MemWidget<'_> {
pub fn new(colorscheme: &Colorscheme, update_interval: Ratio<u64>) -> MemWidget {
let update_count = 0;
let mut main = MemData::default();
@ -38,6 +41,8 @@ impl MemWidget {
MemWidget {
title: " Memory Usage ".to_string(),
update_interval,
colorscheme,
update_count,
main,
@ -46,7 +51,7 @@ impl MemWidget {
}
}
impl UpdatableWidget for MemWidget {
impl UpdatableWidget for MemWidget<'_> {
fn update(&mut self) {
self.update_count += 1;
@ -71,10 +76,10 @@ impl UpdatableWidget for MemWidget {
}
}
impl Widget for MemWidget {
impl Widget for MemWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
Chart::<String, String>::default()
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.x_axis(Axis::default().bounds([
self.update_count as f64 - 100.0,
self.update_count as f64 + 1.0,

View File

@ -5,14 +5,16 @@ use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{RenderDirection, Sparkline, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
pub struct NetWidget {
pub struct NetWidget<'a, 'b> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
interfaces: String,
interfaces: &'b str,
bytes_recv: Vec<u64>,
bytes_sent: Vec<u64>,
@ -23,11 +25,12 @@ pub struct NetWidget {
collector: network::NetIoCountersCollector,
}
impl NetWidget {
pub fn new(interfaces: String) -> NetWidget {
impl NetWidget<'_, '_> {
pub fn new<'a, 'b>(colorscheme: &'a Colorscheme, interfaces: &'b str) -> NetWidget<'a, 'b> {
NetWidget {
title: " Network Usage ".to_string(),
update_interval: Ratio::from_integer(1),
colorscheme,
interfaces,
@ -42,7 +45,7 @@ impl NetWidget {
}
}
impl UpdatableWidget for NetWidget {
impl UpdatableWidget for NetWidget<'_, '_> {
fn update(&mut self) {
let io_counters = self.collector.net_io_counters().unwrap();
@ -65,9 +68,9 @@ impl UpdatableWidget for NetWidget {
}
}
impl Widget for NetWidget {
impl Widget for NetWidget<'_, '_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
block::new().title(&self.title).draw(area, buf);
block::new(self.colorscheme, &self.title).draw(area, buf);
let inner = Rect {
x: area.x + 1,

View File

@ -5,6 +5,7 @@ use tui::layout::{Constraint, Rect};
use tui::style::{Color, Modifier, Style};
use tui::widgets::{Row, Table, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
@ -16,25 +17,27 @@ struct Proc {
mem: f32,
}
pub struct ProcWidget {
pub struct ProcWidget<'a> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
procs: Vec<Proc>,
}
impl ProcWidget {
pub fn new() -> ProcWidget {
impl ProcWidget<'_> {
pub fn new(colorscheme: &Colorscheme) -> ProcWidget {
ProcWidget {
title: " Processes ".to_string(),
update_interval: Ratio::from_integer(1),
colorscheme,
procs: Vec::new(),
}
}
}
impl UpdatableWidget for ProcWidget {
impl UpdatableWidget for ProcWidget<'_> {
fn update(&mut self) {
self.procs = process::processes()
.unwrap()
@ -61,7 +64,7 @@ impl UpdatableWidget for ProcWidget {
}
}
impl Widget for ProcWidget {
impl Widget for ProcWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let row_style = Style::default().fg(Color::White);
@ -80,7 +83,7 @@ impl Widget for ProcWidget {
)
}),
)
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.header_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD))
.widths(&[
Constraint::Length(20),

View File

@ -5,22 +5,25 @@ use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{List, Text, Widget};
use crate::colorscheme::Colorscheme;
use crate::update::UpdatableWidget;
use crate::widgets::block;
pub struct TempWidget {
pub struct TempWidget<'a> {
title: String,
update_interval: Ratio<u64>,
colorscheme: &'a Colorscheme,
fahrenheit: bool,
temp_data: Vec<(String, f64)>,
}
impl TempWidget {
pub fn new(fahrenheit: bool) -> TempWidget {
impl TempWidget<'_> {
pub fn new(colorscheme: &Colorscheme, fahrenheit: bool) -> TempWidget {
TempWidget {
title: " Temperatures ".to_string(),
update_interval: Ratio::from_integer(5),
colorscheme,
fahrenheit,
temp_data: Vec::new(),
@ -28,7 +31,7 @@ impl TempWidget {
}
}
impl UpdatableWidget for TempWidget {
impl UpdatableWidget for TempWidget<'_> {
fn update(&mut self) {
self.temp_data = sensors::temperatures()
.into_iter()
@ -52,7 +55,7 @@ impl UpdatableWidget for TempWidget {
}
}
impl Widget for TempWidget {
impl Widget for TempWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
List::new(self.temp_data.iter().map(|item| {
Text::Raw(std::borrow::Cow::from(format!(
@ -63,7 +66,7 @@ impl Widget for TempWidget {
width = area.width as usize - 6
)))
}))
.block(block::new().title(&self.title))
.block(block::new(self.colorscheme, &self.title))
.style(Style::default().fg(Color::White))
.draw(area, buf);
}