2
0
Fork 0
mirror of https://github.com/ClementTsang/bottom synced 2025-02-15 12:48:28 +00:00

Some cleaning to avoid duplicate code

This commit is contained in:
ClementTsang 2019-12-27 18:19:57 -05:00
parent a8bcccc8cf
commit 25d0ae45b4
7 changed files with 99 additions and 83 deletions

View file

@ -40,10 +40,10 @@ pub async fn get_network_data(
}
}
let cur_time = Instant::now();
let elapsed_time = cur_time.duration_since(*prev_net_access_time).as_millis();
let elapsed_time = cur_time.duration_since(*prev_net_access_time).as_secs_f64();
let rx = (((net_rx - *prev_net_rx_bytes) * 1000) as u128 / elapsed_time) as u64;
let tx = (((net_tx - *prev_net_tx_bytes) * 1000) as u128 / elapsed_time) as u64;
let rx = ((net_rx - *prev_net_rx_bytes) as f64 / elapsed_time) as u64;
let tx = ((net_tx - *prev_net_tx_bytes) as f64 / elapsed_time) as u64;
*prev_net_rx_bytes = net_rx;
*prev_net_tx_bytes = net_tx;

View file

@ -39,7 +39,7 @@ pub async fn get_temperature_data(sys: &System, temp_type: &TemperatureType) ->
});
}
}
} else if cfg!(target_os = "windows") {
} else {
let sensor_data = sys.get_components_list();
for component in sensor_data {
temperature_vec.push(TempData {

View file

@ -1,5 +1,4 @@
use crate::{app, constants, utils::error};
use std::cmp::Ordering;
use crate::{app, constants, utils::error, utils::gen_util::*};
use tui::{
backend,
layout::{Alignment, Constraint, Direction, Layout},
@ -12,7 +11,7 @@ const TEXT_COLOUR: Color = Color::Gray;
const GRAPH_COLOUR: Color = Color::Gray;
const BORDER_STYLE_COLOUR: Color = Color::Gray;
const HIGHLIGHTED_BORDER_STYLE_COLOUR: Color = Color::LightBlue;
const GOLDEN_RATIO: f32 = 0.618_034;
const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long)
lazy_static! {
static ref HELP_TEXT: [Text<'static>; 14] = [
@ -60,26 +59,6 @@ fn gen_n_colours(num_to_gen: i32) -> Vec<Color> {
new_val
}
}
fn float_min(a: f32, b: f32) -> f32 {
match a.partial_cmp(&b) {
Some(x) => match x {
Ordering::Greater => b,
Ordering::Less => a,
Ordering::Equal => a,
},
None => a,
}
}
fn float_max(a: f32, b: f32) -> f32 {
match a.partial_cmp(&b) {
Some(x) => match x {
Ordering::Greater => a,
Ordering::Less => b,
Ordering::Equal => a,
},
None => a,
}
}
/// This takes in an h, s, and v value of range [0, 1]
/// For explanation of what this does, see
/// https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative

View file

@ -1,4 +1,8 @@
use crate::{app::data_collection, constants};
use crate::{
app::data_collection,
constants,
utils::gen_util::{get_exact_byte_values, get_simple_byte_values},
};
use constants::*;
pub fn update_temp_row(app_data: &data_collection::Data, temp_type: &data_collection::temperature::TemperatureType) -> Vec<Vec<String>> {
@ -40,48 +44,33 @@ pub fn update_disk_row(app_data: &data_collection::Data) -> Vec<Vec<String>> {
let prev = &prev_io_hashmap[trimmed_mount];
let read_bytes_per_sec = ((ele.read_bytes - prev.read_bytes) as f64 / time_difference) as u64;
let write_bytes_per_sec = ((ele.write_bytes - prev.write_bytes) as f64 / time_difference) as u64;
let converted_read = get_simple_byte_values(read_bytes_per_sec);
let converted_write = get_simple_byte_values(write_bytes_per_sec);
(
if read_bytes_per_sec < 1000 {
format!("{}B", read_bytes_per_sec)
} else if read_bytes_per_sec < 1000 * 1000 {
format!("{}KB", read_bytes_per_sec / 1000)
} else {
format!("{}MB", read_bytes_per_sec / 1000 / 1000)
},
if write_bytes_per_sec < 1000 {
format!("{}B", write_bytes_per_sec)
} else if write_bytes_per_sec < 1000 * 1000 {
format!("{}KB", write_bytes_per_sec / 1000)
} else {
format!("{}MB", write_bytes_per_sec / 1000 / 1000)
},
format!("{:.*}{}/s", 0, converted_read.0, converted_read.1),
format!("{:.*}{}/s", 0, converted_write.0, converted_write.1),
)
} else {
("0B".to_string(), "0B".to_string())
("0B/s".to_string(), "0B/s".to_string())
}
} else {
("0B".to_string(), "0B".to_string())
("0B/s".to_string(), "0B/s".to_string())
}
} else {
("0B".to_string(), "0B".to_string())
("0B/s".to_string(), "0B/s".to_string())
}
} else {
("0B".to_string(), "0B".to_string())
("0B/s".to_string(), "0B/s".to_string())
};
let converted_free_space = get_simple_byte_values(disk.free_space);
let converted_total_space = get_simple_byte_values(disk.total_space);
disk_vector.push(vec![
disk.name.to_string(),
disk.mount_point.to_string(),
format!("{:.0}%", disk.used_space as f64 / disk.total_space as f64 * 100_f64),
if disk.free_space < 1000 {
disk.free_space.to_string() + "MB"
} else {
(disk.free_space / 1000).to_string() + "GB"
},
if disk.total_space < 1000 {
disk.total_space.to_string() + "MB"
} else {
(disk.total_space / 1000).to_string() + "GB"
},
format!("{:4.*}{}", 0, converted_free_space.0, converted_free_space.1),
format!("{:4.*}{}", 0, converted_total_space.0, converted_total_space.1),
io_activity.0,
io_activity.1,
]);
@ -237,6 +226,8 @@ pub struct ConvertedNetworkData {
pub tx: Vec<(f64, f64)>,
pub rx_display: String,
pub tx_display: String,
pub total_rx_display: String,
pub total_tx_display: String,
}
pub fn update_network_data_points(app_data: &data_collection::Data) -> ConvertedNetworkData {
@ -286,40 +277,37 @@ pub fn convert_network_data_points(network_data: &[data_collection::network::Net
tx.push(tx_data);
}
let rx_display = if let Some(last_num_bytes_entry) = network_data.last() {
let num_bytes = last_num_bytes_entry.rx;
if num_bytes < 1024 {
format!("RX: {:5.*} B/s", 1, num_bytes as f64)
} else if num_bytes < (1024 * 1024) {
format!("RX: {:5.*}KiB/s", 1, num_bytes as f64 / 1024.0)
} else if num_bytes < (1024 * 1024 * 1024) {
format!("RX: {:5.*}MiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0)
} else {
format!("RX: {:5.*}GiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0 / 1024.0)
}
} else {
"0.0B/s".to_string()
};
let total_rx_converted_result: (f64, String);
let rx_converted_result: (f64, String);
let total_tx_converted_result: (f64, String);
let tx_converted_result: (f64, String);
let tx_display = if let Some(last_num_bytes_entry) = network_data.last() {
let num_bytes = last_num_bytes_entry.tx;
if num_bytes < 1024 {
format!("TX: {:5.*} B/s", 1, num_bytes as f64)
} else if num_bytes < (1024 * 1024) {
format!("TX: {:5.*}KiB/s", 1, num_bytes as f64 / 1024.0)
} else if num_bytes < (1024 * 1024 * 1024) {
format!("TX: {:5.*}MiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0)
} else {
format!("TX: {:5.*}GiB/s", 1, num_bytes as f64 / 1024.0 / 1024.0 / 1024.0)
}
if let Some(last_num_bytes_entry) = network_data.last() {
rx_converted_result = get_exact_byte_values(last_num_bytes_entry.rx);
total_rx_converted_result = get_exact_byte_values(last_num_bytes_entry.total_rx);
} else {
"0.0B/s".to_string()
};
rx_converted_result = get_exact_byte_values(0);
total_rx_converted_result = get_exact_byte_values(0);
}
let rx_display = format!("RX: {:5.*}{}", 1, rx_converted_result.0, rx_converted_result.1);
let total_rx_display = format!("Total RX: {:5.*}{}", 1, total_rx_converted_result.0, total_rx_converted_result.1);
if let Some(last_num_bytes_entry) = network_data.last() {
tx_converted_result = get_exact_byte_values(last_num_bytes_entry.tx);
total_tx_converted_result = get_exact_byte_values(last_num_bytes_entry.total_tx);
} else {
tx_converted_result = get_exact_byte_values(0);
total_tx_converted_result = get_exact_byte_values(0);
}
let tx_display = format!("TX: {:5.*}{}", 1, tx_converted_result.0, tx_converted_result.1);
let total_tx_display = format!("Total TX: {:5.*}{}", 1, total_tx_converted_result.0, total_tx_converted_result.1);
ConvertedNetworkData {
rx,
tx,
rx_display,
tx_display,
total_rx_display,
total_tx_display,
}
}

View file

@ -25,6 +25,7 @@ use tui::{backend::CrosstermBackend, Terminal};
pub mod app;
mod utils {
pub mod error;
pub mod gen_util;
pub mod logging;
}
mod canvas;

View file

@ -1,2 +1,3 @@
pub mod error;
pub mod general_utility;
pub mod logging;

47
src/utils/gen_util.rs Normal file
View file

@ -0,0 +1,47 @@
use std::cmp::Ordering;
pub fn float_min(a: f32, b: f32) -> f32 {
match a.partial_cmp(&b) {
Some(x) => match x {
Ordering::Greater => b,
Ordering::Less => a,
Ordering::Equal => a,
},
None => a,
}
}
pub fn float_max(a: f32, b: f32) -> f32 {
match a.partial_cmp(&b) {
Some(x) => match x {
Ordering::Greater => a,
Ordering::Less => b,
Ordering::Equal => a,
},
None => a,
}
}
/// Returns a tuple containing the value and the unit. In units of 1024.
/// This only supports up to a tebibyte.
pub fn get_exact_byte_values(bytes: u64) -> (f64, String) {
match bytes {
b if b < 1024 => (bytes as f64, "B".to_string()),
b if b < 1_048_576 => (bytes as f64 / 1024.0, "KiB".to_string()),
b if b < 1_073_741_824 => (bytes as f64 / 1_048_576.0, "MiB".to_string()),
b if b < 1_099_511_627_776 => (bytes as f64 / 1_073_741_824.0, "GiB".to_string()),
_ => (bytes as f64 / 1_099_511_627_776.0, "TiB".to_string()),
}
}
/// Returns a tuple containing the value and the unit. In units of 1000.
/// This only supports up to a terabyte. Note the "byte" unit will have a space appended to match the others.
pub fn get_simple_byte_values(bytes: u64) -> (f64, String) {
match bytes {
b if b < 1000 => (bytes as f64, "B".to_string()),
b if b < 1_000_000 => (bytes as f64 / 1000.0, "KB".to_string()),
b if b < 1_000_000_000 => (bytes as f64 / 1_000_000.0, "MB".to_string()),
b if b < 1_000_000_000_000 => (bytes as f64 / 1_000_000_000.0, "GB".to_string()),
_ => (bytes as f64 / 1_000_000_000_000.0, "TB".to_string()),
}
}