Skip to content

Commit

Permalink
refactor: quick variable/struct/file rename (#1620)
Browse files Browse the repository at this point in the history
Some renames and file movement. No functional changes.
  • Loading branch information
ClementTsang authored Nov 8, 2024
1 parent ae14685 commit 02b947d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 50 deletions.
5 changes: 2 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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)]
Expand Down Expand Up @@ -671,7 +670,7 @@ impl App {
}
}

pub fn get_process_filter(&self, widget_id: u64) -> &Option<query::Query> {
pub fn get_process_filter(&self, widget_id: u64) -> &Option<ProcessQuery> {
if let Some(process_widget_state) = self.states.proc_state.widget_states.get(&widget_id) {
&process_widget_state.proc_search.search_state.query
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/app/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct AppSearchState {
pub size_mappings: IndexMap<usize, Range<usize>>,

/// The query. TODO: Merge this as one enum.
pub query: Option<Query>,
pub query: Option<ProcessQuery>,
pub error_message: Option<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BottomWidgetType>)> {
let cpu_left_legend = is_flag_enabled!(cpu_left_legend, args.cpu, config);
Expand Down
43 changes: 22 additions & 21 deletions src/widgets/process_table.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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::{
Expand Down Expand Up @@ -75,7 +76,7 @@ fn make_column(column: ProcColumn) -> SortColumn<ProcColumn> {

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),
Expand All @@ -89,9 +90,9 @@ fn make_column(column: ProcColumn) -> SortColumn<ProcColumn> {
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(),
}
Expand Down Expand Up @@ -247,7 +248,7 @@ impl ProcWidgetState {
ProcWidgetColumn::Cpu => CpuPercent,
ProcWidgetColumn::Mem => {
if mem_as_values {
MemoryVal
MemoryValue
} else {
MemoryPercent
}
Expand All @@ -262,9 +263,9 @@ impl ProcWidgetState {
#[cfg(feature = "gpu")]
ProcWidgetColumn::GpuMem => {
if mem_as_values {
GpuMem
GpuMemoryValue
} else {
GpuMemPercent
GpuMemoryPercent
}
}
#[cfg(feature = "gpu")]
Expand All @@ -280,7 +281,7 @@ impl ProcWidgetState {
if is_command { Command } else { Name },
CpuPercent,
if mem_as_values {
MemoryVal
MemoryValue
} else {
MemoryPercent
},
Expand All @@ -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,
Expand All @@ -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,
}
Expand Down Expand Up @@ -385,7 +386,7 @@ impl ProcWidgetState {
.unwrap_or(false)
}

fn get_query(&self) -> &Option<Query> {
fn get_query(&self) -> &Option<ProcessQuery> {
if self.proc_search.search_state.is_invalid_or_blank_search() {
&None
} else {
Expand Down Expand Up @@ -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!(),
}
Expand All @@ -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!(),
}
Expand Down Expand Up @@ -1291,7 +1292,7 @@ mod test {
];
let new_columns = vec![
ProcColumn::Pid,
ProcColumn::MemoryVal,
ProcColumn::MemoryValue,
ProcColumn::State,
ProcColumn::Name,
];
Expand All @@ -1316,7 +1317,7 @@ mod test {
];
let original_columns = vec![
ProcColumn::Pid,
ProcColumn::MemoryVal,
ProcColumn::MemoryValue,
ProcColumn::State,
ProcColumn::Name,
];
Expand Down Expand Up @@ -1381,7 +1382,7 @@ mod test {
];
let original_columns = vec![
ProcColumn::Pid,
ProcColumn::MemoryVal,
ProcColumn::MemoryValue,
ProcColumn::State,
ProcColumn::Name,
];
Expand Down Expand Up @@ -1422,7 +1423,7 @@ mod test {
ProcColumn::Name,
];
let new_columns = vec![
ProcColumn::MemoryVal,
ProcColumn::MemoryValue,
ProcColumn::Pid,
ProcColumn::State,
ProcColumn::Name,
Expand Down
22 changes: 11 additions & 11 deletions src/widgets/process_table/proc_widget_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum ProcColumn {
CpuPercent,
MemoryVal,
MemoryValue,
MemoryPercent,
Pid,
Count,
Expand All @@ -23,9 +23,9 @@ pub enum ProcColumn {
User,
Time,
#[cfg(feature = "gpu")]
GpuMem,
GpuMemoryValue,
#[cfg(feature = "gpu")]
GpuMemPercent,
GpuMemoryPercent,
#[cfg(feature = "gpu")]
GpuUtilPercent,
}
Expand All @@ -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",
Expand All @@ -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%",
}
Expand All @@ -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",
Expand All @@ -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%",
}
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)
});
Expand Down
14 changes: 10 additions & 4 deletions src/widgets/process_table/proc_widget_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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),
}
Expand All @@ -310,7 +312,9 @@ impl DataToCell<ProcColumn> 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(),
Expand All @@ -328,7 +332,9 @@ impl DataToCell<ProcColumn> 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(),
})
Expand Down
12 changes: 6 additions & 6 deletions src/app/query.rs → src/widgets/process_table/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ 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<Query> {
fn process_string_to_filter(query: &mut VecDeque<String>) -> QueryResult<Query> {
) -> QueryResult<ProcessQuery> {
fn process_string_to_filter(query: &mut VecDeque<String>) -> QueryResult<ProcessQuery> {
let lhs = process_or(query)?;
let mut list_of_ors = vec![lhs];

while query.front().is_some() {
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<String>) -> QueryResult<Or> {
Expand Down Expand Up @@ -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<Or>,
}

impl Query {
impl ProcessQuery {
fn process_regexes(
&mut self, is_searching_whole_word: bool, is_ignoring_case: bool,
is_searching_with_regex: bool,
Expand All @@ -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))
}
Expand Down

0 comments on commit 02b947d

Please sign in to comment.