First StyleComponentList should remove from 'auto' style.

This happens when there are no `--style` arguments other than the one
passed in as a command line argument.

Prior to this change, removing a style component (e.g. `--style=-numbers`)
would remove the component from an empty style component set, resulting
in no styles at all. That behaviour was less intuitive than the new
behaviour, which starts out with the default components and
removes the line numbers.
This commit is contained in:
Ethan P. 2024-06-10 17:39:55 -07:00
parent 180a77ee99
commit aa3ec109b7
No known key found for this signature in database
GPG Key ID: B29B90B1B228FEBC
2 changed files with 43 additions and 13 deletions

View File

@ -405,7 +405,7 @@ impl App {
.map(|v| StyleComponentList::from_str(v)) .map(|v| StyleComponentList::from_str(v))
.collect::<Result<Vec<StyleComponentList>>>()?; .collect::<Result<Vec<StyleComponentList>>>()?;
StyleComponentList::to_components(lists, self.interactive_output) StyleComponentList::to_components(lists, self.interactive_output, true)
} }
// Use the default. // Use the default.

View File

@ -189,22 +189,27 @@ impl StyleComponentList {
/// [numbers,grid] + [header,changes] -> [header,changes] /// [numbers,grid] + [header,changes] -> [header,changes]
/// [numbers,grid] + [+header,-grid] -> [numbers,header] /// [numbers,grid] + [+header,-grid] -> [numbers,header]
/// ``` /// ```
///
/// ## Parameters
/// - `with_default`: If true, the styles lists will build upon the StyleComponent::Auto style.
pub fn to_components( pub fn to_components(
lists: impl IntoIterator<Item = StyleComponentList>, lists: impl IntoIterator<Item = StyleComponentList>,
interactive_terminal: bool, interactive_terminal: bool,
with_default: bool,
) -> StyleComponents { ) -> StyleComponents {
StyleComponents( let mut components: HashSet<StyleComponent> = HashSet::new();
lists if with_default {
.into_iter() components.extend(StyleComponent::Auto.components(interactive_terminal))
.fold(HashSet::new(), |mut components, list| { }
StyleComponents(lists.into_iter().fold(components, |mut components, list| {
if list.contains_override() { if list.contains_override() {
components.clear(); components.clear();
} }
list.expand_into(&mut components, interactive_terminal); list.expand_into(&mut components, interactive_terminal);
components components
}), }))
)
} }
} }
@ -233,6 +238,7 @@ mod test {
use std::str::FromStr; use std::str::FromStr;
use super::ComponentAction::*; use super::ComponentAction::*;
use super::StyleComponent;
use super::StyleComponent::*; use super::StyleComponent::*;
use super::StyleComponentList; use super::StyleComponentList;
@ -261,6 +267,7 @@ mod test {
assert_eq!( assert_eq!(
StyleComponentList::to_components( StyleComponentList::to_components(
vec![StyleComponentList::from_str("grid,numbers").expect("no error")], vec![StyleComponentList::from_str("grid,numbers").expect("no error")],
false,
false false
) )
.0, .0,
@ -273,6 +280,7 @@ mod test {
assert_eq!( assert_eq!(
StyleComponentList::to_components( StyleComponentList::to_components(
vec![StyleComponentList::from_str("grid,numbers,-grid").expect("no error")], vec![StyleComponentList::from_str("grid,numbers,-grid").expect("no error")],
false,
false false
) )
.0, .0,
@ -285,6 +293,7 @@ mod test {
assert_eq!( assert_eq!(
StyleComponentList::to_components( StyleComponentList::to_components(
vec![StyleComponentList::from_str("full").expect("no error")], vec![StyleComponentList::from_str("full").expect("no error")],
false,
false false
) )
.0, .0,
@ -296,7 +305,8 @@ mod test {
pub fn style_component_list_expand_negates_subcomponents() { pub fn style_component_list_expand_negates_subcomponents() {
assert!(!StyleComponentList::to_components( assert!(!StyleComponentList::to_components(
vec![StyleComponentList::from_str("full,-numbers").expect("no error")], vec![StyleComponentList::from_str("full,-numbers").expect("no error")],
true true,
false
) )
.numbers()); .numbers());
} }
@ -309,6 +319,7 @@ mod test {
StyleComponentList::from_str("grid").expect("no error"), StyleComponentList::from_str("grid").expect("no error"),
StyleComponentList::from_str("numbers").expect("no error"), StyleComponentList::from_str("numbers").expect("no error"),
], ],
false,
false false
) )
.0, .0,
@ -325,10 +336,29 @@ mod test {
StyleComponentList::from_str("-grid").expect("no error"), StyleComponentList::from_str("-grid").expect("no error"),
StyleComponentList::from_str("+numbers").expect("no error"), StyleComponentList::from_str("+numbers").expect("no error"),
], ],
false,
false false
) )
.0, .0,
HashSet::from([HeaderFilename, LineNumbers]) HashSet::from([HeaderFilename, LineNumbers])
); );
} }
#[test]
pub fn style_component_list_default_builds_on_auto() {
assert_eq!(
StyleComponentList::to_components(
vec![StyleComponentList::from_str("-numbers").expect("no error"),],
true,
true
)
.0,
{
let mut expected: HashSet<StyleComponent> = HashSet::new();
expected.extend(Auto.components(true));
expected.remove(&LineNumbers);
expected
}
);
}
} }