diff options
author | Saurav Sharma <appdroiddeveloper@gmail.com> | 2019-08-19 10:20:11 +0545 |
---|---|---|
committer | Kevin Song <chipbuster@users.noreply.github.com> | 2019-08-18 21:35:11 -0700 |
commit | f54322f2ab1e29111043d09f8d533bcfc8f0be45 (patch) | |
tree | aa381e0fc46793dd0d7b3002c52bd33fe42b33b6 /src | |
parent | 51f723df22c3444678d8cea7b89e7e8ce09931bc (diff) |
feat: Add configuration for reordering the prompt module and disabling default order (#171)
Adds functionality for reordering the prompt module through the use of the prompt_order configuration option in starship.toml
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 16 | ||||
-rw-r--r-- | src/print.rs | 60 |
2 files changed, 74 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs index ba339468b..c51c37bce 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub trait Config { fn get_as_bool(&self, key: &str) -> Option<bool>; fn get_as_str(&self, key: &str) -> Option<&str>; fn get_as_i64(&self, key: &str) -> Option<i64>; + fn get_as_array(&self, key: &str) -> Option<&Vec<toml::value::Value>>; // Internal implementation for accessors fn get_config(&self, key: &str) -> Option<&toml::value::Value>; @@ -141,6 +142,21 @@ impl Config for Table { i64_value } + + /// Get a key from a module's configuration as a vector + fn get_as_array(&self, key: &str) -> Option<&Vec<toml::value::Value>> { + let value = self.get_config(key)?; + let array_value = value.as_array(); + if array_value.is_none() { + log::debug!( + "Expected \"{}\" to be a array. Instead received {} of type {}.", + key, + value, + value.type_str() + ); + } + array_value + } } #[cfg(test)] diff --git a/src/print.rs b/src/print.rs index 918d3f19d..0cf495f72 100644 --- a/src/print.rs +++ b/src/print.rs @@ -7,7 +7,29 @@ use crate::context::Context; use crate::module::Module; use crate::modules; -const PROMPT_ORDER: &[&str] = &[ +// List of all modules +const ALL_MODULES: &[&str] = &[ + "battery", + "character", + "cmd_duration", + "directory", + "git_branch", + "git_status", + "golang", + "jobs", + "line_break", + "nodejs", + "package", + "python", + "ruby", + "rust", + "username", +]; + +// List of default prompt order +// NOTE: If this const value is changed then Default prompt order subheading inside +// prompt heading of config docs needs to be updated according to changes made here. +const DEFAULT_PROMPT_ORDER: &[&str] = &[ "username", "directory", "git_branch", @@ -36,7 +58,41 @@ pub fn prompt(args: ArgMatches) { writeln!(handle).unwrap(); } - let modules = PROMPT_ORDER + let mut prompt_order: Vec<&str> = Vec::new(); + + // Write out a custom prompt order + if let Some(modules) = config.get_as_array("prompt_order") { + // if prompt_order = [] use default_prompt_order + if !modules.is_empty() { + for module in modules { + let str_value = module.as_str(); + + if let Some(value) = str_value { + if ALL_MODULES.contains(&value) { + prompt_order.push(value); + } else { + log::debug!( + "Expected prompt_order to contain value from {:?}. Instead received {}", + ALL_MODULES, + value, + ); + } + } else { + log::debug!( + "Expected prompt_order to be an array of strings. Instead received {} of type {}", + module, + module.type_str() + ); + } + } + } else { + prompt_order = DEFAULT_PROMPT_ORDER.to_vec(); + } + } else { + prompt_order = DEFAULT_PROMPT_ORDER.to_vec(); + } + + let modules = &prompt_order .par_iter() .map(|module| modules::handle(module, &context)) // Compute modules .flatten() |