1
0
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:
Matt Van Horn
2026-05-16 13:51:00 -07:00
parent 4a38eab3ea
commit 530e83a455
2 changed files with 141 additions and 2 deletions
+2 -2
View File
@@ -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 }))