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:
Saurav Sharma 2019-08-19 10:20:11 +05:45 committed by Kevin Song
parent 51f723df22
commit f54322f2ab
3 changed files with 102 additions and 5 deletions

View File

@ -46,9 +46,10 @@ This is the list of prompt-wide configuration options.
### Options
| Variable | Default | Description |
| ------------- | ------- | ---------------------------------------------- |
| `add_newline` | `true` | Add a new line before the start of the prompt. |
| Variable | Default | Description |
| -------------- | ------- | ------------------------------------------------------------------ |
| `add_newline` | `true` | Add a new line before the start of the prompt. |
| `prompt_order` | [link](#default-prompt-order) | Configure the order in which the prompt module occurs. |
### Example
@ -57,8 +58,32 @@ This is the list of prompt-wide configuration options.
# Disable the newline at the start of the prompt
add_newline = false
# Overwrite a default_prompt_order and use custom prompt_order
prompt_order=["rust","line_break","package","line_break","character"]
```
### Default prompt order
The ```default_prompt_order``` configuration option is used to define the order in which modules are shown in the prompt, if empty or no ```prompt_order``` is provided. The default is as shown:
```
default_prompt_order = [
"username",
"directory",
"git_branch",
"git_status",
"package",
"nodejs",
"rust",
"python",
"golang",
"cmd_duration",
"line_break",
"jobs",
"battery",
"character",
]
```
## Battery
The `battery` module shows how charged the device's battery is and its current charging status.

View File

@ -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)]

View File

@ -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()