This commit is contained in:
sxyazi 2023-07-28 11:09:47 +08:00
parent 151f91f58e
commit 5d76177f32
No known key found for this signature in database
11 changed files with 32 additions and 32 deletions

View file

@ -4,7 +4,7 @@ sort_reverse = true
show_hidden = false show_hidden = false
[preview] [preview]
adapter = "kitty" adaptor = "kitty"
tab_size = 2 tab_size = 2
max_width = 600 max_width = 600
max_height = 900 max_height = 900

View file

@ -3,19 +3,19 @@ use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq, Eq)] #[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(try_from = "String")] #[serde(try_from = "String")]
pub enum PreviewAdapter { pub enum PreviewAdaptor {
Kitty, Kitty,
Ueberzug, Ueberzug,
} }
impl TryFrom<String> for PreviewAdapter { impl TryFrom<String> for PreviewAdaptor {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(value: String) -> Result<Self, Self::Error> { fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(match value.to_lowercase().as_str() { Ok(match value.to_lowercase().as_str() {
"kitty" => Self::Kitty, "kitty" => Self::Kitty,
"ueberzug" => Self::Ueberzug, "ueberzug" => Self::Ueberzug,
_ => bail!("invalid preview adapter: {}", value), _ => bail!("invalid preview adaptor: {}", value),
}) })
} }
} }

View file

@ -1,5 +1,5 @@
mod adapter; mod adaptor;
mod preview; mod preview;
pub use adapter::*; pub use adaptor::*;
pub use preview::*; pub use preview::*;

View file

@ -1,11 +1,11 @@
use serde::Deserialize; use serde::Deserialize;
use super::PreviewAdapter; use super::PreviewAdaptor;
use crate::config::MERGED_YAZI; use crate::config::MERGED_YAZI;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Preview { pub struct Preview {
pub adapter: PreviewAdapter, pub adaptor: PreviewAdaptor,
pub tab_size: u32, pub tab_size: u32,
pub max_width: u32, pub max_width: u32,

View file

@ -1,5 +0,0 @@
mod adapter;
mod kitty;
mod ueberzug;
pub use adapter::*;

View file

@ -5,27 +5,27 @@ use ratatui::prelude::Rect;
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use super::{kitty::Kitty, ueberzug::Ueberzug}; use super::{kitty::Kitty, ueberzug::Ueberzug};
use crate::config::{preview::PreviewAdapter, PREVIEW}; use crate::config::{preview::PreviewAdaptor, PREVIEW};
pub struct Adapter { pub struct Adaptor {
ueberzug: Option<UnboundedSender<Option<(PathBuf, Rect)>>>, ueberzug: Option<UnboundedSender<Option<(PathBuf, Rect)>>>,
} }
impl Adapter { impl Adaptor {
pub fn new() -> Self { pub fn new() -> Self {
let mut adapter = Self { ueberzug: None }; let mut adaptor = Self { ueberzug: None };
if PREVIEW.adapter == PreviewAdapter::Ueberzug { if PREVIEW.adaptor == PreviewAdaptor::Ueberzug {
adapter.ueberzug = Ueberzug::init().ok(); adaptor.ueberzug = Ueberzug::init().ok();
} }
adapter adaptor
} }
pub async fn image_show(&self, path: &Path, rect: Rect) -> Result<()> { pub async fn image_show(&self, path: &Path, rect: Rect) -> Result<()> {
match PREVIEW.adapter { match PREVIEW.adaptor {
PreviewAdapter::Kitty => Kitty::image_show(path, rect).await, PreviewAdaptor::Kitty => Kitty::image_show(path, rect).await,
PreviewAdapter::Ueberzug => { PreviewAdaptor::Ueberzug => {
if let Some(tx) = &self.ueberzug { if let Some(tx) = &self.ueberzug {
tx.send(Some((path.to_path_buf(), rect))).ok(); tx.send(Some((path.to_path_buf(), rect))).ok();
} }
@ -35,9 +35,9 @@ impl Adapter {
} }
pub fn image_hide(&self) { pub fn image_hide(&self) {
match PREVIEW.adapter { match PREVIEW.adaptor {
PreviewAdapter::Kitty => Kitty::image_hide(), PreviewAdaptor::Kitty => Kitty::image_hide(),
PreviewAdapter::Ueberzug => { PreviewAdaptor::Ueberzug => {
if let Some(tx) = &self.ueberzug { if let Some(tx) = &self.ueberzug {
tx.send(None).ok(); tx.send(None).ok();
} }

5
src/core/adaptor/mod.rs Normal file
View file

@ -0,0 +1,5 @@
mod adaptor;
mod kitty;
mod ueberzug;
pub use adaptor::*;

View file

@ -6,7 +6,7 @@ use syntect::{easy::HighlightFile, highlighting::{Theme, ThemeSet}, parsing::Syn
use tokio::{fs, task::JoinHandle}; use tokio::{fs, task::JoinHandle};
use super::{ALL_RATIO, PREVIEW_BORDER, PREVIEW_PADDING, PREVIEW_RATIO}; use super::{ALL_RATIO, PREVIEW_BORDER, PREVIEW_PADDING, PREVIEW_RATIO};
use crate::{config::{PREVIEW, THEME}, core::{adapter::Adapter, external, files::{Files, FilesOp}, tasks::Precache}, emit, misc::{tty_size, MimeKind}}; use crate::{config::{PREVIEW, THEME}, core::{adaptor::Adaptor, external, files::{Files, FilesOp}, tasks::Precache}, emit, misc::{tty_size, MimeKind}};
static SYNTECT_SYNTAX: OnceLock<SyntaxSet> = OnceLock::new(); static SYNTECT_SYNTAX: OnceLock<SyntaxSet> = OnceLock::new();
static SYNTECT_THEME: OnceLock<Theme> = OnceLock::new(); static SYNTECT_THEME: OnceLock<Theme> = OnceLock::new();
@ -16,7 +16,7 @@ pub struct Preview {
pub data: PreviewData, pub data: PreviewData,
handle: Option<JoinHandle<()>>, handle: Option<JoinHandle<()>>,
adaptor: Arc<Adapter>, adaptor: Arc<Adaptor>,
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -33,7 +33,7 @@ impl Preview {
path: Default::default(), path: Default::default(),
data: Default::default(), data: Default::default(),
handle: Default::default(), handle: Default::default(),
adaptor: Arc::new(Adapter::new()), adaptor: Arc::new(Adaptor::new()),
} }
} }
@ -84,7 +84,7 @@ impl Preview {
Ok(PreviewData::Folder) Ok(PreviewData::Folder)
} }
pub async fn image(adaptor: Arc<Adapter>, mut path: &Path) -> Result<PreviewData> { pub async fn image(adaptor: Arc<Adaptor>, mut path: &Path) -> Result<PreviewData> {
let cache = Precache::cache(path); let cache = Precache::cache(path);
if fs::metadata(&cache).await.is_ok() { if fs::metadata(&cache).await.is_ok() {
path = cache.as_path(); path = cache.as_path();
@ -100,7 +100,7 @@ impl Preview {
Ok(PreviewData::None) Ok(PreviewData::None)
} }
pub async fn video(adaptor: Arc<Adapter>, path: &Path) -> Result<PreviewData> { pub async fn video(adaptor: Arc<Adaptor>, path: &Path) -> Result<PreviewData> {
let cache = Precache::cache(path); let cache = Precache::cache(path);
if fs::metadata(&cache).await.is_err() { if fs::metadata(&cache).await.is_err() {
external::ffmpegthumbnailer(path, &cache).await?; external::ffmpegthumbnailer(path, &cache).await?;

View file

@ -1,4 +1,4 @@
pub mod adapter; pub mod adaptor;
mod blocker; mod blocker;
mod event; mod event;
pub mod external; pub mod external;