Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite column algorithm #227

Merged
merged 12 commits into from
Sep 10, 2020
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"choco",
"cmdline",
"commandline",
"concat",
"crossterm",
"curr",
"czvf",
Expand Down
3 changes: 2 additions & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
cognitive-complexity-threshold = 100
cognitive-complexity-threshold = 100
type-complexity-threshold = 500
4 changes: 3 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl App {
.columns
.toggle(&processes::ProcessSorting::Pid);

proc_widget_state.requires_redraw = true;
self.proc_state.force_update = Some(self.current_widget.widget_id);
}
}
Expand Down Expand Up @@ -502,6 +503,7 @@ impl App {
}

self.proc_state.force_update = Some(self.current_widget.widget_id);
proc_widget_state.requires_redraw = true;
}
}

Expand Down Expand Up @@ -1176,7 +1178,7 @@ impl App {
}
_ => {}
}

proc_widget_state.requires_redraw = true;
self.proc_state.force_update = Some(self.current_widget.widget_id);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/data_farmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl DataCollection {
}

fn eat_network(&mut self, network: &network::NetworkHarvest, new_entry: &mut TimedData) {
// FIXME [NETWORKING; CONFIG]: The ability to config this?
// FIXME [NETWORKING]: Support bits, support switching between decimal and binary units (move the log part to conversion and switch on the fly)
// RX
new_entry.rx_data = if network.rx > 0 {
Expand Down
9 changes: 6 additions & 3 deletions src/app/data_harvester/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use sysinfo::{ProcessorExt, System, SystemExt};

#[derive(Default, Debug, Clone)]
pub struct CpuData {
pub cpu_name: String,
pub cpu_prefix: String,
pub cpu_count: Option<usize>,
pub cpu_usage: f64,
}

Expand All @@ -15,14 +16,16 @@ pub fn get_cpu_data_list(sys: &System, show_average_cpu: bool) -> CpuHarvest {

if show_average_cpu {
cpu_vec.push(CpuData {
cpu_name: "AVG".to_string(),
cpu_prefix: "AVG".to_string(),
cpu_count: None,
cpu_usage: avg_cpu_usage as f64,
});
}

for (itx, cpu) in cpu_data.iter().enumerate() {
cpu_vec.push(CpuData {
cpu_name: format!("CPU{}", itx),
cpu_prefix: "CPU".to_string(),
cpu_count: Some(itx),
cpu_usage: f64::from(cpu.get_cpu_usage()),
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/data_harvester/disks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub async fn get_sysinfo_io_usage_list(
std::collections::HashMap::new();
Ok(Some(io_hash))

// TODO: Rename these functions to be like "get_arm_io_usage_list"

// TODO: Sysinfo disk I/O usage.
// ...sadly, this cannot be done as of now (other than me writing my own), it requires further
// work. See https://github.com/GuillaumeGomez/sysinfo/issues/304.
Expand Down
41 changes: 34 additions & 7 deletions src/app/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ impl AppSearchState {
}
}

/// Meant for canvas operations involving table column widths.
#[derive(Default)]
pub struct CanvasTableWidthState {
pub desired_column_widths: Vec<u16>,
pub calculated_column_widths: Vec<u16>,
}

/// ProcessSearchState only deals with process' search's current settings and state.
pub struct ProcessSearchState {
pub search_state: AppSearchState,
Expand Down Expand Up @@ -321,6 +328,9 @@ impl ProcColumn {
pub fn get_column_headers(
&self, proc_sorting_type: &ProcessSorting, sort_reverse: bool,
) -> Vec<String> {
const DOWN_ARROW: char = '▼';
const UP_ARROW: char = '▲';

// TODO: Gonna have to figure out how to do left/right GUI notation if we add it.
self.ordered_columns
.iter()
Expand All @@ -332,13 +342,20 @@ impl ProcColumn {
}

if mapping.enabled {
Some(if proc_sorting_type == column_type {
column_type.to_string()
+ command_str.as_str()
+ if sort_reverse { "▼" } else { "▲" }
} else {
column_type.to_string() + command_str.as_str()
})
Some(format!(
"{}{}{}",
column_type.to_string(),
command_str.as_str(),
if proc_sorting_type == column_type {
if sort_reverse {
DOWN_ARROW
} else {
UP_ARROW
}
} else {
' '
}
))
} else {
None
}
Expand All @@ -358,6 +375,8 @@ pub struct ProcWidgetState {
pub is_sort_open: bool,
pub columns: ProcColumn,
pub is_tree_mode: bool,
pub table_width_state: CanvasTableWidthState,
pub requires_redraw: bool,
}

impl ProcWidgetState {
Expand Down Expand Up @@ -397,6 +416,8 @@ impl ProcWidgetState {
is_sort_open: false,
columns,
is_tree_mode: false,
table_width_state: CanvasTableWidthState::default(),
requires_redraw: false,
}
}

Expand Down Expand Up @@ -595,6 +616,7 @@ pub struct CpuWidgetState {
pub autohide_timer: Option<Instant>,
pub scroll_state: AppScrollWidgetState,
pub is_multi_graph_mode: bool,
pub table_width_state: CanvasTableWidthState,
}

impl CpuWidgetState {
Expand All @@ -605,6 +627,7 @@ impl CpuWidgetState {
autohide_timer,
scroll_state: AppScrollWidgetState::default(),
is_multi_graph_mode: false,
table_width_state: CanvasTableWidthState::default(),
}
}
}
Expand Down Expand Up @@ -668,12 +691,14 @@ impl MemState {

pub struct TempWidgetState {
pub scroll_state: AppScrollWidgetState,
pub table_width_state: CanvasTableWidthState,
}

impl TempWidgetState {
pub fn init() -> Self {
TempWidgetState {
scroll_state: AppScrollWidgetState::default(),
table_width_state: CanvasTableWidthState::default(),
}
}
}
Expand All @@ -698,12 +723,14 @@ impl TempState {

pub struct DiskWidgetState {
pub scroll_state: AppScrollWidgetState,
pub table_width_state: CanvasTableWidthState,
}

impl DiskWidgetState {
pub fn init() -> Self {
DiskWidgetState {
scroll_state: AppScrollWidgetState::default(),
table_width_state: CanvasTableWidthState::default(),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn main() -> Result<()> {
termination_hook();
})
.unwrap();
let mut first_run = true;

while !is_terminated.load(Ordering::SeqCst) {
if let Ok(recv) = receiver.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
Expand All @@ -123,6 +124,13 @@ fn main() -> Result<()> {
BottomEvent::Update(data) => {
app.data_collection.eat_data(&data);

// This thing is required as otherwise, some widgets can't draw correctly w/o
// some data (or they need to be re-drawn).
if first_run {
first_run = false;
app.is_force_redraw = true;
}

if !app.is_frozen {
// Convert all data into tui-compliant components

Expand Down
28 changes: 10 additions & 18 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct DisplayableData {
pub temp_sensor_data: Vec<Vec<String>>,
pub single_process_data: Vec<ConvertedProcessData>, // Contains single process data
pub finalized_process_data_map: HashMap<u64, Vec<ConvertedProcessData>>, // What's actually displayed
pub stringified_process_data_map: HashMap<u64, Vec<(Vec<(String, Option<String>)>, bool)>>, // Represents the row and whether it is disabled
pub mem_label_percent: String,
pub swap_label_percent: String,
pub mem_label_frac: String,
Expand All @@ -67,7 +68,6 @@ pub struct Painter {
widget_layout: BottomLayout,
derived_widget_draw_locs: Vec<Vec<Vec<Vec<Rect>>>>,
table_height_offset: u16,
requires_boundary_recalculation: bool,
}

impl Painter {
Expand Down Expand Up @@ -152,7 +152,6 @@ impl Painter {
widget_layout,
derived_widget_draw_locs: Vec::default(),
table_height_offset: if is_basic_mode { 2 } else { 4 } + table_gap,
requires_boundary_recalculation: true,
}
}

Expand Down Expand Up @@ -401,25 +400,18 @@ impl Painter {
// Basic mode. This basically removes all graphs but otherwise
// the same info.

let cpu_height = (app_state.canvas_data.cpu_data.len() / 4) as u16
+ (if app_state.canvas_data.cpu_data.len() % 4 == 0 {
0
} else {
1
});

// A little hack to force the widget boundary recalculation. This is required here
// as basic mode has a height of 0 initially, which breaks things.
if self.requires_boundary_recalculation {
app_state.is_determining_widget_boundary = true;
}
self.requires_boundary_recalculation = cpu_height == 0;

let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Length(cpu_height),
Constraint::Length(
(app_state.canvas_data.cpu_data.len() / 4) as u16
+ (if app_state.canvas_data.cpu_data.len() % 4 == 0 {
0
} else {
1
}),
),
Constraint::Length(1),
Constraint::Length(2),
Constraint::Length(2),
Expand Down Expand Up @@ -486,7 +478,7 @@ impl Painter {
self.draw_basic_table_arrows(&mut f, app_state, vertical_chunks[3], widget_id);
}
} else {
// Draws using the passed in (or default) layout. NOT basic so far.
// Draws using the passed in (or default) layout.
if self.derived_widget_draw_locs.is_empty() || app_state.is_force_redraw {
let row_draw_locs = Layout::default()
.margin(0)
Expand Down
Loading