mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
fix: shell input code highlighting is not adapted for the default ANSI theme (#536)
This commit is contained in:
parent
453ca94e18
commit
1bfd1c002f
8 changed files with 24 additions and 39 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -2674,7 +2674,6 @@ dependencies = [
|
||||||
"ratatui",
|
"ratatui",
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"syntect",
|
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
|
|
@ -2691,7 +2690,6 @@ dependencies = [
|
||||||
name = "yazi-fm"
|
name = "yazi-fm"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi-to-tui",
|
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"better-panic",
|
"better-panic",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
|
@ -2701,6 +2699,7 @@ dependencies = [
|
||||||
"mlua",
|
"mlua",
|
||||||
"ratatui",
|
"ratatui",
|
||||||
"signal-hook-tokio",
|
"signal-hook-tokio",
|
||||||
|
"syntect",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-appender",
|
"tracing-appender",
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ parking_lot = "^0"
|
||||||
ratatui = "^0"
|
ratatui = "^0"
|
||||||
regex = "^1"
|
regex = "^1"
|
||||||
serde = "^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 = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs", "process", "io-std", "io-util" ] }
|
||||||
tokio-stream = "^0"
|
tokio-stream = "^0"
|
||||||
tokio-util = "^0"
|
tokio-util = "^0"
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ pub struct Input {
|
||||||
pub(super) completion: bool,
|
pub(super) completion: bool,
|
||||||
|
|
||||||
// Shell
|
// Shell
|
||||||
pub(super) highlight: bool,
|
pub highlight: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Input {
|
impl Input {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ mod commands;
|
||||||
mod input;
|
mod input;
|
||||||
mod mode;
|
mod mode;
|
||||||
mod op;
|
mod op;
|
||||||
mod shell;
|
|
||||||
mod snap;
|
mod snap;
|
||||||
mod snaps;
|
mod snaps;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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(®ions, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
bail!("Failed to find syntax")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -17,7 +17,6 @@ yazi-scheduler = { path = "../yazi-scheduler", version = "0.2.1" }
|
||||||
yazi-shared = { path = "../yazi-shared", version = "0.2.1" }
|
yazi-shared = { path = "../yazi-shared", version = "0.2.1" }
|
||||||
|
|
||||||
# External dependencies
|
# External dependencies
|
||||||
ansi-to-tui = "^3"
|
|
||||||
anyhow = "^1"
|
anyhow = "^1"
|
||||||
better-panic = "^0"
|
better-panic = "^0"
|
||||||
crossterm = { version = "^0", features = [ "event-stream" ] }
|
crossterm = { version = "^0", features = [ "event-stream" ] }
|
||||||
|
|
@ -27,6 +26,7 @@ mlua = { version = "^0", features = [ "lua54", "vendored" ] }
|
||||||
ratatui = "^0"
|
ratatui = "^0"
|
||||||
tokio = { version = "^1", features = [ "parking_lot" ] }
|
tokio = { version = "^1", features = [ "parking_lot" ] }
|
||||||
unicode-width = "^0"
|
unicode-width = "^0"
|
||||||
|
syntect = { version = "^5", default-features = false, features = [ "parsing", "plist-load", "regex-onig" ] }
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
|
tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] }
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
use ansi_to_tui::IntoText;
|
use anyhow::{bail, Result};
|
||||||
use ratatui::{buffer::Buffer, layout::Rect, text::{Line, Text}, widgets::{Block, BorderType, Borders, Paragraph, Widget}};
|
use ratatui::{buffer::Buffer, layout::Rect, text::Line, widgets::{Block, BorderType, Borders, Paragraph, Widget}};
|
||||||
|
use syntect::easy::HighlightLines;
|
||||||
use yazi_config::THEME;
|
use yazi_config::THEME;
|
||||||
use yazi_core::input::InputMode;
|
use yazi_core::input::InputMode;
|
||||||
|
use yazi_plugin::external::Highlighter;
|
||||||
use yazi_shared::term::Term;
|
use yazi_shared::term::Term;
|
||||||
|
|
||||||
use crate::{widgets, Ctx};
|
use crate::{widgets, Ctx};
|
||||||
|
|
@ -14,6 +16,20 @@ pub(crate) struct Input<'a> {
|
||||||
|
|
||||||
impl<'a> Input<'a> {
|
impl<'a> Input<'a> {
|
||||||
pub(crate) fn new(cx: &'a Ctx) -> Self { Self { cx } }
|
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> {
|
impl<'a> Widget for Input<'a> {
|
||||||
|
|
@ -21,14 +37,8 @@ impl<'a> Widget for Input<'a> {
|
||||||
let input = &self.cx.input;
|
let input = &self.cx.input;
|
||||||
let area = self.cx.area(&input.position);
|
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);
|
widgets::Clear.render(area, buf);
|
||||||
Paragraph::new(value)
|
Paragraph::new(self.highlighted_value().unwrap_or_else(|_| Line::from(input.value())))
|
||||||
.block(
|
.block(
|
||||||
Block::new()
|
Block::new()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
|
|
|
||||||
4
yazi-plugin/src/external/highlighter.rs
vendored
4
yazi-plugin/src/external/highlighter.rs
vendored
|
|
@ -136,7 +136,7 @@ impl Highlighter {
|
||||||
|
|
||||||
impl Highlighter {
|
impl Highlighter {
|
||||||
// Copy from https://github.com/sharkdp/bat/blob/master/src/terminal.rs
|
// 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 {
|
if color.a == 0 {
|
||||||
// Themes can specify one of the user-configurable terminal colors by
|
// Themes can specify one of the user-configurable terminal colors by
|
||||||
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
|
// 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 indent = " ".repeat(PREVIEW.tab_size as usize);
|
||||||
let spans: Vec<_> = regions
|
let spans: Vec<_> = regions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue