From 1296632ff4f2a7b85f18f2043039b5cab2201136 Mon Sep 17 00:00:00 2001 From: Daniel Beckwith Date: Mon, 14 Dec 2020 15:40:21 -0500 Subject: [PATCH] feat(shlvl): Add `repeat` option (#1995) --- docs/config/README.md | 1 + src/configs/shlvl.rs | 2 ++ src/modules/shlvl.rs | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/config/README.md b/docs/config/README.md index 97b569e0..3cc7ea25 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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. | diff --git a/src/configs/shlvl.rs b/src/configs/shlvl.rs index 82dc6cda..e5648b5e 100644 --- a/src/configs/shlvl.rs +++ b/src/configs/shlvl.rs @@ -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, } diff --git a/src/modules/shlvl.rs b/src/modules/shlvl.rs index b21f9121..4a33d4f7 100644 --- a/src/modules/shlvl.rs +++ b/src/modules/shlvl.rs @@ -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> { 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(()) + } }