feat: detect terminal type in tmux with CSI sequence in passthrough mode (#977)

This commit is contained in:
三咲雅 · Misaki Masa 2024-04-30 01:43:04 +08:00 committed by GitHub
parent 0016876dc9
commit 4c35f26e1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 11 deletions

1
Cargo.lock generated
View file

@ -2704,6 +2704,7 @@ dependencies = [
"imagesize", "imagesize",
"kamadak-exif", "kamadak-exif",
"ratatui", "ratatui",
"scopeguard",
"tokio", "tokio",
"tracing", "tracing",
"yazi-config", "yazi-config",

View file

@ -23,6 +23,7 @@ image = "0.24.9"
imagesize = "0.12.0" imagesize = "0.12.0"
kamadak-exif = "0.5.5" kamadak-exif = "0.5.5"
ratatui = "=0.26.1" ratatui = "=0.26.1"
scopeguard = "1.2.0"
tokio = { version = "1.37.0", features = [ "full" ] } tokio = { version = "1.37.0", features = [ "full" ] }
# Logging # Logging

View file

@ -2,10 +2,11 @@ use std::{env, io::{stderr, LineWriter}};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use crossterm::{cursor::{RestorePosition, SavePosition}, execute, style::Print, terminal::{disable_raw_mode, enable_raw_mode}}; use crossterm::{cursor::{RestorePosition, SavePosition}, execute, style::Print, terminal::{disable_raw_mode, enable_raw_mode}};
use scopeguard::defer;
use tracing::warn; use tracing::warn;
use yazi_shared::{env_exists, term::Term}; use yazi_shared::{env_exists, term::Term};
use crate::{Adaptor, TMUX}; use crate::{Adaptor, CLOSE, ESCAPE, START, TMUX};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Emulator { pub enum Emulator {
@ -112,17 +113,20 @@ impl Emulator {
} }
pub fn via_csi() -> Result<Self> { pub fn via_csi() -> Result<Self> {
defer! { disable_raw_mode().ok(); }
enable_raw_mode()?; enable_raw_mode()?;
execute!( execute!(
LineWriter::new(stderr()), LineWriter::new(stderr()),
SavePosition, 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 RestorePosition
)?; )?;
let resp = futures::executor::block_on(Term::read_until_da1())?; let resp = futures::executor::block_on(Term::read_until_da1())?;
disable_raw_mode().ok();
let names = [ let names = [
("kitty", Self::Kitty), ("kitty", Self::Kitty),
("Konsole", Self::Konsole), ("Konsole", Self::Konsole),

View file

@ -31,22 +31,22 @@ static CLOSE: RoCell<&'static str> = RoCell::new();
static SHOWN: RoCell<arc_swap::ArcSwapOption<ratatui::layout::Rect>> = RoCell::new(); static SHOWN: RoCell<arc_swap::ArcSwapOption<ratatui::layout::Rect>> = RoCell::new();
pub fn init() { 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" }); START.init(if *TMUX { "\x1bPtmux;\x1b\x1b" } else { "\x1b" });
CLOSE.init(if *TMUX { "\x1b\\" } else { "" }); CLOSE.init(if *TMUX { "\x1b\\" } else { "" });
ESCAPE.init(if *TMUX { "\x1b\x1b" } else { "\x1b" }); ESCAPE.init(if *TMUX { "\x1b\x1b" } else { "\x1b" });
SHOWN.with(Default::default);
ADAPTOR.init(Adaptor::matches());
ADAPTOR.start();
if *TMUX { if *TMUX {
_ = std::process::Command::new("tmux") _ = std::process::Command::new("tmux")
.args(["set", "-p", "allow-passthrough", "on"]) .args(["set", "-p", "allow-passthrough", "on"])
.stdin(std::process::Stdio::null()) .stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null()) .stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null()) .stderr(std::process::Stdio::null())
.spawn(); .status();
} }
SHOWN.with(Default::default);
ADAPTOR.init(Adaptor::matches());
ADAPTOR.start();
} }