mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
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
This commit is contained in:
committed by
Kevin Song
parent
51f723df22
commit
f54322f2ab
@@ -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)]
|
||||
|
||||
+58
-2
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user