mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-11 09:24:55 +03:00
fix: shell input code highlighting is not adapted for the default ANSI theme (#536)
This commit is contained in:
parent
453ca94e18
commit
1bfd1c002f
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -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",
|
||||
|
@ -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"
|
||||
|
@ -23,7 +23,7 @@ pub struct Input {
|
||||
pub(super) completion: bool,
|
||||
|
||||
// Shell
|
||||
pub(super) highlight: bool,
|
||||
pub highlight: bool,
|
||||
}
|
||||
|
||||
impl Input {
|
||||
|
@ -2,7 +2,6 @@ mod commands;
|
||||
mod input;
|
||||
mod mode;
|
||||
mod op;
|
||||
mod shell;
|
||||
mod snap;
|
||||
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" }
|
||||
|
||||
# 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" ] }
|
||||
|
@ -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)
|
||||
|
4
yazi-plugin/src/external/highlighter.rs
vendored
4
yazi-plugin/src/external/highlighter.rs
vendored
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user