2019-11-07 03:38:30 +00:00
|
|
|
use crate::config::SegmentConfig;
|
2019-05-01 20:34:24 +00:00
|
|
|
use crate::segment::Segment;
|
|
|
|
use ansi_term::Style;
|
|
|
|
use ansi_term::{ANSIString, ANSIStrings};
|
|
|
|
use std::fmt;
|
|
|
|
|
2019-08-20 04:42:25 +00:00
|
|
|
// List of all modules
|
2019-10-02 06:56:49 +00:00
|
|
|
// Keep these ordered alphabetically.
|
|
|
|
// Default ordering is handled in configs/mod.rs
|
2019-08-20 04:42:25 +00:00
|
|
|
pub const ALL_MODULES: &[&str] = &[
|
2019-09-26 02:55:47 +00:00
|
|
|
"aws",
|
2019-08-26 18:09:39 +00:00
|
|
|
#[cfg(feature = "battery")]
|
2019-08-20 04:42:25 +00:00
|
|
|
"battery",
|
|
|
|
"character",
|
|
|
|
"cmd_duration",
|
2019-10-05 18:25:25 +00:00
|
|
|
"conda",
|
2019-08-20 04:42:25 +00:00
|
|
|
"directory",
|
2019-10-02 06:56:49 +00:00
|
|
|
"dotnet",
|
2019-09-26 08:30:58 +00:00
|
|
|
"env_var",
|
2019-08-20 04:42:25 +00:00
|
|
|
"git_branch",
|
2019-09-05 16:45:04 +00:00
|
|
|
"git_state",
|
2019-08-20 04:42:25 +00:00
|
|
|
"git_status",
|
|
|
|
"golang",
|
2019-12-02 22:37:18 +00:00
|
|
|
"hg_branch",
|
2019-09-05 15:33:24 +00:00
|
|
|
"hostname",
|
2019-09-19 23:02:53 +00:00
|
|
|
"java",
|
2019-08-20 04:42:25 +00:00
|
|
|
"jobs",
|
2019-10-01 18:58:24 +00:00
|
|
|
"kubernetes",
|
2019-08-20 04:42:25 +00:00
|
|
|
"line_break",
|
2019-09-29 05:55:49 +00:00
|
|
|
"memory_usage",
|
2019-08-30 13:39:21 +00:00
|
|
|
"nix_shell",
|
2019-09-05 15:33:24 +00:00
|
|
|
"nodejs",
|
2019-08-20 04:42:25 +00:00
|
|
|
"package",
|
|
|
|
"python",
|
|
|
|
"ruby",
|
|
|
|
"rust",
|
2019-09-10 17:54:40 +00:00
|
|
|
"time",
|
2019-08-20 04:42:25 +00:00
|
|
|
"username",
|
|
|
|
];
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// A module is a collection of segments showing data for a single integration
|
|
|
|
/// (e.g. The git module shows the current git branch and status)
|
2019-06-10 14:56:17 +00:00
|
|
|
pub struct Module<'a> {
|
|
|
|
/// The module's configuration map if available
|
2019-09-30 12:10:35 +00:00
|
|
|
pub config: Option<&'a toml::Value>,
|
2019-06-10 14:56:17 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The module's name, to be used in configuration and logging.
|
2019-09-14 14:23:53 +00:00
|
|
|
_name: String,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
|
|
/// The styling to be inherited by all segments contained within this module.
|
|
|
|
style: Style,
|
|
|
|
|
|
|
|
/// The prefix used to separate the current module from the previous one.
|
2019-07-31 23:48:51 +00:00
|
|
|
prefix: Affix,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
|
|
/// The collection of segments that compose this module.
|
|
|
|
segments: Vec<Segment>,
|
|
|
|
|
|
|
|
/// The suffix used to separate the current module from the next one.
|
2019-07-31 23:48:51 +00:00
|
|
|
suffix: Affix,
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
impl<'a> Module<'a> {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Creates a module with no segments.
|
2019-09-30 12:10:35 +00:00
|
|
|
pub fn new(name: &str, config: Option<&'a toml::Value>) -> Module<'a> {
|
2019-05-01 20:34:24 +00:00
|
|
|
Module {
|
2019-06-10 14:56:17 +00:00
|
|
|
config,
|
2019-09-14 14:23:53 +00:00
|
|
|
_name: name.to_string(),
|
2019-05-01 20:34:24 +00:00
|
|
|
style: Style::default(),
|
2019-07-31 23:48:51 +00:00
|
|
|
prefix: Affix::default_prefix(name),
|
2019-05-01 20:34:24 +00:00
|
|
|
segments: Vec::new(),
|
2019-07-31 23:48:51 +00:00
|
|
|
suffix: Affix::default_suffix(name),
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 12:10:35 +00:00
|
|
|
/// Get a reference to a newly created segment in the module
|
|
|
|
pub fn create_segment(&mut self, name: &str, segment_config: &SegmentConfig) -> &mut Segment {
|
|
|
|
let mut segment = Segment::new(name);
|
|
|
|
segment.set_style(segment_config.style.unwrap_or(self.style));
|
|
|
|
segment.set_value(segment_config.value);
|
2019-05-01 20:34:24 +00:00
|
|
|
self.segments.push(segment);
|
|
|
|
|
|
|
|
self.segments.last_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-10-06 15:46:46 +00:00
|
|
|
/// Get module's name
|
|
|
|
pub fn get_name(&self) -> &String {
|
|
|
|
&self._name
|
|
|
|
}
|
|
|
|
|
2019-09-16 05:03:44 +00:00
|
|
|
/// Whether a module has non-empty segments
|
2019-05-14 04:43:11 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2019-09-16 05:03:44 +00:00
|
|
|
self.segments.iter().all(|segment| segment.is_empty())
|
2019-05-14 04:43:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Get the module's prefix
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn get_prefix(&mut self) -> &mut Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
&mut self.prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the module's suffix
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn get_suffix(&mut self) -> &mut Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
&mut self.suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the style of the segment.
|
|
|
|
///
|
|
|
|
/// Accepts either `Color` or `Style`.
|
2019-06-10 14:56:17 +00:00
|
|
|
pub fn set_style<T>(&mut self, style: T) -> &mut Module<'a>
|
2019-05-01 20:34:24 +00:00
|
|
|
where
|
|
|
|
T: Into<Style>,
|
|
|
|
{
|
|
|
|
self.style = style.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector of colored ANSIString elements to be later used with
|
|
|
|
/// `ANSIStrings()` to optimize ANSI codes
|
|
|
|
pub fn ansi_strings(&self) -> Vec<ANSIString> {
|
2019-08-19 03:33:12 +00:00
|
|
|
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
|
|
|
|
let ansi_strings = self
|
2019-05-01 20:34:24 +00:00
|
|
|
.segments
|
|
|
|
.iter()
|
2019-07-31 23:48:51 +00:00
|
|
|
.map(Segment::ansi_string)
|
2019-05-01 20:34:24 +00:00
|
|
|
.collect::<Vec<ANSIString>>();
|
|
|
|
|
2019-08-19 03:33:12 +00:00
|
|
|
let mut ansi_strings = match shell.as_str() {
|
|
|
|
"bash" => ansi_strings_modified(ansi_strings, shell),
|
|
|
|
"zsh" => ansi_strings_modified(ansi_strings, shell),
|
|
|
|
_ => ansi_strings,
|
|
|
|
};
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
ansi_strings.insert(0, self.prefix.ansi_string());
|
|
|
|
ansi_strings.push(self.suffix.ansi_string());
|
|
|
|
|
|
|
|
ansi_strings
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string_without_prefix(&self) -> String {
|
|
|
|
ANSIStrings(&self.ansi_strings()[1..]).to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
impl<'a> fmt::Display for Module<'a> {
|
2019-05-01 20:34:24 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let ansi_strings = self.ansi_strings();
|
|
|
|
write!(f, "{}", ANSIStrings(&ansi_strings))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 03:33:12 +00:00
|
|
|
/// Many shells cannot deal with raw unprintable characters (like ANSI escape sequences) and
|
|
|
|
/// miscompute the cursor position as a result, leading to strange visual bugs. Here, we wrap these
|
|
|
|
/// characters in shell-specific escape codes to indicate to the shell that they are zero-length.
|
|
|
|
fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: String) -> Vec<ANSIString> {
|
|
|
|
const ESCAPE_BEGIN: char = '\u{1b}';
|
|
|
|
const MAYBE_ESCAPE_END: char = 'm';
|
|
|
|
ansi_strings
|
|
|
|
.iter()
|
|
|
|
.map(|ansi| {
|
|
|
|
let mut escaped = false;
|
|
|
|
let final_string: String = ansi
|
|
|
|
.to_string()
|
|
|
|
.chars()
|
|
|
|
.map(|x| match x {
|
|
|
|
ESCAPE_BEGIN => {
|
|
|
|
escaped = true;
|
|
|
|
match shell.as_str() {
|
|
|
|
"bash" => String::from("\u{5c}\u{5b}\u{1b}"), // => \[ESC
|
|
|
|
"zsh" => String::from("\u{25}\u{7b}\u{1b}"), // => %{ESC
|
|
|
|
_ => x.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MAYBE_ESCAPE_END => {
|
|
|
|
if escaped {
|
|
|
|
escaped = false;
|
|
|
|
match shell.as_str() {
|
|
|
|
"bash" => String::from("m\u{5c}\u{5d}"), // => m\]
|
|
|
|
"zsh" => String::from("m\u{25}\u{7d}"), // => m%}
|
|
|
|
_ => x.to_string(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => x.to_string(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
ANSIString::from(final_string)
|
|
|
|
})
|
|
|
|
.collect::<Vec<ANSIString>>()
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// Module affixes are to be used for the prefix or suffix of a module.
|
2019-07-31 23:48:51 +00:00
|
|
|
pub struct Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The affix's name, to be used in configuration and logging.
|
2019-09-14 14:23:53 +00:00
|
|
|
_name: String,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
|
|
/// The affix's style.
|
|
|
|
style: Style,
|
|
|
|
|
|
|
|
/// The string value of the affix.
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
impl Affix {
|
|
|
|
pub fn default_prefix(name: &str) -> Self {
|
|
|
|
Self {
|
2019-09-14 14:23:53 +00:00
|
|
|
_name: format!("{}_prefix", name),
|
2019-05-01 20:34:24 +00:00
|
|
|
style: Style::default(),
|
|
|
|
value: "via ".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn default_suffix(name: &str) -> Self {
|
|
|
|
Self {
|
2019-09-14 14:23:53 +00:00
|
|
|
_name: format!("{}_suffix", name),
|
2019-05-01 20:34:24 +00:00
|
|
|
style: Style::default(),
|
|
|
|
value: " ".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the style of the module.
|
|
|
|
///
|
|
|
|
/// Accepts either `Color` or `Style`.
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn set_style<T>(&mut self, style: T) -> &mut Self
|
2019-05-01 20:34:24 +00:00
|
|
|
where
|
|
|
|
T: Into<Style>,
|
|
|
|
{
|
|
|
|
self.style = style.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value of the module.
|
2019-07-31 23:48:51 +00:00
|
|
|
pub fn set_value<T>(&mut self, value: T) -> &mut Self
|
2019-05-01 20:34:24 +00:00
|
|
|
where
|
|
|
|
T: Into<String>,
|
|
|
|
{
|
|
|
|
self.value = value.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates the colored ANSIString output.
|
|
|
|
pub fn ansi_string(&self) -> ANSIString {
|
|
|
|
self.style.paint(&self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:48:51 +00:00
|
|
|
impl fmt::Display for Affix {
|
2019-05-01 20:34:24 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.ansi_string())
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 05:03:44 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_is_empty_with_no_segments() {
|
|
|
|
let name = "unit_test";
|
|
|
|
let module = Module {
|
|
|
|
config: None,
|
|
|
|
_name: name.to_string(),
|
|
|
|
style: Style::default(),
|
|
|
|
prefix: Affix::default_prefix(name),
|
|
|
|
segments: Vec::new(),
|
|
|
|
suffix: Affix::default_suffix(name),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_is_empty_with_all_empty_segments() {
|
|
|
|
let name = "unit_test";
|
|
|
|
let module = Module {
|
|
|
|
config: None,
|
|
|
|
_name: name.to_string(),
|
|
|
|
style: Style::default(),
|
|
|
|
prefix: Affix::default_prefix(name),
|
|
|
|
segments: vec![Segment::new("test_segment")],
|
|
|
|
suffix: Affix::default_suffix(name),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
}
|
|
|
|
}
|