fix: Apply formatting and linting

This commit is contained in:
azy 2025-02-23 13:41:12 +09:00
parent 74421b8758
commit 51eef8ba8d

View File

@ -1,12 +1,12 @@
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use image::{DynamicImage, GenericImageView, Pixel, Rgba, RgbaImage};
use image::imageops::FilterType; use image::imageops::FilterType;
use image::{DynamicImage, GenericImageView, Pixel, Rgba, RgbaImage};
use ratatui::layout::{Alignment, Rect}; use ratatui::layout::{Alignment, Rect};
use ratatui::prelude::{Color, Span, Style, Text}; use ratatui::prelude::{Color, Span, Style, Text};
use ratatui::text::Line; use ratatui::text::Line;
use ratatui::widgets::{Block, Paragraph}; use ratatui::widgets::{Block, Paragraph};
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
const PIXEL: char = '▀'; const PIXEL: char = '▀';
const FILTER_TYPE: FilterType = FilterType::Lanczos3; const FILTER_TYPE: FilterType = FilterType::Lanczos3;
@ -18,7 +18,6 @@ const CACHED_HEIGHT: u32 = 128;
const GRAY: Rgba<u8> = Rgba([242, 242, 242, 255]); const GRAY: Rgba<u8> = Rgba([242, 242, 242, 255]);
const WHITE: Rgba<u8> = Rgba([255, 255, 255, 255]); const WHITE: Rgba<u8> = Rgba([255, 255, 255, 255]);
struct Cache { struct Cache {
area: Rect, area: Rect,
image: RgbaImage, image: RgbaImage,
@ -36,7 +35,8 @@ impl Hash for CachedImageData {
impl Debug for CachedImageData { impl Debug for CachedImageData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CachedImageData") f.debug_struct("CachedImageData")
.field("image", &self.image.dimensions()) // Show dimensions instead of full image .field("dimensions", &self.image.dimensions()) // Show dimensions instead of full image
.field("inner_cache", &self.inner_cache.lock().unwrap().is_some()) // Indicate if cache exists
.finish() .finish()
} }
} }
@ -75,7 +75,6 @@ impl CachedImageData {
} else { } else {
*mutex_cache = Some(Cache { area, image }); *mutex_cache = Some(Cache { area, image });
}; };
} }
pub fn from_dynamic_image(dynamic_image: DynamicImage) -> Self { pub fn from_dynamic_image(dynamic_image: DynamicImage) -> Self {
// if the image is smaller than the preview window, keep it small // if the image is smaller than the preview window, keep it small
@ -92,7 +91,7 @@ impl CachedImageData {
}; };
CachedImageData::new(resized_image) CachedImageData::new(resized_image)
} }
fn text_from_rgba_image_ref(image_rgba: &RgbaImage) -> Text<'static>{ fn text_from_rgba_image_ref(image_rgba: &RgbaImage) -> Text<'static> {
let lines = image_rgba let lines = image_rgba
// iter over pair of rows // iter over pair of rows
.rows() .rows()
@ -113,8 +112,6 @@ impl CachedImageData {
.collect::<Vec<Line>>(); .collect::<Vec<Line>>();
Text::from(lines).centered() Text::from(lines).centered()
} }
pub fn paragraph<'a>( pub fn paragraph<'a>(
&self, &self,
@ -123,13 +120,14 @@ impl CachedImageData {
) -> Paragraph<'a> { ) -> Paragraph<'a> {
let preview_width = u32::from(inner.width); let preview_width = u32::from(inner.width);
let preview_height = u32::from(inner.height) * 2; // *2 because 2 pixels per character let preview_height = u32::from(inner.height) * 2; // *2 because 2 pixels per character
let text_image = if self.cache().lock().unwrap().is_none() || self.cache().lock().unwrap().as_ref().unwrap().area != inner { let text_image = if self.cache().lock().unwrap().is_none()
|| self.cache().lock().unwrap().as_ref().unwrap().area != inner
{
let image_rgba = if self.image.width() > preview_width let image_rgba = if self.image.width() > preview_width
|| self.image.height() > preview_height || self.image.height() > preview_height
{ {
//warn!("==========================="); //warn!("===========================");
self self.image
.image
.resize(preview_width, preview_height, FILTER_TYPE) .resize(preview_width, preview_height, FILTER_TYPE)
.into_rgba8() .into_rgba8()
} else { } else {
@ -141,7 +139,6 @@ impl CachedImageData {
// cached resized image // cached resized image
self.set_cache(inner, image_rgba); self.set_cache(inner, image_rgba);
text text
} else { } else {
let cache = self.cache().lock().unwrap(); let cache = self.cache().lock().unwrap();
let image = &cache.as_ref().unwrap().image; let image = &cache.as_ref().unwrap().image;