From 02b947dd2d101125c208ce3178bffdc1677f3427 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Thu, 7 Nov 2024 23:54:52 -0500 Subject: [PATCH] refactor: quick variable/struct/file rename (#1620) Some renames and file movement. No functional changes. --- src/app.rs | 5 +-- src/app/states.rs | 8 ++-- src/options.rs | 2 +- src/widgets/process_table.rs | 43 ++++++++++--------- .../process_table/proc_widget_column.rs | 22 +++++----- src/widgets/process_table/proc_widget_data.rs | 14 ++++-- src/{app => widgets/process_table}/query.rs | 12 +++--- 7 files changed, 56 insertions(+), 50 deletions(-) rename src/{app => widgets/process_table}/query.rs (99%) diff --git a/src/app.rs b/src/app.rs index b2d82fcc..3b3b28a7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,6 @@ pub mod filter; pub mod frozen_state; pub mod layout_manager; mod process_killer; -pub mod query; pub mod states; use std::{ @@ -28,7 +27,7 @@ use crate::{ data_conversion::ConvertedData, get_network_points, utils::data_units::DataUnit, - widgets::{ProcWidgetColumn, ProcWidgetMode}, + widgets::{query::ProcessQuery, ProcWidgetColumn, ProcWidgetMode}, }; #[derive(Debug, Clone, Eq, PartialEq, Default)] @@ -671,7 +670,7 @@ impl App { } } - pub fn get_process_filter(&self, widget_id: u64) -> &Option { + pub fn get_process_filter(&self, widget_id: u64) -> &Option { if let Some(process_widget_state) = self.states.proc_state.widget_states.get(&widget_id) { &process_widget_state.proc_search.search_state.query } else { diff --git a/src/app/states.rs b/src/app/states.rs index 28ce85c8..433d3b0a 100644 --- a/src/app/states.rs +++ b/src/app/states.rs @@ -6,11 +6,11 @@ use unicode_ellipsis::grapheme_width; use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete, UnicodeSegmentation}; use crate::{ - app::{layout_manager::BottomWidgetType, query::*}, + app::layout_manager::BottomWidgetType, constants, widgets::{ - BatteryWidgetState, CpuWidgetState, DiskTableWidget, MemWidgetState, NetWidgetState, - ProcWidgetState, TempWidgetState, + query::ProcessQuery, BatteryWidgetState, CpuWidgetState, DiskTableWidget, MemWidgetState, + NetWidgetState, ProcWidgetState, TempWidgetState, }, }; @@ -90,7 +90,7 @@ pub struct AppSearchState { pub size_mappings: IndexMap>, /// The query. TODO: Merge this as one enum. - pub query: Option, + pub query: Option, pub error_message: Option, } diff --git a/src/options.rs b/src/options.rs index 9ec5cd5d..ed857acc 100644 --- a/src/options.rs +++ b/src/options.rs @@ -519,7 +519,7 @@ pub(crate) fn init_app( )) } -pub fn get_widget_layout( +fn get_widget_layout( args: &BottomArgs, config: &Config, ) -> OptionResult<(BottomLayout, u64, Option)> { let cpu_left_legend = is_flag_enabled!(cpu_left_legend, args.cpu, config); diff --git a/src/widgets/process_table.rs b/src/widgets/process_table.rs index 6ad4ec98..d63b1055 100644 --- a/src/widgets/process_table.rs +++ b/src/widgets/process_table.rs @@ -1,5 +1,6 @@ pub mod proc_widget_column; pub mod proc_widget_data; +pub mod query; mod sort_table; use std::{borrow::Cow, collections::BTreeMap}; @@ -9,12 +10,12 @@ use indexmap::IndexSet; use itertools::Itertools; pub use proc_widget_column::*; pub use proc_widget_data::*; +use query::{parse_query, ProcessQuery}; use sort_table::SortTableColumn; use crate::{ app::{ data_farmer::{DataCollection, ProcessData}, - query::*, AppConfigFields, AppSearchState, }, canvas::components::data_table::{ @@ -75,7 +76,7 @@ fn make_column(column: ProcColumn) -> SortColumn { match column { CpuPercent => SortColumn::new(CpuPercent).default_descending(), - MemoryVal => SortColumn::new(MemoryVal).default_descending(), + MemoryValue => SortColumn::new(MemoryValue).default_descending(), MemoryPercent => SortColumn::new(MemoryPercent).default_descending(), Pid => SortColumn::new(Pid), Count => SortColumn::new(Count), @@ -89,9 +90,9 @@ fn make_column(column: ProcColumn) -> SortColumn { State => SortColumn::hard(State, 9), Time => SortColumn::new(Time), #[cfg(feature = "gpu")] - GpuMem => SortColumn::new(GpuMem).default_descending(), + GpuMemoryValue => SortColumn::new(GpuMemoryValue).default_descending(), #[cfg(feature = "gpu")] - GpuMemPercent => SortColumn::new(GpuMemPercent).default_descending(), + GpuMemoryPercent => SortColumn::new(GpuMemoryPercent).default_descending(), #[cfg(feature = "gpu")] GpuUtilPercent => SortColumn::new(GpuUtilPercent).default_descending(), } @@ -247,7 +248,7 @@ impl ProcWidgetState { ProcWidgetColumn::Cpu => CpuPercent, ProcWidgetColumn::Mem => { if mem_as_values { - MemoryVal + MemoryValue } else { MemoryPercent } @@ -262,9 +263,9 @@ impl ProcWidgetState { #[cfg(feature = "gpu")] ProcWidgetColumn::GpuMem => { if mem_as_values { - GpuMem + GpuMemoryValue } else { - GpuMemPercent + GpuMemoryPercent } } #[cfg(feature = "gpu")] @@ -280,7 +281,7 @@ impl ProcWidgetState { if is_command { Command } else { Name }, CpuPercent, if mem_as_values { - MemoryVal + MemoryValue } else { MemoryPercent }, @@ -305,7 +306,7 @@ impl ProcWidgetState { match col.inner() { CpuPercent => ProcWidgetColumn::Cpu, - MemoryVal | MemoryPercent => ProcWidgetColumn::Mem, + MemoryValue | MemoryPercent => ProcWidgetColumn::Mem, Pid | Count => ProcWidgetColumn::PidOrCount, Name | Command => ProcWidgetColumn::ProcNameOrCommand, ReadPerSecond => ProcWidgetColumn::ReadPerSecond, @@ -316,7 +317,7 @@ impl ProcWidgetState { User => ProcWidgetColumn::User, Time => ProcWidgetColumn::Time, #[cfg(feature = "gpu")] - GpuMem | GpuMemPercent => ProcWidgetColumn::GpuMem, + GpuMemoryValue | GpuMemoryPercent => ProcWidgetColumn::GpuMem, #[cfg(feature = "gpu")] GpuUtilPercent => ProcWidgetColumn::GpuUtil, } @@ -385,7 +386,7 @@ impl ProcWidgetState { .unwrap_or(false) } - fn get_query(&self) -> &Option { + fn get_query(&self) -> &Option { if self.proc_search.search_state.is_invalid_or_blank_search() { &None } else { @@ -741,11 +742,11 @@ impl ProcWidgetState { if let Some(index) = self.column_mapping.get_index_of(&ProcWidgetColumn::Mem) { if let Some(mem) = self.get_mut_proc_col(index) { match mem { - ProcColumn::MemoryVal => { + ProcColumn::MemoryValue => { *mem = ProcColumn::MemoryPercent; } ProcColumn::MemoryPercent => { - *mem = ProcColumn::MemoryVal; + *mem = ProcColumn::MemoryValue; } _ => unreachable!(), } @@ -758,11 +759,11 @@ impl ProcWidgetState { if let Some(index) = self.column_mapping.get_index_of(&ProcWidgetColumn::GpuMem) { if let Some(mem) = self.get_mut_proc_col(index) { match mem { - ProcColumn::GpuMem => { - *mem = ProcColumn::GpuMemPercent; + ProcColumn::GpuMemoryValue => { + *mem = ProcColumn::GpuMemoryPercent; } - ProcColumn::GpuMemPercent => { - *mem = ProcColumn::GpuMem; + ProcColumn::GpuMemoryPercent => { + *mem = ProcColumn::GpuMemoryValue; } _ => unreachable!(), } @@ -1291,7 +1292,7 @@ mod test { ]; let new_columns = vec![ ProcColumn::Pid, - ProcColumn::MemoryVal, + ProcColumn::MemoryValue, ProcColumn::State, ProcColumn::Name, ]; @@ -1316,7 +1317,7 @@ mod test { ]; let original_columns = vec![ ProcColumn::Pid, - ProcColumn::MemoryVal, + ProcColumn::MemoryValue, ProcColumn::State, ProcColumn::Name, ]; @@ -1381,7 +1382,7 @@ mod test { ]; let original_columns = vec![ ProcColumn::Pid, - ProcColumn::MemoryVal, + ProcColumn::MemoryValue, ProcColumn::State, ProcColumn::Name, ]; @@ -1422,7 +1423,7 @@ mod test { ProcColumn::Name, ]; let new_columns = vec![ - ProcColumn::MemoryVal, + ProcColumn::MemoryValue, ProcColumn::Pid, ProcColumn::State, ProcColumn::Name, diff --git a/src/widgets/process_table/proc_widget_column.rs b/src/widgets/process_table/proc_widget_column.rs index 1312a3f0..81b00592 100644 --- a/src/widgets/process_table/proc_widget_column.rs +++ b/src/widgets/process_table/proc_widget_column.rs @@ -9,7 +9,7 @@ use crate::{ #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] pub enum ProcColumn { CpuPercent, - MemoryVal, + MemoryValue, MemoryPercent, Pid, Count, @@ -23,9 +23,9 @@ pub enum ProcColumn { User, Time, #[cfg(feature = "gpu")] - GpuMem, + GpuMemoryValue, #[cfg(feature = "gpu")] - GpuMemPercent, + GpuMemoryPercent, #[cfg(feature = "gpu")] GpuUtilPercent, } @@ -34,7 +34,7 @@ impl ColumnHeader for ProcColumn { fn text(&self) -> Cow<'static, str> { match self { ProcColumn::CpuPercent => "CPU%", - ProcColumn::MemoryVal => "Mem", + ProcColumn::MemoryValue => "Mem", ProcColumn::MemoryPercent => "Mem%", ProcColumn::Pid => "PID", ProcColumn::Count => "Count", @@ -48,9 +48,9 @@ impl ColumnHeader for ProcColumn { ProcColumn::User => "User", ProcColumn::Time => "Time", #[cfg(feature = "gpu")] - ProcColumn::GpuMem => "GMem", + ProcColumn::GpuMemoryValue => "GMem", #[cfg(feature = "gpu")] - ProcColumn::GpuMemPercent => "GMem%", + ProcColumn::GpuMemoryPercent => "GMem%", #[cfg(feature = "gpu")] ProcColumn::GpuUtilPercent => "GPU%", } @@ -60,7 +60,7 @@ impl ColumnHeader for ProcColumn { fn header(&self) -> Cow<'static, str> { match self { ProcColumn::CpuPercent => "CPU%(c)", - ProcColumn::MemoryVal => "Mem(m)", + ProcColumn::MemoryValue => "Mem(m)", ProcColumn::MemoryPercent => "Mem%(m)", ProcColumn::Pid => "PID(p)", ProcColumn::Count => "Count", @@ -74,9 +74,9 @@ impl ColumnHeader for ProcColumn { ProcColumn::User => "User", ProcColumn::Time => "Time", #[cfg(feature = "gpu")] - ProcColumn::GpuMem => "GMem", + ProcColumn::GpuMemoryValue => "GMem", #[cfg(feature = "gpu")] - ProcColumn::GpuMemPercent => "GMem%", + ProcColumn::GpuMemoryPercent => "GMem%", #[cfg(feature = "gpu")] ProcColumn::GpuUtilPercent => "GPU%", } @@ -94,7 +94,7 @@ impl SortsRow for ProcColumn { sort_partial_fn(descending)(a.cpu_usage_percent, b.cpu_usage_percent) }); } - ProcColumn::MemoryVal | ProcColumn::MemoryPercent => { + ProcColumn::MemoryValue | ProcColumn::MemoryPercent => { data.sort_by(|a, b| sort_partial_fn(descending)(&a.mem_usage, &b.mem_usage)); } ProcColumn::Pid => { @@ -140,7 +140,7 @@ impl SortsRow for ProcColumn { data.sort_by(|a, b| sort_partial_fn(descending)(a.time, b.time)); } #[cfg(feature = "gpu")] - ProcColumn::GpuMem | ProcColumn::GpuMemPercent => { + ProcColumn::GpuMemoryValue | ProcColumn::GpuMemoryPercent => { data.sort_by(|a, b| { sort_partial_fn(descending)(&a.gpu_mem_usage, &b.gpu_mem_usage) }); diff --git a/src/widgets/process_table/proc_widget_data.rs b/src/widgets/process_table/proc_widget_data.rs index d2eb6b97..4bd30cbe 100644 --- a/src/widgets/process_table/proc_widget_data.rs +++ b/src/widgets/process_table/proc_widget_data.rs @@ -280,7 +280,7 @@ impl ProcWidgetData { fn to_string(&self, column: &ProcColumn) -> String { match column { ProcColumn::CpuPercent => format!("{:.1}%", self.cpu_usage_percent), - ProcColumn::MemoryVal | ProcColumn::MemoryPercent => self.mem_usage.to_string(), + ProcColumn::MemoryValue | ProcColumn::MemoryPercent => self.mem_usage.to_string(), ProcColumn::Pid => self.pid.to_string(), ProcColumn::Count => self.num_similar.to_string(), ProcColumn::Name | ProcColumn::Command => self.id.to_prefixed_string(), @@ -292,7 +292,9 @@ impl ProcWidgetData { ProcColumn::User => self.user.clone(), ProcColumn::Time => format_time(self.time), #[cfg(feature = "gpu")] - ProcColumn::GpuMem | ProcColumn::GpuMemPercent => self.gpu_mem_usage.to_string(), + ProcColumn::GpuMemoryValue | ProcColumn::GpuMemoryPercent => { + self.gpu_mem_usage.to_string() + } #[cfg(feature = "gpu")] ProcColumn::GpuUtilPercent => format!("{:.1}%", self.gpu_usage), } @@ -310,7 +312,9 @@ impl DataToCell for ProcWidgetData { // differences. Some(match column { ProcColumn::CpuPercent => format!("{:.1}%", self.cpu_usage_percent).into(), - ProcColumn::MemoryVal | ProcColumn::MemoryPercent => self.mem_usage.to_string().into(), + ProcColumn::MemoryValue | ProcColumn::MemoryPercent => { + self.mem_usage.to_string().into() + } ProcColumn::Pid => self.pid.to_string().into(), ProcColumn::Count => self.num_similar.to_string().into(), ProcColumn::Name | ProcColumn::Command => self.id.to_prefixed_string().into(), @@ -328,7 +332,9 @@ impl DataToCell for ProcWidgetData { ProcColumn::User => self.user.clone().into(), ProcColumn::Time => format_time(self.time).into(), #[cfg(feature = "gpu")] - ProcColumn::GpuMem | ProcColumn::GpuMemPercent => self.gpu_mem_usage.to_string().into(), + ProcColumn::GpuMemoryValue | ProcColumn::GpuMemoryPercent => { + self.gpu_mem_usage.to_string().into() + } #[cfg(feature = "gpu")] ProcColumn::GpuUtilPercent => format!("{:.1}%", self.gpu_usage).into(), }) diff --git a/src/app/query.rs b/src/widgets/process_table/query.rs similarity index 99% rename from src/app/query.rs rename to src/widgets/process_table/query.rs index fcb5f95d..a9189243 100644 --- a/src/app/query.rs +++ b/src/widgets/process_table/query.rs @@ -77,8 +77,8 @@ const AND_LIST: [&str; 2] = ["and", "&&"]; pub(crate) fn parse_query( search_query: &str, is_searching_whole_word: bool, is_ignoring_case: bool, is_searching_with_regex: bool, -) -> QueryResult { - fn process_string_to_filter(query: &mut VecDeque) -> QueryResult { +) -> QueryResult { + fn process_string_to_filter(query: &mut VecDeque) -> QueryResult { let lhs = process_or(query)?; let mut list_of_ors = vec![lhs]; @@ -86,7 +86,7 @@ pub(crate) fn parse_query( list_of_ors.push(process_or(query)?); } - Ok(Query { query: list_of_ors }) + Ok(ProcessQuery { query: list_of_ors }) } fn process_or(query: &mut VecDeque) -> QueryResult { @@ -532,12 +532,12 @@ pub(crate) fn parse_query( Ok(process_filter) } -pub struct Query { +pub struct ProcessQuery { /// Remember, AND > OR, but AND must come after OR when we parse. query: Vec, } -impl Query { +impl ProcessQuery { fn process_regexes( &mut self, is_searching_whole_word: bool, is_ignoring_case: bool, is_searching_with_regex: bool, @@ -560,7 +560,7 @@ impl Query { } } -impl Debug for Query { +impl Debug for ProcessQuery { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_fmt(format_args!("{:?}", self.query)) }