diff --git a/src/configure.rs b/src/configure.rs new file mode 100644 index 00000000..f4fb5bf1 --- /dev/null +++ b/src/configure.rs @@ -0,0 +1,37 @@ +use std::env; +use std::process::Command; + +const UNKNOWN_CONFIG: &str = ""; +const STD_EDITOR: &str = "vi"; + +pub fn edit_configuration() { + let editor = get_editor(); + let config_path = get_config_path(); + + Command::new(editor) + .arg(config_path) + .status() + .expect("failed to open file"); +} + +fn get_editor() -> String { + match env::var("EDITOR") { + Ok(val) => val, + Err(_) => STD_EDITOR.to_string(), + } +} + +fn get_config_path() -> String { + let home_dir = dirs::home_dir(); + + if home_dir.is_none() { + return UNKNOWN_CONFIG.to_string(); + } + + let path = home_dir.unwrap().join(".config/starship.toml"); + + match path.to_str() { + Some(p) => String::from(p), + None => UNKNOWN_CONFIG.to_string(), + } +} diff --git a/src/main.rs b/src/main.rs index 47c7e80a..2f995f0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate clap; mod bug_report; mod config; mod configs; +mod configure; mod context; mod init; mod module; @@ -110,6 +111,7 @@ fn main() { .arg(&keymap_arg) .arg(&jobs_arg), ) + .subcommand(SubCommand::with_name("configure").about("Edit the starship configuration")) .subcommand(SubCommand::with_name("bug-report").about( "Create a pre-populated GitHub issue with information about your configuration", )) @@ -137,6 +139,7 @@ fn main() { print::module(module_name, sub_m.clone()); } } + ("configure", Some(_)) => configure::edit_configuration(), ("bug-report", Some(_)) => bug_report::create(), _ => {} }