fix: generate unique image IDs for Kgp to tolerate tmux (#3038)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
yuvals1 2025-09-04 16:59:06 +03:00 committed by sxyazi
parent 49910fcfe7
commit 3c42195bac
No known key found for this signature in database
2 changed files with 22 additions and 7 deletions

View file

@ -6,6 +6,7 @@ use base64::{Engine, engine::general_purpose};
use crossterm::{cursor::MoveTo, queue}; use crossterm::{cursor::MoveTo, queue};
use image::DynamicImage; use image::DynamicImage;
use ratatui::layout::Rect; use ratatui::layout::Rect;
use yazi_shared::SyncCell;
use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter, image::Image}; use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter, image::Image};
@ -350,9 +351,10 @@ impl Kgp {
if let Some(first) = it.next() { if let Some(first) = it.next() {
write!( write!(
buf, 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.0,
size.1, size.1,
Kgp::image_id(),
it.peek().is_some() as u8, it.peek().is_some() as u8,
unsafe { str::from_utf8_unchecked(first) }, unsafe { str::from_utf8_unchecked(first) },
)?; )?;
@ -379,7 +381,11 @@ impl Kgp {
fn place(area: &Rect) -> Result<Vec<u8>> { fn place(area: &Rect) -> Result<Vec<u8>> {
let mut buf = Vec::with_capacity(area.width as usize * area.height as usize * 3 + 50); 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 { for y in 0..area.height {
write!(buf, "\x1b[{};{}H", area.y + y + 1, area.x + 1)?; write!(buf, "\x1b[{};{}H", area.y + y + 1, area.x + 1)?;
for x in 0..area.width { for x in 0..area.width {
@ -390,4 +396,16 @@ impl Kgp {
} }
Ok(buf) Ok(buf)
} }
fn image_id() -> u32 {
static CACHE: SyncCell<Option<u32>> = SyncCell::new(None);
match CACHE.get() {
Some(n) => n,
None => {
let n = std::process::id() % (0xffffff + 1);
CACHE.set(Some(n));
n
}
}
}
} }

View file

@ -160,10 +160,7 @@ fn final_path(path: &Path) -> io::Result<PathBuf> {
match inner(&file, &mut [0u16; 512])? { match inner(&file, &mut [0u16; 512])? {
Either::Left(path) => Ok(path), Either::Left(path) => Ok(path),
Either::Right(len) => { Either::Right(len) => inner(&file, &mut vec![0u16; len as usize])?
let mut buf = vec![0u16; len as usize]; .left_or_err(|| io::Error::new(io::ErrorKind::InvalidData, "path too long")),
inner(&file, &mut buf)?
.left_or_err(|| io::Error::new(io::ErrorKind::InvalidData, "path too long"))
}
} }
} }