From 0c5a1d1ae02934f0b54665ab049f68cf6b4d9f64 Mon Sep 17 00:00:00 2001 From: ClementTsang <34804052+ClementTsang@users.noreply.github.com> Date: Mon, 29 Jan 2024 03:52:34 -0500 Subject: first big migration hurdle done --- src/options/args.rs | 93 +++++++++++++++++++++++------------- src/options/config.rs | 30 +++++++----- src/options/config/battery.rs | 1 - src/options/config/gpu.rs | 7 ++- src/options/config/layout.rs | 14 +++--- src/options/config/style/colours.rs | 2 +- src/options/config/style/palettes.rs | 5 -- src/options/config/temperature.rs | 4 ++ 8 files changed, 98 insertions(+), 58 deletions(-) (limited to 'src/options') diff --git a/src/options/args.rs b/src/options/args.rs index 7648b8e5..ee80a42c 100644 --- a/src/options/args.rs +++ b/src/options/args.rs @@ -1,9 +1,12 @@ -//! Argument parsing via clap. +//! Argument parsing via clap + config files. //! //! Note that you probably want to keep this as a single file so the build script doesn't //! trip all over itself. // TODO: New sections are misaligned! See if we can get that fixed. +// TODO: This might need some more work when we do config screens. For example, we can't just merge args + config, +// since we need to know the state of the config, the overwriting args, and adjust the calculated app settings as +// they change. use clap::*; use indoc::indoc; @@ -100,12 +103,13 @@ impl BottomArgs { /// Returns the config path if it is set. #[inline] pub fn config_path(&self) -> Option<&str> { - self.general.config_location.as_ref().map(|p| p.as_str()) + self.general.config_location.as_deref() } } +/// General arguments/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "General Options")] +#[command(next_help_heading = "General Options", rename_all = "snake_case")] pub(crate) struct GeneralArgs { #[arg( long, @@ -306,8 +310,9 @@ impl GeneralArgs { } } +/// Process arguments/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "Process Options")] +#[command(next_help_heading = "Process Options", rename_all = "snake_case")] pub(crate) struct ProcessArgs { #[arg( short = 'S', @@ -342,6 +347,13 @@ pub(crate) struct ProcessArgs { )] pub(crate) group_processes: Option, + #[arg( + long, + help = "Defaults to showing process memory usage by value.", + long_help = "Defaults to showing process memory usage by value. Otherwise, it defaults to showing it by percentage." + )] + pub(crate) mem_as_value: Option, + #[arg(long, help = "Show processes as their commands by default.")] pub(crate) process_command: Option, @@ -377,6 +389,7 @@ impl ProcessArgs { set_if_some!(current_usage, self, other); set_if_some!(disable_advanced_kill, self, other); set_if_some!(group_processes, self, other); + set_if_some!(mem_as_value, self, other); set_if_some!(process_command, self, other); set_if_some!(regex, self, other); set_if_some!(tree, self, other); @@ -385,9 +398,10 @@ impl ProcessArgs { } } +/// Temperature arguments/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "Temperature Options")] -#[group(multiple = false)] +#[command(next_help_heading = "Temperature Options", rename_all = "snake_case")] +#[group(id = "temperature_unit", multiple = false)] pub(crate) struct TemperatureArgs { #[arg( short = 'c', @@ -396,15 +410,17 @@ pub(crate) struct TemperatureArgs { help = "Use Celsius as the temperature unit. Default.", long_help = "Use Celsius as the temperature unit. This is the default option." )] - pub(crate) celsius: Option, + #[serde(skip)] + pub(crate) celsius: bool, #[arg( short = 'f', long, group = "temperature_unit", - help = "Use Fahrenheit as the temperature unit. Default." + help = "Use Fahrenheit as the temperature unit." )] - pub(crate) fahrenheit: Option, + #[serde(skip)] + pub(crate) fahrenheit: bool, #[arg( short = 'k', @@ -412,11 +428,21 @@ pub(crate) struct TemperatureArgs { group = "temperature_unit", help = "Use Kelvin as the temperature unit." )] - pub(crate) kelvin: Option, + #[serde(skip)] + pub(crate) kelvin: bool, +} + +impl TemperatureArgs { + pub(crate) fn merge(&mut self, other: &Self) { + self.celsius |= other.celsius; + self.fahrenheit |= other.fahrenheit; + self.kelvin |= other.kelvin; + } } +/// CPU arguments/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "CPU Options")] +#[command(next_help_heading = "CPU Options", rename_all = "snake_case")] pub(crate) struct CpuArgs { #[arg(long, help = "Defaults to selecting the average CPU entry.")] pub(crate) default_avg_cpu: Option, @@ -447,8 +473,9 @@ impl CpuArgs { } } +/// Memory argument/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "Memory Options")] +#[command(next_help_heading = "Memory Options", rename_all = "snake_case")] pub(crate) struct MemoryArgs { #[cfg(not(target_os = "windows"))] #[arg( @@ -456,25 +483,22 @@ pub(crate) struct MemoryArgs { help = "Enables collecting and displaying cache and buffer memory." )] pub(crate) enable_cache_memory: Option, - - #[arg( - long, - help = "Defaults to showing process memory usage by value.", - long_help = "Defaults to showing process memory usage by value. Otherwise, it defaults to showing it by percentage." - )] - pub(crate) mem_as_value: Option, } impl MemoryArgs { + // Lint needed because of target_os. + #[allow(unused_variables)] pub(crate) fn merge(&mut self, other: &Self) { + #[cfg(not(target_os = "windows"))] set_if_some!(enable_cache_memory, self, other); - set_if_some!(mem_as_value, self, other); } } +/// Network arguments/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "Network Options")] +#[command(next_help_heading = "Network Options", rename_all = "snake_case")] pub(crate) struct NetworkArgs { + // TODO: Rename some of these to remove the network prefix for serde. #[arg( long, help = "Displays the network widget using bytes.", @@ -514,9 +538,10 @@ impl NetworkArgs { } } +/// Battery arguments/config options. #[cfg(feature = "battery")] #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "Battery Options")] +#[command(next_help_heading = "Battery Options", rename_all = "snake_case")] pub(crate) struct BatteryArgs { #[arg( long, @@ -536,9 +561,10 @@ impl BatteryArgs { } } +/// GPU arguments/config options. #[cfg(feature = "gpu")] #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "GPU Options")] +#[command(next_help_heading = "GPU Options", rename_all = "snake_case")] pub(crate) struct GpuArgs { #[arg(long, help = "Enables collecting and displaying GPU usage.")] pub(crate) enable_gpu: Option, @@ -551,13 +577,14 @@ impl GpuArgs { } } +/// Style arguments/config options. #[derive(Args, Clone, Debug, Default, Deserialize)] -#[command(next_help_heading = "Style Options")] +#[command(next_help_heading = "Style Options", rename_all = "snake_case")] pub(crate) struct StyleArgs { #[arg( long, - value_name="SCHEME", - value_parser=[ + value_name = "SCHEME", + value_parser = [ "default", "default-light", "gruvbox", @@ -567,10 +594,10 @@ pub(crate) struct StyleArgs { "custom", ], - hide_possible_values=true, + hide_possible_values = true, help = "Use a color scheme, use --help for info on the colors.\n [possible values: default, default-light, gruvbox, gruvbox-light, nord, nord-light]", - long_help=indoc! { + long_help = indoc! { "Use a pre-defined color scheme. Currently supported values are: - default - default-light (default but adjusted for lighter backgrounds) @@ -590,13 +617,14 @@ impl StyleArgs { } } +/// Other arguments. This just handle options that are for help/version displaying. #[derive(Args, Clone, Debug)] -#[command(next_help_heading = "Other Options")] +#[command(next_help_heading = "Other Options", rename_all = "snake_case")] pub(crate) struct OtherArgs { - #[arg(short='h', long, action=ArgAction::Help, help="Prints help info (for more details use `--help`.")] + #[arg(short = 'h', long, action = ArgAction::Help, help = "Prints help info (for more details use `--help`.")] help: (), - #[arg(short='v', long, action=ArgAction::Version, help="Prints version information.")] + #[arg(short = 'v', long, action = ArgAction::Version, help = "Prints version information.")] version: (), } @@ -606,7 +634,8 @@ pub fn get_args() -> BottomArgs { } /// Returns an [`Command`] based off of [`BottomArgs`]. -fn build_cmd() -> Command { +#[allow(dead_code)] +pub(crate) fn build_cmd() -> Command { BottomArgs::command() } diff --git a/src/options/config.rs b/src/options/config.rs index e6e8e10a..f57d613d 100644 --- a/src/options/config.rs +++ b/src/options/config.rs @@ -1,6 +1,8 @@ +#[cfg(feature = "battery")] pub mod battery; pub mod cpu; pub mod general; +#[cfg(feature = "gpu")] pub mod gpu; pub mod layout; pub mod memory; @@ -14,20 +16,25 @@ use std::{fs, io::Write, path::PathBuf}; use serde::Deserialize; pub use style::*; +#[cfg(feature = "battery")] +use self::battery::BatteryConfig; + +#[cfg(feature = "gpu")] +use self::gpu::GpuConfig; + +use self::{ + colours::ColourConfig, cpu::CpuConfig, general::GeneralConfig, layout::Row, + memory::MemoryConfig, network::NetworkConfig, process::ProcessConfig, + temperature::TemperatureConfig, +}; use crate::{ args::BottomArgs, constants::{CONFIG_TEXT, DEFAULT_CONFIG_FILE_PATH}, error, }; -use self::{ - battery::BatteryConfig, colours::ColourConfig, cpu::CpuConfig, general::GeneralConfig, - gpu::GpuConfig, layout::Row, memory::MemoryConfig, network::NetworkConfig, - process::ProcessConfig, temperature::TemperatureConfig, -}; - #[derive(Debug, Default, Deserialize)] -pub struct NewConfig { +pub struct ConfigV2 { #[serde(default)] pub(crate) general: GeneralConfig, #[serde(default)] @@ -58,7 +65,7 @@ pub struct NewConfig { pub(crate) net_filter: Option, } -impl NewConfig { +impl ConfigV2 { /// Merges a [`BottomArgs`] with the internal shared "args" of the config file. /// /// In general, we favour whatever is set in `args` if it is set, then fall @@ -66,6 +73,7 @@ impl NewConfig { pub fn merge(&mut self, args: BottomArgs) { self.general.args.merge(&args.general); self.process.args.merge(&args.process); + self.temperature.args.merge(&args.temperature); self.cpu.args.merge(&args.cpu); self.memory.args.merge(&args.memory); self.network.args.merge(&args.network); @@ -134,7 +142,7 @@ pub fn get_config_path(override_path: Option<&str>) -> error::Result) -> error::Result { +pub fn create_or_get_config(config_path: &Option) -> error::Result { if let Some(path) = config_path { if let Ok(config_string) = fs::read_to_string(path) { Ok(toml_edit::de::from_str(config_string.as_str())?) @@ -144,10 +152,10 @@ pub fn create_or_get_config(config_path: &Option) -> error::Result bool { + self.args.enable_gpu.unwrap_or(false) + } +} diff --git a/src/options/config/layout.rs b/src/options/config/layout.rs index 90467e11..fa9caccf 100644 --- a/src/options/config/layout.rs +++ b/src/options/config/layout.rs @@ -238,7 +238,7 @@ mod test { use super::*; use crate::{ constants::{DEFAULT_LAYOUT, DEFAULT_WIDGET_ID}, - options::Config, + options::ConfigV2, utils::error, }; @@ -292,7 +292,7 @@ mod test { #[test] /// Tests the default setup. fn test_default_movement() { - let rows = from_str::(DEFAULT_LAYOUT).unwrap().row.unwrap(); + let rows = from_str::(DEFAULT_LAYOUT).unwrap().row.unwrap(); let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, false); // Simple tests for the top CPU widget @@ -366,7 +366,7 @@ mod test { fn test_default_battery_movement() { use crate::constants::DEFAULT_BATTERY_LAYOUT; - let rows = from_str::(DEFAULT_BATTERY_LAYOUT) + let rows = from_str::(DEFAULT_BATTERY_LAYOUT) .unwrap() .row .unwrap(); @@ -412,7 +412,7 @@ mod test { #[test] /// Tests using left_legend. fn test_left_legend() { - let rows = from_str::(DEFAULT_LAYOUT).unwrap().row.unwrap(); + let rows = from_str::(DEFAULT_LAYOUT).unwrap().row.unwrap(); let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, true); // Legend @@ -472,7 +472,7 @@ mod test { type="proc" "#; - let rows = from_str::(proc_layout).unwrap().row.unwrap(); + let rows = from_str::(proc_layout).unwrap().row.unwrap(); let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs* let mut total_height_ratio = 0; let mut default_widget_count = 1; @@ -505,7 +505,7 @@ mod test { #[test] /// Tests default widget by setting type and count. fn test_default_widget_by_option() { - let rows = from_str::(PROC_LAYOUT).unwrap().row.unwrap(); + let rows = from_str::(PROC_LAYOUT).unwrap().row.unwrap(); let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs* let mut total_height_ratio = 0; let mut default_widget_count = 3; @@ -537,7 +537,7 @@ mod test { #[test] fn test_proc_custom_layout() { - let rows = from_str::(PROC_LAYOUT).unwrap().row.unwrap(); + let rows = from_str::(PROC_LAYOUT).unwrap().row.unwrap(); let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, false); // First proc widget diff --git a/src/options/config/style/colours.rs b/src/options/config/style/colours.rs index 04a800d1..45d68459 100644 --- a/src/options/config/style/colours.rs +++ b/src/options/config/style/colours.rs @@ -31,7 +31,7 @@ pub struct ColourConfig { } impl ColourConfig { - /// Returns `true` if there is a [`ConfigColours`] that is empty or there isn't one at all. + /// Returns `true` if there is a [`ColourConfig`] 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(); diff --git a/src/options/config/style/palettes.rs b/src/options/config/style/palettes.rs index 2c913f94..44b35ecd 100644 --- a/src/options/config/style/palettes.rs +++ b/src/options/config/style/palettes.rs @@ -12,7 +12,6 @@ pub fn default_light_mode_colour_palette() -> ColourConfig { graph_color: Some("black".into()), disabled_text_color: Some("gray".into()), ram_color: Some("blue".into()), - #[cfg(not(target_os = "windows"))] cache_color: Some("LightRed".into()), swap_color: Some("red".into()), arc_color: Some("LightBlue".into()), @@ -70,7 +69,6 @@ pub fn gruvbox_colour_palette() -> ColourConfig { "#af3a03".into(), ]), ram_color: Some("#8ec07c".into()), - #[cfg(not(target_os = "windows"))] cache_color: Some("#b16286".into()), swap_color: Some("#fabd2f".into()), arc_color: Some("#689d6a".into()), @@ -129,7 +127,6 @@ pub fn gruvbox_light_colour_palette() -> ColourConfig { "#af3a03".into(), ]), ram_color: Some("#427b58".into()), - #[cfg(not(target_os = "windows"))] cache_color: Some("#d79921".into()), swap_color: Some("#cc241d".into()), arc_color: Some("#689d6a".into()), @@ -176,7 +173,6 @@ pub fn nord_colour_palette() -> ColourConfig { "#bf616a".into(), ]), ram_color: Some("#88c0d0".into()), - #[cfg(not(target_os = "windows"))] cache_color: Some("#d8dee9".into()), swap_color: Some("#d08770".into()), arc_color: Some("#5e81ac".into()), @@ -223,7 +219,6 @@ pub fn nord_light_colour_palette() -> ColourConfig { "#bf616a".into(), ]), ram_color: Some("#81a1c1".into()), - #[cfg(not(target_os = "windows"))] cache_color: Some("#4c566a".into()), swap_color: Some("#d08770".into()), arc_color: Some("#5e81ac".into()), diff --git a/src/options/config/temperature.rs b/src/options/config/temperature.rs index 90e9bb17..7ae359f9 100644 --- a/src/options/config/temperature.rs +++ b/src/options/config/temperature.rs @@ -1,6 +1,10 @@ use serde::Deserialize; +use crate::args::TemperatureArgs; + #[derive(Clone, Debug, Default, Deserialize)] pub(crate) struct TemperatureConfig { + #[serde(flatten)] + pub(crate) args: TemperatureArgs, pub(crate) temperature_type: Option, } -- cgit v1.2.3