From 62073d69ccc022d75bcc6bc5adc4472bdfe5b7f5 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:49:46 +0100 Subject: [PATCH] perf(preview): remove temporary plaintext previews in favor of loading message preview (#10) * perf(preview): remove temporary plaintext previews in favor of a loading message * linting * dead code --- crates/television/ui/preview.rs | 5 +++-- .../television_previewers/src/previewers/files.rs | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/television/ui/preview.rs b/crates/television/ui/preview.rs index da55a9d..c0923af 100644 --- a/crates/television/ui/preview.rs +++ b/crates/television/ui/preview.rs @@ -110,6 +110,7 @@ impl Television { Ok(()) } + #[allow(dead_code)] const FILL_CHAR_SLANTED: char = '╱'; const FILL_CHAR_EMPTY: char = ' '; @@ -194,7 +195,7 @@ impl Television { .build_meta_preview_paragraph( inner, PREVIEW_NOT_SUPPORTED_MSG, - Self::FILL_CHAR_SLANTED, + Self::FILL_CHAR_EMPTY, ) .block(preview_block) .alignment(Alignment::Left) @@ -203,7 +204,7 @@ impl Television { .build_meta_preview_paragraph( inner, FILE_TOO_LARGE_MSG, - Self::FILL_CHAR_SLANTED, + Self::FILL_CHAR_EMPTY, ) .block(preview_block) .alignment(Alignment::Left) diff --git a/crates/television_previewers/src/previewers/files.rs b/crates/television_previewers/src/previewers/files.rs index 02fef75..f697a20 100644 --- a/crates/television_previewers/src/previewers/files.rs +++ b/crates/television_previewers/src/previewers/files.rs @@ -69,6 +69,10 @@ impl FilePreviewer { } } + /// Get a preview for a file entry. + /// + /// # Panics + /// Panics if seeking to the start of the file fails. pub async fn preview(&mut self, entry: &entry::Entry) -> Arc { let path_buf = PathBuf::from(&entry.name); @@ -94,9 +98,8 @@ impl FilePreviewer { FileType::Text => { 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); + // insert a loading preview into the cache + let preview = meta::loading(&entry.name); self.cache_preview( entry.name.clone(), preview.clone(), @@ -104,8 +107,7 @@ impl FilePreviewer { .await; // compute the highlighted version in the background - let mut reader = - BufReader::new(file.try_clone().unwrap()); + let mut reader = BufReader::new(file); reader.seek(std::io::SeekFrom::Start(0)).unwrap(); self.compute_highlighted_text_preview(entry, reader) .await; @@ -216,6 +218,8 @@ impl FilePreviewer { } Err(e) => { warn!("Error computing highlights: {:?}", e); + let preview = meta::not_supported(&entry_c.name); + cache.lock().insert(entry_c.name.clone(), preview); } }; }); @@ -281,6 +285,7 @@ impl FilePreviewer { /// This should be enough to most standard terminal sizes const TEMP_PLAIN_TEXT_PREVIEW_HEIGHT: usize = 200; +#[allow(dead_code)] fn plain_text_preview(title: &str, reader: BufReader<&File>) -> Arc { debug!("Creating plain text preview for {:?}", title); let mut lines = Vec::with_capacity(TEMP_PLAIN_TEXT_PREVIEW_HEIGHT);