mirror of
https://github.com/sharkdp/bat
synced 2026-08-10 20:01:45 +00:00
feat: bat auto-detects language from stdin via first-line patterns
When piped input has no filename hint, bat falls back to plain text. Many
common formats can be reliably identified by shebang or magic-string in the
first line (#!/bin/bash, <?xml, {"json: ...}, etc.). Adds a first-line
pattern matcher to assets.rs.
Fixes #3697
This commit is contained in:
+2
-2
@@ -357,11 +357,11 @@ impl HighlightingAssets {
|
||||
reader: &mut InputReader,
|
||||
) -> Result<Option<SyntaxReferenceInSet<'_>>> {
|
||||
let syntax_set = self.get_syntax_set()?;
|
||||
Ok(String::from_utf8(reader.first_line.clone())
|
||||
Ok(std::str::from_utf8(&reader.first_line)
|
||||
.ok()
|
||||
.and_then(|l| {
|
||||
// Strip UTF-8 BOM if present
|
||||
let line = l.strip_prefix('\u{feff}').unwrap_or(&l);
|
||||
let line = l.strip_prefix('\u{feff}').unwrap_or(l);
|
||||
syntax_set.find_syntax_by_first_line(line)
|
||||
})
|
||||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
|
||||
|
||||
Reference in New Issue
Block a user