Skip to content

Commit f03c3f1

Browse files
authored
feat(command): add 'toggle' command (starship#1917)
Closes starship#894 Signed-off-by: Dentrax <[email protected]>
1 parent 8cd4850 commit f03c3f1

File tree

2 files changed

+90
-19
lines changed

2 files changed

+90
-19
lines changed

src/configure.rs

+61-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::config::StarshipConfig;
88
use std::fs::File;
99
use std::io::Write;
1010
use toml::map::Map;
11+
use toml::value::Table;
1112
use toml::Value;
1213

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

1819
pub fn update_configuration(name: &str, value: &str) {
19-
let config_path = get_config_path();
20-
2120
let keys: Vec<&str> = name.split('.').collect();
2221
if keys.len() != 2 {
2322
log::error!("Please pass in a config key with a '.'");
2423
process::exit(1);
2524
}
2625

27-
let starship_config = StarshipConfig::initialize();
28-
let mut config = starship_config
29-
.config
30-
.expect("Failed to load starship config");
31-
32-
if let Some(table) = config.as_table_mut() {
26+
if let Some(table) = get_configuration().as_table_mut() {
3327
if !table.contains_key(keys[0]) {
3428
table.insert(keys[0].to_string(), Value::Table(Map::new()));
3529
}
@@ -54,14 +48,68 @@ pub fn update_configuration(name: &str, value: &str) {
5448
table.insert(keys[0].to_string(), Value::Table(updated_values));
5549
}
5650

57-
let config_str =
58-
toml::to_string_pretty(&table).expect("Failed to serialize the config to string");
59-
File::create(&config_path)
60-
.and_then(|mut file| file.write_all(config_str.as_ref()))
61-
.expect("Error writing starship config");
51+
write_configuration(table);
6252
}
6353
}
6454

55+
pub fn toggle_configuration(name: &str, key: &str) {
56+
if let Some(table) = get_configuration().as_table_mut() {
57+
match table.get(name) {
58+
Some(v) => {
59+
if let Some(values) = v.as_table() {
60+
let mut updated_values = values.clone();
61+
62+
let current: bool = match updated_values.get(key) {
63+
Some(v) => match v.as_bool() {
64+
Some(b) => b,
65+
_ => {
66+
log::error!(
67+
"Given config key '{}' must be in 'boolean' format",
68+
key
69+
);
70+
process::exit(1);
71+
}
72+
},
73+
_ => {
74+
log::error!("Given config key '{}' must be exist in config file", key);
75+
process::exit(1);
76+
}
77+
};
78+
79+
updated_values.insert(key.to_string(), Value::Boolean(!current));
80+
81+
table.insert(name.to_string(), Value::Table(updated_values));
82+
83+
write_configuration(table);
84+
}
85+
}
86+
_ => {
87+
log::error!("Given module '{}' not found in config file", name);
88+
process::exit(1);
89+
}
90+
};
91+
}
92+
}
93+
94+
pub fn get_configuration() -> Value {
95+
let starship_config = StarshipConfig::initialize();
96+
97+
starship_config
98+
.config
99+
.expect("Failed to load starship config")
100+
}
101+
102+
pub fn write_configuration(table: &mut Table) {
103+
let config_path = get_config_path();
104+
105+
let config_str =
106+
toml::to_string_pretty(&table).expect("Failed to serialize the config to string");
107+
108+
File::create(&config_path)
109+
.and_then(|mut file| file.write_all(config_str.as_ref()))
110+
.expect("Error writing starship config");
111+
}
112+
65113
pub fn edit_configuration() {
66114
let config_path = get_config_path();
67115
let editor_cmd = shell_words::split(&get_editor()).expect("Unmatched quotes found in $EDITOR.");
@@ -119,7 +167,6 @@ mod tests {
119167
use super::*;
120168

121169
// This is every possible permutation, 3² = 9.
122-
123170
#[test]
124171
fn visual_set_editor_set() {
125172
let actual = get_editor_internal(Some("foo".into()), Some("bar".into()));

src/main.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ fn main() {
2929
.takes_value(true);
3030

3131
let shell_arg = Arg::with_name("shell")
32-
.value_name("SHELL")
33-
.help(
34-
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion",
35-
)
36-
.required(true);
32+
.value_name("SHELL")
33+
.help(
34+
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion",
35+
)
36+
.required(true);
3737

3838
let cmd_duration_arg = Arg::with_name("cmd_duration")
3939
.short("d")
@@ -117,6 +117,21 @@ fn main() {
117117
)
118118
.arg(Arg::with_name("value").help("Value to place into that key")),
119119
)
120+
.subcommand(
121+
SubCommand::with_name("toggle")
122+
.about("Toggle a given starship module")
123+
.arg(
124+
Arg::with_name("name")
125+
.help("The name of the module to be toggled")
126+
.required(true),
127+
)
128+
.arg(
129+
Arg::with_name("key")
130+
.help("The key of the config to be toggled")
131+
.required(false)
132+
.required_unless("name"),
133+
),
134+
)
120135
.subcommand(
121136
SubCommand::with_name("bug-report").about(
122137
"Create a pre-populated GitHub issue with information about your configuration",
@@ -179,6 +194,15 @@ fn main() {
179194
configure::edit_configuration()
180195
}
181196
}
197+
("toggle", Some(sub_m)) => {
198+
if let Some(name) = sub_m.value_of("name") {
199+
if let Some(value) = sub_m.value_of("key") {
200+
configure::toggle_configuration(name, value)
201+
} else {
202+
configure::toggle_configuration(name, "disabled")
203+
}
204+
}
205+
}
182206
("bug-report", Some(_)) => bug_report::create(),
183207
("time", _) => {
184208
match SystemTime::now()

0 commit comments

Comments
 (0)