Skip to content

Commit

Permalink
fix: use a less intrusive DSR instead of DA1 workaround to forwar…
Browse files Browse the repository at this point in the history
…d terminal responses twice in tmux (#2058)
  • Loading branch information
sxyazi authored Dec 16, 2024
1 parent c96b1d3 commit 63ad289
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
29 changes: 29 additions & 0 deletions yazi-adapter/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl Emulator {
)?;

let resp = futures::executor::block_on(Self::read_until_da1());
Mux::tmux_drain()?;

let kind = if let Some(brand) = Brand::from_csi(&resp) {
Either::Left(brand)
} else {
Expand Down Expand Up @@ -130,6 +132,31 @@ impl Emulator {
String::from_utf8_lossy(&buf).into_owned()
}

pub async fn read_until_dsr() -> String {
let mut buf: Vec<u8> = Vec::with_capacity(200);
let read = async {
let mut stdin = BufReader::new(tokio::io::stdin());
loop {
let mut c = [0; 1];
if stdin.read(&mut c).await? == 0 {
bail!("unexpected EOF");
}
buf.push(c[0]);
if c[0] == b'n' && (buf.ends_with(b"\x1b[0n") || buf.ends_with(b"\x1b[3n")) {
break;
}
}
Ok(())
};

match timeout(Duration::from_secs(10), read).await {
Err(e) => error!("read_until_dsr timed out: {buf:?}, error: {e:?}"),
Ok(Err(e)) => error!("read_until_dsr failed: {buf:?}, error: {e:?}"),
Ok(Ok(())) => debug!("read_until_dsr: {buf:?}"),
}
String::from_utf8_lossy(&buf).into_owned()
}

fn detect_base() -> Result<Self> {
defer! { disable_raw_mode().ok(); }
enable_raw_mode()?;
Expand All @@ -142,6 +169,8 @@ impl Emulator {
)?;

let resp = futures::executor::block_on(Self::read_until_da1());
Mux::tmux_drain()?;

Ok(Self {
light: Self::light_bg(&resp).unwrap_or_default(),
cell_size: Self::cell_size(&resp),
Expand Down
11 changes: 10 additions & 1 deletion yazi-adapter/src/mux.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use tracing::error;
use yazi_shared::env_exists;

use crate::{CLOSE, ESCAPE, NVIM, START, TMUX};
use crate::{CLOSE, ESCAPE, Emulator, NVIM, START, TMUX};

pub struct Mux;

Expand Down Expand Up @@ -47,6 +48,14 @@ impl Mux {
1
}

pub fn tmux_drain() -> Result<()> {
if *TMUX == 2 && !*NVIM {
crossterm::execute!(std::io::stderr(), crossterm::style::Print(Mux::csi("\x1b[5n")))?;
_ = futures::executor::block_on(Emulator::read_until_dsr());
}
Ok(())
}

pub fn tmux_sixel_flag() -> &'static str {
let stdout = std::process::Command::new("tmux")
.args(["-LwU0dju1is5", "-f/dev/null", "start", ";", "display", "-p", "#{sixel_support}"])
Expand Down
2 changes: 2 additions & 0 deletions yazi-fm/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl Term {
)?;

let da = futures::executor::block_on(Emulator::read_until_da1());
Mux::tmux_drain()?;

CSI_U.store(da.contains("\x1b[?0u"), Ordering::Relaxed);
BLINK.store(da.contains("\x1b[?12;1$y"), Ordering::Relaxed);
SHAPE.store(
Expand Down

0 comments on commit 63ad289

Please sign in to comment.