From a7e8567e81c8c39d1d6a6f866be00d8d15aa4bb2 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 26 Aug 2023 06:38:58 +0800 Subject: [PATCH] .. --- config/src/boot/boot.rs | 12 ++++++++- core/src/external/ffmpegthumbnailer.rs | 8 +++--- core/src/external/jq.rs | 6 ++--- core/src/external/lsar.rs | 6 ++--- core/src/external/pdftoppm.rs | 6 ++--- core/src/manager/preview/preview.rs | 5 ++-- core/src/manager/preview/provider.rs | 32 ++++++++++++------------ core/src/manager/tab.rs | 4 +-- shared/src/errors/mod.rs | 3 +++ shared/src/{errors.rs => errors/peek.rs} | 16 ++++++------ 10 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 shared/src/errors/mod.rs rename shared/src/{errors.rs => errors/peek.rs} (70%) diff --git a/config/src/boot/boot.rs b/config/src/boot/boot.rs index 17b48046..982a1826 100644 --- a/config/src/boot/boot.rs +++ b/config/src/boot/boot.rs @@ -1,4 +1,4 @@ -use std::{env, fs, path::{Path, PathBuf}, time::{self, SystemTime}}; +use std::{env, fs, path::{Path, PathBuf}, process, time::{self, SystemTime}}; use clap::{command, Parser}; use md5::{Digest, Md5}; @@ -30,6 +30,10 @@ struct Args { /// Write the selected files on open emitted by the chooser mode #[arg(long)] chooser_file: Option, + + /// Clear the cache directory + #[arg(long, action)] + clear_cache: bool, } impl Default for Boot { @@ -58,6 +62,12 @@ impl Default for Boot { fs::create_dir_all(&boot.state_dir).unwrap(); } + if args.clear_cache { + println!("Clearing cache directory: {:?}", boot.cache_dir); + fs::remove_dir_all(&boot.cache_dir).unwrap(); + process::exit(0); + } + boot } } diff --git a/core/src/external/ffmpegthumbnailer.rs b/core/src/external/ffmpegthumbnailer.rs index f7d2da43..98fbb695 100644 --- a/core/src/external/ffmpegthumbnailer.rs +++ b/core/src/external/ffmpegthumbnailer.rs @@ -1,13 +1,13 @@ use std::path::Path; use config::PREVIEW; -use shared::PagedError; +use shared::PeekError; use tokio::process::Command; -pub async fn ffmpegthumbnailer(src: &Path, dest: &Path, skip: usize) -> Result<(), PagedError> { +pub async fn ffmpegthumbnailer(src: &Path, dest: &Path, skip: usize) -> Result<(), PeekError> { let percentage = 5 + skip; - if percentage >= 100 { - return Err(PagedError::Exceed(94)); + if percentage > 95 { + return Err(PeekError::Exceed(95 - 5)); } let output = Command::new("ffmpegthumbnailer") diff --git a/core/src/external/jq.rs b/core/src/external/jq.rs index 7b0b2a1c..aa0c0cd5 100644 --- a/core/src/external/jq.rs +++ b/core/src/external/jq.rs @@ -2,10 +2,10 @@ use std::{path::Path, process::Stdio}; use anyhow::Result; use config::PREVIEW; -use shared::PagedError; +use shared::PeekError; use tokio::{io::{AsyncBufReadExt, BufReader}, process::Command}; -pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result { +pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result { let mut child = Command::new("jq") .args(["-C", "--indent", &PREVIEW.tab_size.to_string(), "."]) .arg(path) @@ -30,7 +30,7 @@ pub async fn jq(path: &Path, skip: usize, limit: usize) -> Result 0 && i < skip + limit { - Err(PagedError::Exceed(i.saturating_sub(limit))) + Err(PeekError::Exceed(i.saturating_sub(limit))) } else { Ok(lines) } diff --git a/core/src/external/lsar.rs b/core/src/external/lsar.rs index 2a8abcaa..3e015d78 100644 --- a/core/src/external/lsar.rs +++ b/core/src/external/lsar.rs @@ -3,7 +3,7 @@ use std::path::Path; use anyhow::anyhow; use serde::Deserialize; use serde_json::Value; -use shared::PagedError; +use shared::PeekError; use tokio::process::Command; #[derive(Debug)] @@ -31,7 +31,7 @@ pub struct LsarFile { } #[allow(clippy::manual_map)] -pub async fn lsar(path: &Path, skip: usize, limit: usize) -> Result, PagedError> { +pub async fn lsar(path: &Path, skip: usize, limit: usize) -> Result, PeekError> { let output = Command::new("lsar").args(["-j", "-jss"]).arg(path).kill_on_drop(true).output().await?; @@ -74,7 +74,7 @@ pub async fn lsar(path: &Path, skip: usize, limit: usize) -> Result 0 && files.len() < limit { - Err(PagedError::Exceed(i.saturating_sub(limit))) + Err(PeekError::Exceed(i.saturating_sub(limit))) } else { Ok(files) } diff --git a/core/src/external/pdftoppm.rs b/core/src/external/pdftoppm.rs index 255e93ec..ded40197 100644 --- a/core/src/external/pdftoppm.rs +++ b/core/src/external/pdftoppm.rs @@ -2,10 +2,10 @@ use std::path::Path; use adaptor::Image; use regex::Regex; -use shared::PagedError; +use shared::PeekError; use tokio::process::Command; -pub async fn pdftoppm(src: &Path, dest: impl AsRef, skip: usize) -> Result<(), PagedError> { +pub async fn pdftoppm(src: &Path, dest: impl AsRef, skip: usize) -> Result<(), PeekError> { let output = Command::new("pdftoppm") .args(["-singlefile", "-jpeg", "-jpegopt", "quality=75", "-f"]) .arg((skip + 1).to_string()) @@ -22,7 +22,7 @@ pub async fn pdftoppm(src: &Path, dest: impl AsRef, skip: usize) -> Result .map(|cap| cap[1].parse().unwrap()) .unwrap_or(0); - return if pages > 0 { Err(PagedError::Exceed(pages - 1)) } else { Err(s.to_string().into()) }; + return if pages > 0 { Err(PeekError::Exceed(pages - 1)) } else { Err(s.to_string().into()) }; } Ok(Image::precache_anyway(output.stdout.into(), dest).await?) diff --git a/core/src/manager/preview/preview.rs b/core/src/manager/preview/preview.rs index e90160d0..0a6cc51c 100644 --- a/core/src/manager/preview/preview.rs +++ b/core/src/manager/preview/preview.rs @@ -2,7 +2,7 @@ use std::{mem, path::{Path, PathBuf}, sync::atomic::Ordering}; use adaptor::Adaptor; use config::MANAGER; -use shared::{MimeKind, PagedError}; +use shared::{MimeKind, PeekError}; use tokio::task::JoinHandle; use super::{provider::INCR, Provider}; @@ -51,7 +51,6 @@ impl Preview { self.handle.take().map(|h| h.abort()); INCR.fetch_add(1, Ordering::Relaxed); - Adaptor::image_hide(MANAGER.layout.preview_rect()).ok(); self.skip = skip; self.handle = Some(tokio::spawn(async move { @@ -59,7 +58,7 @@ impl Preview { Ok(result) => { emit!(Preview(path, mime, result)); } - Err(PagedError::Exceed(max)) => { + Err(PeekError::Exceed(max)) => { emit!(Peek(path, max)); } _ => {} diff --git a/core/src/manager/preview/provider.rs b/core/src/manager/preview/provider.rs index dd52fb1c..e7d15f96 100644 --- a/core/src/manager/preview/provider.rs +++ b/core/src/manager/preview/provider.rs @@ -3,7 +3,7 @@ use std::{io::BufRead, path::Path, sync::atomic::{AtomicUsize, Ordering}}; use adaptor::Adaptor; use anyhow::anyhow; use config::{BOOT, MANAGER, PREVIEW}; -use shared::{MimeKind, PagedError}; +use shared::{MimeKind, PeekError}; use syntect::{easy::HighlightFile, util::as_24_bit_terminal_escaped}; use tokio::fs; @@ -19,7 +19,7 @@ impl Provider { kind: MimeKind, path: &Path, skip: usize, - ) -> Result { + ) -> Result { match kind { MimeKind::Empty => Ok(PreviewData::None), MimeKind::Archive => Provider::archive(path, skip).await.map(PreviewData::Text), @@ -47,7 +47,7 @@ impl Provider { } } - pub(super) async fn folder(path: &Path) -> Result { + pub(super) async fn folder(path: &Path) -> Result { emit!(Files(match Files::read_dir(path).await { Ok(items) => FilesOp::Read(path.to_path_buf(), items), Err(_) => FilesOp::IOErr(path.to_path_buf()), @@ -56,12 +56,12 @@ impl Provider { Ok(PreviewData::Folder) } - pub(super) async fn image(path: &Path) -> Result { + pub(super) async fn image(path: &Path) -> Result { Adaptor::image_show(path, MANAGER.layout.preview_rect()).await?; Ok(PreviewData::Image) } - pub(super) async fn video(path: &Path, skip: usize) -> Result { + pub(super) async fn video(path: &Path, skip: usize) -> Result { let cache = BOOT.cache(path, skip); if fs::metadata(&cache).await.is_err() { external::ffmpegthumbnailer(path, &cache, skip).await?; @@ -70,7 +70,7 @@ impl Provider { Self::image(&cache).await } - pub(super) async fn pdf(path: &Path, skip: usize) -> Result { + pub(super) async fn pdf(path: &Path, skip: usize) -> Result { let cache = BOOT.cache(path, skip); if fs::metadata(&cache).await.is_err() { external::pdftoppm(path, &cache, skip).await?; @@ -79,11 +79,11 @@ impl Provider { Self::image(&cache).await } - pub(super) async fn json(path: &Path, skip: usize) -> Result { + pub(super) async fn json(path: &Path, skip: usize) -> Result { external::jq(path, skip, MANAGER.layout.preview_height()).await } - pub(super) async fn archive(path: &Path, skip: usize) -> Result { + pub(super) async fn archive(path: &Path, skip: usize) -> Result { Ok( external::lsar(path, skip, MANAGER.layout.preview_height()) .await? @@ -94,13 +94,13 @@ impl Provider { ) } - pub(super) async fn highlight(path: &Path, skip: usize) -> Result { + pub(super) async fn highlight(path: &Path, skip: usize) -> Result { let tick = INCR.load(Ordering::Relaxed); let path = path.to_path_buf(); let spaces = " ".repeat(PREVIEW.tab_size as usize); let (syntaxes, theme) = highlighter(); - tokio::task::spawn_blocking(move || -> Result { + tokio::task::spawn_blocking(move || -> Result { let mut h = HighlightFile::new(path, syntaxes, theme)?; let mut line = String::new(); let mut buf = String::new(); @@ -109,25 +109,25 @@ impl Provider { let limit = MANAGER.layout.preview_height(); while h.reader.read_line(&mut line)? > 0 { if tick != INCR.load(Ordering::Relaxed) { - return Err("Preview cancelled".into()); + return Err("Highlighting cancelled".into()); } i += 1; if i > skip + limit { break; - } else if i <= skip { - line.clear(); - continue; } line = line.replace('\t', &spaces); let regions = h.highlight_lines.highlight_line(&line, syntaxes).map_err(|e| anyhow!(e))?; - buf.push_str(&as_24_bit_terminal_escaped(®ions, false)); + + if i > skip { + buf.push_str(&as_24_bit_terminal_escaped(®ions, false)); + } line.clear(); } if skip > 0 && i < skip + limit { - return Err(PagedError::Exceed(i.saturating_sub(limit))); + return Err(PeekError::Exceed(i.saturating_sub(limit))); } buf.push_str("\x1b[0m"); diff --git a/core/src/manager/tab.rs b/core/src/manager/tab.rs index ec8891ae..3a3b13ed 100644 --- a/core/src/manager/tab.rs +++ b/core/src/manager/tab.rs @@ -90,8 +90,8 @@ impl Tab { }; let path = self.preview.lock.as_ref().unwrap().0.clone(); - if let Some(dir) = self.history(&path) { - let max = dir.files.len().saturating_sub(MANAGER.layout.preview_height()); + if let Some(folder) = self.history(&path) { + let max = folder.files.len().saturating_sub(MANAGER.layout.preview_height()); if new > max { emit!(Peek(path, max)); return false; diff --git a/shared/src/errors/mod.rs b/shared/src/errors/mod.rs new file mode 100644 index 00000000..b70c7117 --- /dev/null +++ b/shared/src/errors/mod.rs @@ -0,0 +1,3 @@ +mod peek; + +pub use peek::*; diff --git a/shared/src/errors.rs b/shared/src/errors/peek.rs similarity index 70% rename from shared/src/errors.rs rename to shared/src/errors/peek.rs index a4d31f54..96f8c805 100644 --- a/shared/src/errors.rs +++ b/shared/src/errors/peek.rs @@ -1,12 +1,12 @@ use std::{error::Error, fmt::{self, Display}}; #[derive(Debug)] -pub enum PagedError { +pub enum PeekError { Exceed(usize), Unexpected(String), } -impl Display for PagedError { +impl Display for PeekError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Exceed(lines) => write!(f, "Exceed: {lines}"), @@ -15,20 +15,20 @@ impl Display for PagedError { } } -impl Error for PagedError {} +impl Error for PeekError {} -impl From for PagedError { +impl From for PeekError { fn from(error: String) -> Self { Self::Unexpected(error) } } -impl From<&str> for PagedError { +impl From<&str> for PeekError { fn from(error: &str) -> Self { Self::from(error.to_owned()) } } -impl From for PagedError { +impl From for PeekError { fn from(error: anyhow::Error) -> Self { Self::from(error.to_string()) } } -impl From for PagedError { +impl From for PeekError { fn from(error: std::io::Error) -> Self { Self::from(error.to_string()) } } -impl From for PagedError { +impl From for PeekError { fn from(error: tokio::task::JoinError) -> Self { Self::from(error.to_string()) } }