refactor: move point definition to tui_rs widget (#832)

This commit is contained in:
Clement Tsang 2022-10-13 15:40:19 -04:00 committed by GitHub
parent e68a7bdfdc
commit 8b72a33f40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 27 deletions

View File

@ -18,7 +18,10 @@ cfg_if::cfg_if! {
} }
} }
use crate::data_harvester::cpu::{CpuData, CpuDataType, CpuHarvest, PastCpuTotal, PastCpuWork}; use crate::{
components::tui_widget::time_chart::Point,
data_harvester::cpu::{CpuData, CpuDataType, CpuHarvest, PastCpuTotal, PastCpuWork},
};
use futures::StreamExt; use futures::StreamExt;
use std::collections::VecDeque; use std::collections::VecDeque;
@ -28,8 +31,8 @@ pub async fn get_cpu_data_list(
previous_average_cpu_time: &mut Option<(PastCpuWork, PastCpuTotal)>, previous_average_cpu_time: &mut Option<(PastCpuWork, PastCpuTotal)>,
) -> crate::error::Result<CpuHarvest> { ) -> crate::error::Result<CpuHarvest> {
fn calculate_cpu_usage_percentage( fn calculate_cpu_usage_percentage(
(previous_working_time, previous_total_time): (f64, f64), (previous_working_time, previous_total_time): Point,
(current_working_time, current_total_time): (f64, f64), (current_working_time, current_total_time): Point,
) -> f64 { ) -> f64 {
((if current_working_time > previous_working_time { ((if current_working_time > previous_working_time {
current_working_time - previous_working_time current_working_time - previous_working_time

View File

@ -1,7 +1,9 @@
//! Linux-specific functions regarding CPU usage. //! Linux-specific functions regarding CPU usage.
use crate::components::tui_widget::time_chart::Point;
use heim::cpu::os::linux::CpuTimeExt; use heim::cpu::os::linux::CpuTimeExt;
pub fn convert_cpu_times(cpu_time: &heim::cpu::CpuTime) -> (f64, f64) {
pub fn convert_cpu_times(cpu_time: &heim::cpu::CpuTime) -> Point {
let working_time: f64 = (cpu_time.user() let working_time: f64 = (cpu_time.user()
+ cpu_time.nice() + cpu_time.nice()
+ cpu_time.system() + cpu_time.system()

View File

@ -1,6 +1,8 @@
//! Windows and macOS-specific functions regarding CPU usage. //! Windows and macOS-specific functions regarding CPU usage.
pub fn convert_cpu_times(cpu_time: &heim::cpu::CpuTime) -> (f64, f64) { use crate::components::tui_widget::time_chart::Point;
pub fn convert_cpu_times(cpu_time: &heim::cpu::CpuTime) -> Point {
let working_time: f64 = let working_time: f64 =
(cpu_time.user() + cpu_time.system()).get::<heim::units::time::second>(); (cpu_time.user() + cpu_time.system()).get::<heim::units::time::second>();
( (

View File

@ -2,6 +2,7 @@
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use crate::components::tui_widget::time_chart::Point;
use crate::utils::error::{self, BottomError}; use crate::utils::error::{self, BottomError};
use crate::Pid; use crate::Pid;
@ -36,7 +37,7 @@ impl PrevProcDetails {
} }
} }
fn calculate_idle_values(line: String) -> (f64, f64) { fn calculate_idle_values(line: String) -> Point {
/// Converts a `Option<&str>` value to an f64. If it fails to parse or is `None`, then it will return `0_f64`. /// Converts a `Option<&str>` value to an f64. If it fails to parse or is `None`, then it will return `0_f64`.
fn str_to_f64(val: Option<&str>) -> f64 { fn str_to_f64(val: Option<&str>) -> f64 {
val.and_then(|v| v.parse::<f64>().ok()).unwrap_or(0_f64) val.and_then(|v| v.parse::<f64>().ok()).unwrap_or(0_f64)
@ -61,9 +62,7 @@ fn calculate_idle_values(line: String) -> (f64, f64) {
(idle, non_idle) (idle, non_idle)
} }
fn cpu_usage_calculation( fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> error::Result<Point> {
prev_idle: &mut f64, prev_non_idle: &mut f64,
) -> error::Result<(f64, f64)> {
use std::io::prelude::*; use std::io::prelude::*;
use std::io::BufReader; use std::io::BufReader;

View File

@ -455,7 +455,7 @@ impl Painter {
#[cfg(feature = "zfs")] #[cfg(feature = "zfs")]
{ {
let arc_data: &[(f64, f64)] = &app_state.converted_data.arc_data; let arc_data = &app_state.converted_data.arc_data;
if let Some(arc) = arc_data.last() { if let Some(arc) = arc_data.last() {
if arc.1 != 0.0 { if arc.1 != 0.0 {
mem_rows += 1; // add row for arc mem_rows += 1; // add row for arc

View File

@ -13,8 +13,8 @@ impl Painter {
pub fn draw_basic_memory<B: Backend>( pub fn draw_basic_memory<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64, &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
) { ) {
let mem_data: &[(f64, f64)] = &app_state.converted_data.mem_data; let mem_data = &app_state.converted_data.mem_data;
let swap_data: &[(f64, f64)] = &app_state.converted_data.swap_data; let swap_data = &app_state.converted_data.swap_data;
let margined_loc = Layout::default() let margined_loc = Layout::default()
.constraints({ .constraints({
@ -90,7 +90,7 @@ impl Painter {
#[cfg(feature = "zfs")] #[cfg(feature = "zfs")]
{ {
let arc_data: &[(f64, f64)] = &app_state.converted_data.arc_data; let arc_data = &app_state.converted_data.arc_data;
let arc_ratio = if let Some(arc) = arc_data.last() { let arc_ratio = if let Some(arc) = arc_data.last() {
arc.1 / 100.0 arc.1 / 100.0
} else { } else {

View File

@ -32,7 +32,7 @@ impl Painter {
let mut size = 0; let mut size = 0;
#[cfg(feature = "zfs")] #[cfg(feature = "zfs")]
{ {
let arc_data: &[(f64, f64)] = &app_state.converted_data.arc_data; let arc_data = &app_state.converted_data.arc_data;
if let Some(arc) = arc_data.last() { if let Some(arc) = arc_data.last() {
if arc.1 != 0.0 { if arc.1 != 0.0 {
size += 1; // add capacity for ARC size += 1; // add capacity for ARC
@ -61,7 +61,7 @@ impl Painter {
} }
#[cfg(feature = "zfs")] #[cfg(feature = "zfs")]
if let Some((label_percent, label_frac)) = &app_state.converted_data.arc_labels { if let Some((label_percent, label_frac)) = &app_state.converted_data.arc_labels {
let arc_data: &[(f64, f64)] = &app_state.converted_data.arc_data; let arc_data = &app_state.converted_data.arc_data;
if let Some(arc) = arc_data.last() { if let Some(arc) = arc_data.last() {
if arc.1 != 0.0 { if arc.1 != 0.0 {
let arc_label = format!("ARC:{}{}", label_percent, label_frac); let arc_label = format!("ARC:{}{}", label_percent, label_frac);

View File

@ -1,7 +1,10 @@
use crate::{ use crate::{
app::{App, AxisScaling}, app::{App, AxisScaling},
canvas::{drawing_utils::should_hide_x_label, Painter}, canvas::{drawing_utils::should_hide_x_label, Painter},
components::time_graph::{GraphData, Point, TimeGraph}, components::{
time_graph::{GraphData, TimeGraph},
tui_widget::time_chart::Point,
},
units::data_units::DataUnit, units::data_units::DataUnit,
utils::gen_util::*, utils::gen_util::*,
}; };
@ -52,8 +55,8 @@ impl Painter {
hide_legend: bool, hide_legend: bool,
) { ) {
if let Some(network_widget_state) = app_state.net_state.widget_states.get_mut(&widget_id) { if let Some(network_widget_state) = app_state.net_state.widget_states.get_mut(&widget_id) {
let network_data_rx: &[(f64, f64)] = &app_state.converted_data.network_data_rx; let network_data_rx = &app_state.converted_data.network_data_rx;
let network_data_tx: &[(f64, f64)] = &app_state.converted_data.network_data_tx; let network_data_tx = &app_state.converted_data.network_data_tx;
let time_start = -(network_widget_state.current_display_time as f64); let time_start = -(network_widget_state.current_display_time as f64);
let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id); let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id);
let x_bounds = [0, network_widget_state.current_display_time]; let x_bounds = [0, network_widget_state.current_display_time];
@ -201,7 +204,7 @@ impl Painter {
fn get_max_entry( fn get_max_entry(
rx: &[Point], tx: &[Point], time_start: f64, network_scale_type: &AxisScaling, rx: &[Point], tx: &[Point], time_start: f64, network_scale_type: &AxisScaling,
network_use_binary_prefix: bool, network_use_binary_prefix: bool,
) -> (f64, f64) { ) -> Point {
/// Determines a "fake" max value in circumstances where we couldn't find one from the data. /// Determines a "fake" max value in circumstances where we couldn't find one from the data.
fn calculate_missing_max( fn calculate_missing_max(
network_scale_type: &AxisScaling, network_use_binary_prefix: bool, network_scale_type: &AxisScaling, network_use_binary_prefix: bool,

View File

@ -13,10 +13,7 @@ use tui::{
use concat_string::concat_string; use concat_string::concat_string;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use super::tui_widget::time_chart::{Axis, Dataset, TimeChart, DEFAULT_LEGEND_CONSTRAINTS}; use super::tui_widget::time_chart::{Axis, Dataset, Point, TimeChart, DEFAULT_LEGEND_CONSTRAINTS};
/// A single graph point.
pub type Point = (f64, f64); // FIXME: Move this to tui time chart?
/// Represents the data required by the [`TimeGraph`]. /// Represents the data required by the [`TimeGraph`].
pub struct GraphData<'a> { pub struct GraphData<'a> {

View File

@ -14,6 +14,9 @@ use unicode_width::UnicodeWidthStr;
use crate::utils::gen_util::partial_ordering; use crate::utils::gen_util::partial_ordering;
/// A single graph point.
pub type Point = (f64, f64);
/// An X or Y axis for the chart widget /// An X or Y axis for the chart widget
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Axis<'a> { pub struct Axis<'a> {
@ -70,7 +73,7 @@ pub struct Dataset<'a> {
/// Name of the dataset (used in the legend if shown) /// Name of the dataset (used in the legend if shown)
name: Cow<'a, str>, name: Cow<'a, str>,
/// A reference to the actual data /// A reference to the actual data
data: &'a [(f64, f64)], data: &'a [Point],
/// Symbol used for each points of this dataset /// Symbol used for each points of this dataset
marker: symbols::Marker, marker: symbols::Marker,
/// Determines graph type used for drawing points /// Determines graph type used for drawing points
@ -101,7 +104,7 @@ impl<'a> Dataset<'a> {
self self
} }
pub fn data(mut self, data: &'a [(f64, f64)]) -> Dataset<'a> { pub fn data(mut self, data: &'a [Point]) -> Dataset<'a> {
self.data = data; self.data = data;
self self
} }
@ -589,7 +592,7 @@ fn get_end(dataset: &Dataset<'_>, end_bound: f64) -> (usize, Option<usize>) {
} }
/// Returns the y-axis value for a given `x`, given two points to draw a line between. /// Returns the y-axis value for a given `x`, given two points to draw a line between.
fn interpolate_point(older_point: &(f64, f64), newer_point: &(f64, f64), x: f64) -> f64 { fn interpolate_point(older_point: &Point, newer_point: &Point, x: f64) -> f64 {
let delta_x = newer_point.0 - older_point.0; let delta_x = newer_point.0 - older_point.0;
let delta_y = newer_point.1 - older_point.1; let delta_y = newer_point.1 - older_point.1;
let slope = delta_y / delta_x; let slope = delta_y / delta_x;

View File

@ -7,7 +7,7 @@ use crate::app::{
data_harvester::temperature::TemperatureType, data_harvester::temperature::TemperatureType,
widgets::{DiskWidgetData, TempWidgetData}, widgets::{DiskWidgetData, TempWidgetData},
}; };
use crate::components::time_graph::Point; use crate::components::tui_widget::time_chart::Point;
use crate::utils::gen_util::*; use crate::utils::gen_util::*;
use crate::{app::AxisScaling, units::data_units::DataUnit}; use crate::{app::AxisScaling, units::data_units::DataUnit};