summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfilip <filipbachul@gmail.com>2021-06-30 01:33:43 +0200
committerGitHub <noreply@github.com>2021-06-29 19:33:43 -0400
commit72e5a544fc92eb5313a1fa895bd5b9042a8cc601 (patch)
treec36aa3a7a50d6f9f08f47541b75efe3ce518d244
parentc811e0e5d5f292fad5d20769c6e5d9cc6d5c8a1f (diff)
feat: treat empty string as none when formating (#2738)
* treat empty string as none when formating * update docs * format & clippy
-rw-r--r--docs/config/README.md2
-rw-r--r--src/formatter/string_formatter.rs37
2 files changed, 36 insertions, 3 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 67a685018..7555ba138 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -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\] )`.
diff --git a/src/formatter/string_formatter.rs b/src/formatter/string_formatter.rs
index 01d6bd9ac..f8be0128e 100644
--- a/src/formatter/string_formatter.rs
+++ b/src/formatter/string_formatter.rs
@@ -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
@@ -579,6 +584,34 @@ mod tests {
}
#[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))";