Reverse precedence of syntax mappings specified on CLI

This commit is contained in:
cyqsimon 2024-01-18 22:41:57 +08:00
parent 15ab4478c9
commit e4d637a3d8
No known key found for this signature in database
GPG Key ID: 1D8CE2F297390D65
3 changed files with 5 additions and 3 deletions

View File

@ -16,8 +16,6 @@
- Update Arch Linux package URL in README files #2779 (@brunobell) - Update Arch Linux package URL in README files #2779 (@brunobell)
- Update and improve `zsh` completion, see #2772 (@okapia) - Update and improve `zsh` completion, see #2772 (@okapia)
- More extensible syntax mapping mechanism #2755 (@cyqsimon) - More extensible syntax mapping mechanism #2755 (@cyqsimon)
- [BREAKING] Precedence order of user-defined syntax mappings (using `-m/--map-syntax`) has been reversed
- `-m '*.foo:alpha' -m '*.foo:bravo'` now maps `bar.foo` to `alpha`
## Syntaxes ## Syntaxes

View File

@ -130,7 +130,9 @@ impl App {
} }
if let Some(values) = self.matches.get_many::<String>("map-syntax") { if let Some(values) = self.matches.get_many::<String>("map-syntax") {
for from_to in values { // later args take precedence over earlier ones, hence `.rev()`
// see: https://github.com/sharkdp/bat/pull/2755#discussion_r1456416875
for from_to in values.rev() {
let parts: Vec<_> = from_to.split(':').collect(); let parts: Vec<_> = from_to.split(':').collect();
if parts.len() != 2 { if parts.len() != 2 {

View File

@ -41,6 +41,8 @@ pub enum MappingTarget<'a> {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct SyntaxMapping<'a> { pub struct SyntaxMapping<'a> {
/// User-defined mappings at run time. /// User-defined mappings at run time.
///
/// Rules in front have precedence.
custom_mappings: Vec<(GlobMatcher, MappingTarget<'a>)>, custom_mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
pub(crate) ignored_suffixes: IgnoredSuffixes<'a>, pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
} }