1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 13:47:38 +00:00

feat: treat empty string as none when formating (#2738)

* treat empty string as none when formating

* update docs

* format & clippy
This commit is contained in:
filip 2021-06-30 01:33:43 +02:00 committed by GitHub
parent c811e0e5d5
commit 72e5a544fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -107,7 +107,7 @@ A conditional format string wrapped in `(` and `)` will not render if all variab
For example:
- `(@$region)` will show nothing if the variable `region` is `None`, otherwise `@` followed by the value of region.
- `(@$region)` will show nothing if the variable `region` is `None` or empty string, otherwise `@` followed by the value of region.
- `(some text)` will always show nothing since there are no variables wrapped in the braces.
- When `$all` is a shortcut for `\[$a$b\] `, `($all)` will show nothing only if `$a` and `$b` are both `None`.
This works the same as `(\[$a$b\] )`.

View File

@ -284,7 +284,7 @@ impl<'a> StringFormatter<'a> {
.unwrap_or_else(|| Ok(Vec::new())),
FormatElement::Conditional(format) => {
// Show the conditional format string if all the variables inside are not
// none.
// none or empty string.
fn should_show_elements<'a>(
format_elements: &[FormatElement],
variables: &'a VariableMapType<'a>,
@ -307,7 +307,12 @@ impl<'a> StringFormatter<'a> {
&meta_variables,
)
}
_ => true,
VariableValue::Plain(plain_value) => {
!plain_value.is_empty()
}
VariableValue::Styled(segments) => {
segments.iter().any(|x| !x.value.is_empty())
}
})
// The variable is None or Err, or a meta variable
// that shouldn't show
@ -578,6 +583,34 @@ mod tests {
match_next!(result_iter, " shouldn't", None);
}
#[test]
fn test_empty() {
const FORMAT_STR: &str = "(@$empty)";
let formatter = StringFormatter::new(FORMAT_STR)
.unwrap()
.map(|var| match var {
"empty" => Some(Ok("")),
_ => None,
});
let result = formatter.parse(None).unwrap();
assert_eq!(result.len(), 0);
}
#[test]
fn test_styled_empty() {
const FORMAT_STR: &str = "[(@$empty)](red bold)";
let formatter = StringFormatter::new(FORMAT_STR)
.unwrap()
.map(|variable| match variable {
"empty" => Some(Ok("")),
_ => None,
});
let result = formatter.parse(None).unwrap();
assert_eq!(result.len(), 0);
}
#[test]
fn test_nested_conditional() {
const FORMAT_STR: &str = "($some ($none)) and ($none ($some))";