2020-01-26 22:37:18 +00:00
|
|
|
use crate::context::Shell;
|
2019-05-01 20:34:24 +00:00
|
|
|
use crate::segment::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;
|
|
|
|
|
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",
|
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",
|
2020-04-27 10:09:42 +00:00
|
|
|
"erlang",
|
2019-09-26 08:30:58 +00:00
|
|
|
"env_var",
|
2019-08-20 04:42:25 +00:00
|
|
|
"git_branch",
|
2019-12-06 16:57:42 +00:00
|
|
|
"git_commit",
|
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",
|
2020-04-03 18:16:34 +00:00
|
|
|
"julia",
|
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",
|
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",
|
2019-08-20 04:42:25 +00:00
|
|
|
"package",
|
2020-05-22 16:26:58 +00:00
|
|
|
"purescript",
|
2019-08-20 04:42:25 +00:00
|
|
|
"python",
|
|
|
|
"ruby",
|
2020-02-04 23:27:06 +00:00
|
|
|
"crystal",
|
2019-08-20 04:42:25 +00:00
|
|
|
"rust",
|
2019-12-05 18:04:27 +00:00
|
|
|
"php",
|
2019-12-09 01:42:51 +00:00
|
|
|
"terraform",
|
2020-02-26 16:18:19 +00:00
|
|
|
"singularity",
|
2019-09-10 17:54:40 +00:00
|
|
|
"time",
|
2019-08-20 04:42:25 +00:00
|
|
|
"username",
|
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.
|
2019-09-14 14:23:53 +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>,
|
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,
|
2019-09-14 14:23:53 +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-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 {
|
|
|
|
&self._name
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-09-16 05:03:44 +00:00
|
|
|
self.segments.iter().all(|segment| segment.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> {
|
|
|
|
self.segments.iter().map(Segment::get_value).collect()
|
|
|
|
}
|
|
|
|
|
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> {
|
2020-01-26 22:37:18 +00:00
|
|
|
self.ansi_strings_for_shell(Shell::Unknown)
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 22:37:18 +00:00
|
|
|
pub fn ansi_strings_for_shell(&self, shell: Shell) -> Vec<ANSIString> {
|
2020-07-07 22:45:32 +00:00
|
|
|
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>>();
|
|
|
|
|
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),
|
|
|
|
_ => 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>>()
|
|
|
|
}
|
|
|
|
|
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";
|
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,
|
|
|
|
_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(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
_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![Segment::new("test_segment")],
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(module.is_empty());
|
|
|
|
}
|
|
|
|
}
|