1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 01:04:00 +00:00

feat(shlvl): add repeat_offset for repeated symbol (#5289)

Signed-off-by: Vivek Kushwaha <yoursvivek@users.noreply.github.com>
This commit is contained in:
Vivek Kushwaha 2023-07-14 14:29:02 +05:30 committed by GitHub
parent c5edb413dd
commit 3402f0e82a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 9 deletions

View File

@ -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"

View File

@ -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

View File

@ -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,
}

View File

@ -22,11 +22,20 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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<String> {
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("~"))));
}
}