diff --git a/Cargo.lock b/Cargo.lock index ee4fe4e1..beb0eb31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/yazi-core/Cargo.toml b/yazi-core/Cargo.toml index dfee9b36..a79b232a 100644 --- a/yazi-core/Cargo.toml +++ b/yazi-core/Cargo.toml @@ -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" diff --git a/yazi-core/src/input/input.rs b/yazi-core/src/input/input.rs index 91ce3484..8303836c 100644 --- a/yazi-core/src/input/input.rs +++ b/yazi-core/src/input/input.rs @@ -23,7 +23,7 @@ pub struct Input { pub(super) completion: bool, // Shell - pub(super) highlight: bool, + pub highlight: bool, } impl Input { diff --git a/yazi-core/src/input/mod.rs b/yazi-core/src/input/mod.rs index bd00b9e5..255e7c73 100644 --- a/yazi-core/src/input/mod.rs +++ b/yazi-core/src/input/mod.rs @@ -2,7 +2,6 @@ mod commands; mod input; mod mode; mod op; -mod shell; mod snap; mod snaps; diff --git a/yazi-core/src/input/shell.rs b/yazi-core/src/input/shell.rs deleted file mode 100644 index 9ff89fee..00000000 --- a/yazi-core/src/input/shell.rs +++ /dev/null @@ -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 { - 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(®ions, false)); - } - - bail!("Failed to find syntax") - } -} diff --git a/yazi-fm/Cargo.toml b/yazi-fm/Cargo.toml index fbaa7c3a..83b1cdec 100644 --- a/yazi-fm/Cargo.toml +++ b/yazi-fm/Cargo.toml @@ -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" ] } diff --git a/yazi-fm/src/input/input.rs b/yazi-fm/src/input/input.rs index bfe93743..69dc5ded 100644 --- a/yazi-fm/src/input/input.rs +++ b/yazi-fm/src/input/input.rs @@ -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> { + 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) diff --git a/yazi-plugin/src/external/highlighter.rs b/yazi-plugin/src/external/highlighter.rs index 1fc52154..584e614f 100644 --- a/yazi-plugin/src/external/highlighter.rs +++ b/yazi-plugin/src/external/highlighter.rs @@ -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 { + pub fn to_ansi_color(color: highlighting::Color) -> Option { 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()