cli: Don't highlight unknown languages

Previously, we'd show "Empty file".
This commit is contained in:
cloudhead 2024-03-12 16:12:16 +01:00
parent eb1ae13e68
commit de0b9ae44d
No known key found for this signature in database
3 changed files with 12 additions and 16 deletions

View File

@ -13,7 +13,7 @@ use crate::terminal::highlight::{Highlighter, Theme};
use super::unified_diff::{Decode, HunkHeader};
/// Blob returned by the [`Repo`] trait.
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Debug)]
pub enum Blob {
Binary,
Empty,
@ -222,10 +222,10 @@ impl ToPretty for DiffContent {
let mut blobs = Blobs::default();
if let Some(Blob::Plain(content)) = old {
blobs.old = hi.highlight(context.path(), &content).ok().flatten();
blobs.old = hi.highlight(context.path(), &content).ok();
}
if let Some(Blob::Plain(content)) = new {
blobs.new = hi.highlight(context.path(), &content).ok().flatten();
blobs.new = hi.highlight(context.path(), &content).ok();
}
let mut vstack = term::VStack::default()
.border(Some(term::colors::FAINT))

View File

@ -175,16 +175,15 @@ impl Builder {
}
impl Highlighter {
/// Highlight a source code file. Returns `None` if the file type was not recognized.
pub fn highlight(
&mut self,
path: &Path,
code: &[u8],
) -> Result<Option<Vec<term::Line>>, ts::Error> {
/// Highlight a source code file.
pub fn highlight(&mut self, path: &Path, code: &[u8]) -> Result<Vec<term::Line>, ts::Error> {
let theme = Theme::default();
let mut highlighter = ts::Highlighter::new();
let Some(config) = self.detect(path, code) else {
return Ok(None);
let Ok(code) = std::str::from_utf8(code) else {
return Err(ts::Error::Unknown);
};
return Ok(code.lines().map(term::Line::new).collect());
};
config.configure(HIGHLIGHTS);
@ -193,7 +192,7 @@ impl Highlighter {
None
})?;
Builder::default().run(highlights, code, &theme).map(Some)
Builder::default().run(highlights, code, &theme)
}
/// Detect language.

View File

@ -6,10 +6,7 @@ use crate::terminal as term;
pub fn to_pretty(value: &impl serde::Serialize, path: &Path) -> anyhow::Result<Vec<term::Line>> {
let json = serde_json::to_string_pretty(&value)?;
let mut highlighter = term::highlight::Highlighter::default();
let highlighted = highlighter.highlight(path, json.as_bytes())?;
if let Some(highlighted) = highlighter.highlight(path, json.as_bytes())? {
Ok(highlighted)
} else {
Ok(json.split('\n').map(term::Line::new).collect())
}
Ok(highlighted)
}