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
This commit is contained in:
Clement Tsang 2024-01-18 18:46:00 -05:00 committed by GitHub
parent be4fa27b84
commit 975e3d776b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 157 additions and 146 deletions

View File

@ -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,

View File

@ -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<ConfigFlags>,
pub colors: Option<ConfigColours>,
pub row: Option<Vec<Row>>,
pub disk_filter: Option<IgnoreList>,
pub mount_filter: Option<IgnoreList>,
pub temp_filter: Option<IgnoreList>,
pub net_filter: Option<IgnoreList>,
pub processes: Option<ProcessConfig>,
pub cpu: Option<CpuConfig>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum StringOrNum {
String(String),
Num(u64),
}
impl From<String> for StringOrNum {
fn from(value: String) -> Self {
StringOrNum::String(value)
}
}
impl From<u64> for StringOrNum {
fn from(value: u64) -> Self {
StringOrNum::Num(value)
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ConfigFlags {
hide_avg_cpu: Option<bool>,
dot_marker: Option<bool>,
temperature_type: Option<String>,
rate: Option<StringOrNum>,
left_legend: Option<bool>,
current_usage: Option<bool>,
unnormalized_cpu: Option<bool>,
group_processes: Option<bool>,
case_sensitive: Option<bool>,
whole_word: Option<bool>,
regex: Option<bool>,
basic: Option<bool>,
default_time_value: Option<StringOrNum>,
time_delta: Option<StringOrNum>,
autohide_time: Option<bool>,
hide_time: Option<bool>,
default_widget_type: Option<String>,
default_widget_count: Option<u64>,
expanded_on_startup: Option<bool>,
use_old_network_legend: Option<bool>,
hide_table_gap: Option<bool>,
battery: Option<bool>,
disable_click: Option<bool>,
no_write: Option<bool>,
/// For built-in colour palettes.
color: Option<String>,
mem_as_value: Option<bool>,
tree: Option<bool>,
show_table_scroll_position: Option<bool>,
process_command: Option<bool>,
disable_advanced_kill: Option<bool>,
network_use_bytes: Option<bool>,
network_use_log: Option<bool>,
network_use_binary_prefix: Option<bool>,
enable_gpu: Option<bool>,
enable_cache_memory: Option<bool>,
retention: Option<StringOrNum>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ConfigColours {
pub table_header_color: Option<Cow<'static, str>>,
pub all_cpu_color: Option<Cow<'static, str>>,
pub avg_cpu_color: Option<Cow<'static, str>>,
pub cpu_core_colors: Option<Vec<Cow<'static, str>>>,
pub ram_color: Option<Cow<'static, str>>,
#[cfg(not(target_os = "windows"))]
pub cache_color: Option<Cow<'static, str>>,
pub swap_color: Option<Cow<'static, str>>,
pub arc_color: Option<Cow<'static, str>>,
pub gpu_core_colors: Option<Vec<Cow<'static, str>>>,
pub rx_color: Option<Cow<'static, str>>,
pub tx_color: Option<Cow<'static, str>>,
pub rx_total_color: Option<Cow<'static, str>>, // These only affect basic mode.
pub tx_total_color: Option<Cow<'static, str>>, // These only affect basic mode.
pub border_color: Option<Cow<'static, str>>,
pub highlighted_border_color: Option<Cow<'static, str>>,
pub disabled_text_color: Option<Cow<'static, str>>,
pub text_color: Option<Cow<'static, str>>,
pub selected_text_color: Option<Cow<'static, str>>,
pub selected_bg_color: Option<Cow<'static, str>>,
pub widget_title_color: Option<Cow<'static, str>>,
pub graph_color: Option<Cow<'static, str>>,
pub high_battery_color: Option<Cow<'static, str>>,
pub medium_battery_color: Option<Cow<'static, str>>,
pub low_battery_color: Option<Cow<'static, str>>,
}
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<String>,
#[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,
},
};

43
src/options/colours.rs Normal file
View File

@ -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<Cow<'static, str>>,
pub all_cpu_color: Option<Cow<'static, str>>,
pub avg_cpu_color: Option<Cow<'static, str>>,
pub cpu_core_colors: Option<Vec<Cow<'static, str>>>,
pub ram_color: Option<Cow<'static, str>>,
#[cfg(not(target_os = "windows"))]
pub cache_color: Option<Cow<'static, str>>,
pub swap_color: Option<Cow<'static, str>>,
pub arc_color: Option<Cow<'static, str>>,
pub gpu_core_colors: Option<Vec<Cow<'static, str>>>,
pub rx_color: Option<Cow<'static, str>>,
pub tx_color: Option<Cow<'static, str>>,
pub rx_total_color: Option<Cow<'static, str>>, // These only affect basic mode.
pub tx_total_color: Option<Cow<'static, str>>, // These only affect basic mode.
pub border_color: Option<Cow<'static, str>>,
pub highlighted_border_color: Option<Cow<'static, str>>,
pub disabled_text_color: Option<Cow<'static, str>>,
pub text_color: Option<Cow<'static, str>>,
pub selected_text_color: Option<Cow<'static, str>>,
pub selected_bg_color: Option<Cow<'static, str>>,
pub widget_title_color: Option<Cow<'static, str>>,
pub graph_color: Option<Cow<'static, str>>,
pub high_battery_color: Option<Cow<'static, str>>,
pub medium_battery_color: Option<Cow<'static, str>>,
pub low_battery_color: Option<Cow<'static, str>>,
}
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
}
}

View File

@ -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<ConfigFlags>,
pub(crate) colors: Option<ConfigColours>,
pub(crate) row: Option<Vec<Row>>,
pub(crate) disk_filter: Option<IgnoreList>,
pub(crate) mount_filter: Option<IgnoreList>,
pub(crate) temp_filter: Option<IgnoreList>,
pub(crate) net_filter: Option<IgnoreList>,
pub(crate) processes: Option<ProcessConfig>,
pub(crate) cpu: Option<CpuConfig>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub(crate) enum StringOrNum {
String(String),
Num(u64),
}
impl From<String> for StringOrNum {
fn from(value: String) -> Self {
StringOrNum::String(value)
}
}
impl From<u64> 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<bool>,
pub(crate) dot_marker: Option<bool>,
pub(crate) temperature_type: Option<String>,
pub(crate) rate: Option<StringOrNum>,
pub(crate) left_legend: Option<bool>,
pub(crate) current_usage: Option<bool>,
pub(crate) unnormalized_cpu: Option<bool>,
pub(crate) group_processes: Option<bool>,
pub(crate) case_sensitive: Option<bool>,
pub(crate) whole_word: Option<bool>,
pub(crate) regex: Option<bool>,
pub(crate) basic: Option<bool>,
pub(crate) default_time_value: Option<StringOrNum>,
pub(crate) time_delta: Option<StringOrNum>,
pub(crate) autohide_time: Option<bool>,
pub(crate) hide_time: Option<bool>,
pub(crate) default_widget_type: Option<String>,
pub(crate) default_widget_count: Option<u64>,
pub(crate) expanded_on_startup: Option<bool>,
pub(crate) use_old_network_legend: Option<bool>,
pub(crate) hide_table_gap: Option<bool>,
pub(crate) battery: Option<bool>,
pub(crate) disable_click: Option<bool>,
pub(crate) no_write: Option<bool>,
/// For built-in colour palettes.
pub(crate) color: Option<String>,
pub(crate) mem_as_value: Option<bool>,
pub(crate) tree: Option<bool>,
pub(crate) show_table_scroll_position: Option<bool>,
pub(crate) process_command: Option<bool>,
pub(crate) disable_advanced_kill: Option<bool>,
pub(crate) network_use_bytes: Option<bool>,
pub(crate) network_use_log: Option<bool>,
pub(crate) network_use_binary_prefix: Option<bool>,
pub(crate) enable_gpu: Option<bool>,
pub(crate) enable_cache_memory: Option<bool>,
pub(crate) retention: Option<StringOrNum>,
}

View File

@ -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<String>,
#[serde(default)]
pub regex: bool,
#[serde(default)]
pub case_sensitive: bool,
#[serde(default)]
pub whole_word: bool,
}

View File

@ -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]]

View File

@ -1,7 +1,6 @@
use std::{env, ffi::OsString, path::Path, process::Command};
use hashbrown::HashMap;
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};