From 3c42195bacdd73fc1d8e84cc4190c24016b89bf6 Mon Sep 17 00:00:00 2001 From: yuvals1 Date: Thu, 4 Sep 2025 16:59:06 +0300 Subject: [PATCH] fix: generate unique image IDs for Kgp to tolerate tmux (#3038) Co-authored-by: sxyazi --- yazi-adapter/src/drivers/kgp.rs | 22 ++++++++++++++++++++-- yazi-fs/src/provider/local/casefold.rs | 7 ++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/yazi-adapter/src/drivers/kgp.rs b/yazi-adapter/src/drivers/kgp.rs index 4d39cb4f..88203280 100644 --- a/yazi-adapter/src/drivers/kgp.rs +++ b/yazi-adapter/src/drivers/kgp.rs @@ -6,6 +6,7 @@ use base64::{Engine, engine::general_purpose}; use crossterm::{cursor::MoveTo, queue}; use image::DynamicImage; use ratatui::layout::Rect; +use yazi_shared::SyncCell; use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter, image::Image}; @@ -350,9 +351,10 @@ impl Kgp { if let Some(first) = it.next() { write!( buf, - "{START}_Gq=2,a=T,i=1,C=1,U=1,f={format},s={},v={},m={};{}{ESCAPE}\\{CLOSE}", + "{START}_Gq=2,a=T,C=1,U=1,f={format},s={},v={},i={},m={};{}{ESCAPE}\\{CLOSE}", size.0, size.1, + Kgp::image_id(), it.peek().is_some() as u8, unsafe { str::from_utf8_unchecked(first) }, )?; @@ -379,7 +381,11 @@ impl Kgp { fn place(area: &Rect) -> Result> { let mut buf = Vec::with_capacity(area.width as usize * area.height as usize * 3 + 50); - write!(buf, "\x1b[38;2;0;0;1m")?; + + let id = Self::image_id(); + let (r, g, b) = ((id >> 16) & 0xff, (id >> 8) & 0xff, id & 0xff); + write!(buf, "\x1b[38;2;{r};{g};{b}m")?; + for y in 0..area.height { write!(buf, "\x1b[{};{}H", area.y + y + 1, area.x + 1)?; for x in 0..area.width { @@ -390,4 +396,16 @@ impl Kgp { } Ok(buf) } + + fn image_id() -> u32 { + static CACHE: SyncCell> = SyncCell::new(None); + match CACHE.get() { + Some(n) => n, + None => { + let n = std::process::id() % (0xffffff + 1); + CACHE.set(Some(n)); + n + } + } + } } diff --git a/yazi-fs/src/provider/local/casefold.rs b/yazi-fs/src/provider/local/casefold.rs index 73c631f8..3e9e9c8f 100644 --- a/yazi-fs/src/provider/local/casefold.rs +++ b/yazi-fs/src/provider/local/casefold.rs @@ -160,10 +160,7 @@ fn final_path(path: &Path) -> io::Result { match inner(&file, &mut [0u16; 512])? { Either::Left(path) => Ok(path), - Either::Right(len) => { - let mut buf = vec![0u16; len as usize]; - inner(&file, &mut buf)? - .left_or_err(|| io::Error::new(io::ErrorKind::InvalidData, "path too long")) - } + Either::Right(len) => inner(&file, &mut vec![0u16; len as usize])? + .left_or_err(|| io::Error::new(io::ErrorKind::InvalidData, "path too long")), } }