feat(shlvl): Add repeat option (#1995)

This commit is contained in:
Daniel Beckwith 2020-12-14 15:40:21 -05:00 committed by GitHub
parent 55b662eaf3
commit 1296632ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View File

@ -2154,6 +2154,7 @@ set to a number and meets or exceeds the specified threshold.
| `threshold` | `2` | Display threshold. |
| `format` | `"[$symbol$shlvl]($style) "` | The format for the module. |
| `symbol` | `"↕️ "` | The symbol used to represent the SHLVL. |
| `repeat` | `false` | Causes `symbol` to be repeated by the current SHLVL amount. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `true` | Disables the `shlvl` module. |

View File

@ -7,6 +7,7 @@ pub struct ShLvlConfig<'a> {
pub threshold: i64,
pub format: &'a str,
pub symbol: &'a str,
pub repeat: bool,
pub style: &'a str,
pub disabled: bool,
}
@ -17,6 +18,7 @@ impl<'a> RootModuleConfig<'a> for ShLvlConfig<'a> {
threshold: 2,
format: "[$symbol$shlvl]($style) ",
symbol: "↕️ ", // extra space for emoji
repeat: false,
style: "bold yellow",
disabled: true,
}

View File

@ -3,6 +3,8 @@ use super::{Context, Module};
use crate::config::RootModuleConfig;
use crate::configs::shlvl::ShLvlConfig;
use crate::formatter::StringFormatter;
use std::borrow::Cow;
use std::convert::TryInto;
const SHLVL_ENV_VAR: &str = "SHLVL";
@ -20,10 +22,21 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let shlvl_str = &shlvl.to_string();
let repeat_count = if config.repeat {
shlvl.try_into().unwrap_or(1)
} else {
1
};
let symbol = if repeat_count != 1 {
Cow::Owned(config.symbol.repeat(repeat_count))
} else {
Cow::Borrowed(config.symbol)
};
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|var, _| match var {
"symbol" => Some(config.symbol),
"symbol" => Some(symbol.as_ref()),
_ => None,
})
.map_style(|variable| match variable {
@ -199,4 +212,22 @@ mod tests {
assert_eq!(expected, actual);
Ok(())
}
#[test]
fn repeat() -> io::Result<()> {
let actual = ModuleRenderer::new("shlvl")
.config(toml::toml! {
[shlvl]
format = "[$symbol>]($style) "
symbol = "~"
repeat = true
disabled = false
})
.env(SHLVL_ENV_VAR, "3")
.collect();
let expected = Some(format!("{} ", style().paint("~~~>")));
assert_eq!(expected, actual);
Ok(())
}
}