diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/context.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/module.rs | 37 | ||||
-rw-r--r-- | src/modules/mod.rs | 38 | ||||
-rw-r--r-- | src/print.rs | 119 | ||||
-rw-r--r-- | src/segment.rs | 5 |
6 files changed, 178 insertions, 29 deletions
diff --git a/src/context.rs b/src/context.rs index 97923dbaa..ead8e1e79 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,6 +1,7 @@ use crate::config::StarshipConfig; use crate::module::Module; +use crate::modules; use clap::ArgMatches; use git2::{Repository, RepositoryState}; use once_cell::sync::OnceCell; @@ -91,8 +92,9 @@ impl<'a> Context<'a> { /// Create a new module pub fn new_module(&self, name: &str) -> Module { let config = self.config.get_module_config(name); + let desc = modules::description(name); - Module::new(name, config) + Module::new(name, desc, config) } /// Check if `disabled` option of the module is true in configuration file. diff --git a/src/main.rs b/src/main.rs index 9fb1e0fee..9b917a34b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,6 +122,9 @@ fn main() { .about("Prints time in milliseconds") .settings(&[AppSettings::Hidden]), ) + .subcommand( + SubCommand::with_name("explain").about("Explains the currently showing modules"), + ) .get_matches(); match matches.subcommand() { @@ -157,6 +160,7 @@ fn main() { None => println!("{}", -1), } } + ("explain", Some(sub_m)) => print::explain(sub_m.clone()), _ => {} } } diff --git a/src/module.rs b/src/module.rs index 0643631ce..3de985d6c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -50,6 +50,9 @@ pub struct Module<'a> { /// The module's name, to be used in configuration and logging. _name: String, + /// The module's description + description: String, + /// The styling to be inherited by all segments contained within this module. style: Style, @@ -65,10 +68,11 @@ pub struct Module<'a> { impl<'a> Module<'a> { /// Creates a module with no segments. - pub fn new(name: &str, config: Option<&'a toml::Value>) -> Module<'a> { + pub fn new(name: &str, desc: &str, config: Option<&'a toml::Value>) -> Module<'a> { Module { config, _name: name.to_string(), + description: desc.to_string(), style: Style::default(), prefix: Affix::default_prefix(name), segments: Vec::new(), @@ -91,11 +95,20 @@ impl<'a> Module<'a> { &self._name } + /// Get module's description + pub fn get_description(&self) -> &String { + &self.description + } + /// Whether a module has non-empty segments pub fn is_empty(&self) -> bool { self.segments.iter().all(|segment| segment.is_empty()) } + pub fn get_segments(&self) -> Vec<&str> { + self.segments.iter().map(Segment::get_value).collect() + } + /// Get the module's prefix pub fn get_prefix(&mut self) -> &mut Affix { &mut self.prefix @@ -120,7 +133,10 @@ impl<'a> Module<'a> { /// Returns a vector of colored ANSIString elements to be later used with /// `ANSIStrings()` to optimize ANSI codes pub fn ansi_strings(&self) -> Vec<ANSIString> { - let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); + self.ansi_strings_for_prompt(true) + } + + pub fn ansi_strings_for_prompt(&self, is_prompt: bool) -> Vec<ANSIString> { let mut ansi_strings = self .segments .iter() @@ -130,11 +146,14 @@ impl<'a> Module<'a> { ansi_strings.insert(0, self.prefix.ansi_string()); ansi_strings.push(self.suffix.ansi_string()); - ansi_strings = match shell.as_str() { - "bash" => ansi_strings_modified(ansi_strings, shell), - "zsh" => ansi_strings_modified(ansi_strings, shell), - _ => ansi_strings, - }; + if is_prompt { + let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); + ansi_strings = match shell.as_str() { + "bash" => ansi_strings_modified(ansi_strings, shell), + "zsh" => ansi_strings_modified(ansi_strings, shell), + _ => ansi_strings, + }; + } ansi_strings } @@ -261,9 +280,11 @@ mod tests { #[test] fn test_module_is_empty_with_no_segments() { let name = "unit_test"; + let desc = "This is a unit test"; let module = Module { config: None, _name: name.to_string(), + description: desc.to_string(), style: Style::default(), prefix: Affix::default_prefix(name), segments: Vec::new(), @@ -276,9 +297,11 @@ mod tests { #[test] fn test_module_is_empty_with_all_empty_segments() { let name = "unit_test"; + let desc = "This is a unit test"; let module = Module { config: None, _name: name.to_string(), + description: desc.to_string(), style: Style::default(), prefix: Affix::default_prefix(name), segments: vec![Segment::new("test_segment")], diff --git a/src/modules/mod.rs b/src/modules/mod.rs index a95ea4fd3..57928576d 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -78,3 +78,41 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> { } } } + +pub fn description(module: &str) -> &'static str { + match module { + "aws" => "The current AWS region and profile", + "battery" => "The current charge of the device's battery and its current charging status", + "character" => { + "A character (usually an arrow) beside where the text is entered in your terminal" + } + "cmd_duration" => "How long the last command took to execute", + "conda" => "The current conda environment, if $CONDA_DEFAULT_ENV is set", + "directory" => "The current working directory", + "dotnet" => "The relevant version of the .NET Core SDK for the current directory", + "env_var" => "Displays the current value of a selected environment variable", + "git_branch" => "The active branch of the repo in your current directory", + "git_commit" => "The active branch of the repo in your current directory", + "git_state" => "The current git operation, and it's progress", + "git_status" => "Symbol representing the state of the repo", + "golang" => "The currently installed version of Golang", + "hg_branch" => "The active branch of the repo in your current directory", + "hostname" => "The system hostname", + "java" => "The currently installed version of Java", + "jobs" => "The current number of jobs running", + "kubernetes" => "The current Kubernetes context name and, if set, the namespace", + "line_break" => "Separates the prompt into two lines", + "memory_usage" => "Current system memory and swap usage", + "nix_shell" => "The nix-shell environment", + "nodejs" => "The currently installed version of NodeJS", + "package" => "The package version of the current directory's project", + "php" => "The currently installed version of PHP", + "python" => "The currently installed version of Python", + "ruby" => "The currently installed version of Ruby", + "rust" => "The currently installed version of Rust", + "terraform" => "The currently selected terraform workspace and version", + "time" => "The current local time", + "username" => "The active user's username", + _ => "<no description>", + } +} diff --git a/src/print.rs b/src/print.rs index 70826827f..ee3b36662 100644 --- a/src/print.rs +++ b/src/print.rs @@ -2,6 +2,7 @@ use clap::ArgMatches; use rayon::prelude::*; use std::fmt::Write as FmtWrite; use std::io::{self, Write}; +use unicode_width::UnicodeWidthChar; use crate::context::Context; use crate::module::Module; @@ -26,27 +27,7 @@ pub fn get_prompt(context: Context) -> String { buf.push_str("\x1b[J"); - let mut prompt_order: Vec<&str> = Vec::new(); - - // Write out a custom prompt order - for module in config.prompt_order { - if ALL_MODULES.contains(&module) { - prompt_order.push(module); - } else { - log::debug!( - "Expected prompt_order to contain value from {:?}. Instead received {}", - ALL_MODULES, - module, - ); - } - } - - let modules = &prompt_order - .par_iter() - .filter(|module| !context.is_module_disabled_in_config(module)) - .map(|module| modules::handle(module, &context)) // Compute modules - .flatten() - .collect::<Vec<Module>>(); // Remove segments set to `None` + let modules = compute_modules(&context); let mut print_without_prefix = true; let printable = modules.iter(); @@ -76,3 +57,99 @@ pub fn module(module_name: &str, args: ArgMatches) { print!("{}", module); } + +pub fn explain(args: ArgMatches) { + let context = Context::new(args); + + struct ModuleInfo { + value: String, + value_len: usize, + desc: String, + } + + let dont_print = vec!["line_break", "character"]; + + let modules = compute_modules(&context) + .into_iter() + .filter(|module| !dont_print.contains(&module.get_name().as_str())) + .map(|module| { + let ansi_strings = module.ansi_strings_for_prompt(false); + let value = module.get_segments().join(""); + ModuleInfo { + value: ansi_term::ANSIStrings(&ansi_strings[1..ansi_strings.len() - 1]).to_string(), + value_len: value.chars().count() + count_wide_chars(&value), + desc: module.get_description().to_owned(), + } + }) + .collect::<Vec<ModuleInfo>>(); + + let mut max_ansi_module_width = 0; + let mut max_module_width = 0; + + for info in &modules { + max_ansi_module_width = std::cmp::max( + max_ansi_module_width, + info.value.chars().count() + count_wide_chars(&info.value), + ); + max_module_width = std::cmp::max(max_module_width, info.value_len); + } + + let desc_width = term_size::dimensions() + .map(|(w, _)| w) + .map(|width| width - std::cmp::min(width, max_ansi_module_width)); + + println!("\n Here's a breakdown of your prompt:"); + for info in modules { + let wide_chars = count_wide_chars(&info.value); + + if let Some(desc_width) = desc_width { + let wrapped = textwrap::fill(&info.desc, desc_width); + let mut lines = wrapped.split('\n'); + println!( + " {:width$} - {}", + info.value, + lines.next().unwrap(), + width = max_ansi_module_width - wide_chars + ); + + for line in lines { + println!("{}{}", " ".repeat(max_module_width + 6), line.trim()); + } + } else { + println!( + " {:width$} - {}", + info.value, + info.desc, + width = max_ansi_module_width - wide_chars + ); + }; + } +} + +fn compute_modules<'a>(context: &'a Context) -> Vec<Module<'a>> { + let mut prompt_order: Vec<&str> = Vec::new(); + + // Write out a custom prompt order + for module in context.config.get_root_config().prompt_order { + if ALL_MODULES.contains(&module) { + prompt_order.push(module); + } else { + log::debug!( + "Expected prompt_order to contain value from {:?}. Instead received {}", + ALL_MODULES, + module, + ); + } + } + + prompt_order + .par_iter() + .filter(|module| !context.is_module_disabled_in_config(module)) + .map(|module| modules::handle(module, &context)) // Compute modules + .flatten() // Remove segments set to `None` + .collect::<Vec<Module<'a>>>() +} + +fn count_wide_chars(value: &str) -> usize { + value.chars().filter(|c| c.width().unwrap_or(0) > 1).count() +} diff --git a/src/segment.rs b/src/segment.rs index 8c0140ac1..d01034bb0 100644 --- a/src/segment.rs +++ b/src/segment.rs @@ -45,6 +45,11 @@ impl Segment { self } + /// Gets the value of the segment. + pub fn get_value(&self) -> &str { + &self.value + } + // Returns the ANSIString of the segment value, not including its prefix and suffix pub fn ansi_string(&self) -> ANSIString { match self.style { |