From 3402f0e82aab907a77f0967abb99d4cde512c069 Mon Sep 17 00:00:00 2001 From: Vivek Kushwaha Date: Fri, 14 Jul 2023 14:29:02 +0530 Subject: [PATCH] feat(shlvl): add repeat_offset for repeated symbol (#5289) Signed-off-by: Vivek Kushwaha --- .github/config-schema.json | 7 +++++++ docs/config/README.md | 33 +++++++++++++++++++++++-------- src/configs/shlvl.rs | 2 ++ src/modules/shlvl.rs | 40 +++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 20e4fa3b..98d6348a 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -1499,6 +1499,7 @@ "disabled": true, "format": "[$symbol$shlvl]($style) ", "repeat": false, + "repeat_offset": 0, "style": "bold yellow", "symbol": "↕️ ", "threshold": 2 @@ -5243,6 +5244,12 @@ "default": false, "type": "boolean" }, + "repeat_offset": { + "default": 0, + "type": "integer", + "format": "uint64", + "minimum": 0.0 + }, "style": { "default": "bold yellow", "type": "string" diff --git a/docs/config/README.md b/docs/config/README.md index 5050631c..47dbfdff 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -3772,14 +3772,15 @@ set to a number and meets or exceeds the specified threshold. ### Options -| Option | Default | Description | -| ----------- | ---------------------------- | ------------------------------------------------------------- | -| `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. | +| Option | Default | Description | +| --------------- | ---------------------------- | ------------------------------------------------------------------- | +| `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. | +| `repeat_offset` | `0` | Decrements number of times `symbol` is repeated by the offset value | +| `style` | `'bold yellow'` | The style for the module. | +| `disabled` | `true` | Disables the `shlvl` module. | ### Variables @@ -3802,6 +3803,22 @@ format = '$shlvl level(s) down' threshold = 3 ``` +Using `repeat` and `repeat_offset` along with `character` module, one can get +prompt like `❯❯❯` where last character is colored appropriately for return +status code and preceeding characters are provided by `shlvl`. + +```toml +# ~/.config/starship.toml + +[shlvl] +disabled = false +format = '[$symbol$shlvl]($style)' +repeat = true +symbol = '❯' +repeat_offset = 1 +threshold = 0 +``` + ## Singularity The `singularity` module shows the current [Singularity](https://sylabs.io/singularity/) image, if inside a container diff --git a/src/configs/shlvl.rs b/src/configs/shlvl.rs index ad05a213..05209356 100644 --- a/src/configs/shlvl.rs +++ b/src/configs/shlvl.rs @@ -12,6 +12,7 @@ pub struct ShLvlConfig<'a> { pub format: &'a str, pub symbol: &'a str, pub repeat: bool, + pub repeat_offset: u64, pub style: &'a str, pub disabled: bool, } @@ -23,6 +24,7 @@ impl<'a> Default for ShLvlConfig<'a> { format: "[$symbol$shlvl]($style) ", symbol: "↕️ ", // extra space for emoji repeat: false, + repeat_offset: 0, style: "bold yellow", disabled: true, } diff --git a/src/modules/shlvl.rs b/src/modules/shlvl.rs index 1e1e3cae..48d22ec8 100644 --- a/src/modules/shlvl.rs +++ b/src/modules/shlvl.rs @@ -22,11 +22,20 @@ pub fn module<'a>(context: &'a Context) -> Option> { let shlvl_str = &shlvl.to_string(); - let repeat_count = if config.repeat { + let mut repeat_count: usize = if config.repeat { shlvl.try_into().unwrap_or(1) } else { 1 }; + + if config.repeat_offset > 0 { + repeat_count = + repeat_count.saturating_sub(config.repeat_offset.try_into().unwrap_or(usize::MAX)); + if repeat_count == 0 { + return None; + } + } + let symbol = if repeat_count != 1 { Cow::Owned(config.symbol.repeat(repeat_count)) } else { @@ -219,4 +228,33 @@ mod tests { assert_eq!(expected, actual); } + + #[test] + fn repeat_offset() { + fn get_actual(shlvl: usize, repeat_offset: usize, threshold: usize) -> Option { + ModuleRenderer::new("shlvl") + .config(toml::toml! { + [shlvl] + format = "[$symbol]($style)" + symbol = "~" + repeat = true + repeat_offset = repeat_offset + disabled = false + threshold = threshold + }) + .env(SHLVL_ENV_VAR, format!("{}", shlvl)) + .collect() + } + + assert_eq!( + get_actual(2, 0, 0), + Some(format!("{}", style().paint("~~"))) + ); + assert_eq!(get_actual(2, 1, 0), Some(format!("{}", style().paint("~")))); + assert_eq!(get_actual(2, 2, 0), None); // offset same as shlvl; hide + assert_eq!(get_actual(2, 3, 0), None); // offset larger than shlvl; hide + assert_eq!(get_actual(2, 1, 3), None); // high threshold; hide + // threshold not high enough; hide + assert_eq!(get_actual(2, 1, 2), Some(format!("{}", style().paint("~")))); + } }