From 2df1dae5af6dde80c5e6cb7622980c38fbcfdaca Mon Sep 17 00:00:00 2001 From: Sinkerine <15cm.github@15cm.net> Date: Sun, 22 Oct 2023 23:46:03 -0700 Subject: [PATCH] feat: add config support for coordinate and size adjustment for ueberzugpp image preview (#304) --- yazi-adaptor/Cargo.toml | 2 +- yazi-adaptor/src/ueberzug.rs | 17 +++++++++++++++++ yazi-config/preset/yazi.toml | 10 ++++++---- yazi-config/src/preview/preview.rs | 9 +++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/yazi-adaptor/Cargo.toml b/yazi-adaptor/Cargo.toml index 5e6b9c28..46d9057d 100644 --- a/yazi-adaptor/Cargo.toml +++ b/yazi-adaptor/Cargo.toml @@ -21,4 +21,4 @@ ratatui = "^0" tokio = { version = "^1", features = [ "parking_lot", "io-util", "process" ] } # Logging -tracing = "^0" +tracing = { version = "^0", features = [ "max_level_debug", "release_max_level_warn" ] } diff --git a/yazi-adaptor/src/ueberzug.rs b/yazi-adaptor/src/ueberzug.rs index 27d891b6..15e90c4f 100644 --- a/yazi-adaptor/src/ueberzug.rs +++ b/yazi-adaptor/src/ueberzug.rs @@ -3,6 +3,8 @@ use std::{path::PathBuf, process::Stdio}; use anyhow::Result; use ratatui::prelude::Rect; use tokio::{io::AsyncWriteExt, process::{Child, Command}, sync::mpsc::{self, UnboundedSender}}; +use tracing::debug; +use yazi_config::PREVIEW; use crate::Adaptor; @@ -42,9 +44,24 @@ impl Ueberzug { ) } + fn adjust_rect(mut rect: Rect) -> Rect { + let scale = PREVIEW.ueberzug_scale; + let (x, y, w, h) = PREVIEW.ueberzug_offset; + + rect.x = 0f64.max(rect.x as f64 * scale + x) as u16; + rect.y = 0f64.max(rect.y as f64 * scale + y) as u16; + rect.width = 0f64.max(rect.width as f64 * scale + w) as u16; + rect.height = 0f64.max(rect.height as f64 * scale + h) as u16; + rect + } + async fn send_command(child: &mut Child, cmd: Option<(PathBuf, Rect)>) -> Result<()> { let stdin = child.stdin.as_mut().unwrap(); if let Some((path, rect)) = cmd { + debug!("ueberzug rect before adjustment: {:?}", rect); + let rect = Self::adjust_rect(rect); + debug!("ueberzug rect after adjustment: {:?}", rect); + let s = format!( r#"{{"action":"add","identifier":"yazi","x":{},"y":{},"max_width":{},"max_height":{},"path":"{}"}}{}"#, rect.x, diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 0fa22775..c234909b 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -9,10 +9,12 @@ show_hidden = false show_symlink = true [preview] -tab_size = 2 -max_width = 600 -max_height = 900 -cache_dir = "" +tab_size = 2 +max_width = 600 +max_height = 900 +cache_dir = "" +ueberzug_scale = 1 +ueberzug_offset = [ 0, 0, 0, 0 ] [opener] edit = [ diff --git a/yazi-config/src/preview/preview.rs b/yazi-config/src/preview/preview.rs index 4484f1b0..ca5d2287 100644 --- a/yazi-config/src/preview/preview.rs +++ b/yazi-config/src/preview/preview.rs @@ -13,6 +13,9 @@ pub struct Preview { pub max_height: u32, pub cache_dir: PathBuf, + + pub ueberzug_scale: f64, + pub ueberzug_offset: (f64, f64, f64, f64), } impl Default for Preview { @@ -28,6 +31,9 @@ impl Default for Preview { max_height: u32, cache_dir: Option, + + ueberzug_scale: f64, + ueberzug_offset: (f64, f64, f64, f64), } let preview = toml::from_str::(&MERGED_YAZI).unwrap().preview; @@ -41,6 +47,9 @@ impl Default for Preview { max_height: preview.max_height, cache_dir, + + ueberzug_scale: preview.ueberzug_scale, + ueberzug_offset: preview.ueberzug_offset, } } }