diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-03-15 23:01:35 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-03-15 23:01:35 +0100 |
commit | cab247ad53321dd3ca174388e4b6cf665bc98e47 (patch) | |
tree | 05c46adbcd0fbe6ccf381038f718bb3161ef7d7c | |
parent | 0eb4f7138463dd5253b038948ac38d5058410027 (diff) |
preview fonts & svg by creating thumbnails
-rw-r--r-- | development.md | 4 | ||||
-rw-r--r-- | readme.md | 2 | ||||
-rw-r--r-- | src/preview.rs | 70 |
3 files changed, 69 insertions, 7 deletions
diff --git a/development.md b/development.md index 681ec576..8fd144a7 100644 --- a/development.md +++ b/development.md @@ -486,8 +486,8 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally. - [ ] edit folder like a buffer [oil like](https://github.com/stevearc/oil.nvim) - [ ] allow pipe in execution - [ ] search in preview or in help - - [ ] preview SVG like [ranger](https://github.com/ranger/ranger/pull/2537/files) does - - [ ] preview font with fontimage like [ranger](https://github.com/ranger/ranger/blob/46660c277c2ceb7c7c41ffce794d68f8f559030f/ranger/data/scope.sh#L207-L225) + - [x] preview SVG like [ranger](https://github.com/ranger/ranger/pull/2537/files) does + - [x] preview font with fontimage like [ranger](https://github.com/ranger/ranger/blob/46660c277c2ceb7c7c41ffce794d68f8f559030f/ranger/data/scope.sh#L207-L225) - [x] preview doc with pandoc or odt2txt [ranger](https://github.com/ranger/ranger/blob/46660c277c2ceb7c7c41ffce794d68f8f559030f/ranger/data/scope.sh#L84-L93) - [x] preview notebooks @@ -266,6 +266,8 @@ Most of the openers and tui applications are configurable from config files. Som - [isoinfo](https://command-not-found.com/isoinfo) allow the content preview of an iso file - [jupyter](https://jupyter.org/) preview jupyter notebooks by converting them to markdown - [pandoc](https://pandoc.org) preview open documents by converting them to markdown with pandoc +- [fontimage](https://fontforge.org/docs/fontutils/fontimage.html) preview fonts by creating a thumbnail +- [rsvg-convert](https://github.com/brion/librsvg) preview svg by creating a thumbnail ## Contribution diff --git a/src/preview.rs b/src/preview.rs index 2f89208a..7e6f68cd 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -86,6 +86,10 @@ impl Preview { e if is_ext_video(e) => { Ok(Self::Ueberzug(Ueberzug::video_thumbnail(&file_info.path)?)) } + e if is_ext_font(e) => { + Ok(Self::Ueberzug(Ueberzug::font_thumbnail(&file_info.path)?)) + } + e if is_ext_svg(e) => Ok(Self::Ueberzug(Ueberzug::svg_thumbnail(&file_info.path)?)), e if is_ext_iso(e) => Ok(Self::Iso(Iso::new(&file_info.path)?)), e if is_ext_notebook(e) => { Ok(Self::notebook(&file_info.path) @@ -562,16 +566,30 @@ impl Ueberzug { }) } - fn video_thumbnail(video_path: &Path) -> Result<Self> { - Self::make_thumbnail(video_path)?; - Ok(Self { + fn thumbnail() -> Self { + Self { path: THUMBNAIL_PATH.to_owned(), filename: "thumbnail".to_owned(), ueberzug: ueberzug::Ueberzug::new(), - }) + } + } + + fn video_thumbnail(video_path: &Path) -> Result<Self> { + Self::make_video_thumbnail(video_path)?; + Ok(Self::thumbnail()) + } + + fn font_thumbnail(font_path: &Path) -> Result<Self> { + Self::make_font_thumbnail(font_path)?; + Ok(Self::thumbnail()) } - fn make_thumbnail(video_path: &Path) -> Result<()> { + fn svg_thumbnail(svg_path: &Path) -> Result<Self> { + Self::make_svg_thumbnail(svg_path)?; + Ok(Self::thumbnail()) + } + + fn make_video_thumbnail(video_path: &Path) -> Result<()> { let path_str = video_path .to_str() .context("make_thumbnail: couldn't parse the path into a string")?; @@ -597,6 +615,40 @@ impl Ueberzug { Ok(()) } + fn make_font_thumbnail(font_path: &Path) -> Result<()> { + let path_str = font_path + .to_str() + .context("make_thumbnail: couldn't parse the path into a string")?; + let output = std::process::Command::new("fontimage") + .args(["-o", THUMBNAIL_PATH, path_str]) + .output()?; + if !output.stderr.is_empty() { + info!( + "fontimage thumbnail output: {} {}", + String::from_utf8(output.stdout).unwrap_or_default(), + String::from_utf8(output.stderr).unwrap_or_default() + ); + } + Ok(()) + } + + fn make_svg_thumbnail(svg_path: &Path) -> Result<()> { + let path_str = svg_path + .to_str() + .context("make_thumbnail: couldn't parse the path into a string")?; + let output = std::process::Command::new("rsvg-convert") + .args(["--keep-aspect-ratio", path_str, "-o", THUMBNAIL_PATH]) + .output()?; + if !output.stderr.is_empty() { + info!( + "ffmpeg thumbnail output: {} {}", + String::from_utf8(output.stdout).unwrap_or_default(), + String::from_utf8(output.stderr).unwrap_or_default() + ); + } + Ok(()) + } + /// Draw the image with ueberzug in the current window. /// The position is absolute, which is problematic when the app is embeded into a floating terminal. /// The whole struct instance is dropped when the preview is reset and the image is deleted. @@ -934,6 +986,14 @@ fn is_ext_video(ext: &str) -> bool { matches!(ext, "mkv" | "webm" | "mpeg" | "mp4" | "avi" | "flv" | "mpg") } +fn is_ext_font(ext: &str) -> bool { + matches!(ext, "ttf") +} + +fn is_ext_svg(ext: &str) -> bool { + matches!(ext, "svg") +} + fn is_ext_pdf(ext: &str) -> bool { ext == "pdf" } |