mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
add pdf to ppm and remove pdf to text
This commit is contained in:
parent
9c7538d10f
commit
f177c449e4
4 changed files with 38 additions and 33 deletions
4
core/src/external/mod.rs
vendored
4
core/src/external/mod.rs
vendored
|
|
@ -5,7 +5,7 @@ mod file;
|
||||||
mod fzf;
|
mod fzf;
|
||||||
mod jq;
|
mod jq;
|
||||||
mod lsar;
|
mod lsar;
|
||||||
mod pdftotext;
|
mod pdftoppm;
|
||||||
mod rg;
|
mod rg;
|
||||||
mod unar;
|
mod unar;
|
||||||
mod zoxide;
|
mod zoxide;
|
||||||
|
|
@ -17,7 +17,7 @@ pub use file::*;
|
||||||
pub use fzf::*;
|
pub use fzf::*;
|
||||||
pub use jq::*;
|
pub use jq::*;
|
||||||
pub use lsar::*;
|
pub use lsar::*;
|
||||||
pub use pdftotext::*;
|
pub use pdftoppm::*;
|
||||||
pub use rg::*;
|
pub use rg::*;
|
||||||
pub use unar::*;
|
pub use unar::*;
|
||||||
pub use zoxide::*;
|
pub use zoxide::*;
|
||||||
|
|
|
||||||
22
core/src/external/pdftoppm.rs
vendored
Normal file
22
core/src/external/pdftoppm.rs
vendored
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use tokio::process::Command;
|
||||||
|
|
||||||
|
pub async fn pdftoppm(path: &Path, dest: &Path) -> Result<()> {
|
||||||
|
let output = Command::new("pdftoppm")
|
||||||
|
.args(["-png", "-singlefile"])
|
||||||
|
.arg(path)
|
||||||
|
.arg(dest)
|
||||||
|
.kill_on_drop(true)
|
||||||
|
.output()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
bail!(
|
||||||
|
"failed to get pdf: {}",
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
19
core/src/external/pdftotext.rs
vendored
19
core/src/external/pdftotext.rs
vendored
|
|
@ -1,19 +0,0 @@
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use anyhow::{bail, Result};
|
|
||||||
use tokio::process::Command;
|
|
||||||
|
|
||||||
pub async fn pdftotext(path: &Path) -> Result<String> {
|
|
||||||
let output = Command::new("pdftotext")
|
|
||||||
.args(["-l", "10", "-nopgbrk", "-q", "--"])
|
|
||||||
.arg(path)
|
|
||||||
.arg("-")
|
|
||||||
.kill_on_drop(true)
|
|
||||||
.output()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
|
||||||
bail!("failed to get json: {}", String::from_utf8_lossy(&output.stderr));
|
|
||||||
}
|
|
||||||
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
|
|
||||||
}
|
|
||||||
|
|
@ -63,7 +63,7 @@ impl Preview {
|
||||||
MimeKind::JSON => Self::json(&path).await.map(PreviewData::Text),
|
MimeKind::JSON => Self::json(&path).await.map(PreviewData::Text),
|
||||||
MimeKind::Text => Self::highlight(&path).await.map(PreviewData::Text),
|
MimeKind::Text => Self::highlight(&path).await.map(PreviewData::Text),
|
||||||
MimeKind::Image => Self::image(&path).await,
|
MimeKind::Image => Self::image(&path).await,
|
||||||
MimeKind::Pdf => Self::pdf(&path).await.map(PreviewData::Text),
|
MimeKind::Pdf => Self::pdf(&path).await,
|
||||||
MimeKind::Video => Self::video(&path).await,
|
MimeKind::Video => Self::video(&path).await,
|
||||||
MimeKind::Archive => Self::archive(&path).await.map(PreviewData::Text),
|
MimeKind::Archive => Self::archive(&path).await.map(PreviewData::Text),
|
||||||
MimeKind::Others => Err(anyhow!("Unsupported mimetype: {}", mime)),
|
MimeKind::Others => Err(anyhow!("Unsupported mimetype: {}", mime)),
|
||||||
|
|
@ -118,6 +118,19 @@ impl Preview {
|
||||||
Self::image(&cache).await
|
Self::image(&cache).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn pdf(path: &Path) -> Result<PreviewData> {
|
||||||
|
let cache = Image::cache(path);
|
||||||
|
if fs::metadata(&cache).await.is_err() {
|
||||||
|
external::pdftoppm(path, &cache).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut dest = cache.into_os_string();
|
||||||
|
dest.push(".png");
|
||||||
|
|
||||||
|
let dest = Path::new(&dest);
|
||||||
|
Self::image(dest).await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn json(path: &Path) -> Result<String> {
|
pub async fn json(path: &Path) -> Result<String> {
|
||||||
Ok(
|
Ok(
|
||||||
external::jq(path)
|
external::jq(path)
|
||||||
|
|
@ -129,17 +142,6 @@ impl Preview {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn pdf(path: &Path) -> Result<String> {
|
|
||||||
Ok(
|
|
||||||
external::pdftotext(path)
|
|
||||||
.await?
|
|
||||||
.lines()
|
|
||||||
.take(Self::rect().height as usize)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn archive(path: &Path) -> Result<String> {
|
pub async fn archive(path: &Path) -> Result<String> {
|
||||||
Ok(
|
Ok(
|
||||||
external::lsar(path)
|
external::lsar(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue