From a18a5e6957e1f399b45e29c4e1ccf8399024915a Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 28 Aug 2024 02:24:35 +0800 Subject: [PATCH] fix: `DECSET` and `DECRQM` tmux passthrough --- yazi-adapter/src/emulator.rs | 7 ++----- yazi-adapter/src/lib.rs | 9 +++++---- yazi-adapter/src/macros.rs | 15 +++++++++++++++ yazi-fm/src/term.rs | 8 ++++---- 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 yazi-adapter/src/macros.rs diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index 1d6cf101..3045adc8 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -7,7 +7,7 @@ use tokio::{io::{AsyncReadExt, BufReader}, time::timeout}; use tracing::{error, warn}; use yazi_shared::env_exists; -use crate::{Adapter, CLOSE, ESCAPE, START, TMUX}; +use crate::{tmux, Adapter, TMUX}; #[derive(Clone, Debug)] pub enum Emulator { @@ -123,10 +123,7 @@ impl Emulator { execute!( LineWriter::new(stderr()), SavePosition, - Print(format!( - "{}[>q{}_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA{}\\{}[c{}", - START, ESCAPE, ESCAPE, ESCAPE, CLOSE - )), + Print(tmux!("\x1b[>q\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c")), RestorePosition )?; diff --git a/yazi-adapter/src/lib.rs b/yazi-adapter/src/lib.rs index a0fe43cc..29a66888 100644 --- a/yazi-adapter/src/lib.rs +++ b/yazi-adapter/src/lib.rs @@ -8,6 +8,7 @@ mod image; mod iterm2; mod kitty; mod kitty_old; +mod macros; mod sixel; mod ueberzug; @@ -28,18 +29,18 @@ pub static ADAPTOR: RoCell = RoCell::new(); // Tmux support pub static TMUX: RoCell = RoCell::new(); -static ESCAPE: RoCell<&'static str> = RoCell::new(); -static START: RoCell<&'static str> = RoCell::new(); -static CLOSE: RoCell<&'static str> = RoCell::new(); +pub static ESCAPE: RoCell<&'static str> = RoCell::new(); +pub static START: RoCell<&'static str> = RoCell::new(); +pub static CLOSE: RoCell<&'static str> = RoCell::new(); // Image state static SHOWN: RoCell> = RoCell::new(); pub fn init() { TMUX.init(env_exists("TMUX") && env_exists("TMUX_PANE")); + ESCAPE.init(if *TMUX { "\x1b\x1b" } else { "\x1b" }); START.init(if *TMUX { "\x1bPtmux;\x1b\x1b" } else { "\x1b" }); CLOSE.init(if *TMUX { "\x1b\\" } else { "" }); - ESCAPE.init(if *TMUX { "\x1b\x1b" } else { "\x1b" }); if *TMUX { _ = std::process::Command::new("tmux") diff --git a/yazi-adapter/src/macros.rs b/yazi-adapter/src/macros.rs new file mode 100644 index 00000000..49256c99 --- /dev/null +++ b/yazi-adapter/src/macros.rs @@ -0,0 +1,15 @@ +#[macro_export] +macro_rules! tmux { + ($s:literal) => { + if *$crate::TMUX { + std::borrow::Cow::Owned(format!( + "{}{}{}", + *$crate::START, + $s.trim_start_matches('\x1b').replace('\x1b', *$crate::ESCAPE), + *$crate::CLOSE + )) + } else { + std::borrow::Cow::Borrowed($s) + } + }; +} diff --git a/yazi-fm/src/term.rs b/yazi-fm/src/term.rs index f68cd61e..5eaf130e 100644 --- a/yazi-fm/src/term.rs +++ b/yazi-fm/src/term.rs @@ -4,7 +4,7 @@ use anyhow::Result; use crossterm::{event::{DisableBracketedPaste, EnableBracketedPaste, KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags}, execute, queue, style::Print, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, SetTitle}}; use cursor::RestoreCursor; use ratatui::{backend::CrosstermBackend, buffer::Buffer, layout::Rect, CompletedFrame, Frame, Terminal}; -use yazi_adapter::Emulator; +use yazi_adapter::{tmux, Emulator}; use yazi_config::{INPUT, MANAGER}; static CSI_U: AtomicBool = AtomicBool::new(false); @@ -28,9 +28,9 @@ impl Term { enable_raw_mode()?; execute!( BufWriter::new(stderr()), - Print("\x1b[?12$p"), // Request cursor blink status (DECSET) - Print("\x1bP$q q\x1b\\"), // Request cursor shape (DECRQM) - Print("\x1b[?u\x1b[c"), // Request keyboard enhancement flags (CSI u) + Print(tmux!("\x1b[?12$p")), // Request cursor blink status (DECSET) + Print(tmux!("\x1bP$q q\x1b\\")), // Request cursor shape (DECRQM) + Print(tmux!("\x1b[?u\x1b[c")), // Request keyboard enhancement flags (CSI u) EnterAlternateScreen, EnableBracketedPaste, mouse::SetMouse(true),