From fe5f911ad3fd697eabebfc40d2958b5a3390ded2 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Thu, 13 Feb 2020 21:06:11 -0500 Subject: [PATCH] Added ability to set default highlighted widget. --- sample_config.toml | 13 ++++++++++++ src/app.rs | 4 ++-- src/main.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/sample_config.toml b/sample_config.toml index da5b4a7d..69c6cb22 100644 --- a/sample_config.toml +++ b/sample_config.toml @@ -1,7 +1,12 @@ [flags] avg_cpu = true dot_marker = false + +# Temperature is one of: +#temperature_type = "k" +#temperature_type = "f" temperature_type = "c" + rate = 1000 left_legend = false current_usage = false @@ -10,6 +15,14 @@ case_sensitive = false whole_word = true regex = true +# Default widget is one of: +default_widget = "cpu_default" +#default_widget = "memory_default" +#default_widget = "disk_default" +#default_widget = "temperature_default" +#default_widget = "network_default" +#default_widget = "process_default" + [colors] # Based on gruvbox: https://github.com/morhetz/gruvbox table_header_color="#458588" diff --git a/src/app.rs b/src/app.rs index 99ea5ee7..d971a6a7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -184,13 +184,13 @@ impl App { pub fn new( show_average_cpu: bool, temperature_type: temperature::TemperatureType, update_rate_in_milliseconds: u64, use_dot: bool, left_legend: bool, - use_current_cpu_total: bool, + use_current_cpu_total: bool, current_widget_selected: WidgetPosition, ) -> App { App { process_sorting_type: processes::ProcessSorting::CPU, process_sorting_reverse: true, update_process_gui: false, - current_widget_selected: WidgetPosition::Process, + current_widget_selected, app_scroll_positions: AppScrollState::default(), data: data_harvester::Data::default(), awaiting_second_char: false, diff --git a/src/main.rs b/src/main.rs index 1ff556c4..1841a3d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,6 +71,7 @@ struct ConfigFlags { case_sensitive: Option, whole_word: Option, regex: Option, + default_widget: Option, } #[derive(Default, Deserialize)] @@ -112,6 +113,14 @@ fn get_matches() -> clap::ArgMatches<'static> { (@arg CASE_SENSITIVE: -S --case_sensitive "Match case when searching by default.") (@arg WHOLE_WORD: -W --whole_word "Match whole word when searching by default.") (@arg REGEX_DEFAULT: -R --regex "Use regex in searching by default.") + (@group DEFAULT_WIDGET => + (@arg CPU_WIDGET: --cpu_default "Selects the CPU widget to be selected by default.") + (@arg MEM_WIDGET: --memory_default "Selects the memory widget to be selected by default.") + (@arg DISK_WIDGET: --disk_default "Selects the disk widget to be selected by default.") + (@arg TEMP_WIDGET: --temperature_default "Selects the temp widget to be selected by default.") + (@arg NET_WIDGET: --network_default "Selects the network widget to be selected by default.") + (@arg PROC_WIDGET: --process_default "Selects the process widget to be selected by default. This is the default if nothing is set.") + ) ) .get_matches() } @@ -132,6 +141,7 @@ fn main() -> error::Result<()> { let use_dot = get_use_dot_option(&matches, &config); let left_legend = get_use_left_legend_option(&matches, &config); let use_current_cpu_total = get_use_current_cpu_total_option(&matches, &config); + let current_widget_selected = get_default_widget(&matches, &config); // Create "app" struct, which will control most of the program and store settings/state let mut app = app::App::new( @@ -141,6 +151,7 @@ fn main() -> error::Result<()> { use_dot, left_legend, use_current_cpu_total, + current_widget_selected, ); enable_app_grouping(&matches, &config, &mut app); @@ -559,6 +570,48 @@ fn enable_app_use_regex(matches: &clap::ArgMatches<'static>, config: &Config, ap } } +fn get_default_widget(matches: &clap::ArgMatches<'static>, config: &Config) -> app::WidgetPosition { + if matches.is_present("CPU_WIDGET") { + return app::WidgetPosition::Cpu; + } else if matches.is_present("MEM_WIDGET") { + return app::WidgetPosition::Mem; + } else if matches.is_present("DISK_WIDGET") { + return app::WidgetPosition::Disk; + } else if matches.is_present("TEMP_WIDGET") { + return app::WidgetPosition::Temp; + } else if matches.is_present("NET_WIDGET") { + return app::WidgetPosition::Network; + } else if matches.is_present("PROC_WIDGET") { + return app::WidgetPosition::Process; + } else if let Some(flags) = &config.flags { + if let Some(default_widget) = &flags.default_widget { + match default_widget.to_lowercase().as_str() { + "cpu_default" => { + return app::WidgetPosition::Cpu; + } + "memory_default" => { + return app::WidgetPosition::Mem; + } + "processes_default" => { + return app::WidgetPosition::Process; + } + "network_default" => { + return app::WidgetPosition::Network; + } + "temperature_default" => { + return app::WidgetPosition::Temp; + } + "disk_default" => { + return app::WidgetPosition::Disk; + } + _ => {} + } + } + } + + app::WidgetPosition::Process +} + fn try_drawing( terminal: &mut tui::terminal::Terminal>, app: &mut app::App, painter: &mut canvas::Painter,