From aa3ec109b74eb6e23d1fe15ade3087ca8430eeb2 Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Mon, 10 Jun 2024 17:39:55 -0700 Subject: [PATCH] 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. --- src/bin/bat/app.rs | 2 +- src/style.rs | 54 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index c95c2bd1..d6628668 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -405,7 +405,7 @@ impl App { .map(|v| StyleComponentList::from_str(v)) .collect::>>()?; - StyleComponentList::to_components(lists, self.interactive_output) + StyleComponentList::to_components(lists, self.interactive_output, true) } // Use the default. diff --git a/src/style.rs b/src/style.rs index 8c2cc05d..b8d8b09f 100644 --- a/src/style.rs +++ b/src/style.rs @@ -189,22 +189,27 @@ impl StyleComponentList { /// [numbers,grid] + [header,changes] -> [header,changes] /// [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( lists: impl IntoIterator, interactive_terminal: bool, + with_default: bool, ) -> StyleComponents { - StyleComponents( - lists - .into_iter() - .fold(HashSet::new(), |mut components, list| { - if list.contains_override() { - components.clear(); - } + let mut components: HashSet = HashSet::new(); + if with_default { + components.extend(StyleComponent::Auto.components(interactive_terminal)) + } - list.expand_into(&mut components, interactive_terminal); - components - }), - ) + StyleComponents(lists.into_iter().fold(components, |mut components, list| { + if list.contains_override() { + components.clear(); + } + + list.expand_into(&mut components, interactive_terminal); + components + })) } } @@ -233,6 +238,7 @@ mod test { use std::str::FromStr; use super::ComponentAction::*; + use super::StyleComponent; use super::StyleComponent::*; use super::StyleComponentList; @@ -261,6 +267,7 @@ mod test { assert_eq!( StyleComponentList::to_components( vec![StyleComponentList::from_str("grid,numbers").expect("no error")], + false, false ) .0, @@ -273,6 +280,7 @@ mod test { assert_eq!( StyleComponentList::to_components( vec![StyleComponentList::from_str("grid,numbers,-grid").expect("no error")], + false, false ) .0, @@ -285,6 +293,7 @@ mod test { assert_eq!( StyleComponentList::to_components( vec![StyleComponentList::from_str("full").expect("no error")], + false, false ) .0, @@ -296,7 +305,8 @@ mod test { pub fn style_component_list_expand_negates_subcomponents() { assert!(!StyleComponentList::to_components( vec![StyleComponentList::from_str("full,-numbers").expect("no error")], - true + true, + false ) .numbers()); } @@ -309,6 +319,7 @@ mod test { StyleComponentList::from_str("grid").expect("no error"), StyleComponentList::from_str("numbers").expect("no error"), ], + false, false ) .0, @@ -325,10 +336,29 @@ mod test { StyleComponentList::from_str("-grid").expect("no error"), StyleComponentList::from_str("+numbers").expect("no error"), ], + false, false ) .0, 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 = HashSet::new(); + expected.extend(Auto.components(true)); + expected.remove(&LineNumbers); + expected + } + ); + } }