From 975e3d776bad3d0111394ae7871b96c513b4f10c Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Thu, 18 Jan 2024 18:46:00 -0500 Subject: refactor: move some files around in prep for a future options refactor (#1393) * some formatting * refactor: move some files around in prep for a bigger config/options refactor --- src/canvas/styling.rs | 10 ++- src/options.rs | 144 ++------------------------------------ src/options/colours.rs | 43 ++++++++++++ src/options/config.rs | 81 +++++++++++++++++++++ src/options/config/ignore_list.rs | 21 ++++++ src/options/config/layout.rs | 3 +- 6 files changed, 157 insertions(+), 145 deletions(-) create mode 100644 src/options/colours.rs create mode 100644 src/options/config/ignore_list.rs (limited to 'src') diff --git a/src/canvas/styling.rs b/src/canvas/styling.rs index dec1800f..88273352 100644 --- a/src/canvas/styling.rs +++ b/src/canvas/styling.rs @@ -1,14 +1,12 @@ +mod colour_utils; + use anyhow::Context; use colour_utils::*; use tui::style::{Color, Style}; use super::ColourScheme; -use crate::{ - constants::*, - options::{Config, ConfigColours}, - utils::error, -}; -mod colour_utils; +pub use crate::options::Config; +use crate::{constants::*, options::colours::ConfigColours, utils::error}; pub struct CanvasStyling { pub currently_selected_text_colour: Color, diff --git a/src/options.rs b/src/options.rs index 95c780f6..092d982d 100644 --- a/src/options.rs +++ b/src/options.rs @@ -3,10 +3,10 @@ // TODO: Break this apart or do something a bit smarter. pub mod args; +pub mod colours; pub mod config; use std::{ - borrow::Cow, convert::TryInto, str::FromStr, time::{Duration, Instant}, @@ -14,14 +14,14 @@ use std::{ use anyhow::{Context, Result}; use clap::ArgMatches; +pub use colours::ConfigColours; use hashbrown::{HashMap, HashSet}; use indexmap::IndexSet; use regex::Regex; -use serde::{Deserialize, Serialize}; #[cfg(feature = "battery")] use starship_battery::Manager; -use self::config::{cpu::CpuConfig, layout::Row, process_columns::ProcessConfig}; +use self::config::{layout::Row, IgnoreList, StringOrNum}; use crate::{ app::{filter::Filter, layout_manager::*, *}, canvas::{styling::CanvasStyling, ColourScheme}, @@ -33,139 +33,7 @@ use crate::{ }, widgets::*, }; - -#[derive(Clone, Debug, Default, Deserialize)] -pub struct Config { - pub flags: Option, - pub colors: Option, - pub row: Option>, - pub disk_filter: Option, - pub mount_filter: Option, - pub temp_filter: Option, - pub net_filter: Option, - pub processes: Option, - pub cpu: Option, -} - -#[derive(Clone, Debug, Deserialize, Serialize)] -#[serde(untagged)] -enum StringOrNum { - String(String), - Num(u64), -} - -impl From for StringOrNum { - fn from(value: String) -> Self { - StringOrNum::String(value) - } -} - -impl From for StringOrNum { - fn from(value: u64) -> Self { - StringOrNum::Num(value) - } -} - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct ConfigFlags { - hide_avg_cpu: Option, - dot_marker: Option, - temperature_type: Option, - rate: Option, - left_legend: Option, - current_usage: Option, - unnormalized_cpu: Option, - group_processes: Option, - case_sensitive: Option, - whole_word: Option, - regex: Option, - basic: Option, - default_time_value: Option, - time_delta: Option, - autohide_time: Option, - hide_time: Option, - default_widget_type: Option, - default_widget_count: Option, - expanded_on_startup: Option, - use_old_network_legend: Option, - hide_table_gap: Option, - battery: Option, - disable_click: Option, - no_write: Option, - /// For built-in colour palettes. - color: Option, - mem_as_value: Option, - tree: Option, - show_table_scroll_position: Option, - process_command: Option, - disable_advanced_kill: Option, - network_use_bytes: Option, - network_use_log: Option, - network_use_binary_prefix: Option, - enable_gpu: Option, - enable_cache_memory: Option, - retention: Option, -} - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct ConfigColours { - pub table_header_color: Option>, - pub all_cpu_color: Option>, - pub avg_cpu_color: Option>, - pub cpu_core_colors: Option>>, - pub ram_color: Option>, - #[cfg(not(target_os = "windows"))] - pub cache_color: Option>, - pub swap_color: Option>, - pub arc_color: Option>, - pub gpu_core_colors: Option>>, - pub rx_color: Option>, - pub tx_color: Option>, - pub rx_total_color: Option>, // These only affect basic mode. - pub tx_total_color: Option>, // These only affect basic mode. - pub border_color: Option>, - pub highlighted_border_color: Option>, - pub disabled_text_color: Option>, - pub text_color: Option>, - pub selected_text_color: Option>, - pub selected_bg_color: Option>, - pub widget_title_color: Option>, - pub graph_color: Option>, - pub high_battery_color: Option>, - pub medium_battery_color: Option>, - pub low_battery_color: Option>, -} - -impl ConfigColours { - /// Returns `true` if there is a [`ConfigColours`] that is empty or there isn't one at all. - pub fn is_empty(&self) -> bool { - if let Ok(serialized_string) = toml_edit::ser::to_string(self) { - return serialized_string.is_empty(); - } - - true - } -} - -/// Workaround as per https://github.com/serde-rs/serde/issues/1030 -fn default_as_true() -> bool { - true -} - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct IgnoreList { - #[serde(default = "default_as_true")] - // TODO: Deprecate and/or rename, current name sounds awful. - // Maybe to something like "deny_entries"? Currently it defaults to a denylist anyways, so maybe "allow_entries"? - pub is_list_ignored: bool, - pub list: Vec, - #[serde(default = "bool::default")] - pub regex: bool, - #[serde(default = "bool::default")] - pub case_sensitive: bool, - #[serde(default = "bool::default")] - pub whole_word: bool, -} +pub use config::Config; macro_rules! is_flag_enabled { ($flag_name:ident, $matches:expr, $config:expr) => { @@ -261,6 +129,7 @@ pub fn init_app( } }; + // TODO: Can probably just reuse the options struct. let app_config_fields = AppConfigFields { update_rate: get_update_rate(matches, config) .context("Update 'rate' in your config file.")?, @@ -917,7 +786,8 @@ mod test { app::App, canvas::styling::CanvasStyling, options::{ - get_default_time_value, get_retention, get_update_rate, try_parse_ms, ConfigFlags, + config::ConfigFlags, get_default_time_value, get_retention, get_update_rate, + try_parse_ms, }, }; diff --git a/src/options/colours.rs b/src/options/colours.rs new file mode 100644 index 00000000..edef00b4 --- /dev/null +++ b/src/options/colours.rs @@ -0,0 +1,43 @@ +use std::borrow::Cow; + +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct ConfigColours { + pub table_header_color: Option>, + pub all_cpu_color: Option>, + pub avg_cpu_color: Option>, + pub cpu_core_colors: Option>>, + pub ram_color: Option>, + #[cfg(not(target_os = "windows"))] + pub cache_color: Option>, + pub swap_color: Option>, + pub arc_color: Option>, + pub gpu_core_colors: Option>>, + pub rx_color: Option>, + pub tx_color: Option>, + pub rx_total_color: Option>, // These only affect basic mode. + pub tx_total_color: Option>, // These only affect basic mode. + pub border_color: Option>, + pub highlighted_border_color: Option>, + pub disabled_text_color: Option>, + pub text_color: Option>, + pub selected_text_color: Option>, + pub selected_bg_color: Option>, + pub widget_title_color: Option>, + pub graph_color: Option>, + pub high_battery_color: Option>, + pub medium_battery_color: Option>, + pub low_battery_color: Option>, +} + +impl ConfigColours { + /// Returns `true` if there is a [`ConfigColours`] that is empty or there isn't one at all. + pub fn is_empty(&self) -> bool { + if let Ok(serialized_string) = toml_edit::ser::to_string(self) { + return serialized_string.is_empty(); + } + + true + } +} diff --git a/src/options/config.rs b/src/options/config.rs index e76d867c..23c2f956 100644 --- a/src/options/config.rs +++ b/src/options/config.rs @@ -1,3 +1,84 @@ pub mod cpu; +mod ignore_list; pub mod layout; pub mod process_columns; + +use serde::{Deserialize, Serialize}; + +pub use self::ignore_list::IgnoreList; +use self::{cpu::CpuConfig, layout::Row, process_columns::ProcessConfig}; + +use super::ConfigColours; + +#[derive(Clone, Debug, Default, Deserialize)] +pub struct Config { + pub(crate) flags: Option, + pub(crate) colors: Option, + pub(crate) row: Option>, + pub(crate) disk_filter: Option, + pub(crate) mount_filter: Option, + pub(crate) temp_filter: Option, + pub(crate) net_filter: Option, + pub(crate) processes: Option, + pub(crate) cpu: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(untagged)] +pub(crate) enum StringOrNum { + String(String), + Num(u64), +} + +impl From for StringOrNum { + fn from(value: String) -> Self { + StringOrNum::String(value) + } +} + +impl From for StringOrNum { + fn from(value: u64) -> Self { + StringOrNum::Num(value) + } +} + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub(crate) struct ConfigFlags { + pub(crate) hide_avg_cpu: Option, + pub(crate) dot_marker: Option, + pub(crate) temperature_type: Option, + pub(crate) rate: Option, + pub(crate) left_legend: Option, + pub(crate) current_usage: Option, + pub(crate) unnormalized_cpu: Option, + pub(crate) group_processes: Option, + pub(crate) case_sensitive: Option, + pub(crate) whole_word: Option, + pub(crate) regex: Option, + pub(crate) basic: Option, + pub(crate) default_time_value: Option, + pub(crate) time_delta: Option, + pub(crate) autohide_time: Option, + pub(crate) hide_time: Option, + pub(crate) default_widget_type: Option, + pub(crate) default_widget_count: Option, + pub(crate) expanded_on_startup: Option, + pub(crate) use_old_network_legend: Option, + pub(crate) hide_table_gap: Option, + pub(crate) battery: Option, + pub(crate) disable_click: Option, + pub(crate) no_write: Option, + /// For built-in colour palettes. + pub(crate) color: Option, + pub(crate) mem_as_value: Option, + pub(crate) tree: Option, + pub(crate) show_table_scroll_position: Option, + pub(crate) process_command: Option, + pub(crate) disable_advanced_kill: Option, + pub(crate) network_use_bytes: Option, + pub(crate) network_use_log: Option, + pub(crate) network_use_binary_prefix: Option, + pub(crate) enable_gpu: Option, + pub(crate) enable_cache_memory: Option, + pub(crate) retention: Option, +} diff --git a/src/options/config/ignore_list.rs b/src/options/config/ignore_list.rs new file mode 100644 index 00000000..d70224c1 --- /dev/null +++ b/src/options/config/ignore_list.rs @@ -0,0 +1,21 @@ +use serde::{Deserialize, Serialize}; + +/// Workaround as per https://github.com/serde-rs/serde/issues/1030 +fn default_as_true() -> bool { + true +} + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct IgnoreList { + #[serde(default = "default_as_true")] + // TODO: Deprecate and/or rename, current name sounds awful. + // Maybe to something like "deny_entries"? Currently it defaults to a denylist anyways, so maybe "allow_entries"? + pub is_list_ignored: bool, + pub list: Vec, + #[serde(default)] + pub regex: bool, + #[serde(default)] + pub case_sensitive: bool, + #[serde(default)] + pub whole_word: bool, +} diff --git a/src/options/config/layout.rs b/src/options/config/layout.rs index cfb1d705..90467e11 100644 --- a/src/options/config/layout.rs +++ b/src/options/config/layout.rs @@ -235,14 +235,13 @@ pub struct FinalWidget { mod test { use toml_edit::de::from_str; + use super::*; use crate::{ constants::{DEFAULT_LAYOUT, DEFAULT_WIDGET_ID}, options::Config, utils::error, }; - use super::*; - const PROC_LAYOUT: &str = r#" [[row]] [[row.child]] -- cgit v1.2.3