mirror of
https://github.com/starship/starship
synced 2024-09-06 00:09:26 +00:00
feat: Modify config keys from shell (#1095)
* feat: Modify config keys from shell * chore: Fix clippy * refactor: Add `configure` as alias * chore: Remove redundant code * fix: Soft error if user doesn't pass in valid key * feat: Support integers and booleans * chore: Fix clippy * refactor: Use exit instead of abort Co-Authored-By: Thomas O'Donnell <andytom@users.noreply.github.com> Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
This commit is contained in:
parent
decd4e2a5d
commit
a3fef5becf
|
@ -1,10 +1,64 @@
|
|||
use std::env;
|
||||
use std::ffi::OsString;
|
||||
use std::io::ErrorKind;
|
||||
use std::process;
|
||||
use std::process::Command;
|
||||
|
||||
use starship::config::StarshipConfig;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use toml::map::Map;
|
||||
use toml::Value;
|
||||
|
||||
const STD_EDITOR: &str = "vi";
|
||||
|
||||
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 !table.contains_key(keys[0]) {
|
||||
table.insert(keys[0].to_string(), Value::Table(Map::new()));
|
||||
}
|
||||
|
||||
if let Some(values) = table.get(keys[0]).unwrap().as_table() {
|
||||
let mut updated_values = values.clone();
|
||||
|
||||
if value.parse::<bool>().is_ok() {
|
||||
updated_values.insert(
|
||||
keys[1].to_string(),
|
||||
Value::Boolean(value.parse::<bool>().unwrap()),
|
||||
);
|
||||
} else if value.parse::<i64>().is_ok() {
|
||||
updated_values.insert(
|
||||
keys[1].to_string(),
|
||||
Value::Integer(value.parse::<i64>().unwrap()),
|
||||
);
|
||||
} else {
|
||||
updated_values.insert(keys[1].to_string(), Value::String(value.to_string()));
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn edit_configuration() {
|
||||
let config_path = get_config_path();
|
||||
let editor_cmd = get_editor();
|
||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -116,7 +116,18 @@ fn main() {
|
|||
.arg(&keymap_arg)
|
||||
.arg(&jobs_arg),
|
||||
)
|
||||
.subcommand(SubCommand::with_name("configure").about("Edit the starship configuration"))
|
||||
.subcommand(
|
||||
SubCommand::with_name("config")
|
||||
.alias("configure")
|
||||
.about("Edit the starship configuration")
|
||||
.arg(
|
||||
Arg::with_name("name")
|
||||
.help("Configuration key to edit")
|
||||
.required(false)
|
||||
.requires("value"),
|
||||
)
|
||||
.arg(Arg::with_name("value").help("Value to place into that key")),
|
||||
)
|
||||
.subcommand(SubCommand::with_name("bug-report").about(
|
||||
"Create a pre-populated GitHub issue with information about your configuration",
|
||||
))
|
||||
|
@ -152,7 +163,15 @@ fn main() {
|
|||
print::module(module_name, sub_m.clone());
|
||||
}
|
||||
}
|
||||
("configure", Some(_)) => configure::edit_configuration(),
|
||||
("config", Some(sub_m)) => {
|
||||
if let Some(name) = sub_m.value_of("name") {
|
||||
if let Some(value) = sub_m.value_of("value") {
|
||||
configure::update_configuration(name, value)
|
||||
}
|
||||
} else {
|
||||
configure::edit_configuration()
|
||||
}
|
||||
}
|
||||
("bug-report", Some(_)) => bug_report::create(),
|
||||
("time", _) => {
|
||||
match SystemTime::now()
|
||||
|
|
Loading…
Reference in a new issue