mirror of
https://github.com/starship/starship.git
synced 2025-06-30 14:50:55 +02:00
fix(format_string): Allow multiple variable mappers (#1142)
This commit is contained in:
@ -62,9 +62,12 @@ impl<'a> StringFormatter<'a> {
|
||||
|
||||
/// Maps variable name to its value
|
||||
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)| {
|
||||
*value = mapper(key).map(|var| var.into()).map(VariableValue::Plain);
|
||||
});
|
||||
self.variables
|
||||
.par_iter_mut()
|
||||
.filter(|(_, value)| value.is_none())
|
||||
.for_each(|(key, value)| {
|
||||
*value = mapper(key).map(|var| var.into()).map(VariableValue::Plain);
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
@ -73,18 +76,22 @@ impl<'a> StringFormatter<'a> {
|
||||
mut self,
|
||||
mapper: impl Fn(&str) -> Option<Vec<Segment>> + Sync,
|
||||
) -> Self {
|
||||
self.variables.par_iter_mut().for_each(|(key, value)| {
|
||||
*value = mapper(key).map(VariableValue::Styled);
|
||||
});
|
||||
self.variables
|
||||
.par_iter_mut()
|
||||
.filter(|(_, value)| value.is_none())
|
||||
.for_each(|(key, value)| {
|
||||
*value = mapper(key).map(VariableValue::Styled);
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
/// 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
|
||||
.par_iter_mut()
|
||||
.filter(|(_, value)| value.is_none())
|
||||
.for_each(|(key, value)| {
|
||||
*value = mapper(key);
|
||||
*value = mapper(key).map(|var| var.into());
|
||||
});
|
||||
self
|
||||
}
|
||||
@ -361,6 +368,29 @@ mod tests {
|
||||
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]
|
||||
fn test_positional() {
|
||||
const FORMAT_STR: &str = "($some) should render but ($none) shouldn't";
|
||||
|
Reference in New Issue
Block a user