mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Refactor
This commit is contained in:
parent
a7c01303ac
commit
2ee65152b0
9 changed files with 40 additions and 18 deletions
|
|
@ -19,7 +19,7 @@ light = ""
|
|||
# : App {{{
|
||||
|
||||
[app]
|
||||
background = ""
|
||||
overall = {}
|
||||
|
||||
# : }}}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ light = ""
|
|||
# : App {{{
|
||||
|
||||
[app]
|
||||
background = ""
|
||||
overall = {}
|
||||
|
||||
# : }}}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
16
yazi-term/src/background.rs
Normal file
16
yazi-term/src/background.rs
Normal 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(()) }
|
||||
}
|
||||
|
|
@ -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(); }
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue