2022-05-23 10:58:27 +00:00
|
|
|
use crate::segment;
|
2021-09-12 23:59:15 +00:00
|
|
|
use crate::segment::{FillSegment, Segment};
|
2024-06-28 21:40:35 +00:00
|
|
|
use nu_ansi_term::{AnsiString, AnsiStrings, Style as AnsiStyle};
|
2019-05-01 20:34:24 +00:00
|
|
|
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",
|
2022-08-01 10:59:36 +00:00
|
|
|
"bun",
|
2022-03-25 04:10:19 +00:00
|
|
|
"c",
|
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",
|
2022-05-26 14:42:31 +00:00
|
|
|
"daml",
|
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",
|
2023-12-17 07:22:29 +00:00
|
|
|
"direnv",
|
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",
|
2021-07-02 18:00:46 +00:00
|
|
|
"erlang",
|
2022-12-21 16:53:53 +00:00
|
|
|
"fennel",
|
2021-09-12 23:59:15 +00:00
|
|
|
"fill",
|
2023-01-31 08:06:46 +00:00
|
|
|
"fossil_branch",
|
2023-09-02 07:19:04 +00:00
|
|
|
"fossil_metrics",
|
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",
|
2024-04-05 21:56:15 +00:00
|
|
|
"gleam",
|
2019-08-20 04:42:25 +00:00
|
|
|
"golang",
|
2022-12-17 12:32:40 +00:00
|
|
|
"gradle",
|
2022-10-25 05:44:04 +00:00
|
|
|
"guix_shell",
|
2022-03-18 06:45:51 +00:00
|
|
|
"haskell",
|
2022-12-04 17:28:49 +00:00
|
|
|
"haxe",
|
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",
|
2022-10-11 16:02:46 +00:00
|
|
|
"meson",
|
2024-05-15 16:46:14 +00:00
|
|
|
"nats",
|
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",
|
2024-04-05 21:56:49 +00:00
|
|
|
"odin",
|
2022-10-15 16:15:33 +00:00
|
|
|
"opa",
|
2020-10-24 09:46:43 +00:00
|
|
|
"openstack",
|
2022-11-06 21:37:58 +00:00
|
|
|
"os",
|
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",
|
2022-12-31 14:55:23 +00:00
|
|
|
"pijul_channel",
|
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",
|
2024-03-20 21:50:12 +00:00
|
|
|
"quarto",
|
2022-06-26 10:00:55 +00:00
|
|
|
"raku",
|
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",
|
2023-04-13 19:05:12 +00:00
|
|
|
"solidity",
|
2022-04-03 13:33:14 +00:00
|
|
|
"spack",
|
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",
|
2023-10-26 13:31:16 +00:00
|
|
|
"typst",
|
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.
|
2024-06-22 07:52:46 +00:00
|
|
|
pub fn new(name: &str, desc: &str, config: Option<&'a toml::Value>) -> Self {
|
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> {
|
2022-05-23 10:58:27 +00:00
|
|
|
self.segments.iter().map(segment::Segment::value).collect()
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 16:44:54 +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> {
|
2024-04-06 13:28:26 +00:00
|
|
|
self.ansi_strings_for_width(None)
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-06 13:28:26 +00:00
|
|
|
pub fn ansi_strings_for_width(&self, width: Option<usize>) -> Vec<AnsiString> {
|
2021-09-12 23:59:15 +00:00
|
|
|
let mut iter = self.segments.iter().peekable();
|
2022-09-04 16:44:54 +00:00
|
|
|
let mut ansi_strings: Vec<AnsiString> = Vec::new();
|
2021-09-12 23:59:15 +00:00
|
|
|
while iter.peek().is_some() {
|
|
|
|
ansi_strings.extend(ansi_line(&mut iter, width));
|
|
|
|
}
|
2024-04-06 13:28:26 +00:00
|
|
|
ansi_strings
|
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();
|
2022-09-04 16:44:54 +00:00
|
|
|
write!(f, "{}", AnsiStrings(&ansi_strings))
|
2019-05-01 20:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 16:44:54 +00:00
|
|
|
fn ansi_line<'a, I>(segments: &mut I, term_width: Option<usize>) -> Vec<AnsiString<'a>>
|
2021-09-12 23:59:15 +00:00
|
|
|
where
|
|
|
|
I: Iterator<Item = &'a Segment>,
|
|
|
|
{
|
|
|
|
let mut used = 0usize;
|
2022-09-04 16:44:54 +00:00
|
|
|
let mut current: Vec<AnsiString> = Vec::new();
|
|
|
|
let mut chunks: Vec<(Vec<AnsiString>, &FillSegment)> = Vec::new();
|
2024-06-28 21:40:35 +00:00
|
|
|
let mut prev_style: Option<AnsiStyle> = None;
|
2021-09-12 23:59:15 +00:00
|
|
|
|
|
|
|
for segment in segments {
|
|
|
|
match segment {
|
|
|
|
Segment::Fill(fs) => {
|
|
|
|
chunks.push((current, fs));
|
|
|
|
current = Vec::new();
|
2024-06-28 21:40:35 +00:00
|
|
|
prev_style = None;
|
2021-09-12 23:59:15 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
used += segment.width_graphemes();
|
2024-06-28 21:40:35 +00:00
|
|
|
let current_segment_string = segment.ansi_string(prev_style.as_ref());
|
|
|
|
|
|
|
|
prev_style = Some(*current_segment_string.style_ref());
|
|
|
|
current.push(current_segment_string);
|
2021-09-12 23:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 17:01:27 +00:00
|
|
|
if matches!(segment, Segment::LineTerm) {
|
2021-09-12 23:59:15 +00:00
|
|
|
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)| {
|
2024-06-28 21:40:35 +00:00
|
|
|
let fill_string =
|
|
|
|
fill.ansi_string(fill_size, strs.last().map(|segment| segment.style_ref()));
|
|
|
|
strs.into_iter().chain(std::iter::once(fill_string))
|
2021-09-12 23:59:15 +00:00
|
|
|
})
|
2023-08-25 20:53:35 +00:00
|
|
|
.chain(current)
|
2022-09-04 16:44:54 +00:00
|
|
|
.collect::<Vec<AnsiString>>()
|
2021-09-12 23:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|