From f54322f2ab1e29111043d09f8d533bcfc8f0be45 Mon Sep 17 00:00:00 2001 From: Saurav Sharma Date: Mon, 19 Aug 2019 10:20:11 +0545 Subject: [PATCH] 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 --- docs/config/README.md | 31 +++++++++++++++++++--- src/config.rs | 16 ++++++++++++ src/print.rs | 60 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 8f2697fb..46bda21b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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. diff --git a/src/config.rs b/src/config.rs index ba339468..c51c37bc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub trait Config { fn get_as_bool(&self, key: &str) -> Option; fn get_as_str(&self, key: &str) -> Option<&str>; fn get_as_i64(&self, key: &str) -> Option; + fn get_as_array(&self, key: &str) -> Option<&Vec>; // 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> { + 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 918d3f19..0cf495f7 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()