1
0
mirror of https://github.com/danog/ytop.git synced 2024-11-30 04:29:10 +01:00

Widgets now use the colorscheme

This commit is contained in:
Caleb Bassi 2020-01-12 16:41:01 -08:00
parent e70d3cf397
commit 640633270c
9 changed files with 45 additions and 45 deletions

View File

@ -6,7 +6,7 @@ use crate::widgets::*;
pub struct App<'a, 'b> {
pub help_menu: HelpMenu<'a>,
pub statusbar: Option<Statusbar>,
pub statusbar: Option<Statusbar<'a>>,
pub widgets: Widgets<'a, 'b>,
}
@ -47,7 +47,7 @@ pub fn setup_app<'a, 'b>(
};
let statusbar = if args.statusbar {
Some(Statusbar::new(program_name))
Some(Statusbar::new(colorscheme, program_name))
} else {
None
};

View File

@ -4,7 +4,6 @@ use battery::Manager;
use num_rational::Ratio;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use crate::colorscheme::Colorscheme;
@ -71,10 +70,11 @@ impl Widget for BatteryWidget<'_> {
let datasets: Vec<Dataset> = self
.battery_data
.values()
.map(|data| {
.enumerate()
.map(|(i, data)| {
Dataset::default()
.marker(Marker::Braille)
.style(Style::default().fg(Color::Yellow))
.style(self.colorscheme.battery_lines[i % self.colorscheme.battery_lines.len()])
.graph_type(GraphType::Line)
.data(&data)
})
@ -95,7 +95,7 @@ impl Widget for BatteryWidget<'_> {
area.x + 3,
area.y + 2 + i as u16,
format!("{} {:3.0}%", data.0, data.1.last().unwrap().1),
Style::default().fg(Color::Yellow),
self.colorscheme.battery_lines[i % self.colorscheme.battery_lines.len()],
);
}
}

View File

@ -2,7 +2,6 @@ use num_rational::Ratio;
use psutil::cpu;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use crate::colorscheme::Colorscheme;
@ -104,18 +103,22 @@ impl Widget for CpuWidget<'_> {
datasets.push(
Dataset::default()
.marker(Marker::Braille)
.style(Style::default().fg(Color::Yellow))
.graph_type(GraphType::Line)
.style(self.colorscheme.cpu_lines[0])
.data(&self.average_data),
)
}
if self.show_percpu {
let offset = if self.show_average { 1 } else { 0 };
for i in 0..self.cpu_count {
datasets.push(
Dataset::default()
.marker(Marker::Braille)
.graph_type(GraphType::Line)
.style(Style::default().fg(Color::Yellow))
.style(
self.colorscheme.cpu_lines
[(i + offset as usize) % self.colorscheme.cpu_lines.len()],
)
.data(&self.percpu_data[i]),
)
}
@ -136,7 +139,7 @@ impl Widget for CpuWidget<'_> {
area.x + 3,
area.y + 2,
format!("AVRG {:3.0}%", self.average_data.last().unwrap().1),
Style::default().fg(Color::Yellow),
self.colorscheme.cpu_lines[0],
);
}
@ -147,7 +150,8 @@ impl Widget for CpuWidget<'_> {
area.x + 3,
area.y + 2 + offset + i as u16,
format!("CPU{} {:3.0}%", i, self.percpu_data[i].last().unwrap().1),
Style::default().fg(Color::Yellow),
self.colorscheme.cpu_lines
[(i + offset as usize) % self.colorscheme.cpu_lines.len()],
);
}
}

View File

@ -5,7 +5,7 @@ use num_rational::Ratio;
use psutil::disk;
use tui::buffer::Buffer;
use tui::layout::{Constraint, Rect};
use tui::style::{Color, Modifier, Style};
use tui::style::Modifier;
use tui::widgets::{Row, Table, Widget};
use crate::colorscheme::Colorscheme;
@ -107,8 +107,6 @@ impl UpdatableWidget for DiskWidget<'_> {
impl Widget for DiskWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let row_style = Style::default().fg(Color::White);
let mut partitions: Vec<Partition> = self
.partitions
.iter()
@ -126,12 +124,12 @@ impl Widget for DiskWidget<'_> {
format!("{:3.0}%", partition.used_percent),
]
.into_iter(),
row_style,
self.colorscheme.text,
)
}),
)
.block(block::new(self.colorscheme, &self.title))
.header_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD))
.header_style(self.colorscheme.text.modifier(Modifier::BOLD))
.widths(&[
Constraint::Length(20),
Constraint::Length(20),
@ -140,7 +138,6 @@ impl Widget for DiskWidget<'_> {
Constraint::Length(10),
Constraint::Length(10),
])
.style(Style::default().fg(Color::White))
.column_spacing(1)
.draw(area, buf);
}

View File

@ -3,7 +3,6 @@ use psutil::memory;
use size::Size;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Axis, Chart, Dataset, GraphType, Marker, Widget};
use crate::colorscheme::Colorscheme;
@ -89,12 +88,12 @@ impl Widget for MemWidget<'_> {
Dataset::default()
.marker(Marker::Braille)
.graph_type(GraphType::Line)
.style(Style::default().fg(Color::Yellow))
.style(self.colorscheme.mem_main)
.data(&self.main.percents),
Dataset::default()
.marker(Marker::Braille)
.graph_type(GraphType::Line)
.style(Style::default().fg(Color::Blue))
.style(self.colorscheme.mem_swap)
.data(&self.swap.percents),
])
.draw(area, buf);
@ -108,7 +107,7 @@ impl Widget for MemWidget<'_> {
Size::Bytes(self.main.used),
Size::Bytes(self.main.total),
),
Style::default().fg(Color::Yellow),
self.colorscheme.mem_main,
);
buf.set_string(
@ -120,7 +119,7 @@ impl Widget for MemWidget<'_> {
Size::Bytes(self.swap.used),
Size::Bytes(self.swap.total),
),
Style::default().fg(Color::Blue),
self.colorscheme.mem_swap,
);
}
}

View File

@ -2,7 +2,6 @@ use num_rational::Ratio;
use psutil::network;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{RenderDirection, Sparkline, Widget};
use crate::colorscheme::Colorscheme;
@ -111,14 +110,14 @@ impl Widget for NetWidget<'_, '_> {
top_half.x + 1,
top_half.y + 1,
format!("Total Rx: {:>3.1} {:>2}", 0.0, ""),
Style::default().fg(Color::Yellow),
self.colorscheme.text,
);
buf.set_string(
top_half.x + 1,
top_half.y + 2,
format!("Rx/s: {:>3.1} {:>2}/s", 0.0, ""),
Style::default().fg(Color::Yellow),
self.colorscheme.text,
);
Sparkline::default()
@ -133,21 +132,21 @@ impl Widget for NetWidget<'_, '_> {
)
.direction(RenderDirection::RTL)
.max(*self.bytes_recv.iter().max().unwrap())
.style(Style::default().fg(Color::Red))
.style(self.colorscheme.net_bars)
.draw(top_sparkline, buf);
buf.set_string(
bottom_half.x + 1,
bottom_half.y + 1,
format!("Total Tx: {:>3.1} {:>2}", 0.0, ""),
Style::default().fg(Color::Yellow),
self.colorscheme.text,
);
buf.set_string(
bottom_half.x + 1,
bottom_half.y + 2,
format!("Tx/s: {:>3.1} {:>2}/s", 0.0, ""),
Style::default().fg(Color::Yellow),
self.colorscheme.text,
);
Sparkline::default()
@ -162,7 +161,7 @@ impl Widget for NetWidget<'_, '_> {
)
.direction(RenderDirection::RTL)
.max(*self.bytes_sent.iter().max().unwrap())
.style(Style::default().fg(Color::Red))
.style(self.colorscheme.net_bars)
.draw(bottom_sparkline, buf);
}
}

View File

@ -2,7 +2,7 @@ use num_rational::Ratio;
use psutil::process;
use tui::buffer::Buffer;
use tui::layout::{Constraint, Rect};
use tui::style::{Color, Modifier, Style};
use tui::style::Modifier;
use tui::widgets::{Row, Table, Widget};
use crate::colorscheme::Colorscheme;
@ -66,8 +66,6 @@ impl UpdatableWidget for ProcWidget<'_> {
impl Widget for ProcWidget<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let row_style = Style::default().fg(Color::White);
Table::new(
["Count", "Command", "CPU%", "Mem%"].iter(),
self.procs.iter().map(|proc| {
@ -79,19 +77,18 @@ impl Widget for ProcWidget<'_> {
proc.mem.to_string(),
]
.into_iter(),
row_style,
self.colorscheme.text,
)
}),
)
.block(block::new(self.colorscheme, &self.title))
.header_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD))
.header_style(self.colorscheme.text.modifier(Modifier::BOLD))
.widths(&[
Constraint::Length(20),
Constraint::Length(20),
Constraint::Length(10),
Constraint::Length(10),
])
.style(Style::default().fg(Color::White))
.column_spacing(1)
.header_gap(0)
.draw(area, buf);

View File

@ -2,40 +2,45 @@ use chrono::prelude::*;
use psutil::host;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::Style;
use tui::widgets::Widget;
pub struct Statusbar {
use crate::colorscheme::Colorscheme;
pub struct Statusbar<'a> {
hostname: String,
program_name: String,
program_name_len: u16,
colorscheme: &'a Colorscheme,
}
impl Statusbar {
pub fn new(program_name: &str) -> Statusbar {
impl Statusbar<'_> {
pub fn new<'a>(colorscheme: &'a Colorscheme, program_name: &str) -> Statusbar<'a> {
Statusbar {
hostname: host::info().hostname().to_owned(),
program_name: program_name.to_string(),
program_name_len: program_name.len() as u16,
colorscheme,
}
}
}
impl Widget for Statusbar {
impl Widget for Statusbar<'_> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let time = Local::now().format("%H:%M:%S").to_string();
buf.set_string(area.x + 1, area.y, &self.hostname, Style::default());
buf.set_string(area.x + 1, area.y, &self.hostname, self.colorscheme.text);
buf.set_string(
(area.x + area.width - time.len() as u16) / 2,
area.y,
time,
Style::default(),
self.colorscheme.text,
);
buf.set_string(
area.x + area.width - self.program_name_len - 1,
area.y,
&self.program_name,
Style::default(),
self.colorscheme.text,
);
}
}

View File

@ -2,7 +2,6 @@ use num_rational::Ratio;
use psutil::sensors;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{List, Text, Widget};
use crate::colorscheme::Colorscheme;
@ -67,7 +66,7 @@ impl Widget for TempWidget<'_> {
)))
}))
.block(block::new(self.colorscheme, &self.title))
.style(Style::default().fg(Color::White))
.style(self.colorscheme.text)
.draw(area, buf);
}
}