Skip to content

Commit

Permalink
feat(shlvl): Add shlvl module (#1385)
Browse files Browse the repository at this point in the history
* initial commit of support for shlvl

* documentation for shlvl

* use a symbol instead

* test coverage for shlvl

* actually disable when the config says to

* fix docs

* tweak defaults some

* refactor from pr comments

* redisable

* return early to avoid indenting

* make default suffix empty space

* fixing tests after suffix change

* updating docs for format

* making shlvl compatible with formatting

* adding variables table for shlvl

* removing extra line

* doc clarity
  • Loading branch information
daniel-white committed Aug 5, 2020
1 parent c07b798 commit 0be9ffc
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ format = """
$username\
$hostname\
$shlvl\
$kubernetes\
$directory\
$git_branch\
Expand Down Expand Up @@ -1985,6 +1986,42 @@ The module will be shown if any of the following conditions are met:
format = "via [鈿欙笍 $version](red bold)"
```

## SHLVL

The `shlvl` module shows the current SHLVL ("shell level") environment variable, if it is
set to a number and meets or exceeds the specified threshold.

### Options

| Variable | Default | Description |
| ----------- | ---------------------------- | ------------------------------------------------ |
| `threshold` | `2` | Display threshold. |
| `format` | `"[$symbol$shlvl]($style) "` | The format for the module. |
| `symbol` | `"鈫曪笍 "` | The symbol used to represent the SHLVL. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `true` | Disables the `shlvl` module. |

### Variables

| Variable | Example | Description |
| -------- | --------- | ------------------------------------ |
| shlvl | `3` | The current value of SHLVL |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

\*: This variable can only be used as a part of a style string

### Example

```toml
# ~/.config/starship.toml

[shlvl]
disabled = false
format = "$shlvl level(s) down"
threshold = 3
```

## Singularity

The `singularity` module shows the current singularity image, if inside a container
Expand Down
1 change: 1 addition & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod purescript;
pub mod python;
pub mod ruby;
pub mod rust;
pub mod shlvl;
pub mod singularity;
mod starship_root;
pub mod swift;
Expand Down
24 changes: 24 additions & 0 deletions src/configs/shlvl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::config::{ModuleConfig, RootModuleConfig};

use starship_module_config_derive::ModuleConfig;

#[derive(Clone, ModuleConfig)]
pub struct ShLvlConfig<'a> {
pub threshold: i64,
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
}

impl<'a> RootModuleConfig<'a> for ShLvlConfig<'a> {
fn new() -> Self {
ShLvlConfig {
threshold: 2,
format: "[$symbol$shlvl]($style) ",
symbol: "鈫曪笍 ", // extra space for emoji
style: "bold yellow",
disabled: true,
}
}
}
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct StarshipRootConfig<'a> {
pub const PROMPT_ORDER: &[&str] = &[
"username",
"hostname",
"shlvl",
"singularity",
"kubernetes",
"directory",
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub const ALL_MODULES: &[&str] = &[
"php",
"swift",
"terraform",
"shlvl",
"singularity",
"time",
"username",
Expand Down
3 changes: 3 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod purescript;
mod python;
mod ruby;
mod rust;
mod shlvl;
mod singularity;
mod swift;
mod terraform;
Expand Down Expand Up @@ -100,6 +101,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),
"shlvl" => shlvl::module(context),
"singularity" => singularity::module(context),
"swift" => swift::module(context),
"terraform" => terraform::module(context),
Expand Down Expand Up @@ -158,6 +160,7 @@ pub fn description(module: &str) -> &'static str {
"ruby" => "The currently installed version of Ruby",
"rust" => "The currently installed version of Rust",
"swift" => "The currently installed version of Swift",
"shlvl" => "The current value of SHLVL",
"terraform" => "The currently selected terraform workspace and version",
"time" => "The current local time",
"username" => "The active user's username",
Expand Down
53 changes: 53 additions & 0 deletions src/modules/shlvl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::env;

use super::{Context, Module};

use crate::config::RootModuleConfig;
use crate::configs::shlvl::ShLvlConfig;
use crate::formatter::StringFormatter;

const SHLVL_ENV_VAR: &str = "SHLVL";

pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let shlvl = get_shlvl_value()?;

let mut module = context.new_module("shlvl");
let config: ShLvlConfig = ShLvlConfig::try_load(module.config);

if config.disabled || shlvl < config.threshold {
return None;
}

let shlvl_str = &shlvl.to_string();

let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"shlvl" => Some(Ok(shlvl_str)),
_ => None,
})
.parse(None)
});

module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `shlvl`:\n{}", error);
return None;
}
});

Some(module)
}

fn get_shlvl_value() -> Option<i64> {
env::var(SHLVL_ENV_VAR).ok()?.parse::<i64>().ok()
}
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod jobs;
mod modules;
mod nix_shell;
mod python;
mod shlvl;
mod singularity;
mod terraform;
mod time;
Expand Down
159 changes: 159 additions & 0 deletions tests/testsuite/shlvl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use ansi_term::{Color, Style};
use std::io;

use crate::common;
use crate::common::TestCommand;

const SHLVL_ENV_VAR: &str = "SHLVL";

fn style() -> Style {
// default style
Color::Yellow.bold()
}

#[test]
fn empty_config() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
})
.env(SHLVL_ENV_VAR, "2")
.output()?;
let expected = "";
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn enabled() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
disabled = false
})
.env(SHLVL_ENV_VAR, "2")
.output()?;
let expected = format!("{} ", style().paint("鈫曪笍 2"));
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn no_level() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
disabled = false
})
.output()?;
let expected = "";
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn enabled_config_level_1() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
disabled = false
})
.env(SHLVL_ENV_VAR, "1")
.output()?;
let expected = "";
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn lower_threshold() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
threshold = 1
disabled = false
})
.env(SHLVL_ENV_VAR, "1")
.output()?;
let expected = format!("{} ", style().paint("鈫曪笍 1"));
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn higher_threshold() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
threshold = 3
disabled = false
})
.env(SHLVL_ENV_VAR, "1")
.output()?;
let expected = "";
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn custom_style() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
style = "Red Underline"
disabled = false
})
.env(SHLVL_ENV_VAR, "2")
.output()?;
let expected = format!("{} ", Color::Red.underline().paint("鈫曪笍 2"));
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn custom_symbol() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
symbol = "shlvl is "
disabled = false
})
.env(SHLVL_ENV_VAR, "2")
.output()?;
let expected = format!("{} ", style().paint("shlvl is 2"));
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

#[test]
fn formatting() -> io::Result<()> {
let output = common::render_module("shlvl")
.env_clear()
.use_config(toml::toml! {
[shlvl]
format = "$symbol going down [$shlvl]($style) GOING UP "
disabled = false
})
.env(SHLVL_ENV_VAR, "2")
.output()?;
let expected = format!("鈫曪笍 going down {} GOING UP ", style().paint("2"));
let actual = String::from_utf8(output.stdout).unwrap();
assert_eq!(expected, actual);
Ok(())
}

0 comments on commit 0be9ffc

Please sign in to comment.