2020-01-26 22:37:18 +00:00
|
|
|
use crate::context::Shell;
|
2021-09-12 23:59:15 +00:00
|
|
|
use crate::segment::{FillSegment, Segment};
|
2020-02-06 16:10:59 +00:00
|
|
|
use crate::utils::wrap_colorseq_for_shell;
|
2019-05-01 20:34:24 +00:00
|
|
|
use ansi_term::{ANSIString, ANSIStrings};
|
|
|
|
use std::fmt;
|
2020-09-21 17:06:15 +00:00
|
|
|
use std::time::Duration;
|
2019-05-01 20:34:24 +00:00
|
|
|
|
2019-08-20 04:42:25 +00:00
|
|
|
// List of all modules
|
2020-09-08 16:09:21 +00:00
|
|
|
// Default ordering is handled in configs/starship_root.rs
|
2019-08-20 04:42:25 +00:00
|
|
|
pub const ALL_MODULES: &[&str] = &[
|
2019-09-26 02:55:47 +00:00
|
|
|
"aws",
|
2021-12-06 22:01:33 +00:00
|
|
|
"azure",
|
2019-08-26 18:09:39 +00:00
|
|
|
#[cfg(feature = "battery")]
|
2019-08-20 04:42:25 +00:00
|
|
|
"battery",
|
2022-03-12 11:10:23 +00:00
|
|
|
"buf",
|
2019-08-20 04:42:25 +00:00
|
|
|
"character",
|
2020-07-09 19:40:33 +00:00
|
|
|
"cmake",
|
2019-08-20 04:42:25 +00:00
|
|
|
"cmd_duration",
|
2021-09-07 14:59:14 +00:00
|
|
|
"cobol",
|
2019-10-05 18:25:25 +00:00
|
|
|
"conda",
|
2022-01-21 15:44:46 +00:00
|
|
|
"container",
|
2021-07-02 18:00:46 +00:00
|
|
|
"crystal",
|
2020-07-29 15:38:23 +00:00
|
|
|
"dart",
|
2021-04-15 13:22:12 +00:00
|
|
|
"deno",
|
2019-08-20 04:42:25 +00:00
|
|
|
"directory",
|
2020-04-05 19:42:55 +00:00
|
|
|
"docker_context",
|
2019-10-02 06:56:49 +00:00
|
|
|
"dotnet",
|
2020-03-02 03:29:27 +00:00
|
|
|
"elixir",
|
2020-02-06 03:57:04 +00:00
|
|
|
"elm",
|
2019-09-26 08:30:58 +00:00
|
|
|
"env_var",
|
2021-07-02 18:00:46 +00:00
|
|
|
"erlang",
|
2021-09-12 23:59:15 +00:00
|
|
|
"fill",
|
2020-08-03 21:30:20 +00:00
|
|
|
"gcloud",
|
2019-08-20 04:42:25 +00:00
|
|
|
"git_branch",
|
2019-12-06 16:57:42 +00:00
|
|
|
"git_commit",
|
2021-07-10 20:54:34 +00:00
|
|
|
"git_metrics",
|
2019-09-05 16:45:04 +00:00
|
|
|
"git_state",
|
2019-08-20 04:42:25 +00:00
|
|
|
"git_status",
|
|
|
|
"golang",
|
2020-07-17 07:51:25 +00:00
|
|
|
"helm",
|
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",
|
2020-04-03 18:16:34 +00:00
|
|
|
"julia",
|
2020-12-26 14:26:50 +00:00
|
|
|
"kotlin",
|
2019-10-01 18:58:24 +00:00
|
|
|
"kubernetes",
|
2019-08-20 04:42:25 +00:00
|
|
|
"line_break",
|
2022-01-30 20:31:26 +00:00
|
|
|
"localip",
|
2020-10-27 18:05:20 +00:00
|
|
|
"lua",
|
2019-09-29 05:55:49 +00:00
|
|
|
"memory_usage",
|
2020-06-09 17:14:47 +00:00
|
|
|
"nim",
|
2019-08-30 13:39:21 +00:00
|
|
|
"nix_shell",
|
2019-09-05 15:33:24 +00:00
|
|
|
"nodejs",
|
2020-05-21 16:43:13 +00:00
|
|
|
"ocaml",
|
2020-10-24 09:46:43 +00:00
|
|
|
"openstack",
|
2019-08-20 04:42:25 +00:00
|
|
|
"package",
|
2020-08-04 16:22:44 +00:00
|
|
|
"perl",
|
2021-07-02 18:00:46 +00:00
|
|
|
"php",
|
2021-10-05 23:27:25 +00:00
|
|
|
"pulumi",
|
2020-05-22 16:26:58 +00:00
|
|
|
"purescript",
|
2019-08-20 04:42:25 +00:00
|
|
|
"python",
|
2021-04-20 16:31:47 +00:00
|
|
|
"red",
|
2021-07-02 18:00:46 +00:00
|
|
|
"rlang",
|
2019-08-20 04:42:25 +00:00
|
|
|
"ruby",
|
|
|
|
"rust",
|
2021-07-02 18:00:46 +00:00
|
|
|
"scala",
|
2021-02-20 14:40:49 +00:00
|
|
|
"shell",
|
2020-08-05 16:30:01 +00:00
|
|
|
"shlvl",
|
2020-02-26 16:18:19 +00:00
|
|
|
"singularity",
|
2020-09-25 22:04:51 +00:00
|
|
|
"status",
|
2021-11-15 05:46:13 +00:00
|
|
|
"sudo",
|
2021-07-02 18:00:46 +00:00
|
|
|
"swift",
|
|
|
|
"terraform",
|
2019-09-10 17:54:40 +00:00
|
|
|
"time",
|
2019-08-20 04:42:25 +00:00
|
|
|
"username",
|
2021-01-30 11:05:16 +00:00
|
|
|
"vagrant",
|
2021-07-02 18:00:46 +00:00
|
|
|
"vcsh",
|
2021-05-03 19:50:29 +00:00
|
|
|
"vlang",
|
2020-05-21 16:49:49 +00:00
|
|
|
"zig",
|
2019-08-20 04:42:25 +00:00
|
|
|
];
|
|
|
|
|
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.
|
2020-08-17 02:16:05 +00:00
|
|
|
name: String,
|
2019-05-01 20:34:24 +00:00
|
|
|
|
2020-01-02 04:19:08 +00:00
|
|
|
/// The module's description
|
|
|
|
description: String,
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
/// The collection of segments that compose this module.
|
2020-07-07 22:45:32 +00:00
|
|
|
pub segments: Vec<Segment>,
|
2020-09-21 17:06:15 +00:00
|
|
|
|
|
|
|
/// the time it took to compute this module
|
|
|
|
pub duration: Duration,
|
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.
|
2020-01-02 04:19:08 +00:00
|
|
|
pub fn new(name: &str, desc: &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,
|
2020-08-17 02:16:05 +00:00
|
|
|
name: name.to_string(),
|
2020-01-02 04:19:08 +00:00
|
|
|
description: desc.to_string(),
|
2019-05-01 20:34:24 +00:00
|
|
|
segments: Vec::new(),
|
2020-09-21 17:06:15 +00:00
|
|
|
duration: Duration::default(),
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:16:18 +00:00
|
|
|
/// Set segments in module
|
2020-04-07 09:58:10 +00:00
|
|
|
pub fn set_segments(&mut self, segments: Vec<Segment>) {
|
2020-04-06 17:16:18 +00:00
|
|
|
self.segments = segments;
|
|
|
|
}
|
|
|
|
|
2019-10-06 15:46:46 +00:00
|
|
|
/// Get module's name
|
|
|
|
pub fn get_name(&self) -> &String {
|
2020-08-17 02:16:05 +00:00
|
|
|
&self.name
|
2019-10-06 15:46:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 04:19:08 +00:00
|
|
|
/// Get module's description
|
|
|
|
pub fn get_description(&self) -> &String {
|
|
|
|
&self.description
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-08-17 02:16:05 +00:00
|
|
|
self.segments
|
|
|
|
.iter()
|
2020-09-21 17:06:15 +00:00
|
|
|
// no trim: if we add spaces/linebreaks it's not "empty" as we change the final output
|
2021-09-12 23:59:15 +00:00
|
|
|
.all(|segment| segment.value().is_empty())
|
2019-05-14 04:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
/// Get values of the module's segments
|
2020-01-02 04:19:08 +00:00
|
|
|
pub fn get_segments(&self) -> Vec<&str> {
|
2020-08-17 02:16:05 +00:00
|
|
|
self.segments
|
|
|
|
.iter()
|
2021-09-12 23:59:15 +00:00
|
|
|
.map(|segment| segment.value())
|
2020-08-17 02:16:05 +00:00
|
|
|
.collect()
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:34:24 +00: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> {
|
2021-09-12 23:59:15 +00:00
|
|
|
self.ansi_strings_for_shell(Shell::Unknown, None)
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 23:59:15 +00:00
|
|
|
pub fn ansi_strings_for_shell(&self, shell: Shell, width: Option<usize>) -> Vec<ANSIString> {
|
|
|
|
let mut iter = self.segments.iter().peekable();
|
|
|
|
let mut ansi_strings: Vec<ANSIString> = Vec::new();
|
|
|
|
while iter.peek().is_some() {
|
|
|
|
ansi_strings.extend(ansi_line(&mut iter, width));
|
|
|
|
}
|
2019-05-01 20:34:24 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
match shell {
|
2020-01-26 22:37:18 +00:00
|
|
|
Shell::Bash => ansi_strings_modified(ansi_strings, shell),
|
|
|
|
Shell::Zsh => ansi_strings_modified(ansi_strings, shell),
|
2021-02-27 18:55:27 +00:00
|
|
|
Shell::Tcsh => ansi_strings_modified(ansi_strings, shell),
|
2020-01-26 22:37:18 +00:00
|
|
|
_ => ansi_strings,
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 22:37:18 +00:00
|
|
|
fn ansi_strings_modified(ansi_strings: Vec<ANSIString>, shell: Shell) -> Vec<ANSIString> {
|
2019-08-19 03:33:12 +00:00
|
|
|
ansi_strings
|
2020-02-06 16:10:59 +00:00
|
|
|
.into_iter()
|
2019-08-19 03:33:12 +00:00
|
|
|
.map(|ansi| {
|
2020-02-06 16:10:59 +00:00
|
|
|
let wrapped = wrap_colorseq_for_shell(ansi.to_string(), shell);
|
|
|
|
ANSIString::from(wrapped)
|
2019-08-19 03:33:12 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<ANSIString>>()
|
|
|
|
}
|
|
|
|
|
2021-09-12 23:59:15 +00:00
|
|
|
fn ansi_line<'a, I>(segments: &mut I, term_width: Option<usize>) -> Vec<ANSIString<'a>>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = &'a Segment>,
|
|
|
|
{
|
|
|
|
let mut used = 0usize;
|
|
|
|
let mut current: Vec<ANSIString> = Vec::new();
|
|
|
|
let mut chunks: Vec<(Vec<ANSIString>, &FillSegment)> = Vec::new();
|
|
|
|
|
|
|
|
for segment in segments {
|
|
|
|
match segment {
|
|
|
|
Segment::Fill(fs) => {
|
|
|
|
chunks.push((current, fs));
|
|
|
|
current = Vec::new();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
used += segment.width_graphemes();
|
|
|
|
current.push(segment.ansi_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Segment::LineTerm = segment {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if chunks.is_empty() {
|
|
|
|
current
|
|
|
|
} else {
|
|
|
|
let fill_size = term_width
|
2022-02-25 04:31:01 +00:00
|
|
|
.and_then(|tw| if tw > used { Some(tw - used) } else { None })
|
2021-09-12 23:59:15 +00:00
|
|
|
.map(|remaining| remaining / chunks.len());
|
|
|
|
chunks
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|(strs, fill)| {
|
|
|
|
strs.into_iter()
|
|
|
|
.chain(std::iter::once(fill.ansi_string(fill_size)))
|
|
|
|
})
|
|
|
|
.chain(current.into_iter())
|
|
|
|
.collect::<Vec<ANSIString>>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 05:03:44 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2021-07-02 18:00:46 +00:00
|
|
|
#[test]
|
|
|
|
fn test_all_modules_is_in_alphabetical_order() {
|
2022-02-25 04:31:01 +00:00
|
|
|
let mut sorted_modules: Vec<&str> = ALL_MODULES.to_vec();
|
2021-07-02 18:00:46 +00:00
|
|
|
sorted_modules.sort_unstable();
|
|
|
|
assert_eq!(sorted_modules.as_slice(), ALL_MODULES);
|
|
|
|
}
|
|
|
|
|
2019-09-16 05:03:44 +00:00
|
|
|
#[test]
|
|
|
|
fn test_module_is_empty_with_no_segments() {
|
|
|
|
let name = "unit_test";
|
2020-01-02 04:19:08 +00:00
|
|
|
let desc = "This is a unit test";
|
2019-09-16 05:03:44 +00:00
|
|
|
let module = Module {
|
|
|
|
config: None,
|
2020-08-17 02:16:05 +00:00
|
|
|
name: name.to_string(),
|
2020-01-02 04:19:08 +00:00
|
|
|
description: desc.to_string(),
|
2019-09-16 05:03:44 +00:00
|
|
|
segments: Vec::new(),
|
2020-09-21 17:06:15 +00:00
|
|
|
duration: Duration::default(),
|
2019-09-16 05:03:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_is_empty_with_all_empty_segments() {
|
|
|
|
let name = "unit_test";
|
2020-01-02 04:19:08 +00:00
|
|
|
let desc = "This is a unit test";
|
2019-09-16 05:03:44 +00:00
|
|
|
let module = Module {
|
|
|
|
config: None,
|
2020-08-17 02:16:05 +00:00
|
|
|
name: name.to_string(),
|
2020-01-02 04:19:08 +00:00
|
|
|
description: desc.to_string(),
|
2021-09-12 23:59:15 +00:00
|
|
|
segments: Segment::from_text(None, ""),
|
2020-09-21 17:06:15 +00:00
|
|
|
duration: Duration::default(),
|
2019-09-16 05:03:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
}
|
2020-09-21 17:06:15 +00: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(),
|
2021-09-12 23:59:15 +00:00
|
|
|
segments: Segment::from_text(None, "\n"),
|
2020-09-21 17:06:15 +00:00
|
|
|
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(),
|
2021-09-12 23:59:15 +00:00
|
|
|
segments: Segment::from_text(None, " "),
|
2020-09-21 17:06:15 +00:00
|
|
|
duration: Duration::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!module.is_empty());
|
|
|
|
}
|
2019-09-16 05:03:44 +00:00
|
|
|
}
|