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",
"kamadak-exif",
"ratatui",
"scopeguard",
"tokio",
"tracing",
"yazi-config",

View File

@ -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

View File

@ -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<Self> {
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),

View File

@ -31,22 +31,22 @@ static CLOSE: RoCell<&'static str> = RoCell::new();
static SHOWN: RoCell<arc_swap::ArcSwapOption<ratatui::layout::Rect>> = 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();
}