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 anyhow::{anyhow, Result};
use ratatui::prelude::Rect; use ratatui::prelude::Rect;
@ -160,25 +160,13 @@ impl Adaptor {
pub(super) fn start(self) { Ueberzug::start(self); } pub(super) fn start(self) { Ueberzug::start(self); }
pub async fn image_show(self, path: &Path, rect: Rect) -> Result<(u32, u32)> { 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::Kitty => Kitty::image_show(path, rect).await,
Self::KittyOld => KittyOld::image_show(path, rect).await, Self::KittyOld => KittyOld::image_show(path, rect).await,
Self::Iterm2 => Iterm2::image_show(path, rect).await, Self::Iterm2 => Iterm2::image_show(path, rect).await,
Self::Sixel => Sixel::image_show(path, rect).await, Self::Sixel => Sixel::image_show(path, rect).await,
_ => Ueberzug::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<()> { 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] #[inline]
pub(super) fn needs_ueberzug(self) -> bool { pub(super) fn needs_ueberzug(self) -> bool {
!matches!(self, Self::Kitty | Self::KittyOld | Self::Iterm2 | Self::Sixel) !matches!(self, Self::Kitty | Self::KittyOld | Self::Iterm2 | Self::Sixel)

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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