feat: new overall option to set the overall background color (#3317)

This commit is contained in:
Zane Weissman 2025-11-11 23:41:13 -05:00 committed by GitHub
parent 4b56e73235
commit 577065cbd0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 75 additions and 9 deletions

View file

@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/):
- Zoom in or out of the preview image ([#2864])
- Improve the UX of the pick and input components ([#2906], [#2935])
- Show progress of each task in task manager ([#3121], [#3131], [#3134])
- New `overall` option to set the overall background color ([#3317])
- New `bulk_rename` command always renames files with the editor ([#2984])
- `key-*` DDS events to allow changing or canceling user key events ([#3005], [#3037])
- New `--bg` specifying image background color in the preset SVG and ImageMagick previewers ([#3189])
@ -1535,3 +1536,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/):
[#3286]: https://github.com/sxyazi/yazi/pull/3286
[#3290]: https://github.com/sxyazi/yazi/pull/3290
[#3313]: https://github.com/sxyazi/yazi/pull/3313
[#3317]: https://github.com/sxyazi/yazi/pull/3317

View file

@ -16,6 +16,14 @@ light = ""
# : }}}
# : App {{{
[app]
overall = {}
# : }}}
# : Manager {{{
[mgr]

View file

@ -16,6 +16,14 @@ light = ""
# : }}}
# : App {{{
[app]
overall = {}
# : }}}
# : Manager {{{
[mgr]

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

@ -12,6 +12,7 @@ use crate::Style;
#[derive(Deserialize, DeserializeOver1)]
pub struct Theme {
pub flavor: Flavor,
pub app: App,
pub mgr: Mgr,
pub tabs: Tabs,
pub mode: Mode,
@ -33,6 +34,11 @@ pub struct Theme {
pub icon: Icon,
}
#[derive(Deserialize, DeserializeOver2)]
pub struct App {
pub overall: Style,
}
#[derive(Deserialize, DeserializeOver2)]
pub struct Mgr {
pub cwd: Style,
@ -229,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;
use yazi_config::{THEME, YAZI};
use yazi_shared::SyncCell;
use yazi_term::tty::{TTY, TtyWriter};
@ -38,6 +38,7 @@ impl Term {
Print("\x1b[?u"), // Request keyboard enhancement flags (CSI u)
Print("\x1b[0c"), // Request device attributes
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),
)?;
@ -100,6 +101,7 @@ impl Term {
execute!(
TTY.writer(),
yazi_term::If(!YAZI.mgr.mouse_events.get().is_empty(), DisableMouseCapture),
yazi_term::SetBackground(false, THEME.app.bg_color()),
yazi_term::RestoreCursor,
DisableBracketedPaste,
LeaveAlternateScreen,

View file

@ -5,6 +5,7 @@ use yazi_config::THEME;
pub fn compose() -> Composer<ComposerGet, ComposerSet> {
fn get(lua: &Lua, key: &[u8]) -> mlua::Result<Value> {
match key {
b"app" => app(),
b"mgr" => mgr(),
b"tabs" => tabs(),
b"mode" => mode(),
@ -28,6 +29,21 @@ pub fn compose() -> Composer<ComposerGet, ComposerSet> {
Composer::new(get, set)
}
fn app() -> Composer<ComposerGet, ComposerSet> {
fn get(lua: &Lua, key: &[u8]) -> mlua::Result<Value> {
let a = &THEME.app;
match key {
b"overall" => Style::from(a.overall).into_lua(lua),
_ => Ok(Value::Nil),
}
}
fn set(_: &Lua, _: &[u8], value: Value) -> mlua::Result<Value> { Ok(value) }
Composer::new(get, set)
}
fn mgr() -> Composer<ComposerGet, ComposerSet> {
fn get(lua: &Lua, key: &[u8]) -> mlua::Result<Value> {
let m = &THEME.mgr;

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