Added a few changes - fix for slow start time feel, changed cpu legend display, added logic to make avg cpu display over everything. Also changed tui source to a fork.

This commit is contained in:
ClementTsang 2019-09-15 01:29:49 -04:00
parent 282acd1395
commit 0eb993d129
5 changed files with 44 additions and 12 deletions

View file

@ -22,7 +22,8 @@ sysinfo = "0.9.4"
tokio = "0.2.0-alpha.4"
[dependencies.tui]
version = "0.6.2"
git = "https://github.com/ClementTsang/tui-rs"
#path = "../tui-rs"
default-features = false
features = ['crossterm']

View file

@ -28,7 +28,7 @@ Currently, I'm unable to test on MacOS, so I'm not sure how well this will work,
* [heim](https://github.com/heim-rs/heim)
* [sysinfo](https://github.com/GuillaumeGomez/sysinfo)
* [tokio](https://github.com/tokio-rs/tokio)
* [tui-rs](https://github.com/fdehau/tui-rs)
* [tui-rs](https://github.com/fdehau/tui-rs) (note I used a fork due to some issues I faced, you can find that [here](https://github.com/ClementTsang/tui-rs))
## Why

View file

@ -97,7 +97,7 @@ fn get_ordering<T : std::cmp::PartialOrd>(a_val : T, b_val : T, reverse_order :
async fn non_linux_cpu_usage(process : heim::process::Process) -> heim::process::ProcessResult<(heim::process::Process, heim_common::units::Ratio)> {
let usage_1 = process.cpu_usage().await?;
futures_timer::Delay::new(std::time::Duration::from_millis(100)).await?;
futures_timer::Delay::new(std::time::Duration::from_millis(100)).await?; // TODO: For windows, make it like the linux check
let usage_2 = process.cpu_usage().await?;
Ok((process, usage_2 - usage_1))

View file

@ -5,7 +5,7 @@ use tui::{
Terminal,
};
use crate::utils::error;
use crate::{app, utils::error};
const COLOUR_LIST : [Color; 6] = [Color::Red, Color::Green, Color::LightYellow, Color::LightBlue, Color::LightCyan, Color::LightMagenta];
const TEXT_COLOUR : Color = Color::Gray;
@ -27,7 +27,7 @@ pub struct CanvasData {
pub cpu_data : Vec<(String, Vec<(f64, f64)>)>,
}
pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_data : &CanvasData) -> error::Result<()> {
pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, app_data : &app::App, canvas_data : &CanvasData) -> error::Result<()> {
let border_style : Style = Style::default().fg(BORDER_STYLE_COLOUR);
let temperature_rows = canvas_data.temp_sensor_data.iter().map(|sensor| Row::StyledData(sensor.iter(), Style::default().fg(TEXT_COLOUR)));
@ -39,7 +39,7 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Percentage(32), Constraint::Percentage(34), Constraint::Percentage(34)].as_ref())
.constraints([Constraint::Percentage(34), Constraint::Percentage(34), Constraint::Percentage(33)].as_ref())
.split(f.size());
let middle_chunks = Layout::default()
@ -67,16 +67,38 @@ pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_
let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([-0.5, 100.0]).labels(&["0%", "100%"]);
let mut dataset_vector : Vec<Dataset> = Vec::new();
for (i, cpu) in canvas_data.cpu_data.iter().enumerate() {
let mut avg_cpu_exist_offset = 0;
if app_data.show_average_cpu {
if i == 0 {
// Skip, we want to render the average cpu last!
continue;
}
else {
avg_cpu_exist_offset = 1;
}
}
dataset_vector.push(
Dataset::default()
.name(&cpu.0)
.marker(GRAPH_MARKER)
.style(Style::default().fg(COLOUR_LIST[i % COLOUR_LIST.len()]))
.style(Style::default().fg(COLOUR_LIST[i - avg_cpu_exist_offset % COLOUR_LIST.len()]))
.data(&(cpu.1)),
);
}
if !canvas_data.cpu_data.is_empty() && app_data.show_average_cpu {
dataset_vector.push(
Dataset::default()
.name(&canvas_data.cpu_data[0].0)
.marker(GRAPH_MARKER)
.style(Style::default().fg(COLOUR_LIST[canvas_data.cpu_data.len() - 1 % COLOUR_LIST.len()]))
.data(&(canvas_data.cpu_data[0].1)),
);
}
Chart::default()
.block(Block::default().title("CPU Usage").borders(Borders::ALL).border_style(border_style))
.x_axis(x_axis)

View file

@ -103,19 +103,28 @@ fn main() -> error::Result<()> {
data_state.set_temperature_type(app.temperature_type.clone());
{
let tx = tx.clone();
let mut first_run = true;
thread::spawn(move || {
let tx = tx.clone();
loop {
futures::executor::block_on(data_state.update_data());
tx.send(Event::Update(Box::from(data_state.data.clone()))).unwrap();
thread::sleep(Duration::from_millis(update_rate_in_milliseconds as u64));
if first_run {
// Fix for if you set a really long time for update periods (and just gives a faster first value)
thread::sleep(Duration::from_millis(250));
first_run = false;
}
else {
thread::sleep(Duration::from_millis(update_rate_in_milliseconds as u64));
}
}
});
}
// Set up up tui and crossterm
let screen = AlternateScreen::to_alternate(true)?;
let backend = CrosstermBackend::with_alternate_screen(screen)?;
let stdout = std::io::stdout();
let backend = CrosstermBackend::with_alternate_screen(stdout, screen)?;
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
terminal.clear()?;
@ -172,7 +181,7 @@ fn main() -> error::Result<()> {
}
}
// Draw!
canvas::draw_data(&mut terminal, &canvas_data)?;
canvas::draw_data(&mut terminal, &app, &canvas_data)?;
}
debug!("Terminating.");
@ -293,8 +302,8 @@ fn update_cpu_data_points(show_avg_cpu : bool, app_data : &data_collection::Data
for (i, data) in cpu_collection.iter().enumerate() {
cpu_data_vector.push((
// + 1 to skip total CPU if show_avg_cpu is false
(&*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i + if show_avg_cpu { 0 } else { 1 }].cpu_name)).to_string()
+ " " + &format!("{:3}%", (data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)),
format!("{:4}: ", &*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i + if show_avg_cpu { 0 } else { 1 }].cpu_name)).to_uppercase()
+ &format!("{:3}%", (data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)),
data.clone(),
))
}