This commit is contained in:
sxyazi 2023-12-26 19:21:58 +08:00
parent 73d5d34b88
commit 1714b23e8c
No known key found for this signature in database
7 changed files with 26 additions and 17 deletions

View File

@ -1,4 +1,4 @@
use std::{env, path::Path, sync::Arc};
use std::{env, path::Path, sync::{atomic::Ordering, Arc}};
use anyhow::{anyhow, Result};
use ratatui::prelude::Rect;
@ -160,25 +160,13 @@ impl Adaptor {
pub(super) fn start(self) { Ueberzug::start(self); }
pub async fn image_show(self, path: &Path, rect: Rect) -> Result<(u32, u32)> {
let size = match self {
match self {
Self::Kitty => Kitty::image_show(path, rect).await,
Self::KittyOld => KittyOld::image_show(path, rect).await,
Self::Iterm2 => Iterm2::image_show(path, rect).await,
Self::Sixel => Sixel::image_show(path, rect).await,
_ => Ueberzug::image_show(path, rect).await,
}?;
let shown = Term::ratio()
.map(|(r1, r2)| Rect {
x: rect.x,
y: rect.y,
width: (size.0 as f64 / r1).ceil() as u16,
height: (size.1 as f64 / r2).ceil() as u16,
})
.unwrap_or(rect);
SHOWN.store(Some(Arc::new(shown)));
Ok(size)
}
}
pub fn image_hide(self) -> Result<()> {
@ -195,6 +183,20 @@ impl Adaptor {
}
}
#[inline]
pub(super) fn shown_store(rect: Rect, size: (u32, u32)) {
SHOWN.store(Some(Arc::new(
Term::ratio()
.map(|(r1, r2)| Rect {
x: rect.x,
y: rect.y,
width: (size.0 as f64 / r1).ceil() as u16,
height: (size.1 as f64 / r2).ceil() as u16,
})
.unwrap_or(rect),
)));
}
#[inline]
pub(super) fn needs_ueberzug(self) -> bool {
!matches!(self, Self::Kitty | Self::KittyOld | Self::Iterm2 | Self::Sixel)

View File

@ -18,6 +18,7 @@ impl Iterm2 {
let b = Self::encode(img).await?;
Adaptor::Iterm2.image_hide()?;
Adaptor::shown_store(rect, size);
Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| {
stdout.write_all(&b)?;
Ok(size)

View File

@ -318,6 +318,7 @@ impl Kitty {
let b = Self::encode(img).await?;
Adaptor::Kitty.image_hide()?;
Adaptor::shown_store(rect, size);
Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| {
stdout.write_all(&b)?;

View File

@ -18,6 +18,7 @@ impl KittyOld {
let b = Self::encode(img).await?;
Adaptor::KittyOld.image_hide()?;
Adaptor::shown_store(rect, size);
Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| {
stdout.write_all(&b)?;
Ok(size)

View File

@ -17,6 +17,7 @@ impl Sixel {
let b = Self::encode(img).await?;
Adaptor::Sixel.image_hide()?;
Adaptor::shown_store(rect, size);
Term::move_lock(stdout().lock(), (rect.x, rect.y), |stdout| {
stdout.write_all(&b)?;
Ok(size)

View File

@ -44,6 +44,7 @@ impl Ueberzug {
pub(super) async fn image_show(path: &Path, rect: Rect) -> Result<(u32, u32)> {
if let Some(tx) = &*DEMON {
tx.send(Some((path.to_path_buf(), rect)))?;
Adaptor::shown_store(rect, (0, 0));
} else {
bail!("uninitialized ueberzug");
}

View File

@ -34,8 +34,10 @@ impl Tabs {
return;
}
// We must reset it before changing the tab index.
self.active_mut().preview.reset_image();
// Reset the preview of the previous active tab
if let Some(active) = self.items.get_mut(self.idx) {
active.preview.reset_image();
}
self.idx = idx;
Manager::_refresh();