Merge pull request #57 from shilangyu/populate-config

create config if doesnt exist
This commit is contained in:
Clement Tsang 2020-03-04 23:41:23 -05:00 committed by GitHub
commit d1039996c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 10 deletions

View file

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

View file

@ -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`).

View file

@ -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"
"##;

View file

@ -25,8 +25,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 serde::Deserialize;
use tui::{backend::CrosstermBackend, Terminal};
@ -119,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.")
@ -426,7 +425,7 @@ fn create_logger() -> error::Result<()> {
}
fn create_config(flag_config_location: Option<&str>) -> error::Result<Config> {
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") {
@ -447,10 +446,14 @@ fn create_config(flag_config_location: Option<&str>) -> error::Result<Config> {
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)?)
}
}
@ -506,7 +509,9 @@ fn get_temperature_option(
}
_ => {
Err(BottomError::ConfigError(
"Invalid temperature type. Please have the value be of the form <kelvin|k|celsius|c|fahrenheit|f>".to_string()
"Invalid temperature type. Please have the value be of the form \
<kelvin|k|celsius|c|fahrenheit|f>"
.to_string(),
))
}
}