Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 61 additions & 14 deletions src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::config::StarshipConfig;
use std::fs::File;
use std::io::Write;
use toml::map::Map;
use toml::value::Table;
use toml::Value;

#[cfg(not(windows))]
Expand All @@ -16,20 +17,13 @@ const STD_EDITOR: &str = "vi";
const STD_EDITOR: &str = "notepad.exe";

pub fn update_configuration(name: &str, value: &str) {
let config_path = get_config_path();

let keys: Vec<&str> = name.split('.').collect();
if keys.len() != 2 {
log::error!("Please pass in a config key with a '.'");
process::exit(1);
}

let starship_config = StarshipConfig::initialize();
let mut config = starship_config
.config
.expect("Failed to load starship config");

if let Some(table) = config.as_table_mut() {
if let Some(table) = get_configuration().as_table_mut() {
if !table.contains_key(keys[0]) {
table.insert(keys[0].to_string(), Value::Table(Map::new()));
}
Expand All @@ -54,14 +48,68 @@ pub fn update_configuration(name: &str, value: &str) {
table.insert(keys[0].to_string(), Value::Table(updated_values));
}

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");
write_configuration(table);
}
}

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() {
let config_path = get_config_path();
let editor_cmd = shell_words::split(&get_editor()).expect("Unmatched quotes found in $EDITOR.");
Expand Down Expand Up @@ -119,7 +167,6 @@ mod tests {
use super::*;

// This is every possible permutation, 3² = 9.

#[test]
fn visual_set_editor_set() {
let actual = get_editor_internal(Some("foo".into()), Some("bar".into()));
Expand Down
34 changes: 29 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ fn main() {
.takes_value(true);

let shell_arg = Arg::with_name("shell")
.value_name("SHELL")
.help(
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion",
)
.required(true);
.value_name("SHELL")
.help(
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion",
)
.required(true);
Comment on lines -32 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what if anything is changing here, is this just a whitespace change? If so is it required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not touch here actually, it's so weird that does not show up my name in annotate. Maybe rustfmt did this?

Screen Shot 2020-12-26 at 17 39 11


let cmd_duration_arg = Arg::with_name("cmd_duration")
.short("d")
Expand Down Expand Up @@ -117,6 +117,21 @@ fn main() {
)
.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::with_name("bug-report").about(
"Create a pre-populated GitHub issue with information about your configuration",
Expand Down Expand Up @@ -179,6 +194,15 @@ fn main() {
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(),
("time", _) => {
match SystemTime::now()
Expand Down