From 622703f4b67748b18135674ecc16f81d4da44747 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sat, 29 Feb 2020 12:49:56 +0100 Subject: [PATCH 1/4] create config if doesnt exist --- src/constants.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 +++++--- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 6f7e119d..3e55ca31 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -54,3 +54,95 @@ pub const SEARCH_HELP_TEXT: [&str; 13] = [ "Alt-w/F2 Toggle whether to match the whole word\n", "Alt-r/F3 Toggle whether to use regex\n", ]; + +pub const DEFAULT_CONFIG_CONTENT: &str = r##" +# This is a default config file for bottom. All of the settings are commented +# out by default; if you wish to change them uncomment and modify as you see +# fit. + +# This group of options represents a command-line flag/option. Flags explicitly +# added when running (ie: btm -a) will override this config file if an option +# is also set here. +[flags] + +#avg_cpu = true +#dot_marker = false +#rate = 1000 +#left_legend = false +#current_usage = false +#group_processes = false +#case_sensitive = false +#whole_word = true +#regex = true +#show_disabled_data = true + +# Defaults to Celsius. Temperature is one of: +#temperature_type = "k" +#temperature_type = "f" +#temperature_type = "c" +#temperature_type = "kelvin" +#temperature_type = "fahrenheit" +#temperature_type = "celsius" + +# Defaults to processes. 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" + + +# These are all the components that support custom theming. Currently, it only +# supports taking in a string representing a hex colour. Note that colour support +# will, at the end of the day, depend on terminal support - for example, the +# macOS default Terminal does NOT like custom colours and it will glitch out. +# +# The default options here are based on gruvbox: https://github.com/morhetz/gruvbox +[colors] + +# Represents the colour of table headers (processes, CPU, disks, temperature). +#table_header_color="#458588" + +# Represents the colour of the label each widget has. +#widget_title_color="#cc241d" + +# Represents the average CPU color +#avg_cpu_color="#d3869b" + +# Represents the colour the core will use in the CPU legend and graph. +#cpu_core_colors=["#cc241d", "#98971a"] + +# Represents the colour RAM will use in the memory legend and graph. +#ram_color="#fb4934" + +# Represents the colour SWAP will use in the memory legend and graph. +#swap_color="#fabd2f" + +# Represents the colour rx will use in the network legend and graph. +#rx_color="#458588" + +# Represents the colour tx will use in the network legend and graph. +#tx_color="#689d6a" + +# Represents the colour of the border of unselected widgets. +#border_color="#ebdbb2" + +# Represents the colour of the border of selected widgets. +#highlighted_border_color="#fe8019" + +# Represents the colour of most text. +#text_color="#ebdbb2" + +# Represents the colour of text that is selected. +#selected_text_color="#282828" + +# Represents the background colour of text that is selected. +#selected_bg_color="#458588" + +# Represents the colour of the lines and text of the graph. +#graph_color="#ebdbb2" + +# Represents the cursor's colour. +#cursor_color="#458588" +"##; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4d65d2ca..bef966de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -425,7 +425,10 @@ fn create_logger() -> error::Result<()> { } fn create_config(flag_config_location: Option<&str>) -> error::Result { - use std::ffi::OsString; + use std::{ + ffi::OsString, + fs + }; let config_path = if let Some(conf_loc) = flag_config_location { OsString::from(conf_loc) } else if cfg!(target_os = "windows") { @@ -446,10 +449,14 @@ fn create_config(flag_config_location: Option<&str>) -> error::Result { let path = std::path::Path::new(&config_path); - if let Ok(config_str) = std::fs::read_to_string(path) { - Ok(toml::from_str(config_str.as_str())?) + if let Ok(config_string) = fs::read_to_string(path) { + Ok(toml::from_str(config_string.as_str())?) } else { - Ok(Config::default()) + if let Some(parent_path) = path.parent() { + fs::create_dir_all(parent_path)?; + } + fs::File::create(path)?.write_all(DEFAULT_CONFIG_CONTENT.as_bytes())?; + Ok(toml::from_str(DEFAULT_CONFIG_CONTENT)?) } } From 6a1fa8f379452630b1be1159afb1a445c2ae0583 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sat, 29 Feb 2020 12:54:57 +0100 Subject: [PATCH 2/4] rust fmt --- src/main.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index bef966de..ee4ff7cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,8 +18,7 @@ use crossterm::{ }, execute, style::Print, - terminal::LeaveAlternateScreen, - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen}, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; use std::{ @@ -425,10 +424,7 @@ fn create_logger() -> error::Result<()> { } fn create_config(flag_config_location: Option<&str>) -> error::Result { - use std::{ - ffi::OsString, - fs - }; + use std::{ffi::OsString, fs}; let config_path = if let Some(conf_loc) = flag_config_location { OsString::from(conf_loc) } else if cfg!(target_os = "windows") { @@ -512,7 +508,9 @@ fn get_temperature_option( } _ => { return Err(BottomError::ConfigError( - "Invalid temperature type. Please have the value be of the form ".to_string() + "Invalid temperature type. Please have the value be of the form \ + " + .to_string(), )); } } From f0da30f2cbc27e8005e1ff3692d3f3f0758fb9d3 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sat, 29 Feb 2020 13:06:49 +0100 Subject: [PATCH 3/4] added missing endline --- src/constants.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants.rs b/src/constants.rs index 3e55ca31..4a88d32a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -145,4 +145,4 @@ pub const DEFAULT_CONFIG_CONTENT: &str = r##" # Represents the cursor's colour. #cursor_color="#458588" -"##; \ No newline at end of file +"##; From 47cc34a8e2a53076e0a6eb3d498aace8d096224b Mon Sep 17 00:00:00 2001 From: shilangyu Date: Mon, 2 Mar 2020 22:07:41 +0100 Subject: [PATCH 4/4] reflected auto config creation --- README.md | 2 +- docs/config.md | 2 +- src/main.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 64794241..645234f9 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ Run using `btm`. - `-s`, `--show_disabled_data` will show data entries in the graph legends even if the lines for that entry are disabled. -- `-C`, `--config` takes in a file path leading to a TOML file. +- `-C`, `--config` takes in a file path leading to a TOML file. If doesn't exist, creates one. ### Keybindings diff --git a/docs/config.md b/docs/config.md index 08e171bf..e18db307 100644 --- a/docs/config.md +++ b/docs/config.md @@ -38,7 +38,7 @@ Note some colours may not be compatible with the terminal you are using. For exa ## Default config locations -bottom will check specific locations by default for a config file. +bottom will check specific locations by default for a config file. If no file is found, it will be created. - For Unix-based systems: `$HOME/.config/bottom/bottom.toml`. - For Windows: `{FOLDERID_RoamingAppData}\bottom\bottom.toml` (for example, `C:\Users\Clement\AppData\Roaming\bottom\bottom.toml`). diff --git a/src/main.rs b/src/main.rs index bc56fd7d..8f02e74a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,7 @@ fn get_matches() -> clap::ArgMatches<'static> { (@arg RATE_MILLIS: -r --rate +takes_value "Sets a refresh rate in milliseconds; the minimum is 250ms, defaults to 1000ms. Smaller values may take more resources.") (@arg LEFT_LEGEND: -l --left_legend "Puts external chart legends on the left side rather than the default right side.") (@arg USE_CURR_USAGE: -u --current_usage "Within Linux, sets a process' CPU usage to be based on the total current CPU usage, rather than assuming 100% usage.") - (@arg CONFIG_LOCATION: -C --config +takes_value "Sets the location of the config file. Expects a config file in the TOML format.") + (@arg CONFIG_LOCATION: -C --config +takes_value "Sets the location of the config file. Expects a config file in the TOML format. If doesn't exist, creates one.") //(@arg BASIC_MODE: -b --basic "Sets bottom to basic mode, not showing graphs and only showing basic tables.") // TODO: [FEATURE] Min mode (@arg GROUP_PROCESSES: -g --group "Groups processes with the same name together on launch.") (@arg CASE_SENSITIVE: -S --case_sensitive "Match case when searching by default.")