feat(command): add 'toggle' command (#1917)

Closes #894

Signed-off-by: Dentrax <furkan.turkal@hotmail.com>
This commit is contained in:
Furkan Türkal 2021-01-07 21:04:06 +03:00 committed by GitHub
parent 8cd4850ab6
commit f03c3f1de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 19 deletions

View File

@ -8,6 +8,7 @@ use crate::config::StarshipConfig;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use toml::map::Map; use toml::map::Map;
use toml::value::Table;
use toml::Value; use toml::Value;
#[cfg(not(windows))] #[cfg(not(windows))]
@ -16,20 +17,13 @@ const STD_EDITOR: &str = "vi";
const STD_EDITOR: &str = "notepad.exe"; const STD_EDITOR: &str = "notepad.exe";
pub fn update_configuration(name: &str, value: &str) { pub fn update_configuration(name: &str, value: &str) {
let config_path = get_config_path();
let keys: Vec<&str> = name.split('.').collect(); let keys: Vec<&str> = name.split('.').collect();
if keys.len() != 2 { if keys.len() != 2 {
log::error!("Please pass in a config key with a '.'"); log::error!("Please pass in a config key with a '.'");
process::exit(1); process::exit(1);
} }
let starship_config = StarshipConfig::initialize(); if let Some(table) = get_configuration().as_table_mut() {
let mut config = starship_config
.config
.expect("Failed to load starship config");
if let Some(table) = config.as_table_mut() {
if !table.contains_key(keys[0]) { if !table.contains_key(keys[0]) {
table.insert(keys[0].to_string(), Value::Table(Map::new())); table.insert(keys[0].to_string(), Value::Table(Map::new()));
} }
@ -54,14 +48,68 @@ pub fn update_configuration(name: &str, value: &str) {
table.insert(keys[0].to_string(), Value::Table(updated_values)); table.insert(keys[0].to_string(), Value::Table(updated_values));
} }
let config_str = write_configuration(table);
toml::to_string_pretty(&table).expect("Failed to serialize the config to string");
File::create(&config_path)
.and_then(|mut file| file.write_all(config_str.as_ref()))
.expect("Error writing starship config");
} }
} }
pub fn toggle_configuration(name: &str, key: &str) {
if let Some(table) = get_configuration().as_table_mut() {
match table.get(name) {
Some(v) => {
if let Some(values) = v.as_table() {
let mut updated_values = values.clone();
let current: bool = match updated_values.get(key) {
Some(v) => match v.as_bool() {
Some(b) => b,
_ => {
log::error!(
"Given config key '{}' must be in 'boolean' format",
key
);
process::exit(1);
}
},
_ => {
log::error!("Given config key '{}' must be exist in config file", key);
process::exit(1);
}
};
updated_values.insert(key.to_string(), Value::Boolean(!current));
table.insert(name.to_string(), Value::Table(updated_values));
write_configuration(table);
}
}
_ => {
log::error!("Given module '{}' not found in config file", name);
process::exit(1);
}
};
}
}
pub fn get_configuration() -> Value {
let starship_config = StarshipConfig::initialize();
starship_config
.config
.expect("Failed to load starship config")
}
pub fn write_configuration(table: &mut Table) {
let config_path = get_config_path();
let config_str =
toml::to_string_pretty(&table).expect("Failed to serialize the config to string");
File::create(&config_path)
.and_then(|mut file| file.write_all(config_str.as_ref()))
.expect("Error writing starship config");
}
pub fn edit_configuration() { pub fn edit_configuration() {
let config_path = get_config_path(); let config_path = get_config_path();
let editor_cmd = shell_words::split(&get_editor()).expect("Unmatched quotes found in $EDITOR."); let editor_cmd = shell_words::split(&get_editor()).expect("Unmatched quotes found in $EDITOR.");
@ -119,7 +167,6 @@ mod tests {
use super::*; use super::*;
// This is every possible permutation, 3² = 9. // This is every possible permutation, 3² = 9.
#[test] #[test]
fn visual_set_editor_set() { fn visual_set_editor_set() {
let actual = get_editor_internal(Some("foo".into()), Some("bar".into())); let actual = get_editor_internal(Some("foo".into()), Some("bar".into()));

View File

@ -29,11 +29,11 @@ fn main() {
.takes_value(true); .takes_value(true);
let shell_arg = Arg::with_name("shell") let shell_arg = Arg::with_name("shell")
.value_name("SHELL") .value_name("SHELL")
.help( .help(
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion", "The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion",
) )
.required(true); .required(true);
let cmd_duration_arg = Arg::with_name("cmd_duration") let cmd_duration_arg = Arg::with_name("cmd_duration")
.short("d") .short("d")
@ -117,6 +117,21 @@ fn main() {
) )
.arg(Arg::with_name("value").help("Value to place into that key")), .arg(Arg::with_name("value").help("Value to place into that key")),
) )
.subcommand(
SubCommand::with_name("toggle")
.about("Toggle a given starship module")
.arg(
Arg::with_name("name")
.help("The name of the module to be toggled")
.required(true),
)
.arg(
Arg::with_name("key")
.help("The key of the config to be toggled")
.required(false)
.required_unless("name"),
),
)
.subcommand( .subcommand(
SubCommand::with_name("bug-report").about( SubCommand::with_name("bug-report").about(
"Create a pre-populated GitHub issue with information about your configuration", "Create a pre-populated GitHub issue with information about your configuration",
@ -179,6 +194,15 @@ fn main() {
configure::edit_configuration() configure::edit_configuration()
} }
} }
("toggle", Some(sub_m)) => {
if let Some(name) = sub_m.value_of("name") {
if let Some(value) = sub_m.value_of("key") {
configure::toggle_configuration(name, value)
} else {
configure::toggle_configuration(name, "disabled")
}
}
}
("bug-report", Some(_)) => bug_report::create(), ("bug-report", Some(_)) => bug_report::create(),
("time", _) => { ("time", _) => {
match SystemTime::now() match SystemTime::now()