2020-01-26 17:37:18 -05:00
|
|
|
use crate::context::Shell;
|
2019-05-01 16:34:24 -04:00
|
|
|
use crate::segment::Segment;
|
2020-02-06 10:10:59 -06:00
|
|
|
use crate::utils::wrap_colorseq_for_shell;
|
2019-05-01 16:34:24 -04:00
|
|
|
use ansi_term::{ANSIString, ANSIStrings};
|
|
|
|
|
use std::fmt;
|
2020-09-21 19:06:15 +02:00
|
|
|
use std::time::Duration;
|
2019-05-01 16:34:24 -04:00
|
|
|
|
2019-08-20 10:27:25 +05:45
|
|
|
// List of all modules
|
2020-09-08 12:09:21 -04:00
|
|
|
// Default ordering is handled in configs/starship_root.rs
|
2019-08-20 10:27:25 +05:45
|
|
|
pub const ALL_MODULES: &[&str] = &[
|
2019-09-26 04:55:47 +02:00
|
|
|
"aws",
|
2019-08-26 14:09:39 -04:00
|
|
|
#[cfg(feature = "battery")]
|
2019-08-20 10:27:25 +05:45
|
|
|
"battery",
|
|
|
|
|
"character",
|
2020-07-09 21:40:33 +02:00
|
|
|
"cmake",
|
2019-08-20 10:27:25 +05:45
|
|
|
"cmd_duration",
|
2019-10-05 20:25:25 +02:00
|
|
|
"conda",
|
2021-07-02 11:00:46 -07:00
|
|
|
"crystal",
|
2020-07-29 17:38:23 +02:00
|
|
|
"dart",
|
2021-04-15 06:22:12 -07:00
|
|
|
"deno",
|
2019-08-20 10:27:25 +05:45
|
|
|
"directory",
|
2020-04-05 20:42:55 +01:00
|
|
|
"docker_context",
|
2019-10-02 16:56:49 +10:00
|
|
|
"dotnet",
|
2020-03-02 04:29:27 +01:00
|
|
|
"elixir",
|
2020-02-05 21:57:04 -06:00
|
|
|
"elm",
|
2019-09-26 10:30:58 +02:00
|
|
|
"env_var",
|
2021-07-02 11:00:46 -07:00
|
|
|
"erlang",
|
2020-08-04 06:30:20 +09:00
|
|
|
"gcloud",
|
2019-08-20 10:27:25 +05:45
|
|
|
"git_branch",
|
2019-12-06 08:57:42 -08:00
|
|
|
"git_commit",
|
2021-07-10 16:54:34 -04:00
|
|
|
"git_metrics",
|
2019-09-06 02:45:04 +10:00
|
|
|
"git_state",
|
2019-08-20 10:27:25 +05:45
|
|
|
"git_status",
|
|
|
|
|
"golang",
|
2020-07-17 10:51:25 +03:00
|
|
|
"helm",
|
2019-12-02 23:37:18 +01:00
|
|
|
"hg_branch",
|
2019-09-05 17:33:24 +02:00
|
|
|
"hostname",
|
2019-09-20 01:02:53 +02:00
|
|
|
"java",
|
2019-08-20 10:27:25 +05:45
|
|
|
"jobs",
|
2020-04-04 03:16:34 +09:00
|
|
|
"julia",
|
2020-12-26 15:26:50 +01:00
|
|
|
"kotlin",
|
2019-10-01 20:58:24 +02:00
|
|
|
"kubernetes",
|
2019-08-20 10:27:25 +05:45
|
|
|
"line_break",
|
2020-10-28 03:05:20 +09:00
|
|
|
"lua",
|
2019-09-28 22:55:49 -07:00
|
|
|
"memory_usage",
|
2020-06-09 10:14:47 -07:00
|
|
|
"nim",
|
2019-08-30 23:39:21 +10:00
|
|
|
"nix_shell",
|
2019-09-05 17:33:24 +02:00
|
|
|
"nodejs",
|
2020-05-22 01:43:13 +09:00
|
|
|
"ocaml",
|
2020-10-24 05:46:43 -04:00
|
|
|
"openstack",
|
2019-08-20 10:27:25 +05:45
|
|
|
"package",
|
2020-08-04 18:22:44 +02:00
|
|
|
"perl",
|
2021-07-02 11:00:46 -07:00
|
|
|
"php",
|
2020-05-23 01:26:58 +09:00
|
|
|
"purescript",
|
2019-08-20 10:27:25 +05:45
|
|
|
"python",
|
2021-04-20 17:31:47 +01:00
|
|
|
"red",
|
2021-07-02 11:00:46 -07:00
|
|
|
"rlang",
|
2019-08-20 10:27:25 +05:45
|
|
|
"ruby",
|
|
|
|
|
"rust",
|
2021-07-02 11:00:46 -07:00
|
|
|
"scala",
|
2021-02-20 14:40:49 +00:00
|
|
|
"shell",
|
2020-08-05 12:30:01 -04:00
|
|
|
"shlvl",
|
2020-02-26 17:18:19 +01:00
|
|
|
"singularity",
|
2020-09-26 00:04:51 +02:00
|
|
|
"status",
|
2021-07-02 11:00:46 -07:00
|
|
|
"swift",
|
|
|
|
|
"terraform",
|
2019-09-10 18:54:40 +01:00
|
|
|
"time",
|
2019-08-20 10:27:25 +05:45
|
|
|
"username",
|
2021-01-30 14:05:16 +03:00
|
|
|
"vagrant",
|
2021-07-02 11:00:46 -07:00
|
|
|
"vcsh",
|
2021-05-03 20:50:29 +01:00
|
|
|
"vlang",
|
2020-05-21 18:49:49 +02:00
|
|
|
"zig",
|
2019-08-20 10:27:25 +05:45
|
|
|
];
|
|
|
|
|
|
2019-05-01 16:34:24 -04: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 15:56:17 +01:00
|
|
|
pub struct Module<'a> {
|
|
|
|
|
/// The module's configuration map if available
|
2019-09-30 20:10:35 +08:00
|
|
|
pub config: Option<&'a toml::Value>,
|
2019-06-10 15:56:17 +01:00
|
|
|
|
2019-05-01 16:34:24 -04:00
|
|
|
/// The module's name, to be used in configuration and logging.
|
2020-08-16 19:16:05 -07:00
|
|
|
name: String,
|
2019-05-01 16:34:24 -04:00
|
|
|
|
2020-01-02 05:19:08 +01:00
|
|
|
/// The module's description
|
|
|
|
|
description: String,
|
|
|
|
|
|
2019-05-01 16:34:24 -04:00
|
|
|
/// The collection of segments that compose this module.
|
2020-07-08 06:45:32 +08:00
|
|
|
pub segments: Vec<Segment>,
|
2020-09-21 19:06:15 +02:00
|
|
|
|
|
|
|
|
/// the time it took to compute this module
|
|
|
|
|
pub duration: Duration,
|
2019-05-01 16:34:24 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-10 15:56:17 +01:00
|
|
|
impl<'a> Module<'a> {
|
2019-05-01 16:34:24 -04:00
|
|
|
/// Creates a module with no segments.
|
2020-01-02 05:19:08 +01:00
|
|
|
pub fn new(name: &str, desc: &str, config: Option<&'a toml::Value>) -> Module<'a> {
|
2019-05-01 16:34:24 -04:00
|
|
|
Module {
|
2019-06-10 15:56:17 +01:00
|
|
|
config,
|
2020-08-16 19:16:05 -07:00
|
|
|
name: name.to_string(),
|
2020-01-02 05:19:08 +01:00
|
|
|
description: desc.to_string(),
|
2019-05-01 16:34:24 -04:00
|
|
|
segments: Vec::new(),
|
2020-09-21 19:06:15 +02:00
|
|
|
duration: Duration::default(),
|
2019-05-01 16:34:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 01:16:18 +08:00
|
|
|
/// Set segments in module
|
2020-04-07 17:58:10 +08:00
|
|
|
pub fn set_segments(&mut self, segments: Vec<Segment>) {
|
2020-04-07 01:16:18 +08:00
|
|
|
self.segments = segments;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-06 08:46:46 -07:00
|
|
|
/// Get module's name
|
|
|
|
|
pub fn get_name(&self) -> &String {
|
2020-08-16 19:16:05 -07:00
|
|
|
&self.name
|
2019-10-06 08:46:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-01-02 05:19:08 +01:00
|
|
|
/// Get module's description
|
|
|
|
|
pub fn get_description(&self) -> &String {
|
|
|
|
|
&self.description
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 00:03:44 -05:00
|
|
|
/// Whether a module has non-empty segments
|
2019-05-13 22:43:11 -06:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2020-08-16 19:16:05 -07:00
|
|
|
self.segments
|
|
|
|
|
.iter()
|
2020-09-21 19:06:15 +02:00
|
|
|
// no trim: if we add spaces/linebreaks it's not "empty" as we change the final output
|
|
|
|
|
.all(|segment| segment.value.is_empty())
|
2019-05-13 22:43:11 -06:00
|
|
|
}
|
|
|
|
|
|
2020-07-08 06:45:32 +08:00
|
|
|
/// Get values of the module's segments
|
2020-01-02 05:19:08 +01:00
|
|
|
pub fn get_segments(&self) -> Vec<&str> {
|
2020-08-16 19:16:05 -07:00
|
|
|
self.segments
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|segment| segment.value.as_str())
|
|
|
|
|
.collect()
|
2020-01-02 05:19:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 16:34:24 -04:00
|
|
|
/// Returns a vector of colored ANSIString elements to be later used with
|
|
|
|
|
/// `ANSIStrings()` to optimize ANSI codes
|
|
|
|
|
pub fn ansi_strings(&self) -> Vec<ANSIString> {
|
2020-01-26 17:37:18 -05:00
|
|
|
self.ansi_strings_for_shell(Shell::Unknown)
|
2020-01-02 05:19:08 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-26 17:37:18 -05:00
|
|
|
pub fn ansi_strings_for_shell(&self, shell: Shell) -> Vec<ANSIString> {
|
2020-07-08 06:45:32 +08:00
|
|
|
let ansi_strings = self
|
2019-05-01 16:34:24 -04:00
|
|
|
.segments
|
|
|
|
|
.iter()
|
2019-07-31 19:48:51 -04:00
|
|
|
.map(Segment::ansi_string)
|
2019-05-01 16:34:24 -04:00
|
|
|
.collect::<Vec<ANSIString>>();
|
|
|
|
|
|
2020-07-08 06:45:32 +08:00
|
|
|
match shell {
|
2020-01-26 17:37:18 -05:00
|
|
|
Shell::Bash => ansi_strings_modified(ansi_strings, shell),
|
|
|
|
|
Shell::Zsh => ansi_strings_modified(ansi_strings, shell),
|
2021-02-27 13:55:27 -05:00
|
|
|
Shell::Tcsh => ansi_strings_modified(ansi_strings, shell),
|
2020-01-26 17:37:18 -05:00
|
|
|
_ => ansi_strings,
|
2020-07-08 06:45:32 +08:00
|
|
|
}
|
2019-05-01 16:34:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 15:56:17 +01:00
|
|
|
impl<'a> fmt::Display for Module<'a> {
|
2019-05-01 16:34:24 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
let ansi_strings = self.ansi_strings();
|
|
|
|
|
write!(f, "{}", ANSIStrings(&ansi_strings))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 17:37:18 -05:00
|
|
|
fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: Shell) -> Vec<ANSIString> {
|
2019-08-19 09:18:12 +05:45
|
|
|
ansi_strings
|
2020-02-06 10:10:59 -06:00
|
|
|
.into_iter()
|
2019-08-19 09:18:12 +05:45
|
|
|
.map(|ansi| {
|
2020-02-06 10:10:59 -06:00
|
|
|
let wrapped = wrap_colorseq_for_shell(ansi.to_string(), shell);
|
|
|
|
|
ANSIString::from(wrapped)
|
2019-08-19 09:18:12 +05:45
|
|
|
})
|
|
|
|
|
.collect::<Vec<ANSIString>>()
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 00:03:44 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2021-07-02 11:00:46 -07:00
|
|
|
#[test]
|
|
|
|
|
fn test_all_modules_is_in_alphabetical_order() {
|
|
|
|
|
let mut sorted_modules: Vec<&str> = ALL_MODULES.iter().copied().collect();
|
|
|
|
|
sorted_modules.sort_unstable();
|
|
|
|
|
assert_eq!(sorted_modules.as_slice(), ALL_MODULES);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-16 00:03:44 -05:00
|
|
|
#[test]
|
|
|
|
|
fn test_module_is_empty_with_no_segments() {
|
|
|
|
|
let name = "unit_test";
|
2020-01-02 05:19:08 +01:00
|
|
|
let desc = "This is a unit test";
|
2019-09-16 00:03:44 -05:00
|
|
|
let module = Module {
|
|
|
|
|
config: None,
|
2020-08-16 19:16:05 -07:00
|
|
|
name: name.to_string(),
|
2020-01-02 05:19:08 +01:00
|
|
|
description: desc.to_string(),
|
2019-09-16 00:03:44 -05:00
|
|
|
segments: Vec::new(),
|
2020-09-21 19:06:15 +02:00
|
|
|
duration: Duration::default(),
|
2019-09-16 00:03:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_module_is_empty_with_all_empty_segments() {
|
|
|
|
|
let name = "unit_test";
|
2020-01-02 05:19:08 +01:00
|
|
|
let desc = "This is a unit test";
|
2019-09-16 00:03:44 -05:00
|
|
|
let module = Module {
|
|
|
|
|
config: None,
|
2020-08-16 19:16:05 -07:00
|
|
|
name: name.to_string(),
|
2020-01-02 05:19:08 +01:00
|
|
|
description: desc.to_string(),
|
2020-08-16 19:16:05 -07:00
|
|
|
segments: vec![Segment::new(None, "")],
|
2020-09-21 19:06:15 +02:00
|
|
|
duration: Duration::default(),
|
2019-09-16 00:03:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
|
}
|
2020-09-21 19:06:15 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_module_is_not_empty_with_linebreak_only() {
|
|
|
|
|
let name = "unit_test";
|
|
|
|
|
let desc = "This is a unit test";
|
|
|
|
|
let module = Module {
|
|
|
|
|
config: None,
|
|
|
|
|
name: name.to_string(),
|
|
|
|
|
description: desc.to_string(),
|
|
|
|
|
segments: vec![Segment::new(None, "\n")],
|
|
|
|
|
duration: Duration::default(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(!module.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_module_is_not_empty_with_space_only() {
|
|
|
|
|
let name = "unit_test";
|
|
|
|
|
let desc = "This is a unit test";
|
|
|
|
|
let module = Module {
|
|
|
|
|
config: None,
|
|
|
|
|
name: name.to_string(),
|
|
|
|
|
description: desc.to_string(),
|
|
|
|
|
segments: vec![Segment::new(None, " ")],
|
|
|
|
|
duration: Duration::default(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert!(!module.is_empty());
|
|
|
|
|
}
|
2019-09-16 00:03:44 -05:00
|
|
|
}
|