Skip to content

Commit

Permalink
general code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 10, 2024
1 parent 2f4260b commit af79937
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 274 deletions.
1 change: 1 addition & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod fuzzy;
mod input;
mod previewers;
pub mod television;
mod ui;
mod utils;

pub use finders::Entry;
Expand Down
2 changes: 1 addition & 1 deletion src/components/previewers/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl EnvVarPreviewer {
title: entry.name.clone(),
content: if let Some(preview) = &entry.preview {
PreviewContent::PlainTextWrapped(maybe_add_newline_after_colon(
&preview,
preview,
&entry.name,
))
} else {
Expand Down
41 changes: 18 additions & 23 deletions src/components/previewers/files.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_eyre::Result;
use image::{ImageReader, Rgb};
use image::Rgb;
use ratatui_image::picker::Picker;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
Expand Down Expand Up @@ -107,7 +107,7 @@ impl FilePreviewer {

fn get_file_type(&self, path: &Path) -> FileType {
debug!("Getting file type for {:?}", path);
let mut file_type = match infer::get_from_path(&path) {
let mut file_type = match infer::get_from_path(path) {
Ok(Some(t)) => {
let mime_type = t.mime_type();
if mime_type.contains("image") {
Expand All @@ -123,15 +123,13 @@ impl FilePreviewer {

// if the file type is unknown, try to determine it from the extension or the content
if matches!(file_type, FileType::Unknown) {
if is_known_text_extension(&path) {
if is_known_text_extension(path) {
file_type = FileType::Text;
} else {
if let Ok(mut f) = File::open(&path) {
let mut buffer = [0u8; 256];
if let Ok(bytes_read) = f.read(&mut buffer) {
if bytes_read > 0 && is_valid_utf8(&buffer) {
file_type = FileType::Text;
}
} else if let Ok(mut f) = File::open(path) {
let mut buffer = [0u8; 256];
if let Ok(bytes_read) = f.read(&mut buffer) {
if bytes_read > 0 && is_valid_utf8(&buffer) {
file_type = FileType::Text;
}
}
}
Expand Down Expand Up @@ -167,21 +165,19 @@ impl FilePreviewer {
debug!("Computing preview for {:?}", entry.name);
match self.get_file_type(&path_buf) {
FileType::Text => {
let preview = match File::open(&path_buf) {

match File::open(&path_buf) {
Ok(file) => {
// insert a non-highlighted version of the preview into the cache
let reader = BufReader::new(file);
let preview = plain_text_preview(&entry.name, reader);
self.cache_preview(entry.name.clone(), preview.clone())
.await;

match preview.content {
PreviewContent::PlainText(ref lines) => {
// compute the highlighted version in the background
self.compute_highlighted_text_preview(entry, lines.to_vec())
.await;
}
_ => {}
if let PreviewContent::PlainText(ref lines) = preview.content {
// compute the highlighted version in the background
self.compute_highlighted_text_preview(entry, lines.to_vec())
.await;
}
preview
}
Expand All @@ -191,8 +187,7 @@ impl FilePreviewer {
self.cache_preview(entry.name.clone(), p.clone()).await;
p
}
};
preview
}
}
FileType::Image => {
debug!("Previewing image file: {:?}", entry.name);
Expand All @@ -202,21 +197,21 @@ impl FilePreviewer {
.await;
// compute the image preview in the background
//self.compute_image_preview(entry).await;
return preview;
preview
}
FileType::Other => {
debug!("Previewing other file: {:?}", entry.name);
let preview = not_supported(&entry.name);
self.cache_preview(entry.name.clone(), preview.clone())
.await;
return preview;
preview
}
FileType::Unknown => {
debug!("Unknown file type: {:?}", entry.name);
let preview = not_supported(&entry.name);
self.cache_preview(entry.name.clone(), preview.clone())
.await;
return preview;
preview
}
}
}
Expand Down
Loading

0 comments on commit af79937

Please sign in to comment.