This commit is contained in:
sxyazi 2024-11-24 19:57:59 +08:00
parent 1de1c606e1
commit 6ff64cd33a
No known key found for this signature in database
10 changed files with 45 additions and 46 deletions

View file

@ -5,8 +5,7 @@ use ratatui::layout::Rect;
use tracing::warn; use tracing::warn;
use yazi_shared::env_exists; use yazi_shared::env_exists;
use super::{Iip, Kgp, KgpOld}; use crate::{Emulator, SHOWN, TMUX, WSL, drivers};
use crate::{Chafa, Emulator, SHOWN, Sixel, TMUX, Ueberzug, WSL};
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Adapter { pub enum Adapter {
@ -42,12 +41,12 @@ impl Adapter {
} }
match self { match self {
Self::Kgp => Kgp::image_show(path, max).await, Self::Kgp => drivers::Kgp::image_show(path, max).await,
Self::KgpOld => KgpOld::image_show(path, max).await, Self::KgpOld => drivers::KgpOld::image_show(path, max).await,
Self::Iip => Iip::image_show(path, max).await, Self::Iip => drivers::Iip::image_show(path, max).await,
Self::Sixel => Sixel::image_show(path, max).await, Self::Sixel => drivers::Sixel::image_show(path, max).await,
Self::X11 | Self::Wayland => Ueberzug::image_show(path, max).await, Self::X11 | Self::Wayland => drivers::Ueberzug::image_show(path, max).await,
Self::Chafa => Chafa::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<()> { pub fn image_erase(self, area: Rect) -> Result<()> {
match self { match self {
Self::Kgp => Kgp::image_erase(area), Self::Kgp => drivers::Kgp::image_erase(area),
Self::KgpOld => KgpOld::image_erase(area), Self::KgpOld => drivers::KgpOld::image_erase(area),
Self::Iip => Iip::image_erase(area), Self::Iip => drivers::Iip::image_erase(area),
Self::Sixel => Sixel::image_erase(area), Self::Sixel => drivers::Sixel::image_erase(area),
Self::X11 | Self::Wayland => Ueberzug::image_erase(area), Self::X11 | Self::Wayland => drivers::Ueberzug::image_erase(area),
Self::Chafa => Chafa::image_erase(area), Self::Chafa => drivers::Chafa::image_erase(area),
} }
} }
@ -72,7 +71,7 @@ impl Adapter {
#[inline] #[inline]
pub(super) fn shown_store(area: Rect) { SHOWN.set(Some(area)); } 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] #[inline]
pub(super) fn needs_ueberzug(self) -> bool { pub(super) fn needs_ueberzug(self) -> bool {
@ -101,7 +100,7 @@ impl Adapter {
return *p; 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() { match env::var("XDG_SESSION_TYPE").unwrap_or_default().as_str() {
"x11" => return Self::X11, "x11" => return Self::X11,
"wayland" if supported_compositor => return Self::Wayland, "wayland" if supported_compositor => return Self::Wayland,

View file

@ -8,10 +8,10 @@ use tokio::process::Command;
use crate::{Adapter, Emulator}; use crate::{Adapter, Emulator};
pub(super) struct Chafa; pub(crate) struct Chafa;
impl Chafa { impl Chafa {
pub(super) async fn image_show(path: &Path, max: Rect) -> Result<Rect> { pub(crate) async fn image_show(path: &Path, max: Rect) -> Result<Rect> {
let output = Command::new("chafa") let output = Command::new("chafa")
.args([ .args([
"-f", "-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); let s = " ".repeat(area.width as usize);
Emulator::move_lock((0, 0), |stderr| { Emulator::move_lock((0, 0), |stderr| {
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {

View file

@ -7,13 +7,12 @@ use image::{DynamicImage, ExtendedColorType, ImageEncoder, codecs::{jpeg::JpegEn
use ratatui::layout::Rect; use ratatui::layout::Rect;
use yazi_config::PREVIEW; use yazi_config::PREVIEW;
use super::image::Image; use crate::{CLOSE, Emulator, Image, START, adapter::Adapter};
use crate::{CLOSE, Emulator, START, adapter::Adapter};
pub(super) struct Iip; pub(crate) struct Iip;
impl Iip { impl Iip {
pub(super) async fn image_show(path: &Path, max: Rect) -> Result<Rect> { pub(crate) async fn image_show(path: &Path, max: Rect) -> Result<Rect> {
let img = Image::downscale(path, max).await?; let img = Image::downscale(path, max).await?;
let area = Image::pixel_area((img.width(), img.height()), max); let area = Image::pixel_area((img.width(), img.height()), max);
let b = Self::encode(img).await?; 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); let s = " ".repeat(area.width as usize);
Emulator::move_lock((0, 0), |stderr| { Emulator::move_lock((0, 0), |stderr| {
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {

View file

@ -7,8 +7,7 @@ use crossterm::{cursor::MoveTo, queue};
use image::DynamicImage; use image::DynamicImage;
use ratatui::layout::Rect; use ratatui::layout::Rect;
use super::image::Image; use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter, image::Image};
use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter};
static DIACRITICS: [char; 297] = [ static DIACRITICS: [char; 297] = [
'\u{0305}', '\u{0305}',
@ -310,10 +309,10 @@ static DIACRITICS: [char; 297] = [
'\u{1D244}', '\u{1D244}',
]; ];
pub(super) struct Kgp; pub(crate) struct Kgp;
impl Kgp { impl Kgp {
pub(super) async fn image_show(path: &Path, max: Rect) -> Result<Rect> { pub(crate) async fn image_show(path: &Path, max: Rect) -> Result<Rect> {
let img = Image::downscale(path, max).await?; let img = Image::downscale(path, max).await?;
let area = Image::pixel_area((img.width(), img.height()), max); 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); let s = " ".repeat(area.width as usize);
Emulator::move_lock((0, 0), |stderr| { Emulator::move_lock((0, 0), |stderr| {
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {

View file

@ -6,13 +6,12 @@ use base64::{Engine, engine::general_purpose};
use image::DynamicImage; use image::DynamicImage;
use ratatui::layout::Rect; use ratatui::layout::Rect;
use super::image::Image; use crate::{CLOSE, ESCAPE, Emulator, Image, START, adapter::Adapter};
use crate::{CLOSE, ESCAPE, Emulator, START, adapter::Adapter};
pub(super) struct KgpOld; pub(crate) struct KgpOld;
impl KgpOld { impl KgpOld {
pub(super) async fn image_show(path: &Path, max: Rect) -> Result<Rect> { pub(crate) async fn image_show(path: &Path, max: Rect) -> Result<Rect> {
let img = Image::downscale(path, max).await?; let img = Image::downscale(path, max).await?;
let area = Image::pixel_area((img.width(), img.height()), max); let area = Image::pixel_area((img.width(), img.height()), max);
let b = Self::encode(img).await?; let b = Self::encode(img).await?;
@ -26,7 +25,7 @@ impl KgpOld {
} }
#[inline] #[inline]
pub(super) fn image_erase(_: Rect) -> Result<()> { pub(crate) fn image_erase(_: Rect) -> Result<()> {
let mut stderr = LineWriter::new(stderr()); let mut stderr = LineWriter::new(stderr());
write!(stderr, "{}_Gq=2,a=d,d=A{}\\{}", START, ESCAPE, CLOSE)?; write!(stderr, "{}_Gq=2,a=d,d=A{}\\{}", START, ESCAPE, CLOSE)?;
stderr.flush()?; stderr.flush()?;

View file

@ -0,0 +1 @@
yazi_macro::mod_flat!(chafa iip kgp kgp_old sixel ueberzug);

View file

@ -9,10 +9,10 @@ use yazi_config::PREVIEW;
use crate::{CLOSE, ESCAPE, Emulator, Image, START, adapter::Adapter}; use crate::{CLOSE, ESCAPE, Emulator, Image, START, adapter::Adapter};
pub(super) struct Sixel; pub(crate) struct Sixel;
impl Sixel { impl Sixel {
pub(super) async fn image_show(path: &Path, max: Rect) -> Result<Rect> { pub(crate) async fn image_show(path: &Path, max: Rect) -> Result<Rect> {
let img = Image::downscale(path, max).await?; let img = Image::downscale(path, max).await?;
let area = Image::pixel_area((img.width(), img.height()), max); let area = Image::pixel_area((img.width(), img.height()), max);
let b = Self::encode(img).await?; 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); let s = " ".repeat(area.width as usize);
Emulator::move_lock((0, 0), |stderr| { Emulator::move_lock((0, 0), |stderr| {
for y in area.top()..area.bottom() { for y in area.top()..area.bottom() {

View file

@ -13,10 +13,10 @@ type Cmd = Option<(PathBuf, Rect)>;
static DEMON: RoCell<Option<UnboundedSender<Cmd>>> = RoCell::new(); static DEMON: RoCell<Option<UnboundedSender<Cmd>>> = RoCell::new();
pub(super) struct Ueberzug; pub(crate) struct Ueberzug;
impl Ueberzug { impl Ueberzug {
pub(super) fn start(adapter: Adapter) { pub(crate) fn start(adapter: Adapter) {
if !adapter.needs_ueberzug() { if !adapter.needs_ueberzug() {
return DEMON.init(None); return DEMON.init(None);
} }
@ -41,7 +41,7 @@ impl Ueberzug {
DEMON.init(Some(tx)) DEMON.init(Some(tx))
} }
pub(super) async fn image_show(path: &Path, max: Rect) -> Result<Rect> { pub(crate) async fn image_show(path: &Path, max: Rect) -> Result<Rect> {
let Some(tx) = &*DEMON else { let Some(tx) = &*DEMON else {
bail!("uninitialized ueberzugpp"); bail!("uninitialized ueberzugpp");
}; };
@ -63,7 +63,7 @@ impl Ueberzug {
Ok(area) Ok(area)
} }
pub(super) fn image_erase(_: Rect) -> Result<()> { pub(crate) fn image_erase(_: Rect) -> Result<()> {
if let Some(tx) = &*DEMON { if let Some(tx) = &*DEMON {
Ok(tx.send(None)?) Ok(tx.send(None)?)
} else { } else {
@ -74,7 +74,7 @@ impl Ueberzug {
// Currently Überzug++'s Wayland output only supports Sway, Hyprland and Wayfire // Currently Überzug++'s Wayland output only supports Sway, Hyprland and Wayfire
// as it requires information from specific compositor socket directly. // as it requires information from specific compositor socket directly.
// These environment variables are from ueberzugpp src/canvas/wayland/config.cpp // 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("SWAYSOCK")
|| env_exists("HYPRLAND_INSTANCE_SIGNATURE") || env_exists("HYPRLAND_INSTANCE_SIGNATURE")
|| env_exists("WAYFIRE_SOCKET") || env_exists("WAYFIRE_SOCKET")

View file

@ -117,7 +117,9 @@ impl Emulator {
execute!( execute!(
LineWriter::new(stderr()), LineWriter::new(stderr()),
SavePosition, 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 RestorePosition
)?; )?;

View file

@ -1,8 +1,8 @@
#![allow(clippy::unit_arg)] #![allow(clippy::unit_arg)]
yazi_macro::mod_flat!( yazi_macro::mod_pub!(drivers);
adapter chafa dimension emulator iip image info kgp kgp_old mux sixel ueberzug
); yazi_macro::mod_flat!(adapter dimension emulator image info mux);
use yazi_shared::{RoCell, SyncCell, env_exists, in_wsl}; use yazi_shared::{RoCell, SyncCell, env_exists, in_wsl};
pub static ADAPTOR: RoCell<Adapter> = RoCell::new(); pub static ADAPTOR: RoCell<Adapter> = RoCell::new();