summaryrefslogtreecommitdiffstats
path: root/src/image/image_view.rs
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2020-11-25 23:21:23 +0100
committerCanop <cano.petrole@gmail.com>2020-11-25 23:21:23 +0100
commit9785535f80573a9b00d2ce738f0c5a8df81db392 (patch)
tree12486770f50cccd14ea38a873bbe191469938c47 /src/image/image_view.rs
parentba9dbf6c0a3827fe6299f3de582db6fc1ce9362a (diff)
[WIP] high-definition image preview when using kitty
(some cleaning to do but I need to sleep)
Diffstat (limited to 'src/image/image_view.rs')
-rw-r--r--src/image/image_view.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/image/image_view.rs b/src/image/image_view.rs
index 4e30562..1c2290b 100644
--- a/src/image/image_view.rs
+++ b/src/image/image_view.rs
@@ -4,6 +4,7 @@ use {
app::AppContext,
display::{fill_bg, Screen, W},
errors::ProgramError,
+ kitty,
skin::PanelSkin,
},
crossterm::{
@@ -20,7 +21,7 @@ use {
GenericImageView,
imageops::FilterType,
},
- std::path::Path,
+ std::path::{Path, PathBuf},
termimad::{Area},
};
@@ -36,6 +37,7 @@ struct CachedImage {
/// an imageview can display an image in the terminal with
/// a ratio of one pixel per char in width.
pub struct ImageView {
+ path: PathBuf,
source_img: DynamicImage,
display_img: Option<CachedImage>,
}
@@ -49,10 +51,17 @@ impl ImageView {
Reader::open(&path)?.decode()?
);
Ok(Self {
+ path: path.to_path_buf(),
source_img,
display_img: None,
})
}
+ pub fn is_png(&self) -> bool {
+ match self.path.extension() {
+ Some(ext) => ext == "png" || ext == "PNG",
+ None => false,
+ }
+ }
pub fn display(
&mut self,
w: &mut W,
@@ -61,6 +70,21 @@ impl ImageView {
area: &Area,
con: &AppContext,
) -> Result<(), ProgramError> {
+
+ #[cfg(unix)]
+ if let Some(renderer) = kitty::image_renderer() {
+ let mut renderer = renderer.lock().unwrap();
+ w.queue(cursor::MoveTo(area.left, area.top))?;
+ renderer.print_with_chunks(
+ w,
+ &self.source_img,
+ area.width,
+ area.height,
+ )?;
+ // TODO clean area below (using z-index?)
+ return Ok(());
+ }
+
let target_width = area.width as u32;
let target_height = (area.height*2) as u32;
let cached = self.display_img.as_ref()