cli: Don't highlight unknown languages
Previously, we'd show "Empty file".
This commit is contained in:
parent
eb1ae13e68
commit
de0b9ae44d
|
|
@ -13,7 +13,7 @@ use crate::terminal::highlight::{Highlighter, Theme};
|
||||||
use super::unified_diff::{Decode, HunkHeader};
|
use super::unified_diff::{Decode, HunkHeader};
|
||||||
|
|
||||||
/// Blob returned by the [`Repo`] trait.
|
/// Blob returned by the [`Repo`] trait.
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
pub enum Blob {
|
pub enum Blob {
|
||||||
Binary,
|
Binary,
|
||||||
Empty,
|
Empty,
|
||||||
|
|
@ -222,10 +222,10 @@ impl ToPretty for DiffContent {
|
||||||
let mut blobs = Blobs::default();
|
let mut blobs = Blobs::default();
|
||||||
|
|
||||||
if let Some(Blob::Plain(content)) = old {
|
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 {
|
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()
|
let mut vstack = term::VStack::default()
|
||||||
.border(Some(term::colors::FAINT))
|
.border(Some(term::colors::FAINT))
|
||||||
|
|
|
||||||
|
|
@ -175,16 +175,15 @@ impl Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Highlighter {
|
impl Highlighter {
|
||||||
/// Highlight a source code file. Returns `None` if the file type was not recognized.
|
/// Highlight a source code file.
|
||||||
pub fn highlight(
|
pub fn highlight(&mut self, path: &Path, code: &[u8]) -> Result<Vec<term::Line>, ts::Error> {
|
||||||
&mut self,
|
|
||||||
path: &Path,
|
|
||||||
code: &[u8],
|
|
||||||
) -> Result<Option<Vec<term::Line>>, ts::Error> {
|
|
||||||
let theme = Theme::default();
|
let theme = Theme::default();
|
||||||
let mut highlighter = ts::Highlighter::new();
|
let mut highlighter = ts::Highlighter::new();
|
||||||
let Some(config) = self.detect(path, code) else {
|
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);
|
config.configure(HIGHLIGHTS);
|
||||||
|
|
||||||
|
|
@ -193,7 +192,7 @@ impl Highlighter {
|
||||||
None
|
None
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Builder::default().run(highlights, code, &theme).map(Some)
|
Builder::default().run(highlights, code, &theme)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Detect language.
|
/// Detect language.
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,7 @@ use crate::terminal as term;
|
||||||
pub fn to_pretty(value: &impl serde::Serialize, path: &Path) -> anyhow::Result<Vec<term::Line>> {
|
pub fn to_pretty(value: &impl serde::Serialize, path: &Path) -> anyhow::Result<Vec<term::Line>> {
|
||||||
let json = serde_json::to_string_pretty(&value)?;
|
let json = serde_json::to_string_pretty(&value)?;
|
||||||
let mut highlighter = term::highlight::Highlighter::default();
|
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)
|
Ok(highlighted)
|
||||||
} else {
|
|
||||||
Ok(json.split('\n').map(term::Line::new).collect())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue