mirror of
https://github.com/ClementTsang/bottom
synced 2024-11-10 22:54:21 +00:00
refactor: move point definition to tui_rs widget (#832)
This commit is contained in:
parent
e68a7bdfdc
commit
8b72a33f40
11 changed files with 36 additions and 27 deletions
|
@ -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 std::collections::VecDeque;
|
||||
|
@ -28,8 +31,8 @@ pub async fn get_cpu_data_list(
|
|||
previous_average_cpu_time: &mut Option<(PastCpuWork, PastCpuTotal)>,
|
||||
) -> crate::error::Result<CpuHarvest> {
|
||||
fn calculate_cpu_usage_percentage(
|
||||
(previous_working_time, previous_total_time): (f64, f64),
|
||||
(current_working_time, current_total_time): (f64, f64),
|
||||
(previous_working_time, previous_total_time): Point,
|
||||
(current_working_time, current_total_time): Point,
|
||||
) -> f64 {
|
||||
((if current_working_time > previous_working_time {
|
||||
current_working_time - previous_working_time
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
//! Linux-specific functions regarding CPU usage.
|
||||
|
||||
use crate::components::tui_widget::time_chart::Point;
|
||||
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()
|
||||
+ cpu_time.nice()
|
||||
+ cpu_time.system()
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
//! 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 =
|
||||
(cpu_time.user() + cpu_time.system()).get::<heim::units::time::second>();
|
||||
(
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
use crate::components::tui_widget::time_chart::Point;
|
||||
use crate::utils::error::{self, BottomError};
|
||||
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`.
|
||||
fn str_to_f64(val: Option<&str>) -> 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)
|
||||
}
|
||||
|
||||
fn cpu_usage_calculation(
|
||||
prev_idle: &mut f64, prev_non_idle: &mut f64,
|
||||
) -> error::Result<(f64, f64)> {
|
||||
fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> error::Result<Point> {
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
|
||||
|
|
|
@ -455,7 +455,7 @@ impl Painter {
|
|||
|
||||
#[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 arc.1 != 0.0 {
|
||||
mem_rows += 1; // add row for arc
|
||||
|
|
|
@ -13,8 +13,8 @@ impl Painter {
|
|||
pub fn draw_basic_memory<B: Backend>(
|
||||
&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 swap_data: &[(f64, f64)] = &app_state.converted_data.swap_data;
|
||||
let mem_data = &app_state.converted_data.mem_data;
|
||||
let swap_data = &app_state.converted_data.swap_data;
|
||||
|
||||
let margined_loc = Layout::default()
|
||||
.constraints({
|
||||
|
@ -90,7 +90,7 @@ impl Painter {
|
|||
|
||||
#[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() {
|
||||
arc.1 / 100.0
|
||||
} else {
|
||||
|
|
|
@ -32,7 +32,7 @@ impl Painter {
|
|||
let mut size = 0;
|
||||
#[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 arc.1 != 0.0 {
|
||||
size += 1; // add capacity for ARC
|
||||
|
@ -61,7 +61,7 @@ impl Painter {
|
|||
}
|
||||
#[cfg(feature = "zfs")]
|
||||
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 arc.1 != 0.0 {
|
||||
let arc_label = format!("ARC:{}{}", label_percent, label_frac);
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
use crate::{
|
||||
app::{App, AxisScaling},
|
||||
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,
|
||||
utils::gen_util::*,
|
||||
};
|
||||
|
@ -52,8 +55,8 @@ impl Painter {
|
|||
hide_legend: bool,
|
||||
) {
|
||||
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_tx: &[(f64, f64)] = &app_state.converted_data.network_data_tx;
|
||||
let network_data_rx = &app_state.converted_data.network_data_rx;
|
||||
let network_data_tx = &app_state.converted_data.network_data_tx;
|
||||
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 x_bounds = [0, network_widget_state.current_display_time];
|
||||
|
@ -201,7 +204,7 @@ impl Painter {
|
|||
fn get_max_entry(
|
||||
rx: &[Point], tx: &[Point], time_start: f64, network_scale_type: &AxisScaling,
|
||||
network_use_binary_prefix: bool,
|
||||
) -> (f64, f64) {
|
||||
) -> Point {
|
||||
/// Determines a "fake" max value in circumstances where we couldn't find one from the data.
|
||||
fn calculate_missing_max(
|
||||
network_scale_type: &AxisScaling, network_use_binary_prefix: bool,
|
||||
|
|
|
@ -13,10 +13,7 @@ use tui::{
|
|||
use concat_string::concat_string;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::tui_widget::time_chart::{Axis, Dataset, TimeChart, DEFAULT_LEGEND_CONSTRAINTS};
|
||||
|
||||
/// A single graph point.
|
||||
pub type Point = (f64, f64); // FIXME: Move this to tui time chart?
|
||||
use super::tui_widget::time_chart::{Axis, Dataset, Point, TimeChart, DEFAULT_LEGEND_CONSTRAINTS};
|
||||
|
||||
/// Represents the data required by the [`TimeGraph`].
|
||||
pub struct GraphData<'a> {
|
||||
|
|
|
@ -14,6 +14,9 @@ use unicode_width::UnicodeWidthStr;
|
|||
|
||||
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
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Axis<'a> {
|
||||
|
@ -70,7 +73,7 @@ pub struct Dataset<'a> {
|
|||
/// Name of the dataset (used in the legend if shown)
|
||||
name: Cow<'a, str>,
|
||||
/// A reference to the actual data
|
||||
data: &'a [(f64, f64)],
|
||||
data: &'a [Point],
|
||||
/// Symbol used for each points of this dataset
|
||||
marker: symbols::Marker,
|
||||
/// Determines graph type used for drawing points
|
||||
|
@ -101,7 +104,7 @@ impl<'a> Dataset<'a> {
|
|||
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
|
||||
}
|
||||
|
@ -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.
|
||||
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_y = newer_point.1 - older_point.1;
|
||||
let slope = delta_y / delta_x;
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::app::{
|
|||
data_harvester::temperature::TemperatureType,
|
||||
widgets::{DiskWidgetData, TempWidgetData},
|
||||
};
|
||||
use crate::components::time_graph::Point;
|
||||
use crate::components::tui_widget::time_chart::Point;
|
||||
use crate::utils::gen_util::*;
|
||||
use crate::{app::AxisScaling, units::data_units::DataUnit};
|
||||
|
||||
|
|
Loading…
Reference in a new issue