From 370e1c53c9e0030e99d3f998d2c73d26ad647150 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 14 Sep 2024 23:50:52 +0800 Subject: [PATCH] Rebase & minor fixes --- yazi-adapter/src/adapter.rs | 14 ++++----- yazi-adapter/src/emulator.rs | 12 ++++---- yazi-adapter/src/{iterm2.rs => iip.rs} | 40 +++++++++++--------------- yazi-adapter/src/image.rs | 33 ++++++++++----------- yazi-adapter/src/kitty.rs | 2 +- yazi-adapter/src/kitty_old.rs | 2 +- yazi-adapter/src/lib.rs | 4 +-- 7 files changed, 49 insertions(+), 58 deletions(-) rename yazi-adapter/src/{iterm2.rs => iip.rs} (66%) diff --git a/yazi-adapter/src/adapter.rs b/yazi-adapter/src/adapter.rs index b9fdad1d4..9199462e9 100644 --- a/yazi-adapter/src/adapter.rs +++ b/yazi-adapter/src/adapter.rs @@ -5,14 +5,14 @@ use ratatui::layout::Rect; use tracing::warn; use yazi_shared::env_exists; -use super::{Iterm2, Kitty, KittyOld}; +use super::{Iip, Kitty, KittyOld}; use crate::{Chafa, Emulator, Sixel, Ueberzug, SHOWN, TMUX, WSL}; #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Adapter { Kitty, KittyOld, - Iterm2, + Iip, Sixel, // Supported by Ɯberzug++ @@ -26,7 +26,7 @@ impl Display for Adapter { match self { Self::Kitty => write!(f, "kitty"), Self::KittyOld => write!(f, "kitty"), - Self::Iterm2 => write!(f, "iterm2"), + Self::Iip => write!(f, "iip"), Self::Sixel => write!(f, "sixel"), Self::X11 => write!(f, "x11"), Self::Wayland => write!(f, "wayland"), @@ -44,7 +44,7 @@ impl Adapter { match self { Self::Kitty => Kitty::image_show(path, max).await, Self::KittyOld => KittyOld::image_show(path, max).await, - Self::Iterm2 => Iterm2::image_show(path, max).await, + Self::Iip => Iip::image_show(path, max).await, Self::Sixel => Sixel::image_show(path, max).await, Self::X11 | Self::Wayland => Ueberzug::image_show(path, max).await, Self::Chafa => Chafa::image_show(path, max).await, @@ -59,7 +59,7 @@ impl Adapter { match self { Self::Kitty => Kitty::image_erase(area), Self::KittyOld => KittyOld::image_erase(area), - Self::Iterm2 => Iterm2::image_erase(area), + Self::Iip => Iip::image_erase(area), Self::Sixel => Sixel::image_erase(area), Self::X11 | Self::Wayland => Ueberzug::image_erase(area), Self::Chafa => Chafa::image_erase(area), @@ -76,7 +76,7 @@ impl Adapter { #[inline] pub(super) fn needs_ueberzug(self) -> bool { - !matches!(self, Self::Kitty | Self::KittyOld | Self::Iterm2 | Self::Sixel) + !matches!(self, Self::Kitty | Self::KittyOld | Self::Iip | Self::Sixel) } } @@ -90,7 +90,7 @@ impl Adapter { let mut protocols = emulator.adapters(); #[cfg(windows)] - protocols.retain(|p| *p == Self::Iterm2); + protocols.retain(|p| *p == Self::Iip); if env_exists("ZELLIJ_SESSION_NAME") { protocols.retain(|p| *p == Self::Sixel); } else if *TMUX { diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index 3a1e2ebe4..c9634103a 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -35,16 +35,16 @@ impl Emulator { Self::Unknown(adapters) => adapters, Self::Kitty => vec![Adapter::Kitty], Self::Konsole => vec![Adapter::KittyOld], - Self::Iterm2 => vec![Adapter::Iterm2, Adapter::Sixel], - Self::WezTerm => vec![Adapter::Iterm2, Adapter::Sixel], + Self::Iterm2 => vec![Adapter::Iip, Adapter::Sixel], + Self::WezTerm => vec![Adapter::Iip, Adapter::Sixel], Self::Foot => vec![Adapter::Sixel], Self::Ghostty => vec![Adapter::Kitty], Self::Microsoft => vec![Adapter::Sixel], Self::BlackBox => vec![Adapter::Sixel], - Self::VSCode => vec![Adapter::Iterm2, Adapter::Sixel], - Self::Tabby => vec![Adapter::Iterm2, Adapter::Sixel], - Self::Hyper => vec![Adapter::Iterm2, Adapter::Sixel], - Self::Mintty => vec![Adapter::Iterm2], + Self::VSCode => vec![Adapter::Iip, Adapter::Sixel], + Self::Tabby => vec![Adapter::Iip, Adapter::Sixel], + Self::Hyper => vec![Adapter::Iip, Adapter::Sixel], + Self::Mintty => vec![Adapter::Iip], Self::Neovim => vec![], Self::Apple => vec![], Self::Urxvt => vec![], diff --git a/yazi-adapter/src/iterm2.rs b/yazi-adapter/src/iip.rs similarity index 66% rename from yazi-adapter/src/iterm2.rs rename to yazi-adapter/src/iip.rs index 3b7108d27..a2cfb7164 100644 --- a/yazi-adapter/src/iterm2.rs +++ b/yazi-adapter/src/iip.rs @@ -1,24 +1,24 @@ use std::{io::Write, path::Path}; use anyhow::Result; -use base64::{engine::general_purpose::STANDARD, Engine}; +use base64::{engine::{general_purpose::STANDARD, Config}, Engine}; use crossterm::{cursor::MoveTo, queue}; -use image::{codecs::{jpeg::JpegEncoder, png::PngEncoder}, DynamicImage, ImageEncoder}; +use image::{codecs::{jpeg::JpegEncoder, png::PngEncoder}, DynamicImage, ExtendedColorType, ImageEncoder}; use ratatui::layout::Rect; use yazi_config::PREVIEW; use super::image::Image; use crate::{adapter::Adapter, Emulator, CLOSE, START}; -pub(super) struct Iterm2; +pub(super) struct Iip; -impl Iterm2 { +impl Iip { pub(super) async fn image_show(path: &Path, max: Rect) -> Result { let img = Image::downscale(path, max).await?; let area = Image::pixel_area((img.width(), img.height()), max); let b = Self::encode(img).await?; - Adapter::Iterm2.image_hide()?; + Adapter::Iip.image_hide()?; Adapter::shown_store(area); Emulator::move_lock((max.x, max.y), |stderr| { stderr.write_all(&b)?; @@ -39,34 +39,28 @@ impl Iterm2 { async fn encode(img: DynamicImage) -> Result> { tokio::task::spawn_blocking(move || { - let width = img.width(); - let height = img.height(); - - let mut img_buf = vec![]; + let (w, h) = (img.width(), img.height()); + let mut b = vec![]; if img.color().has_alpha() { - PngEncoder::new(&mut img_buf).write_image( - &img.into_rgba8(), - width, - height, - image::ExtendedColorType::Rgba8, - )?; + PngEncoder::new(&mut b).write_image(&img.into_rgba8(), w, h, ExtendedColorType::Rgba8)?; } else { - JpegEncoder::new_with_quality(&mut img_buf, PREVIEW.image_quality).encode_image(&img)?; - } + JpegEncoder::new_with_quality(&mut b, PREVIEW.image_quality).encode_image(&img)?; + }; + + let len = base64::encoded_len(b.len(), STANDARD.config().encode_padding()); + let mut buf = Vec::with_capacity(200 + len.unwrap_or(1 << 16)); - let mut buf = vec![]; write!( buf, "{}]1337;File=inline=1;size={};width={}px;height={}px;doNotMoveCursor=1:{}\x07{}", START, - img_buf.len(), - width, - height, - STANDARD.encode(&img_buf), + b.len(), + w, + h, + STANDARD.encode(b), CLOSE )?; - Ok(buf) }) .await? diff --git a/yazi-adapter/src/image.rs b/yazi-adapter/src/image.rs index d7b425754..45ff53705 100644 --- a/yazi-adapter/src/image.rs +++ b/yazi-adapter/src/image.rs @@ -2,7 +2,7 @@ use std::{fs::File, io::BufReader, path::{Path, PathBuf}}; use anyhow::Result; use exif::{In, Tag}; -use image::{codecs::{jpeg::JpegEncoder, png::PngEncoder}, imageops::{self, FilterType}, DynamicImage, ImageEncoder, Limits}; +use image::{codecs::{jpeg::JpegEncoder, png::PngEncoder}, imageops::{self, FilterType}, DynamicImage, ExtendedColorType, ImageEncoder, ImageError, Limits}; use ratatui::layout::Rect; use yazi_config::{PREVIEW, TASKS}; @@ -33,23 +33,20 @@ impl Image { let mut buf = Vec::new(); img = Self::rotate(img, orientation); - match img { - DynamicImage::ImageRgb8(_) => { - JpegEncoder::new_with_quality(&mut buf, PREVIEW.image_quality).encode_image(&img)?; - } - _ => { - let (width, height) = (img.width(), img.height()); - let img_rgba8 = img.into_rgba8(); // Consume `img` here - PngEncoder::new(&mut buf).write_image( - &img_rgba8, - width, - height, - image::ExtendedColorType::Rgba8, - )?; - } + if img.color().has_alpha() { + let rgba = img.into_rgba8(); + PngEncoder::new(&mut buf).write_image( + &rgba, + rgba.width(), + rgba.height(), + ExtendedColorType::Rgba8, + )?; + } else { + JpegEncoder::new_with_quality(&mut buf, PREVIEW.image_quality) + .encode_image(&img.into_rgb8())?; } - Ok::<_, anyhow::Error>(buf) + Ok::<_, ImageError>(buf) }) .await??; @@ -145,7 +142,7 @@ impl Image { // https://magnushoff.com/articles/jpeg-orientation/ fn rotate(mut img: DynamicImage, orientation: u8) -> DynamicImage { - let rgba = img.color().has_alpha(); + let alpha = img.color().has_alpha(); img = match orientation { 2 => DynamicImage::ImageRgba8(imageops::flip_horizontal(&img)), 3 => DynamicImage::ImageRgba8(imageops::rotate180(&img)), @@ -156,7 +153,7 @@ impl Image { 8 => DynamicImage::ImageRgba8(imageops::rotate270(&img)), _ => img, }; - if !rgba { + if !alpha { img = DynamicImage::ImageRgb8(img.into_rgb8()); } img diff --git a/yazi-adapter/src/kitty.rs b/yazi-adapter/src/kitty.rs index b75bbb070..2c90ede48 100644 --- a/yazi-adapter/src/kitty.rs +++ b/yazi-adapter/src/kitty.rs @@ -383,7 +383,7 @@ impl Kitty { tokio::task::spawn_blocking(move || match img { DynamicImage::ImageRgb8(v) => output(v.as_raw(), 24, size), DynamicImage::ImageRgba8(v) => output(v.as_raw(), 32, size), - v => output(v.to_rgb8().as_raw(), 24, size), + v => output(v.into_rgb8().as_raw(), 24, size), }) .await? } diff --git a/yazi-adapter/src/kitty_old.rs b/yazi-adapter/src/kitty_old.rs index 0de07533e..1054707f0 100644 --- a/yazi-adapter/src/kitty_old.rs +++ b/yazi-adapter/src/kitty_old.rs @@ -74,7 +74,7 @@ impl KittyOld { tokio::task::spawn_blocking(move || match img { DynamicImage::ImageRgb8(v) => output(v.as_raw(), 24, size), DynamicImage::ImageRgba8(v) => output(v.as_raw(), 32, size), - v => output(v.to_rgb8().as_raw(), 24, size), + v => output(v.into_rgb8().as_raw(), 24, size), }) .await? } diff --git a/yazi-adapter/src/lib.rs b/yazi-adapter/src/lib.rs index a2e33bb3f..5987a8390 100644 --- a/yazi-adapter/src/lib.rs +++ b/yazi-adapter/src/lib.rs @@ -4,8 +4,8 @@ mod adapter; mod chafa; mod dimension; mod emulator; +mod iip; mod image; -mod iterm2; mod kitty; mod kitty_old; mod sixel; @@ -15,7 +15,7 @@ pub use adapter::*; use chafa::*; pub use dimension::*; pub use emulator::*; -use iterm2::*; +use iip::*; use kitty::*; use kitty_old::*; use sixel::*;