feat: add singularity module (#932)

This commit is contained in:
Tadej Novak 2020-02-26 17:18:19 +01:00 committed by GitHub
parent 15f4202df1
commit 7e66791cb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 106 additions and 0 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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,
}
}
}

View File

@ -19,6 +19,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
prompt_order: vec![
"username",
"hostname",
"singularity",
"kubernetes",
"directory",
"git_branch",

View File

@ -42,6 +42,7 @@ pub const ALL_MODULES: &[&str] = &[
"rust",
"php",
"terraform",
"singularity",
"time",
"username",
];

View File

@ -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<Module<'a>> {
"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),

View File

@ -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<Module<'a>> {
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)
}

View File

@ -18,6 +18,7 @@ mod line_break;
mod modules;
mod nix_shell;
mod python;
mod singularity;
mod terraform;
mod time;
mod username;

View File

@ -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(())
}