diff options
author | Uwe Jugel <532284+ubunatic@users.noreply.github.com> | 2022-10-22 11:51:18 +0200 |
---|---|---|
committer | Uwe Jugel <532284+ubunatic@users.noreply.github.com> | 2022-10-22 11:51:18 +0200 |
commit | 55dd3b9f20e453872fd6dea520476fbb53962f45 (patch) | |
tree | fc5f85b5ae4548e7471ef03c72ed17d1df374a40 /src/layout.rs | |
parent | 1b83c039f82e2f7d9302dc71adbc086a54ae4352 (diff) |
move content check to fn
Diffstat (limited to 'src/layout.rs')
-rw-r--r-- | src/layout.rs | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/src/layout.rs b/src/layout.rs index 0f15427..bc98d5e 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -36,7 +36,6 @@ pub struct Layout { pub enum PreviewType { NotReadable, - UnresolvedSymlink, TooBigSize, Directory, Image, @@ -56,9 +55,6 @@ impl Layout { PreviewType::NotReadable => { print!("(file not readable)"); } - PreviewType::UnresolvedSymlink => { - print!("(unresolved symlink)"); - } PreviewType::TooBigSize => { print!("(file too big for preview)"); } @@ -227,25 +223,9 @@ pub fn make_layout( } } -/// Check preview type. -fn check_preview_type(item: &ItemInfo) -> PreviewType { +fn check_preview_content_type(item: &ItemInfo) -> PreviewType { if item.file_size > MAX_SIZE_TO_PREVIEW { PreviewType::TooBigSize - } else if item.file_type == FileType::Directory { - PreviewType::Directory - } else if item.file_type == FileType::Symlink { - if item.symlink_dir_path.is_some() { - // assmuning symlink to be a directory - PreviewType::Directory - } else if let Ok(content) = &std::fs::read(&item.file_path) { - if content_inspector::inspect(content).is_text() { - PreviewType::Text - } else { - PreviewType::Binary - } - } else { - PreviewType::UnresolvedSymlink - } } else if is_supported_ext(item) { PreviewType::Image } else if let Ok(content) = &std::fs::read(&item.file_path) { @@ -255,10 +235,24 @@ fn check_preview_type(item: &ItemInfo) -> PreviewType { PreviewType::Binary } } else { + // failed to resolve item to any form of supported preview + // it is probably not accessible due to permissions, broken symlink etc. PreviewType::NotReadable } } +/// Check preview type. +fn check_preview_type(item: &ItemInfo) -> PreviewType { + if item.file_type == FileType::Directory { + PreviewType::Directory + } else if item.file_type == FileType::Symlink && item.symlink_dir_path.is_some() { + // symlink was resolved to directory already in the ItemInfo + PreviewType::Directory + } else { + check_preview_content_type(item) + } +} + fn is_supported_ext(item: &ItemInfo) -> bool { match &item.file_ext { None => false, |