Skip to content

Commit

Permalink
Add PreviewType::TooBigText to handle large text file in preview area
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoheiu committed Dec 29, 2024
1 parent 1adf529 commit 6001c47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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` ";

Expand Down Expand Up @@ -40,7 +39,8 @@ pub struct Layout {
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone)]
pub enum PreviewType {
NotReadable,
TooBigSize,
TooBigImage,
TooBigText,
Directory,
Image,
Text,
Expand Down Expand Up @@ -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);
Expand All @@ -214,10 +217,10 @@ impl Layout {
}
}
Some(PreviewType::Binary) => {
print!("(Binary file)");
print!("(binary file)");
}
_ => {
print!("(Not Available)");
print!("(not available)");
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemInfo>,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6001c47

Please sign in to comment.