mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-25 01:03:46 +01:00
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:
parent
180a77ee99
commit
aa3ec109b7
@ -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.
|
||||
|
54
src/style.rs
54
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<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();
|
||||
}
|
||||
let mut components: HashSet<StyleComponent> = 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<StyleComponent> = HashSet::new();
|
||||
expected.extend(Auto.components(true));
|
||||
expected.remove(&LineNumbers);
|
||||
expected
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user