mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-03 11:52:26 +01:00
Implement syntax mapping
This adds a `-m`/`--map-syntax` option that allows users to (re)map certain file extensions or file names to an existing syntax. For example: ``` bat --map-syntax .config:json ``` The option can be use multiple times. Note that you can easily make these mappings permanent by using `bat`s new configuration file. closes #169
This commit is contained in:
35
src/syntax_mapping.rs
Normal file
35
src/syntax_mapping.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SyntaxMapping(HashMap<String, String>);
|
||||
|
||||
impl SyntaxMapping {
|
||||
pub fn new() -> SyntaxMapping {
|
||||
SyntaxMapping(HashMap::new())
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, from: String, to: String) -> Option<String> {
|
||||
self.0.insert(from, to)
|
||||
}
|
||||
|
||||
pub fn replace<'a>(&self, input: &'a str) -> Cow<'a, str> {
|
||||
let mut out = Cow::from(input);
|
||||
if let Some(value) = self.0.get(input) {
|
||||
out = Cow::from(value.clone())
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let mut map = SyntaxMapping::new();
|
||||
map.insert("Cargo.lock".into(), "toml".into());
|
||||
map.insert(".ignore".into(), ".gitignore".into());
|
||||
|
||||
assert_eq!("toml", map.replace("Cargo.lock"));
|
||||
assert_eq!("other.lock", map.replace("other.lock"));
|
||||
|
||||
assert_eq!(".gitignore", map.replace(".ignore"));
|
||||
}
|
Reference in New Issue
Block a user