-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shlvl): Add shlvl module (#1385)
* 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
1 parent
c07b798
commit 0be9ffc
Showing
9 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |