mirror of
https://github.com/starship/starship.git
synced 2025-06-30 23:00:52 +02:00
fix(format_string): Allow multiple variable mappers (#1142)
This commit is contained in:
@ -62,7 +62,10 @@ impl<'a> StringFormatter<'a> {
|
|||||||
|
|
||||||
/// Maps variable name to its value
|
/// Maps variable name to its value
|
||||||
pub fn map<T: Into<String>>(mut self, mapper: impl Fn(&str) -> Option<T> + Sync) -> Self {
|
pub fn map<T: Into<String>>(mut self, mapper: impl Fn(&str) -> Option<T> + Sync) -> Self {
|
||||||
self.variables.par_iter_mut().for_each(|(key, value)| {
|
self.variables
|
||||||
|
.par_iter_mut()
|
||||||
|
.filter(|(_, value)| value.is_none())
|
||||||
|
.for_each(|(key, value)| {
|
||||||
*value = mapper(key).map(|var| var.into()).map(VariableValue::Plain);
|
*value = mapper(key).map(|var| var.into()).map(VariableValue::Plain);
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
@ -73,18 +76,22 @@ impl<'a> StringFormatter<'a> {
|
|||||||
mut self,
|
mut self,
|
||||||
mapper: impl Fn(&str) -> Option<Vec<Segment>> + Sync,
|
mapper: impl Fn(&str) -> Option<Vec<Segment>> + Sync,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.variables.par_iter_mut().for_each(|(key, value)| {
|
self.variables
|
||||||
|
.par_iter_mut()
|
||||||
|
.filter(|(_, value)| value.is_none())
|
||||||
|
.for_each(|(key, value)| {
|
||||||
*value = mapper(key).map(VariableValue::Styled);
|
*value = mapper(key).map(VariableValue::Styled);
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps variable name in a style string to its value
|
/// Maps variable name in a style string to its value
|
||||||
pub fn map_style(mut self, mapper: impl Fn(&str) -> Option<String> + Sync) -> Self {
|
pub fn map_style<T: Into<String>>(mut self, mapper: impl Fn(&str) -> Option<T> + Sync) -> Self {
|
||||||
self.style_variables
|
self.style_variables
|
||||||
.par_iter_mut()
|
.par_iter_mut()
|
||||||
|
.filter(|(_, value)| value.is_none())
|
||||||
.for_each(|(key, value)| {
|
.for_each(|(key, value)| {
|
||||||
*value = mapper(key);
|
*value = mapper(key).map(|var| var.into());
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -361,6 +368,29 @@ mod tests {
|
|||||||
match_next!(result_iter, "styled_no_modifier", styled_no_modifier_style);
|
match_next!(result_iter, "styled_no_modifier", styled_no_modifier_style);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_multiple_mapper() {
|
||||||
|
const FORMAT_STR: &str = "$a$b$c";
|
||||||
|
|
||||||
|
let formatter = StringFormatter::new(FORMAT_STR)
|
||||||
|
.unwrap()
|
||||||
|
.map(|var| match var {
|
||||||
|
"a" => Some("$a"),
|
||||||
|
"b" => Some("$b"),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.map(|var| match var {
|
||||||
|
"b" => Some("$B"),
|
||||||
|
"c" => Some("$c"),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
let result = formatter.parse(None);
|
||||||
|
let mut result_iter = result.iter();
|
||||||
|
match_next!(result_iter, "$a", None);
|
||||||
|
match_next!(result_iter, "$b", None);
|
||||||
|
match_next!(result_iter, "$c", None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_positional() {
|
fn test_positional() {
|
||||||
const FORMAT_STR: &str = "($some) should render but ($none) shouldn't";
|
const FORMAT_STR: &str = "($some) should render but ($none) shouldn't";
|
||||||
|
Reference in New Issue
Block a user