Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Don't highlight unknown languages
cloudhead committed 2 years ago
commit de0b9ae44df7695f74c4012203f1d01d7f258b3a
parent eb1ae13e68d8675e30d85fcb86666c78018e4bac
3 files changed +12 -16
modified 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))
modified 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<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.
modified 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<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)
}