From 7e66791cb2228882530f55c48a79d2917953cb47 Mon Sep 17 00:00:00 2001 From: Tadej Novak Date: Wed, 26 Feb 2020 17:18:19 +0100 Subject: [PATCH] feat: add singularity module (#932) --- docs/config/README.md | 27 +++++++++++++++++++++++++++ src/configs/mod.rs | 1 + src/configs/singularity.rs | 27 +++++++++++++++++++++++++++ src/configs/starship_root.rs | 1 + src/module.rs | 1 + src/modules/mod.rs | 2 ++ src/modules/singularity.rs | 28 ++++++++++++++++++++++++++++ tests/testsuite/main.rs | 1 + tests/testsuite/singularity.rs | 18 ++++++++++++++++++ 9 files changed, 106 insertions(+) create mode 100644 src/configs/singularity.rs create mode 100644 src/modules/singularity.rs create mode 100644 tests/testsuite/singularity.rs diff --git a/docs/config/README.md b/docs/config/README.md index 3e1c7a2c..b63428b2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1091,6 +1091,33 @@ The module will be shown if any of the following conditions are met: symbol = "⚙️ " ``` +## Singularity + +The `singularity` module shows the current singularity image, if inside a container +and `$SINGULARITY_NAME` is set. + +::: + +### Options + +| Variable | Default | Description | +| ---------- | -------------------- | ------------------------------------------------ | +| `label` | `""` | Prefix before the image name display. | +| `prefix` | `"["` | Prefix to display immediately before image name. | +| `suffix` | `"]"` | Suffix to display immediately after image name. | +| `symbol` | `""` | The symbol used before the image name. | +| `style` | `"bold dimmed blue"` | The style for the module. | +| `disabled` | `false` | Disables the `singularity` module. | + +### Example + +```toml +# ~/.config/starship.toml + +[singularity] +symbol = "📦 " +``` + ## Terraform The `terraform` module shows the currently selected terraform workspace and version. diff --git a/src/configs/mod.rs b/src/configs/mod.rs index d3c6d4f3..af62804c 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -27,6 +27,7 @@ pub mod php; pub mod python; pub mod ruby; pub mod rust; +pub mod singularity; mod starship_root; pub mod terraform; pub mod time; diff --git a/src/configs/singularity.rs b/src/configs/singularity.rs new file mode 100644 index 00000000..4ed19242 --- /dev/null +++ b/src/configs/singularity.rs @@ -0,0 +1,27 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct SingularityConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub label: &'a str, + pub prefix: &'a str, + pub suffix: &'a str, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for SingularityConfig<'a> { + fn new() -> Self { + SingularityConfig { + symbol: SegmentConfig::default(), + label: "", + prefix: "[", + suffix: "]", + style: Color::Blue.bold().dimmed(), + disabled: false, + } + } +} diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index db024df7..f645c2c9 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -19,6 +19,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> { prompt_order: vec![ "username", "hostname", + "singularity", "kubernetes", "directory", "git_branch", diff --git a/src/module.rs b/src/module.rs index 81ecad51..cfce216c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -42,6 +42,7 @@ pub const ALL_MODULES: &[&str] = &[ "rust", "php", "terraform", + "singularity", "time", "username", ]; diff --git a/src/modules/mod.rs b/src/modules/mod.rs index a1a5d192..982dabba 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -28,6 +28,7 @@ mod php; mod python; mod ruby; mod rust; +mod singularity; mod terraform; mod time; mod username; @@ -74,6 +75,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "python" => python::module(context), "ruby" => ruby::module(context), "rust" => rust::module(context), + "singularity" => singularity::module(context), "terraform" => terraform::module(context), "time" => time::module(context), "crystal" => crystal::module(context), diff --git a/src/modules/singularity.rs b/src/modules/singularity.rs new file mode 100644 index 00000000..ede81085 --- /dev/null +++ b/src/modules/singularity.rs @@ -0,0 +1,28 @@ +use std::env; + +use super::{Context, Module, SegmentConfig}; + +use crate::config::RootModuleConfig; +use crate::configs::singularity::SingularityConfig; + +/// Creates a module with the current Singularity image +/// +/// Will display the Singularity image if `$SINGULARITY_NAME` is set. +pub fn module<'a>(context: &'a Context) -> Option> { + let singularity_env = env::var("SINGULARITY_NAME").unwrap_or_else(|_| "".into()); + if singularity_env.trim().is_empty() { + return None; + } + + let mut module = context.new_module("singularity"); + let config = SingularityConfig::try_load(module.config); + + module.get_prefix().set_value(config.label); + module.set_style(config.style); + module.create_segment("symbol", &config.symbol); + + let env_var_stacked = format!("{}{}{}", config.prefix, singularity_env, config.suffix); + module.create_segment("singularity", &SegmentConfig::new(&env_var_stacked)); + + Some(module) +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 572a002b..2a8f443d 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -18,6 +18,7 @@ mod line_break; mod modules; mod nix_shell; mod python; +mod singularity; mod terraform; mod time; mod username; diff --git a/tests/testsuite/singularity.rs b/tests/testsuite/singularity.rs new file mode 100644 index 00000000..26067baa --- /dev/null +++ b/tests/testsuite/singularity.rs @@ -0,0 +1,18 @@ +use ansi_term::Color; +use std::io; + +use crate::common; + +#[test] +fn env_set() -> io::Result<()> { + let output = common::render_module("singularity") + .env_clear() + .env("SINGULARITY_NAME", "centos.img") + .output()?; + + let expected = format!("{} ", Color::Blue.bold().dimmed().paint("[centos.img]")); + let actual = String::from_utf8(output.stdout).unwrap(); + + assert_eq!(expected, actual); + Ok(()) +}