Skip to content

Commit

Permalink
First StyleComponentList should remove from 'auto' style.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eth-p committed Jul 31, 2024
1 parent 180a77e commit aa3ec10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl App {
.map(|v| StyleComponentList::from_str(v))
.collect::<Result<Vec<StyleComponentList>>>()?;

StyleComponentList::to_components(lists, self.interactive_output)
StyleComponentList::to_components(lists, self.interactive_output, true)
}

// Use the default.
Expand Down
56 changes: 43 additions & 13 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = StyleComponentList>,
interactive_terminal: bool,
with_default: bool,
) -> StyleComponents {
StyleComponents(
lists
.into_iter()
.fold(HashSet::new(), |mut components, list| {
if list.contains_override() {
components.clear();
}

list.expand_into(&mut components, interactive_terminal);
components
}),
)
let mut components: HashSet<StyleComponent> = HashSet::new();
if with_default {
components.extend(StyleComponent::Auto.components(interactive_terminal))
}

StyleComponents(lists.into_iter().fold(components, |mut components, list| {
if list.contains_override() {
components.clear();
}

list.expand_into(&mut components, interactive_terminal);
components
}))
}
}

Expand Down Expand Up @@ -233,6 +238,7 @@ mod test {
use std::str::FromStr;

use super::ComponentAction::*;
use super::StyleComponent;
use super::StyleComponent::*;
use super::StyleComponentList;

Expand Down Expand Up @@ -261,6 +267,7 @@ mod test {
assert_eq!(
StyleComponentList::to_components(
vec![StyleComponentList::from_str("grid,numbers").expect("no error")],
false,
false
)
.0,
Expand All @@ -273,6 +280,7 @@ mod test {
assert_eq!(
StyleComponentList::to_components(
vec![StyleComponentList::from_str("grid,numbers,-grid").expect("no error")],
false,
false
)
.0,
Expand All @@ -285,6 +293,7 @@ mod test {
assert_eq!(
StyleComponentList::to_components(
vec![StyleComponentList::from_str("full").expect("no error")],
false,
false
)
.0,
Expand All @@ -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());
}
Expand All @@ -309,6 +319,7 @@ mod test {
StyleComponentList::from_str("grid").expect("no error"),
StyleComponentList::from_str("numbers").expect("no error"),
],
false,
false
)
.0,
Expand All @@ -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<StyleComponent> = HashSet::new();
expected.extend(Auto.components(true));
expected.remove(&LineNumbers);
expected
}
);
}
}

0 comments on commit aa3ec10

Please sign in to comment.