This commit is contained in:
sxyazi 2025-11-12 12:30:43 +08:00
parent a7c01303ac
commit 2ee65152b0
No known key found for this signature in database
9 changed files with 40 additions and 18 deletions

View file

@ -19,7 +19,7 @@ light = ""
# : App {{{
[app]
background = ""
overall = {}
# : }}}

View file

@ -19,7 +19,7 @@ light = ""
# : App {{{
[app]
background = ""
overall = {}
# : }}}

View file

@ -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<Color> for ratatui::style::Color {
fn from(value: Color) -> Self { value.0 }
}
impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@ -15,7 +25,3 @@ impl<'de> Deserialize<'de> for Color {
.map(Self)
}
}
impl From<Color> for ratatui::style::Color {
fn from(value: Color) -> Self { value.0 }
}

View file

@ -5,9 +5,7 @@ use crate::Color;
#[derive(Clone, Copy, Debug, Default, Deserialize)]
pub struct Style {
#[serde(default)]
pub fg: Option<Color>,
#[serde(default)]
pub bg: Option<Color>,
#[serde(default)]
pub bold: bool,

View file

@ -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() }
}

View file

@ -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<bool> = 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,

View file

@ -33,7 +33,8 @@ fn app() -> Composer<ComposerGet, ComposerSet> {
fn get(lua: &Lua, key: &[u8]) -> mlua::Result<Value> {
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),
}
}

View file

@ -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(()) }
}

View file

@ -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(); }