From de0b9ae44df7695f74c4012203f1d01d7f258b3a Mon Sep 17 00:00:00 2001 From: cloudhead Date: Tue, 12 Mar 2024 16:12:16 +0100 Subject: [PATCH] cli: Don't highlight unknown languages Previously, we'd show "Empty file". --- radicle-cli/src/git/pretty_diff.rs | 6 +++--- radicle-cli/src/terminal/highlight.rs | 15 +++++++-------- radicle-cli/src/terminal/json.rs | 7 ++----- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/radicle-cli/src/git/pretty_diff.rs b/radicle-cli/src/git/pretty_diff.rs index aec270f1..5098d457 100644 --- a/radicle-cli/src/git/pretty_diff.rs +++ b/radicle-cli/src/git/pretty_diff.rs @@ -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)) diff --git a/radicle-cli/src/terminal/highlight.rs b/radicle-cli/src/terminal/highlight.rs index c6954178..53cd6a9a 100644 --- a/radicle-cli/src/terminal/highlight.rs +++ b/radicle-cli/src/terminal/highlight.rs @@ -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>, ts::Error> { + /// Highlight a source code file. + pub fn highlight(&mut self, path: &Path, code: &[u8]) -> Result, 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. diff --git a/radicle-cli/src/terminal/json.rs b/radicle-cli/src/terminal/json.rs index 722bc2cd..bd8bd4cf 100644 --- a/radicle-cli/src/terminal/json.rs +++ b/radicle-cli/src/terminal/json.rs @@ -6,10 +6,7 @@ use crate::terminal as term; pub fn to_pretty(value: &impl serde::Serialize, path: &Path) -> anyhow::Result> { 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) }