From 2ee65152b07213a7b2737781435d3e16270be89e Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 12 Nov 2025 12:30:43 +0800 Subject: [PATCH] Refactor --- yazi-config/preset/theme-dark.toml | 2 +- yazi-config/preset/theme-light.toml | 2 +- yazi-config/src/color.rs | 16 +++++++++++----- yazi-config/src/style.rs | 2 -- yazi-config/src/theme/theme.rs | 6 +++++- yazi-fm/src/term.rs | 9 +++------ yazi-plugin/src/theme/theme.rs | 3 ++- yazi-term/src/background.rs | 16 ++++++++++++++++ yazi-term/src/lib.rs | 2 +- 9 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 yazi-term/src/background.rs diff --git a/yazi-config/preset/theme-dark.toml b/yazi-config/preset/theme-dark.toml index b522b26f..d4bb9230 100644 --- a/yazi-config/preset/theme-dark.toml +++ b/yazi-config/preset/theme-dark.toml @@ -19,7 +19,7 @@ light = "" # : App {{{ [app] -background = "" +overall = {} # : }}} diff --git a/yazi-config/preset/theme-light.toml b/yazi-config/preset/theme-light.toml index 26a09880..8bd2c90e 100644 --- a/yazi-config/preset/theme-light.toml +++ b/yazi-config/preset/theme-light.toml @@ -19,7 +19,7 @@ light = "" # : App {{{ [app] -background = "" +overall = {} # : }}} diff --git a/yazi-config/src/color.rs b/yazi-config/src/color.rs index fbdbe23b..bf0b63ba 100644 --- a/yazi-config/src/color.rs +++ b/yazi-config/src/color.rs @@ -1,10 +1,20 @@ -use std::str::FromStr; +use std::{ops::Deref, str::FromStr}; use serde::Deserialize; #[derive(Clone, Copy, Debug, Default)] pub struct Color(ratatui::style::Color); +impl Deref for Color { + type Target = ratatui::style::Color; + + fn deref(&self) -> &Self::Target { &self.0 } +} + +impl From for ratatui::style::Color { + fn from(value: Color) -> Self { value.0 } +} + impl<'de> Deserialize<'de> for Color { fn deserialize(deserializer: D) -> Result where @@ -15,7 +25,3 @@ impl<'de> Deserialize<'de> for Color { .map(Self) } } - -impl From for ratatui::style::Color { - fn from(value: Color) -> Self { value.0 } -} diff --git a/yazi-config/src/style.rs b/yazi-config/src/style.rs index 46602ba4..e2531bb0 100644 --- a/yazi-config/src/style.rs +++ b/yazi-config/src/style.rs @@ -5,9 +5,7 @@ use crate::Color; #[derive(Clone, Copy, Debug, Default, Deserialize)] pub struct Style { - #[serde(default)] pub fg: Option, - #[serde(default)] pub bg: Option, #[serde(default)] pub bold: bool, diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index d1be70d4..b607dbb7 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -36,7 +36,7 @@ pub struct Theme { #[derive(Deserialize, DeserializeOver2)] pub struct App { - pub background: String, + pub overall: Style, } #[derive(Deserialize, DeserializeOver2)] @@ -235,3 +235,7 @@ impl Theme { Ok(self) } } + +impl App { + pub fn bg_color(&self) -> String { self.overall.bg.map(|c| c.to_string()).unwrap_or_default() } +} diff --git a/yazi-fm/src/term.rs b/yazi-fm/src/term.rs index 84f93df9..c3bf1c19 100644 --- a/yazi-fm/src/term.rs +++ b/yazi-fm/src/term.rs @@ -4,7 +4,7 @@ use anyhow::Result; use crossterm::{event::{DisableBracketedPaste, DisableMouseCapture, EnableBracketedPaste, EnableMouseCapture, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags}, execute, queue, style::Print, terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle, disable_raw_mode, enable_raw_mode}}; use ratatui::{CompletedFrame, Frame, Terminal, backend::CrosstermBackend, buffer::Buffer, layout::Rect}; use yazi_adapter::{Emulator, Mux, TMUX}; -use yazi_config::{YAZI,THEME}; +use yazi_config::{THEME, YAZI}; use yazi_shared::SyncCell; use yazi_term::tty::{TTY, TtyWriter}; @@ -23,7 +23,6 @@ impl Term { last_area: Default::default(), last_buffer: Default::default(), }; - let background = &THEME.app.background; enable_raw_mode()?; static FIRST: SyncCell = SyncCell::new(false); @@ -38,9 +37,8 @@ impl Term { Print("\x1b[?12$p"), // Request cursor blink status (DECRQM query for DECSET 12) Print("\x1b[?u"), // Request keyboard enhancement flags (CSI u) Print("\x1b[0c"), // Request device attributes - // Set terminal background color - yazi_term::If(!background.is_empty(), Print(format!("\x1b]11;{}\x1b\\", background))), yazi_term::If(TMUX.get(), EnterAlternateScreen), + yazi_term::SetBackground(true, THEME.app.bg_color()), // Set app background EnableBracketedPaste, yazi_term::If(!YAZI.mgr.mouse_events.get().is_empty(), EnableMouseCapture), )?; @@ -103,8 +101,7 @@ impl Term { execute!( TTY.writer(), yazi_term::If(!YAZI.mgr.mouse_events.get().is_empty(), DisableMouseCapture), - // Restore default terminal background - yazi_term::If(!THEME.app.background.is_empty(), Print("\x1b]111\x1b\\")), + yazi_term::SetBackground(false, THEME.app.bg_color()), yazi_term::RestoreCursor, DisableBracketedPaste, LeaveAlternateScreen, diff --git a/yazi-plugin/src/theme/theme.rs b/yazi-plugin/src/theme/theme.rs index df730835..dfc5c8d0 100644 --- a/yazi-plugin/src/theme/theme.rs +++ b/yazi-plugin/src/theme/theme.rs @@ -33,7 +33,8 @@ fn app() -> Composer { fn get(lua: &Lua, key: &[u8]) -> mlua::Result { let a = &THEME.app; match key { - b"background" => lua.create_string(&a.background)?.into_lua(lua), + b"overall" => Style::from(a.overall).into_lua(lua), + _ => Ok(Value::Nil), } } diff --git a/yazi-term/src/background.rs b/yazi-term/src/background.rs new file mode 100644 index 00000000..de20c482 --- /dev/null +++ b/yazi-term/src/background.rs @@ -0,0 +1,16 @@ +pub struct SetBackground(pub bool, pub String); + +impl crossterm::Command for SetBackground { + fn write_ansi(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result { + if self.1.is_empty() { + return Ok(()); + } else if self.0 { + write!(f, "\x1b]11;{}\x1b\\", self.1) + } else { + write!(f, "\x1b]111\x1b\\") + } + } + + #[cfg(windows)] + fn execute_winapi(&self) -> std::io::Result<()> { Ok(()) } +} diff --git a/yazi-term/src/lib.rs b/yazi-term/src/lib.rs index 351a0497..c743409c 100644 --- a/yazi-term/src/lib.rs +++ b/yazi-term/src/lib.rs @@ -2,6 +2,6 @@ yazi_macro::mod_pub!(tty); -yazi_macro::mod_flat!(cursor r#if); +yazi_macro::mod_flat!(background cursor r#if); pub fn init() { tty::init(); }