From 4c35f26e1fee6646bff5df44734fdfd2b7f13472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Tue, 30 Apr 2024 01:43:04 +0800 Subject: [PATCH] feat: detect terminal type in tmux with CSI sequence in passthrough mode (#977) --- Cargo.lock | 1 + yazi-adaptor/Cargo.toml | 1 + yazi-adaptor/src/emulator.rs | 12 ++++++++---- yazi-adaptor/src/lib.rs | 14 +++++++------- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64898c5f..f06f724e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2704,6 +2704,7 @@ dependencies = [ "imagesize", "kamadak-exif", "ratatui", + "scopeguard", "tokio", "tracing", "yazi-config", diff --git a/yazi-adaptor/Cargo.toml b/yazi-adaptor/Cargo.toml index 39e06d07..1fa24f38 100644 --- a/yazi-adaptor/Cargo.toml +++ b/yazi-adaptor/Cargo.toml @@ -23,6 +23,7 @@ image = "0.24.9" imagesize = "0.12.0" kamadak-exif = "0.5.5" ratatui = "=0.26.1" +scopeguard = "1.2.0" tokio = { version = "1.37.0", features = [ "full" ] } # Logging diff --git a/yazi-adaptor/src/emulator.rs b/yazi-adaptor/src/emulator.rs index 1da786c3..9721aa1f 100644 --- a/yazi-adaptor/src/emulator.rs +++ b/yazi-adaptor/src/emulator.rs @@ -2,10 +2,11 @@ use std::{env, io::{stderr, LineWriter}}; use anyhow::{anyhow, Result}; use crossterm::{cursor::{RestorePosition, SavePosition}, execute, style::Print, terminal::{disable_raw_mode, enable_raw_mode}}; +use scopeguard::defer; use tracing::warn; use yazi_shared::{env_exists, term::Term}; -use crate::{Adaptor, TMUX}; +use crate::{Adaptor, CLOSE, ESCAPE, START, TMUX}; #[derive(Clone, Debug)] pub enum Emulator { @@ -112,17 +113,20 @@ impl Emulator { } pub fn via_csi() -> Result { + defer! { disable_raw_mode().ok(); } enable_raw_mode()?; + execute!( LineWriter::new(stderr()), SavePosition, - Print("\x1b[>q\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c"), + Print(format!( + "{}[>q{}_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA{}\\{}[c{}", + START, ESCAPE, ESCAPE, ESCAPE, CLOSE + )), RestorePosition )?; let resp = futures::executor::block_on(Term::read_until_da1())?; - disable_raw_mode().ok(); - let names = [ ("kitty", Self::Kitty), ("Konsole", Self::Konsole), diff --git a/yazi-adaptor/src/lib.rs b/yazi-adaptor/src/lib.rs index 700e944e..bf534082 100644 --- a/yazi-adaptor/src/lib.rs +++ b/yazi-adaptor/src/lib.rs @@ -31,22 +31,22 @@ static CLOSE: RoCell<&'static str> = RoCell::new(); static SHOWN: RoCell> = RoCell::new(); pub fn init() { - TMUX.init(env_exists("TMUX")); + TMUX.init(env_exists("TMUX") && env_exists("TMUX_PANE")); START.init(if *TMUX { "\x1bPtmux;\x1b\x1b" } else { "\x1b" }); CLOSE.init(if *TMUX { "\x1b\\" } else { "" }); ESCAPE.init(if *TMUX { "\x1b\x1b" } else { "\x1b" }); - SHOWN.with(Default::default); - - ADAPTOR.init(Adaptor::matches()); - ADAPTOR.start(); - if *TMUX { _ = std::process::Command::new("tmux") .args(["set", "-p", "allow-passthrough", "on"]) .stdin(std::process::Stdio::null()) .stdout(std::process::Stdio::null()) .stderr(std::process::Stdio::null()) - .spawn(); + .status(); } + + SHOWN.with(Default::default); + + ADAPTOR.init(Adaptor::matches()); + ADAPTOR.start(); }