fix: shell input code highlighting is not adapted for the default ANSI theme (#536)

This commit is contained in:
三咲雅 · Misaki Masa 2024-01-19 11:26:59 +08:00 committed by GitHub
parent 453ca94e18
commit 1bfd1c002f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 24 additions and 39 deletions

3
Cargo.lock generated
View File

@ -2674,7 +2674,6 @@ dependencies = [
"ratatui",
"regex",
"serde",
"syntect",
"tokio",
"tokio-stream",
"tokio-util",
@ -2691,7 +2690,6 @@ dependencies = [
name = "yazi-fm"
version = "0.2.1"
dependencies = [
"ansi-to-tui",
"anyhow",
"better-panic",
"crossterm",
@ -2701,6 +2699,7 @@ dependencies = [
"mlua",
"ratatui",
"signal-hook-tokio",
"syntect",
"tokio",
"tracing",
"tracing-appender",

View File

@ -27,7 +27,6 @@ parking_lot = "^0"
ratatui = "^0"
regex = "^1"
serde = "^1"
syntect = { version = "^5", default-features = false, features = [ "parsing", "plist-load", "regex-onig" ] }
tokio = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs", "process", "io-std", "io-util" ] }
tokio-stream = "^0"
tokio-util = "^0"

View File

@ -23,7 +23,7 @@ pub struct Input {
pub(super) completion: bool,
// Shell
pub(super) highlight: bool,
pub highlight: bool,
}
impl Input {

View File

@ -2,7 +2,6 @@ mod commands;
mod input;
mod mode;
mod op;
mod shell;
mod snap;
mod snaps;

View File

@ -1,22 +0,0 @@
use anyhow::{bail, Result};
use syntect::{easy::HighlightLines, util::as_24_bit_terminal_escaped};
use yazi_plugin::external::Highlighter;
use super::Input;
impl Input {
pub fn value_pretty(&self) -> Result<String> {
if !self.highlight {
bail!("Highlighting is disabled")
}
let (theme, syntaxes) = Highlighter::init();
if let Some(syntax) = syntaxes.find_syntax_by_name("Bourne Again Shell (bash)") {
let mut h = HighlightLines::new(syntax, theme);
let regions = h.highlight_line(self.value(), syntaxes)?;
return Ok(as_24_bit_terminal_escaped(&regions, false));
}
bail!("Failed to find syntax")
}
}

View File

@ -17,7 +17,6 @@ yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.1" }
yazi-shared = { path = "../yazi-shared", version = "0.2.1" }
# External dependencies
ansi-to-tui = "^3"
anyhow = "^1"
better-panic = "^0"
crossterm = { version = "^0", features = [ "event-stream" ] }
@ -27,6 +26,7 @@ mlua = { version = "^0", features = [ "lua54", "vendored" ] }
ratatui = "^0"
tokio = { version = "^1", features = [ "parking_lot" ] }
unicode-width = "^0"
syntect = { version = "^5", default-features = false, features = [ "parsing", "plist-load", "regex-onig" ] }
# Logging
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }

View File

@ -1,9 +1,11 @@
use std::ops::Range;
use ansi_to_tui::IntoText;
use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Text}, widgets::{Block, BorderType, Borders, Paragraph, Widget}};
use anyhow::{bail, Result};
use ratatui::{buffer::Buffer, layout::Rect, text::Line, widgets::{Block, BorderType, Borders, Paragraph, Widget}};
use syntect::easy::HighlightLines;
use yazi_config::THEME;
use yazi_core::input::InputMode;
use yazi_plugin::external::Highlighter;
use yazi_shared::term::Term;
use crate::{widgets, Ctx};
@ -14,6 +16,20 @@ pub(crate) struct Input<'a> {
impl<'a> Input<'a> {
pub(crate) fn new(cx: &'a Ctx) -> Self { Self { cx } }
fn highlighted_value(&self) -> Result<Line<'static>> {
if !self.cx.input.highlight {
bail!("Highlighting is disabled");
}
let (theme, syntaxes) = Highlighter::init();
if let Some(syntax) = syntaxes.find_syntax_by_name("Bourne Again Shell (bash)") {
let mut h = HighlightLines::new(syntax, theme);
let regions = h.highlight_line(self.cx.input.value(), syntaxes)?;
return Ok(Highlighter::to_line_widget(regions));
}
bail!("Failed to find syntax")
}
}
impl<'a> Widget for Input<'a> {
@ -21,14 +37,8 @@ impl<'a> Widget for Input<'a> {
let input = &self.cx.input;
let area = self.cx.area(&input.position);
let value = if let Ok(v) = input.value_pretty() {
v.into_text().unwrap()
} else {
Text::from(input.value())
};
widgets::Clear.render(area, buf);
Paragraph::new(value)
Paragraph::new(self.highlighted_value().unwrap_or_else(|_| Line::from(input.value())))
.block(
Block::new()
.borders(Borders::ALL)

View File

@ -136,7 +136,7 @@ impl Highlighter {
impl Highlighter {
// Copy from https://github.com/sharkdp/bat/blob/master/src/terminal.rs
fn to_ansi_color(color: highlighting::Color) -> Option<ratatui::style::Color> {
pub fn to_ansi_color(color: highlighting::Color) -> Option<ratatui::style::Color> {
if color.a == 0 {
// Themes can specify one of the user-configurable terminal colors by
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
@ -175,7 +175,7 @@ impl Highlighter {
}
}
fn to_line_widget(regions: Vec<(highlighting::Style, &str)>) -> Line<'static> {
pub fn to_line_widget(regions: Vec<(highlighting::Style, &str)>) -> Line<'static> {
let indent = " ".repeat(PREVIEW.tab_size as usize);
let spans: Vec<_> = regions
.into_iter()