diff --git a/yazi-adapter/src/adapter.rs b/yazi-adapter/src/adapter.rs index 7f281234..1de41e75 100644 --- a/yazi-adapter/src/adapter.rs +++ b/yazi-adapter/src/adapter.rs @@ -5,8 +5,7 @@ use ratatui::layout::Rect; use tracing::warn; use yazi_shared::env_exists; -use super::{Iip, Kgp, KgpOld}; -use crate::{Chafa, Emulator, SHOWN, Sixel, TMUX, Ueberzug, WSL}; +use crate::{Emulator, SHOWN, TMUX, WSL, drivers}; #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Adapter { @@ -42,12 +41,12 @@ impl Adapter { } match self { - Self::Kgp => Kgp::image_show(path, max).await, - Self::KgpOld => KgpOld::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, + Self::Kgp => drivers::Kgp::image_show(path, max).await, + Self::KgpOld => drivers::KgpOld::image_show(path, max).await, + Self::Iip => drivers::Iip::image_show(path, max).await, + Self::Sixel => drivers::Sixel::image_show(path, max).await, + Self::X11 | Self::Wayland => drivers::Ueberzug::image_show(path, max).await, + Self::Chafa => drivers::Chafa::image_show(path, max).await, } } @@ -57,12 +56,12 @@ impl Adapter { pub fn image_erase(self, area: Rect) -> Result<()> { match self { - Self::Kgp => Kgp::image_erase(area), - Self::KgpOld => KgpOld::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), + Self::Kgp => drivers::Kgp::image_erase(area), + Self::KgpOld => drivers::KgpOld::image_erase(area), + Self::Iip => drivers::Iip::image_erase(area), + Self::Sixel => drivers::Sixel::image_erase(area), + Self::X11 | Self::Wayland => drivers::Ueberzug::image_erase(area), + Self::Chafa => drivers::Chafa::image_erase(area), } } @@ -72,7 +71,7 @@ impl Adapter { #[inline] pub(super) fn shown_store(area: Rect) { SHOWN.set(Some(area)); } - pub(super) fn start(self) { Ueberzug::start(self); } + pub(super) fn start(self) { drivers::Ueberzug::start(self); } #[inline] pub(super) fn needs_ueberzug(self) -> bool { @@ -101,7 +100,7 @@ impl Adapter { return *p; } - let supported_compositor = Ueberzug::supported_compositor(); + let supported_compositor = drivers::Ueberzug::supported_compositor(); match env::var("XDG_SESSION_TYPE").unwrap_or_default().as_str() { "x11" => return Self::X11, "wayland" if supported_compositor => return Self::Wayland, diff --git a/yazi-adapter/src/chafa.rs b/yazi-adapter/src/drivers/chafa.rs similarity index 91% rename from yazi-adapter/src/chafa.rs rename to yazi-adapter/src/drivers/chafa.rs index 5fe5570f..63bc9ff2 100644 --- a/yazi-adapter/src/chafa.rs +++ b/yazi-adapter/src/drivers/chafa.rs @@ -8,10 +8,10 @@ use tokio::process::Command; use crate::{Adapter, Emulator}; -pub(super) struct Chafa; +pub(crate) struct Chafa; impl Chafa { - pub(super) async fn image_show(path: &Path, max: Rect) -> Result { + pub(crate) async fn image_show(path: &Path, max: Rect) -> Result { let output = Command::new("chafa") .args([ "-f", @@ -64,7 +64,7 @@ impl Chafa { }) } - pub(super) fn image_erase(area: Rect) -> Result<()> { + pub(crate) fn image_erase(area: Rect) -> Result<()> { let s = " ".repeat(area.width as usize); Emulator::move_lock((0, 0), |stderr| { for y in area.top()..area.bottom() { diff --git a/yazi-adapter/src/iip.rs b/yazi-adapter/src/drivers/iip.rs similarity index 88% rename from yazi-adapter/src/iip.rs rename to yazi-adapter/src/drivers/iip.rs index fe039d90..406570ac 100644 --- a/yazi-adapter/src/iip.rs +++ b/yazi-adapter/src/drivers/iip.rs @@ -7,13 +7,12 @@ use image::{DynamicImage, ExtendedColorType, ImageEncoder, codecs::{jpeg::JpegEn use ratatui::layout::Rect; use yazi_config::PREVIEW; -use super::image::Image; -use crate::{CLOSE, Emulator, START, adapter::Adapter}; +use crate::{CLOSE, Emulator, Image, START, adapter::Adapter}; -pub(super) struct Iip; +pub(crate) struct Iip; impl Iip { - pub(super) async fn image_show(path: &Path, max: Rect) -> Result { + pub(crate) 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?; @@ -26,7 +25,7 @@ impl Iip { }) } - pub(super) fn image_erase(area: Rect) -> Result<()> { + pub(crate) fn image_erase(area: Rect) -> Result<()> { let s = " ".repeat(area.width as usize); Emulator::move_lock((0, 0), |stderr| { for y in area.top()..area.bottom() { diff --git a/yazi-adapter/src/kgp.rs b/yazi-adapter/src/drivers/kgp.rs similarity index 96% rename from yazi-adapter/src/kgp.rs rename to yazi-adapter/src/drivers/kgp.rs index e6aa7e58..cbae32b8 100644 --- a/yazi-adapter/src/kgp.rs +++ b/yazi-adapter/src/drivers/kgp.rs @@ -7,8 +7,7 @@ use crossterm::{cursor::MoveTo, queue}; use image::DynamicImage; use ratatui::layout::Rect; -use super::image::Image; -use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter}; +use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter, image::Image}; static DIACRITICS: [char; 297] = [ '\u{0305}', @@ -310,10 +309,10 @@ static DIACRITICS: [char; 297] = [ '\u{1D244}', ]; -pub(super) struct Kgp; +pub(crate) struct Kgp; impl Kgp { - pub(super) async fn image_show(path: &Path, max: Rect) -> Result { + pub(crate) 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); @@ -329,7 +328,7 @@ impl Kgp { }) } - pub(super) fn image_erase(area: Rect) -> Result<()> { + pub(crate) fn image_erase(area: Rect) -> Result<()> { let s = " ".repeat(area.width as usize); Emulator::move_lock((0, 0), |stderr| { for y in area.top()..area.bottom() { diff --git a/yazi-adapter/src/kgp_old.rs b/yazi-adapter/src/drivers/kgp_old.rs similarity index 88% rename from yazi-adapter/src/kgp_old.rs rename to yazi-adapter/src/drivers/kgp_old.rs index 3d483005..86c88159 100644 --- a/yazi-adapter/src/kgp_old.rs +++ b/yazi-adapter/src/drivers/kgp_old.rs @@ -6,13 +6,12 @@ use base64::{Engine, engine::general_purpose}; use image::DynamicImage; use ratatui::layout::Rect; -use super::image::Image; -use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter}; +use crate::{CLOSE, ESCAPE, Emulator, Image, START, adapter::Adapter}; -pub(super) struct KgpOld; +pub(crate) struct KgpOld; impl KgpOld { - pub(super) async fn image_show(path: &Path, max: Rect) -> Result { + pub(crate) 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?; @@ -26,7 +25,7 @@ impl KgpOld { } #[inline] - pub(super) fn image_erase(_: Rect) -> Result<()> { + pub(crate) fn image_erase(_: Rect) -> Result<()> { let mut stderr = LineWriter::new(stderr()); write!(stderr, "{}_Gq=2,a=d,d=A{}\\{}", START, ESCAPE, CLOSE)?; stderr.flush()?; diff --git a/yazi-adapter/src/drivers/mod.rs b/yazi-adapter/src/drivers/mod.rs new file mode 100644 index 00000000..57eeaf4f --- /dev/null +++ b/yazi-adapter/src/drivers/mod.rs @@ -0,0 +1 @@ +yazi_macro::mod_flat!(chafa iip kgp kgp_old sixel ueberzug); diff --git a/yazi-adapter/src/sixel.rs b/yazi-adapter/src/drivers/sixel.rs similarity index 94% rename from yazi-adapter/src/sixel.rs rename to yazi-adapter/src/drivers/sixel.rs index 06a551c2..859fea38 100644 --- a/yazi-adapter/src/sixel.rs +++ b/yazi-adapter/src/drivers/sixel.rs @@ -9,10 +9,10 @@ use yazi_config::PREVIEW; use crate::{CLOSE, ESCAPE, Emulator, Image, START, adapter::Adapter}; -pub(super) struct Sixel; +pub(crate) struct Sixel; impl Sixel { - pub(super) async fn image_show(path: &Path, max: Rect) -> Result { + pub(crate) 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?; @@ -25,7 +25,7 @@ impl Sixel { }) } - pub(super) fn image_erase(area: Rect) -> Result<()> { + pub(crate) fn image_erase(area: Rect) -> Result<()> { let s = " ".repeat(area.width as usize); Emulator::move_lock((0, 0), |stderr| { for y in area.top()..area.bottom() { diff --git a/yazi-adapter/src/ueberzug.rs b/yazi-adapter/src/drivers/ueberzug.rs similarity index 93% rename from yazi-adapter/src/ueberzug.rs rename to yazi-adapter/src/drivers/ueberzug.rs index d04b390a..7620af00 100644 --- a/yazi-adapter/src/ueberzug.rs +++ b/yazi-adapter/src/drivers/ueberzug.rs @@ -13,10 +13,10 @@ type Cmd = Option<(PathBuf, Rect)>; static DEMON: RoCell>> = RoCell::new(); -pub(super) struct Ueberzug; +pub(crate) struct Ueberzug; impl Ueberzug { - pub(super) fn start(adapter: Adapter) { + pub(crate) fn start(adapter: Adapter) { if !adapter.needs_ueberzug() { return DEMON.init(None); } @@ -41,7 +41,7 @@ impl Ueberzug { DEMON.init(Some(tx)) } - pub(super) async fn image_show(path: &Path, max: Rect) -> Result { + pub(crate) async fn image_show(path: &Path, max: Rect) -> Result { let Some(tx) = &*DEMON else { bail!("uninitialized ueberzugpp"); }; @@ -63,7 +63,7 @@ impl Ueberzug { Ok(area) } - pub(super) fn image_erase(_: Rect) -> Result<()> { + pub(crate) fn image_erase(_: Rect) -> Result<()> { if let Some(tx) = &*DEMON { Ok(tx.send(None)?) } else { @@ -74,7 +74,7 @@ impl Ueberzug { // Currently Überzug++'s Wayland output only supports Sway, Hyprland and Wayfire // as it requires information from specific compositor socket directly. // These environment variables are from ueberzugpp src/canvas/wayland/config.cpp - pub(super) fn supported_compositor() -> bool { + pub(crate) fn supported_compositor() -> bool { env_exists("SWAYSOCK") || env_exists("HYPRLAND_INSTANCE_SIGNATURE") || env_exists("WAYFIRE_SOCKET") diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index cae2e53a..4c23460f 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -117,7 +117,9 @@ impl Emulator { execute!( LineWriter::new(stderr()), SavePosition, - Print(Mux::csi("\x1b[>q\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c")), + Print(Mux::csi("\x1b[>q")), // Request terminal version + Print(Mux::csi("\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\")), // Detect KGP + Print(Mux::csi("\x1b[c")), // Request device attributes RestorePosition )?; diff --git a/yazi-adapter/src/lib.rs b/yazi-adapter/src/lib.rs index 3e263e3e..8e341ed4 100644 --- a/yazi-adapter/src/lib.rs +++ b/yazi-adapter/src/lib.rs @@ -1,8 +1,8 @@ #![allow(clippy::unit_arg)] -yazi_macro::mod_flat!( - adapter chafa dimension emulator iip image info kgp kgp_old mux sixel ueberzug -); +yazi_macro::mod_pub!(drivers); + +yazi_macro::mod_flat!(adapter dimension emulator image info mux); use yazi_shared::{RoCell, SyncCell, env_exists, in_wsl}; pub static ADAPTOR: RoCell = RoCell::new();