Files
starship/src/module.rs
T

309 lines
7.7 KiB
Rust
Raw Normal View History

use crate::context::Shell;
2022-05-23 12:58:27 +02:00
use crate::segment;
use crate::segment::{FillSegment, Segment};
use crate::utils::wrap_colorseq_for_shell;
use nu_ansi_term::{AnsiString, AnsiStrings};
2019-05-01 16:34:24 -04:00
use std::fmt;
2020-09-21 19:06:15 +02:00
use std::time::Duration;
2019-05-01 16:34:24 -04:00
// List of all modules
// Default ordering is handled in configs/starship_root.rs
pub const ALL_MODULES: &[&str] = &[
2019-09-26 04:55:47 +02:00
"aws",
2021-12-06 23:01:33 +01:00
"azure",
#[cfg(feature = "battery")]
"battery",
2022-03-12 03:10:23 -08:00
"buf",
2022-08-01 12:59:36 +02:00
"bun",
2022-03-25 04:10:19 +00:00
"c",
"character",
2020-07-09 21:40:33 +02:00
"cmake",
"cmd_duration",
2021-09-07 07:59:14 -07:00
"cobol",
2019-10-05 20:25:25 +02:00
"conda",
2022-01-21 16:44:46 +01:00
"container",
"crystal",
2022-05-26 16:42:31 +02:00
"daml",
2020-07-29 17:38:23 +02:00
"dart",
2021-04-15 06:22:12 -07:00
"deno",
"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",
"erlang",
2022-12-21 18:53:53 +02:00
"fennel",
"fill",
"fossil_branch",
2020-08-04 06:30:20 +09:00
"gcloud",
"git_branch",
2019-12-06 08:57:42 -08:00
"git_commit",
2021-07-10 16:54:34 -04:00
"git_metrics",
"git_state",
"git_status",
"golang",
2022-12-17 13:32:40 +01:00
"gradle",
"guix_shell",
2022-03-18 14:45:51 +08:00
"haskell",
2022-12-04 18:28:49 +01:00
"haxe",
2020-07-17 10:51:25 +03:00
"helm",
2019-12-02 23:37:18 +01:00
"hg_branch",
"hostname",
2019-09-20 01:02:53 +02:00
"java",
"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",
"line_break",
"localip",
2020-10-28 03:05:20 +09:00
"lua",
2019-09-28 22:55:49 -07:00
"memory_usage",
"meson",
2020-06-09 10:14:47 -07:00
"nim",
"nix_shell",
"nodejs",
2020-05-22 01:43:13 +09:00
"ocaml",
"opa",
"openstack",
2022-11-06 14:37:58 -07:00
"os",
"package",
"perl",
"php",
2022-12-31 09:55:23 -05:00
"pijul_channel",
2021-10-05 16:27:25 -07:00
"pulumi",
"purescript",
"python",
2022-06-26 12:00:55 +02:00
"raku",
2021-04-20 17:31:47 +01:00
"red",
"rlang",
"ruby",
"rust",
"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",
2022-04-03 15:33:14 +02:00
"spack",
2020-09-26 00:04:51 +02:00
"status",
2021-11-15 02:46:13 -03:00
"sudo",
"swift",
"terraform",
"time",
"username",
"vagrant",
"vcsh",
2021-05-03 20:50:29 +01:00
"vlang",
2020-05-21 18:49:49 +02:00
"zig",
];
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.
name: String,
2019-05-01 16:34:24 -04:00
/// The module's description
description: String,
2019-05-01 16:34:24 -04:00
/// The collection of segments that compose this module.
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.
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,
name: name.to_string(),
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
}
}
/// Set segments in module
2020-04-07 17:58:10 +08:00
pub fn set_segments(&mut self, segments: Vec<Segment>) {
self.segments = segments;
}
/// Get module's name
pub fn get_name(&self) -> &String {
&self.name
}
/// Get module's description
pub fn get_description(&self) -> &String {
&self.description
}
/// Whether a module has non-empty segments
2019-05-13 22:43:11 -06:00
pub fn is_empty(&self) -> bool {
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
}
/// Get values of the module's segments
pub fn get_segments(&self) -> Vec<&str> {
2022-05-23 12:58:27 +02:00
self.segments.iter().map(segment::Segment::value).collect()
}
/// Returns a vector of colored `AnsiString` elements to be later used with
/// `AnsiStrings()` to optimize ANSI codes
pub fn ansi_strings(&self) -> Vec<AnsiString> {
self.ansi_strings_for_shell(Shell::Unknown, None)
}
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 16:34:24 -04:00
match shell {
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),
_ => ansi_strings,
}
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))
2019-05-01 16:34:24 -04:00
}
}
fn ansi_strings_modified(ansi_strings: Vec<AnsiString>, shell: Shell) -> Vec<AnsiString> {
ansi_strings
.into_iter()
.map(|ansi| {
let wrapped = wrap_colorseq_for_shell(ansi.to_string(), shell);
AnsiString::from(wrapped)
})
.collect::<Vec<AnsiString>>()
}
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 matches!(segment, Segment::LineTerm) {
break;
}
}
if chunks.is_empty() {
current
} else {
let fill_size = term_width
.and_then(|tw| if tw > used { Some(tw - used) } else { None })
.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>>()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_all_modules_is_in_alphabetical_order() {
let mut sorted_modules: Vec<&str> = ALL_MODULES.to_vec();
sorted_modules.sort_unstable();
assert_eq!(sorted_modules.as_slice(), ALL_MODULES);
}
#[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(),
segments: Vec::new(),
2020-09-21 19:06:15 +02:00
duration: Duration::default(),
};
assert!(module.is_empty());
}
#[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(),
segments: Segment::from_text(None, ""),
2020-09-21 19:06:15 +02:00
duration: Duration::default(),
};
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: Segment::from_text(None, "\n"),
2020-09-21 19:06:15 +02: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(),
segments: Segment::from_text(None, " "),
2020-09-21 19:06:15 +02:00
duration: Duration::default(),
};
assert!(!module.is_empty());
}
}