mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-25 22:55:06 +02:00
Fix cpu legend colouring (#83)
* Initial fix, but pending a rewrite. * Initial fix, but pending a rewrite. * Merged two separate iterations into one for cpu legend. * Refactor average cpu, fix bug with legend and cursor.
This commit is contained in:
parent
7156392665
commit
d07cee0f7e
@ -66,6 +66,7 @@ pub struct DataState {
|
|||||||
last_collection_time: Instant,
|
last_collection_time: Instant,
|
||||||
total_rx: u64,
|
total_rx: u64,
|
||||||
total_tx: u64,
|
total_tx: u64,
|
||||||
|
show_average_cpu: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DataState {
|
impl Default for DataState {
|
||||||
@ -82,6 +83,7 @@ impl Default for DataState {
|
|||||||
last_collection_time: Instant::now(),
|
last_collection_time: Instant::now(),
|
||||||
total_rx: 0,
|
total_rx: 0,
|
||||||
total_tx: 0,
|
total_tx: 0,
|
||||||
|
show_average_cpu: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,6 +97,10 @@ impl DataState {
|
|||||||
self.use_current_cpu_total = use_current_cpu_total;
|
self.use_current_cpu_total = use_current_cpu_total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_show_average_cpu(&mut self, show_average_cpu: bool) {
|
||||||
|
self.show_average_cpu = show_average_cpu;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init(&mut self) {
|
pub fn init(&mut self) {
|
||||||
self.mem_total_kb = self.sys.get_total_memory();
|
self.mem_total_kb = self.sys.get_total_memory();
|
||||||
futures::executor::block_on(self.update_data());
|
futures::executor::block_on(self.update_data());
|
||||||
@ -116,7 +122,7 @@ impl DataState {
|
|||||||
let current_instant = std::time::Instant::now();
|
let current_instant = std::time::Instant::now();
|
||||||
|
|
||||||
// CPU
|
// CPU
|
||||||
self.data.cpu = cpu::get_cpu_data_list(&self.sys);
|
self.data.cpu = cpu::get_cpu_data_list(&self.sys, self.show_average_cpu);
|
||||||
|
|
||||||
// Processes. This is the longest part of the harvesting process... changing this might be
|
// Processes. This is the longest part of the harvesting process... changing this might be
|
||||||
// good in the future. What was tried already:
|
// good in the future. What was tried already:
|
||||||
|
@ -8,13 +8,17 @@ pub struct CPUData {
|
|||||||
|
|
||||||
pub type CPUHarvest = Vec<CPUData>;
|
pub type CPUHarvest = Vec<CPUData>;
|
||||||
|
|
||||||
pub fn get_cpu_data_list(sys: &System) -> CPUHarvest {
|
pub fn get_cpu_data_list(sys: &System, show_average_cpu: bool) -> CPUHarvest {
|
||||||
let cpu_data = sys.get_processors();
|
let cpu_data = sys.get_processors();
|
||||||
let avg_cpu_usage = sys.get_global_processor_info().get_cpu_usage();
|
let avg_cpu_usage = sys.get_global_processor_info().get_cpu_usage();
|
||||||
let mut cpu_vec = vec![CPUData {
|
let mut cpu_vec = vec![];
|
||||||
cpu_name: "AVG".to_string(),
|
|
||||||
cpu_usage: avg_cpu_usage as f64,
|
if show_average_cpu {
|
||||||
}];
|
cpu_vec.push(CPUData {
|
||||||
|
cpu_name: "AVG".to_string(),
|
||||||
|
cpu_usage: avg_cpu_usage as f64,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for cpu in cpu_data {
|
for cpu in cpu_data {
|
||||||
cpu_vec.push(CPUData {
|
cpu_vec.push(CPUData {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -168,45 +169,42 @@ impl CpuGraphWidget for Painter {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let sliced_cpu_data = &cpu_data[start_position as usize..];
|
let sliced_cpu_data = &cpu_data[start_position as usize..];
|
||||||
let mut stringified_cpu_data: Vec<Vec<String>> = Vec::new();
|
|
||||||
|
|
||||||
for (itx, cpu) in sliced_cpu_data.iter().enumerate() {
|
let mut offset_scroll_index = (app_state
|
||||||
if app_state.cpu_state.is_showing_tray {
|
.app_scroll_positions
|
||||||
stringified_cpu_data.push(vec![
|
.cpu_scroll_state
|
||||||
cpu.cpu_name.clone(),
|
.current_scroll_position
|
||||||
|
- start_position) as usize;
|
||||||
|
let cpu_rows = sliced_cpu_data.iter().enumerate().filter_map(|(itx, cpu)| {
|
||||||
|
let cpu_string_row: Vec<Cow<'_, str>> = if app_state.cpu_state.is_showing_tray {
|
||||||
|
vec![
|
||||||
|
Cow::Borrowed(&cpu.cpu_name),
|
||||||
if app_state.cpu_state.core_show_vec[itx + start_position as usize] {
|
if app_state.cpu_state.core_show_vec[itx + start_position as usize] {
|
||||||
"[*]".to_string()
|
"[*]".into()
|
||||||
} else {
|
} else {
|
||||||
"[ ]".to_string()
|
"[ ]".into()
|
||||||
},
|
},
|
||||||
]);
|
]
|
||||||
} else if let Some(cpu_data) = cpu.cpu_data.last() {
|
} else if app_state.app_config_fields.show_disabled_data
|
||||||
if app_state.app_config_fields.show_disabled_data
|
|| app_state.cpu_state.core_show_vec[itx]
|
||||||
|| app_state.cpu_state.core_show_vec[itx]
|
{
|
||||||
{
|
vec![
|
||||||
stringified_cpu_data.push(vec![
|
Cow::Borrowed(&cpu.cpu_name),
|
||||||
cpu.cpu_name.clone(),
|
Cow::Borrowed(&cpu.legend_value),
|
||||||
format!("{:.0}%", cpu_data.1.round()),
|
]
|
||||||
]);
|
} else {
|
||||||
}
|
Vec::new()
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
let cpu_rows = stringified_cpu_data
|
if cpu_string_row.is_empty() {
|
||||||
.iter()
|
offset_scroll_index += 1;
|
||||||
.enumerate()
|
None
|
||||||
.map(|(itx, cpu_string_row)| {
|
} else {
|
||||||
Row::StyledData(
|
Some(Row::StyledData(
|
||||||
cpu_string_row.iter(),
|
cpu_string_row.into_iter(),
|
||||||
match app_state.current_widget_selected {
|
match app_state.current_widget_selected {
|
||||||
WidgetPosition::CpuLegend => {
|
WidgetPosition::CpuLegend => {
|
||||||
if itx as u64
|
if itx == offset_scroll_index {
|
||||||
== app_state
|
|
||||||
.app_scroll_positions
|
|
||||||
.cpu_scroll_state
|
|
||||||
.current_scroll_position
|
|
||||||
- start_position
|
|
||||||
{
|
|
||||||
self.colours.currently_selected_text_style
|
self.colours.currently_selected_text_style
|
||||||
} else if app_state.app_config_fields.show_average_cpu && itx == 0 {
|
} else if app_state.app_config_fields.show_average_cpu && itx == 0 {
|
||||||
self.colours.avg_colour_style
|
self.colours.avg_colour_style
|
||||||
@ -226,8 +224,9 @@ impl CpuGraphWidget for Painter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
))
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Calculate widths
|
// Calculate widths
|
||||||
let width = f64::from(draw_loc.width);
|
let width = f64::from(draw_loc.width);
|
||||||
|
@ -38,6 +38,7 @@ pub struct ConvertedCpuData {
|
|||||||
pub cpu_name: String,
|
pub cpu_name: String,
|
||||||
/// Tuple is time, value
|
/// Tuple is time, value
|
||||||
pub cpu_data: Vec<Point>,
|
pub cpu_data: Vec<Point>,
|
||||||
|
pub legend_value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn convert_temp_row(app: &App) -> Vec<Vec<String>> {
|
pub fn convert_temp_row(app: &App) -> Vec<Vec<String>> {
|
||||||
@ -102,8 +103,7 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> Vec<Vec<S
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn convert_cpu_data_points(
|
pub fn convert_cpu_data_points(
|
||||||
show_avg_cpu: bool, current_data: &data_farmer::DataCollection, display_time: u64,
|
current_data: &data_farmer::DataCollection, display_time: u64, is_frozen: bool,
|
||||||
is_frozen: bool,
|
|
||||||
) -> Vec<ConvertedCpuData> {
|
) -> Vec<ConvertedCpuData> {
|
||||||
let mut cpu_data_vector: Vec<ConvertedCpuData> = Vec::new();
|
let mut cpu_data_vector: Vec<ConvertedCpuData> = Vec::new();
|
||||||
let current_time = if is_frozen {
|
let current_time = if is_frozen {
|
||||||
@ -115,25 +115,22 @@ pub fn convert_cpu_data_points(
|
|||||||
} else {
|
} else {
|
||||||
current_data.current_instant
|
current_data.current_instant
|
||||||
};
|
};
|
||||||
let cpu_listing_offset = if show_avg_cpu { 0 } else { 1 };
|
|
||||||
|
|
||||||
for (time, data) in ¤t_data.timed_data_vec {
|
for (time, data) in ¤t_data.timed_data_vec {
|
||||||
let time_from_start: f64 =
|
let time_from_start: f64 =
|
||||||
(display_time as f64 - current_time.duration_since(*time).as_millis() as f64).floor();
|
(display_time as f64 - current_time.duration_since(*time).as_millis() as f64).floor();
|
||||||
|
|
||||||
for (itx, cpu) in data.cpu_data.iter().enumerate() {
|
for (itx, cpu) in data.cpu_data.iter().enumerate() {
|
||||||
if !show_avg_cpu && itx == 0 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the vector exists yet
|
// Check if the vector exists yet
|
||||||
let itx_offset = itx - cpu_listing_offset;
|
let itx_offset = itx;
|
||||||
if cpu_data_vector.len() <= itx_offset {
|
if cpu_data_vector.len() <= itx_offset {
|
||||||
cpu_data_vector.push(ConvertedCpuData::default());
|
cpu_data_vector.push(ConvertedCpuData::default());
|
||||||
cpu_data_vector[itx_offset].cpu_name =
|
cpu_data_vector[itx_offset].cpu_name =
|
||||||
current_data.cpu_harvest[itx].cpu_name.clone();
|
current_data.cpu_harvest[itx].cpu_name.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cpu_data_vector[itx_offset].legend_value = format!("{:.0}%", cpu.0.round());
|
||||||
|
|
||||||
//Insert joiner points
|
//Insert joiner points
|
||||||
for &(joiner_offset, joiner_val) in &cpu.1 {
|
for &(joiner_offset, joiner_val) in &cpu.1 {
|
||||||
let offset_time = time_from_start - joiner_offset as f64;
|
let offset_time = time_from_start - joiner_offset as f64;
|
||||||
|
21
src/main.rs
21
src/main.rs
@ -179,6 +179,7 @@ fn main() -> error::Result<()> {
|
|||||||
use_current_cpu_total,
|
use_current_cpu_total,
|
||||||
update_rate_in_milliseconds as u64,
|
update_rate_in_milliseconds as u64,
|
||||||
app.app_config_fields.temperature_type.clone(),
|
app.app_config_fields.temperature_type.clone(),
|
||||||
|
app.app_config_fields.show_average_cpu,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut painter = canvas::Painter::default();
|
let mut painter = canvas::Painter::default();
|
||||||
@ -242,22 +243,21 @@ fn main() -> error::Result<()> {
|
|||||||
app.canvas_data.mem_label = memory_and_swap_labels.0;
|
app.canvas_data.mem_label = memory_and_swap_labels.0;
|
||||||
app.canvas_data.swap_label = memory_and_swap_labels.1;
|
app.canvas_data.swap_label = memory_and_swap_labels.1;
|
||||||
|
|
||||||
|
// Pre-fill CPU if needed
|
||||||
|
if first_run {
|
||||||
|
let cpu_len = app.data_collection.cpu_harvest.len();
|
||||||
|
app.cpu_state.core_show_vec = vec![true; cpu_len];
|
||||||
|
app.cpu_state.num_cpus_shown = cpu_len as u64;
|
||||||
|
first_run = false;
|
||||||
|
}
|
||||||
|
|
||||||
// CPU
|
// CPU
|
||||||
app.canvas_data.cpu_data = convert_cpu_data_points(
|
app.canvas_data.cpu_data = convert_cpu_data_points(
|
||||||
app.app_config_fields.show_average_cpu,
|
|
||||||
&app.data_collection,
|
&app.data_collection,
|
||||||
app.cpu_state.display_time,
|
app.cpu_state.display_time,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Pre-fill CPU if needed
|
|
||||||
if first_run {
|
|
||||||
app.cpu_state.core_show_vec =
|
|
||||||
vec![true; app.canvas_data.cpu_data.len()];
|
|
||||||
app.cpu_state.num_cpus_shown = app.canvas_data.cpu_data.len() as u64;
|
|
||||||
first_run = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Processes
|
// Processes
|
||||||
let (single, grouped) = convert_process_data(&app.data_collection);
|
let (single, grouped) = convert_process_data(&app.data_collection);
|
||||||
app.canvas_data.process_data = single;
|
app.canvas_data.process_data = single;
|
||||||
@ -589,7 +589,6 @@ fn handle_force_redraws(app: &mut App) {
|
|||||||
|
|
||||||
if app.cpu_state.force_update {
|
if app.cpu_state.force_update {
|
||||||
app.canvas_data.cpu_data = convert_cpu_data_points(
|
app.canvas_data.cpu_data = convert_cpu_data_points(
|
||||||
app.app_config_fields.show_average_cpu,
|
|
||||||
&app.data_collection,
|
&app.data_collection,
|
||||||
app.cpu_state.display_time,
|
app.cpu_state.display_time,
|
||||||
app.is_frozen,
|
app.is_frozen,
|
||||||
@ -755,6 +754,7 @@ fn create_event_thread(
|
|||||||
>,
|
>,
|
||||||
rrx: std::sync::mpsc::Receiver<ResetEvent>, use_current_cpu_total: bool,
|
rrx: std::sync::mpsc::Receiver<ResetEvent>, use_current_cpu_total: bool,
|
||||||
update_rate_in_milliseconds: u64, temp_type: data_harvester::temperature::TemperatureType,
|
update_rate_in_milliseconds: u64, temp_type: data_harvester::temperature::TemperatureType,
|
||||||
|
show_average_cpu: bool,
|
||||||
) {
|
) {
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
@ -762,6 +762,7 @@ fn create_event_thread(
|
|||||||
data_state.init();
|
data_state.init();
|
||||||
data_state.set_temperature_type(temp_type);
|
data_state.set_temperature_type(temp_type);
|
||||||
data_state.set_use_current_cpu_total(use_current_cpu_total);
|
data_state.set_use_current_cpu_total(use_current_cpu_total);
|
||||||
|
data_state.set_show_average_cpu(show_average_cpu);
|
||||||
loop {
|
loop {
|
||||||
if let Ok(message) = rrx.try_recv() {
|
if let Ok(message) = rrx.try_recv() {
|
||||||
match message {
|
match message {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user