summaryrefslogtreecommitdiffstats
path: root/src/options
diff options
context:
space:
mode:
authorClementTsang <34804052+ClementTsang@users.noreply.github.com>2024-01-29 03:52:34 -0500
committerClementTsang <34804052+ClementTsang@users.noreply.github.com>2024-02-19 20:08:54 -0500
commit0c5a1d1ae02934f0b54665ab049f68cf6b4d9f64 (patch)
tree25a125518a40d6f25ed989b47d2aaaf77a82c2eb /src/options
parentc1119e794232d12283900ada494edb1f6f9c0b39 (diff)
first big migration hurdle done
Diffstat (limited to 'src/options')
-rw-r--r--src/options/args.rs93
-rw-r--r--src/options/config.rs30
-rw-r--r--src/options/config/battery.rs1
-rw-r--r--src/options/config/gpu.rs7
-rw-r--r--src/options/config/layout.rs14
-rw-r--r--src/options/config/style/colours.rs2
-rw-r--r--src/options/config/style/palettes.rs5
-rw-r--r--src/options/config/temperature.rs4
8 files changed, 98 insertions, 58 deletions
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<bool>,
+ #[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<bool>,
+
#[arg(long, help = "Show processes as their commands by default.")]
pub(crate) process_command: Option<bool>,
@@ -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<bool>,
+ #[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<bool>,
+ #[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<bool>,
+ #[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<bool>,
@@ -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<bool>,
-
- #[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<bool>,
}
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<bool>,
@@ -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<IgnoreList>,
}
-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<Option<Path
}
/// Either get an existing config or create a new one, and parse it.
-pub fn create_or_get_config(config_path: &Option<PathBuf>) -> error::Result<NewConfig> {
+pub fn create_or_get_config(config_path: &Option<PathBuf>) -> error::Result<ConfigV2> {
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<PathBuf>) -> error::Result<NewC
}
fs::File::create(path)?.write_all(CONFIG_TEXT.as_bytes())?;
- Ok(NewConfig::default())
+ Ok(ConfigV2::default())
}
} else {
// Don't write anything, just assume the default.
- Ok(NewConfig::default())
+ Ok(ConfigV2::default())
}
}
diff --git a/src/options/config/battery.rs b/src/options/config/battery.rs
index ac11fe69..6420a02e 100644
--- a/src/options/config/battery.rs
+++ b/src/options/config/battery.rs
@@ -2,7 +2,6 @@ use serde::Deserialize;
use crate::args::BatteryArgs;
-#[cfg(feature = "battery")]
#[derive(Clone, Debug, Default, Deserialize)]
pub(crate) struct BatteryConfig {
pub(crate) args: BatteryArgs,
diff --git a/src/options/config/gpu.rs b/src/options/config/gpu.rs
index 114eaf46..40b979fa 100644
--- a/src/options/config/gpu.rs
+++ b/src/options/config/gpu.rs
@@ -2,9 +2,14 @@ use serde::Deserialize;
use crate::args::GpuArgs;
-#[cfg(feature = "gpu")]
#[derive(Clone, Debug, Default, Deserialize)]
pub(crate) struct GpuConfig {
#[serde(flatten)]
pub(crate) args: GpuArgs,
}
+
+impl GpuConfig {
+ pub(crate) fn enabled(&self) -> 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::<Config>(DEFAULT_LAYOUT).unwrap().row.unwrap();
+ let rows = from_str::<ConfigV2>(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::<Config>(DEFAULT_BATTERY_LAYOUT)
+ let rows = from_str::<ConfigV2>(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::<Config>(DEFAULT_LAYOUT).unwrap().row.unwrap();
+ let rows = from_str::<ConfigV2>(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::<Config>(proc_layout).unwrap().row.unwrap();
+ let rows = from_str::<ConfigV2>(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::<Config>(PROC_LAYOUT).unwrap().row.unwrap();
+ let rows = from_str::<ConfigV2>(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::<Config>(PROC_LAYOUT).unwrap().row.unwrap();
+ let rows = from_str::<ConfigV2>(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<String>,
}