Skip to content

Commit

Permalink
refactor: various bug fixes and code removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Oct 2, 2021
1 parent 9089231 commit f02daa0
Show file tree
Hide file tree
Showing 29 changed files with 152 additions and 659 deletions.
12 changes: 0 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ thiserror = "1.0.24"
textwrap = "0.14.2"
toml = "0.5.8"
tui = { version = "0.16.0", features = ["crossterm"], default-features = false }
typed-builder = "0.9.0"
unicode-segmentation = "1.8.0"
unicode-width = "0.1"

Expand Down
16 changes: 5 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ pub use filter::*;
use layout_manager::*;
pub use widgets::*;

use crate::{
canvas, constants, units::data_units::DataUnit, utils::error::Result, BottomEvent, Pid,
};
use crate::{constants, units::data_units::DataUnit, utils::error::Result, BottomEvent, Pid};

use self::event::{ComponentEventResult, EventResult, ReturnSignal};

Expand Down Expand Up @@ -87,7 +85,7 @@ pub struct AppConfigFields {
pub hide_time: bool,
pub autohide_time: bool,
pub use_old_network_legend: bool,
pub table_gap: u16, // TODO: [Config, Refactor] Just make this a bool...
pub table_gap: bool,
pub disable_click: bool,
pub no_write: bool,
pub show_table_scroll_position: bool,
Expand All @@ -101,7 +99,7 @@ pub struct AppConfigFields {
/// the data collected at that instant.
pub enum FrozenState {
NotFrozen,
Frozen(DataCollection),
Frozen(Box<DataCollection>),
}

impl Default for FrozenState {
Expand All @@ -115,8 +113,6 @@ pub struct AppState {

to_delete_process_list: Option<(String, Vec<Pid>)>,

pub canvas_data: canvas::DisplayableData,

pub data_collection: DataCollection,

pub is_expanded: bool,
Expand Down Expand Up @@ -167,7 +163,6 @@ impl AppState {
// Use defaults.
dd_err: Default::default(),
to_delete_process_list: Default::default(),
canvas_data: Default::default(),
data_collection: Default::default(),
is_expanded: Default::default(),
delete_dialog_state: Default::default(),
Expand All @@ -186,7 +181,7 @@ impl AppState {
if matches!(self.frozen_state, FrozenState::Frozen(_)) {
self.frozen_state = FrozenState::NotFrozen;
} else {
self.frozen_state = FrozenState::Frozen(self.data_collection.clone());
self.frozen_state = FrozenState::Frozen(Box::new(self.data_collection.clone()));
}
}

Expand Down Expand Up @@ -418,7 +413,6 @@ impl AppState {

if was_id_already_selected {
returned_result = self.convert_widget_event_result(result);
break;
} else {
// If the weren't equal, *force* a redraw, and correct the layout tree.
correct_layout_last_selections(
Expand All @@ -427,8 +421,8 @@ impl AppState {
);
let _ = self.convert_widget_event_result(result);
returned_result = EventResult::Redraw;
break;
}
break;
}
SelectableType::Unselectable => {
let result = widget.handle_mouse_event(event);
Expand Down
13 changes: 5 additions & 8 deletions src/app/data_farmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ use crate::{
};
use regex::Regex;

pub type TimeOffset = f64;
pub type Value = f64;

#[derive(Clone, Debug, Default)]
pub struct TimedData {
pub rx_data: Value,
pub tx_data: Value,
pub cpu_data: Vec<Value>,
pub rx_data: f64,
pub tx_data: f64,
pub cpu_data: Vec<f64>,
pub load_avg_data: [f32; 3],
pub mem_data: Option<Value>,
pub swap_data: Option<Value>,
pub mem_data: Option<f64>,
pub swap_data: Option<f64>,
}

/// AppCollection represents the pooled data stored within the main app
Expand Down
6 changes: 4 additions & 2 deletions src/app/data_harvester/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ impl Default for ProcessSorting {
#[derive(Debug, Clone, Default)]
pub struct ProcessHarvest {
pub pid: Pid,
pub parent_pid: Option<Pid>, // Remember, parent_pid 0 is root...
pub parent_pid: Option<Pid>,
pub children_pids: Vec<Pid>,
pub cpu_usage_percent: f64,
pub mem_usage_percent: f64,
pub mem_usage_bytes: u64,
Expand All @@ -93,10 +94,11 @@ pub struct ProcessHarvest {
pub process_state: String,
pub process_state_char: char,

/// This is the *effective* user ID.
/// This is the *effective* user ID. This is only used on Unix platforms.
#[cfg(target_family = "unix")]
pub uid: libc::uid_t,

/// This is the process' user. This is only used on Unix platforms.
#[cfg(target_family = "unix")]
pub user: Cow<'static, str>,
}
1 change: 1 addition & 0 deletions src/app/data_harvester/processes/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ fn read_proc(
ProcessHarvest {
pid: process.pid,
parent_pid,
children_pids: vec![],
cpu_usage_percent,
mem_usage_percent,
mem_usage_bytes,
Expand Down
1 change: 1 addition & 0 deletions src/app/data_harvester/processes/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub fn get_process_data(
process_vector.push(ProcessHarvest {
pid: process_val.pid(),
parent_pid: process_val.parent(),
children_pids: vec![],
name,
command,
mem_usage_percent: if mem_total_kb > 0 {
Expand Down
1 change: 1 addition & 0 deletions src/app/data_harvester/processes/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn get_process_data(
process_vector.push(ProcessHarvest {
pid: process_val.pid(),
parent_pid: process_val.parent(),
children_pids: vec![],
name,
command,
mem_usage_percent: if mem_total_kb > 0 {
Expand Down
135 changes: 4 additions & 131 deletions src/app/layout_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,138 +13,13 @@ use fxhash::FxHashMap;
use indextree::{Arena, NodeId};
use std::cmp::min;
use tui::layout::Rect;
use typed_builder::*;

use crate::app::widgets::Widget;

use super::{
event::SelectionAction, AppConfigFields, CpuGraph, TimeGraph, TmpBottomWidget, UsedWidgets,
};

/// Represents a more usable representation of the layout, derived from the
/// config.
#[derive(Clone, Debug)]
pub struct BottomLayout {
pub rows: Vec<OldBottomRow>,
pub total_row_height_ratio: u32,
}

/// Represents a single row in the layout.
#[derive(Clone, Debug, TypedBuilder)]
pub struct OldBottomRow {
pub children: Vec<OldBottomCol>,

#[builder(default = 1)]
pub total_col_ratio: u32,

#[builder(default = 1)]
pub row_height_ratio: u32,

#[builder(default = false)]
pub canvas_handle_height: bool,

#[builder(default = false)]
pub flex_grow: bool,
}

/// Represents a single column in the layout. We assume that even if the column
/// contains only ONE element, it is still a column (rather than either a col or
/// a widget, as per the config, for simplicity's sake).
#[derive(Clone, Debug, TypedBuilder)]
pub struct OldBottomCol {
pub children: Vec<BottomColRow>,

#[builder(default = 1)]
pub total_col_row_ratio: u32,

#[builder(default = 1)]
pub col_width_ratio: u32,

#[builder(default = false)]
pub canvas_handle_width: bool,

#[builder(default = false)]
pub flex_grow: bool,
}

#[derive(Clone, Default, Debug, TypedBuilder)]
pub struct BottomColRow {
pub children: Vec<BottomWidget>,

#[builder(default = 1)]
pub total_widget_ratio: u32,

#[builder(default = 1)]
pub col_row_height_ratio: u32,

#[builder(default = false)]
pub canvas_handle_height: bool,

#[builder(default = false)]
pub flex_grow: bool,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum WidgetDirection {
Left,
Right,
Up,
Down,
}

impl WidgetDirection {
pub fn is_opposite(&self, other_direction: &WidgetDirection) -> bool {
match &self {
WidgetDirection::Left => *other_direction == WidgetDirection::Right,
WidgetDirection::Right => *other_direction == WidgetDirection::Left,
WidgetDirection::Up => *other_direction == WidgetDirection::Down,
WidgetDirection::Down => *other_direction == WidgetDirection::Up,
}
}
}

/// Represents a single widget.
#[derive(Debug, Default, Clone, TypedBuilder)]
pub struct BottomWidget {
pub widget_type: BottomWidgetType,
pub widget_id: u64,

#[builder(default = 1)]
pub width_ratio: u32,

#[builder(default = None)]
pub left_neighbour: Option<u64>,

#[builder(default = None)]
pub right_neighbour: Option<u64>,

#[builder(default = None)]
pub up_neighbour: Option<u64>,

#[builder(default = None)]
pub down_neighbour: Option<u64>,

/// If set to true, the canvas will override any ratios.
#[builder(default = false)]
pub canvas_handle_width: bool,

/// Whether we want this widget to take up all available room (and ignore any ratios).
#[builder(default = false)]
pub flex_grow: bool,

/// The value is the direction to bounce, as well as the parent offset.
#[builder(default = None)]
pub parent_reflector: Option<(WidgetDirection, u64)>,

/// Top left corner when drawn, for mouse click detection. (x, y)
#[builder(default = None)]
pub top_left_corner: Option<(u16, u16)>,

/// Bottom right corner when drawn, for mouse click detection. (x, y)
#[builder(default = None)]
pub bottom_right_corner: Option<(u16, u16)>,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum BottomWidgetType {
Empty,
Expand Down Expand Up @@ -238,8 +113,6 @@ Supported widget names:
}
}

// --- New stuff ---

/// Represents a row in the layout tree.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RowLayout {
Expand Down Expand Up @@ -380,7 +253,7 @@ pub fn create_layout_tree(
BottomWidgetType::Proc => {
widget_lookup_map.insert(
widget_id,
ProcessManager::new(process_defaults)
ProcessManager::new(process_defaults, app_config_fields)
.width(width)
.height(height)
.basic_mode(app_config_fields.use_basic_mode)
Expand All @@ -391,7 +264,7 @@ pub fn create_layout_tree(
BottomWidgetType::Temp => {
widget_lookup_map.insert(
widget_id,
TempTable::default()
TempTable::from_config(app_config_fields)
.set_temp_type(app_config_fields.temperature_type.clone())
.width(width)
.height(height)
Expand All @@ -403,7 +276,7 @@ pub fn create_layout_tree(
BottomWidgetType::Disk => {
widget_lookup_map.insert(
widget_id,
DiskTable::default()
DiskTable::from_config(app_config_fields)
.width(width)
.height(height)
.basic_mode(app_config_fields.use_basic_mode)
Expand Down Expand Up @@ -620,7 +493,7 @@ pub fn create_layout_tree(
///
/// We can do this by just going through the ancestors, starting from the widget itself.
pub fn correct_layout_last_selections(arena: &mut Arena<LayoutNode>, selected: NodeId) {
let mut selected_ancestors = selected.ancestors(&arena).collect::<Vec<_>>();
let mut selected_ancestors = selected.ancestors(arena).collect::<Vec<_>>();
let prev_node = selected_ancestors.pop();
if let Some(mut prev_node) = prev_node {
for node in selected_ancestors {
Expand Down
Loading

0 comments on commit f02daa0

Please sign in to comment.