diff --git a/src/layout.rs b/src/layout.rs index 0cad56c..77132e2 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -9,7 +9,6 @@ use super::term::*; use log::error; use serde::{Deserialize, Serialize}; -pub const MAX_SIZE_TO_PREVIEW: u64 = 1_000_000_000; pub const CHAFA_WARNING: &str = "From v1.1.0, the image preview needs chafa (>= v1.10.0). For more details, please see help by `:h` "; @@ -40,7 +39,8 @@ pub struct Layout { #[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone)] pub enum PreviewType { NotReadable, - TooBigSize, + TooBigImage, + TooBigText, Directory, Image, Text, @@ -186,8 +186,11 @@ impl Layout { Some(PreviewType::NotReadable) => { print!("(file not readable)"); } - Some(PreviewType::TooBigSize) => { - print!("(file too big for preview)"); + Some(PreviewType::TooBigImage) => { + print!("(image too big for preview: over 100MB)"); + } + Some(PreviewType::TooBigText) => { + print!("(text too big for preview: over 1MB)"); } Some(PreviewType::Directory) => { self.preview_directory(item); @@ -214,10 +217,10 @@ impl Layout { } } Some(PreviewType::Binary) => { - print!("(Binary file)"); + print!("(binary file)"); } _ => { - print!("(Not Available)"); + print!("(not available)"); } } } diff --git a/src/state.rs b/src/state.rs index 632a818..6cd4f2d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -42,6 +42,9 @@ use std::os::unix::fs::PermissionsExt; pub const BEGINNING_ROW: u16 = 3; pub const EMPTY_WARNING: &str = "Are you sure to empty the trash directory? (if yes: y)"; +const MAX_SIZE_TO_PREVIEW: u64 = 1_000_000_000; +const MAX_SIZE_TO_PREVIEW_TEXT: u64 = 1_000_000; + #[derive(Debug, Default)] pub struct State { pub list: Vec, @@ -1914,11 +1917,15 @@ fn check_zoxide() -> bool { /// Set content type from ItemInfo. fn set_preview_content_type(item: &mut ItemInfo) { if item.file_size > MAX_SIZE_TO_PREVIEW { - item.preview_type = Some(PreviewType::TooBigSize); + item.preview_type = Some(PreviewType::TooBigImage); } else if is_supported_image(item) { item.preview_type = Some(PreviewType::Image); } else if let Ok(content) = &std::fs::read(&item.file_path) { if content_inspector::inspect(content).is_text() { + if item.file_size > MAX_SIZE_TO_PREVIEW_TEXT { + item.preview_type = Some(PreviewType::TooBigText); + return; + } if let Ok(content) = String::from_utf8(content.to_vec()) { let content = content.replace('\t', " "); item.content = Some(content);