mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-07 12:05:34 +00:00
fix: correction after linting
This commit is contained in:
parent
e9f385c8eb
commit
b3e8e8d2e5
@ -9,7 +9,7 @@ pub mod previewers;
|
|||||||
|
|
||||||
// previewer types
|
// previewer types
|
||||||
use crate::utils::cache::RingSet;
|
use crate::utils::cache::RingSet;
|
||||||
use crate::utils::image::{CachedImageData};
|
use crate::utils::image::CachedImageData;
|
||||||
use crate::utils::syntax::HighlightedLines;
|
use crate::utils::syntax::HighlightedLines;
|
||||||
pub use previewers::basic::BasicPreviewer;
|
pub use previewers::basic::BasicPreviewer;
|
||||||
pub use previewers::basic::BasicPreviewerConfig;
|
pub use previewers::basic::BasicPreviewerConfig;
|
||||||
@ -208,10 +208,7 @@ impl Previewer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_request(
|
fn dispatch_request(&mut self, entry: &Entry) -> Option<Arc<Preview>> {
|
||||||
&mut self,
|
|
||||||
entry: &Entry,
|
|
||||||
) -> Option<Arc<Preview>> {
|
|
||||||
match &entry.preview_type {
|
match &entry.preview_type {
|
||||||
PreviewType::Basic => Some(self.basic.preview(entry)),
|
PreviewType::Basic => Some(self.basic.preview(entry)),
|
||||||
PreviewType::EnvVar => Some(self.env_var.preview(entry)),
|
PreviewType::EnvVar => Some(self.env_var.preview(entry)),
|
||||||
@ -225,10 +222,7 @@ impl Previewer {
|
|||||||
// faster, but since it's already running in the background and quite
|
// faster, but since it's already running in the background and quite
|
||||||
// fast for most standard file sizes, plus we're caching the previews,
|
// fast for most standard file sizes, plus we're caching the previews,
|
||||||
// I'm not sure the extra complexity is worth it.
|
// I'm not sure the extra complexity is worth it.
|
||||||
pub fn preview(
|
pub fn preview(&mut self, entry: &Entry) -> Option<Arc<Preview>> {
|
||||||
&mut self,
|
|
||||||
entry: &Entry,
|
|
||||||
) -> Option<Arc<Preview>> {
|
|
||||||
// if we haven't acknowledged the request yet, acknowledge it
|
// if we haven't acknowledged the request yet, acknowledge it
|
||||||
self.requests.push(entry.clone());
|
self.requests.push(entry.clone());
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::utils::files::{read_into_lines_capped, ReadResult};
|
use crate::utils::files::{read_into_lines_capped, ReadResult};
|
||||||
use crate::utils::syntax::HighlightedLines;
|
use crate::utils::syntax::HighlightedLines;
|
||||||
use image::{ImageReader};
|
use image::ImageReader;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::{FxBuildHasher, FxHashSet};
|
use rustc_hash::{FxBuildHasher, FxHashSet};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
@ -18,12 +18,12 @@ use tracing::{debug, trace, warn};
|
|||||||
use crate::channels::entry;
|
use crate::channels::entry;
|
||||||
use crate::preview::cache::PreviewCache;
|
use crate::preview::cache::PreviewCache;
|
||||||
use crate::preview::{previewers::meta, Preview, PreviewContent};
|
use crate::preview::{previewers::meta, Preview, PreviewContent};
|
||||||
|
use crate::utils::image::CachedImageData;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
files::FileType,
|
files::FileType,
|
||||||
strings::preprocess_line,
|
strings::preprocess_line,
|
||||||
syntax::{self, load_highlighting_assets, HighlightingAssetsExt},
|
syntax::{self, load_highlighting_assets, HighlightingAssetsExt},
|
||||||
};
|
};
|
||||||
use crate::utils::image::CachedImageData;
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct FilePreviewer {
|
pub struct FilePreviewer {
|
||||||
@ -80,20 +80,14 @@ impl FilePreviewer {
|
|||||||
self.cache.lock().get(&entry.name)
|
self.cache.lock().get(&entry.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preview(
|
pub fn preview(&mut self, entry: &entry::Entry) -> Option<Arc<Preview>> {
|
||||||
&mut self,
|
|
||||||
entry: &entry::Entry,
|
|
||||||
) -> Option<Arc<Preview>> {
|
|
||||||
if let Some(preview) = self.cached(entry) {
|
if let Some(preview) = self.cached(entry) {
|
||||||
trace!("Preview cache hit for {:?}", entry.name);
|
trace!("Preview cache hit for {:?}", entry.name);
|
||||||
if preview.partial_offset.is_some() {
|
if preview.partial_offset.is_some() {
|
||||||
// preview is partial, spawn a task to compute the next chunk
|
// preview is partial, spawn a task to compute the next chunk
|
||||||
// and return the partial preview
|
// and return the partial preview
|
||||||
debug!("Spawning partial preview task for {:?}", entry.name);
|
debug!("Spawning partial preview task for {:?}", entry.name);
|
||||||
self.handle_preview_request(
|
self.handle_preview_request(entry, Some(preview.clone()));
|
||||||
entry,
|
|
||||||
Some(preview.clone())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Some(preview)
|
Some(preview)
|
||||||
} else {
|
} else {
|
||||||
@ -107,7 +101,7 @@ impl FilePreviewer {
|
|||||||
pub fn handle_preview_request(
|
pub fn handle_preview_request(
|
||||||
&mut self,
|
&mut self,
|
||||||
entry: &entry::Entry,
|
entry: &entry::Entry,
|
||||||
partial_preview: Option<Arc<Preview>>
|
partial_preview: Option<Arc<Preview>>,
|
||||||
) {
|
) {
|
||||||
if self.in_flight_previews.lock().contains(&entry.name) {
|
if self.in_flight_previews.lock().contains(&entry.name) {
|
||||||
trace!("Preview already in flight for {:?}", entry.name);
|
trace!("Preview already in flight for {:?}", entry.name);
|
||||||
@ -252,7 +246,8 @@ pub fn try_preview(
|
|||||||
debug!("File {:?} is an image", entry.name);
|
debug!("File {:?} is an image", entry.name);
|
||||||
match ImageReader::open(path).unwrap().decode() {
|
match ImageReader::open(path).unwrap().decode() {
|
||||||
Ok(image) => {
|
Ok(image) => {
|
||||||
let cached_image_data = CachedImageData::from_dynamic_image(image);
|
let cached_image_data =
|
||||||
|
CachedImageData::from_dynamic_image(image);
|
||||||
let total_lines =
|
let total_lines =
|
||||||
cached_image_data.height().try_into().unwrap_or(u16::MAX);
|
cached_image_data.height().try_into().unwrap_or(u16::MAX);
|
||||||
let content = PreviewContent::Image(cached_image_data);
|
let content = PreviewContent::Image(cached_image_data);
|
||||||
@ -271,7 +266,6 @@ pub fn try_preview(
|
|||||||
cache.lock().insert(entry.name.clone(), &p);
|
cache.lock().insert(entry.name.clone(), &p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
debug!("File isn't text-based: {:?}", entry.name);
|
debug!("File isn't text-based: {:?}", entry.name);
|
||||||
let preview = meta::not_supported(&entry.name);
|
let preview = meta::not_supported(&entry.name);
|
||||||
|
@ -3,9 +3,7 @@ use crate::preview::{
|
|||||||
ansi::IntoText, PreviewContent, FILE_TOO_LARGE_MSG, LOADING_MSG,
|
ansi::IntoText, PreviewContent, FILE_TOO_LARGE_MSG, LOADING_MSG,
|
||||||
PREVIEW_NOT_SUPPORTED_MSG, TIMEOUT_MSG,
|
PREVIEW_NOT_SUPPORTED_MSG, TIMEOUT_MSG,
|
||||||
};
|
};
|
||||||
use crate::screen::{
|
use crate::screen::colors::{Colorscheme, PreviewColorscheme};
|
||||||
colors::{Colorscheme, PreviewColorscheme},
|
|
||||||
};
|
|
||||||
use crate::utils::strings::{
|
use crate::utils::strings::{
|
||||||
replace_non_printable, shrink_with_ellipsis, ReplaceNonPrintableConfig,
|
replace_non_printable, shrink_with_ellipsis, ReplaceNonPrintableConfig,
|
||||||
EMPTY_STRING,
|
EMPTY_STRING,
|
||||||
@ -96,9 +94,7 @@ pub fn build_preview_paragraph<'a>(
|
|||||||
inner.height,
|
inner.height,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
PreviewContent::Image(image) => {
|
PreviewContent::Image(image) => image.paragraph(inner, preview_block),
|
||||||
image.paragraph(inner, preview_block)
|
|
||||||
}
|
|
||||||
|
|
||||||
// meta
|
// meta
|
||||||
PreviewContent::Loading => {
|
PreviewContent::Loading => {
|
||||||
@ -247,7 +243,6 @@ fn build_syntect_highlighted_paragraph<'a>(
|
|||||||
//.scroll((preview_scroll, 0))
|
//.scroll((preview_scroll, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn build_meta_preview_paragraph<'a>(
|
pub fn build_meta_preview_paragraph<'a>(
|
||||||
inner: Rect,
|
inner: Rect,
|
||||||
message: &str,
|
message: &str,
|
||||||
@ -434,5 +429,3 @@ fn convert_syn_color_to_ratatui_color(
|
|||||||
) -> Color {
|
) -> Color {
|
||||||
Color::Rgb(color.r, color.g, color.b)
|
Color::Rgb(color.r, color.g, color.b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
|
use image::{DynamicImage, Rgba};
|
||||||
use image::{DynamicImage, Rgba };
|
|
||||||
|
|
||||||
|
|
||||||
use image::imageops::FilterType;
|
use image::imageops::FilterType;
|
||||||
use ratatui::layout::{Alignment, Rect};
|
use ratatui::layout::{Alignment, Rect};
|
||||||
@ -29,24 +27,27 @@ impl Hash for CachedImageData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl CachedImageData {
|
impl CachedImageData {
|
||||||
pub fn new(rgba_image: DynamicImage) -> Self{
|
pub fn new(rgba_image: DynamicImage) -> Self {
|
||||||
CachedImageData{image: rgba_image}
|
CachedImageData { image: rgba_image }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn height(&self) -> u32{
|
pub fn height(&self) -> u32 {
|
||||||
self.image.height()
|
self.image.height()
|
||||||
}
|
}
|
||||||
pub fn width(&self) -> u32{
|
pub fn width(&self) -> u32 {
|
||||||
self.image.width()
|
self.image.width()
|
||||||
}
|
}
|
||||||
pub fn from_dynamic_image(
|
pub fn from_dynamic_image(dynamic_image: DynamicImage) -> Self {
|
||||||
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
|
||||||
let resized_image = if dynamic_image.width() > CACHED_WIDTH || dynamic_image.height() > CACHED_HEIGHT {
|
let resized_image = if dynamic_image.width() > CACHED_WIDTH
|
||||||
dynamic_image.resize(CACHED_WIDTH, CACHED_HEIGHT , FilterType::Nearest)
|
|| dynamic_image.height() > CACHED_HEIGHT
|
||||||
|
{
|
||||||
|
dynamic_image.resize(
|
||||||
|
CACHED_WIDTH,
|
||||||
|
CACHED_HEIGHT,
|
||||||
|
FilterType::Nearest,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
dynamic_image
|
dynamic_image
|
||||||
};
|
};
|
||||||
@ -55,12 +56,21 @@ impl CachedImageData {
|
|||||||
let rgba_image = resized_image.into_rgba8();
|
let rgba_image = resized_image.into_rgba8();
|
||||||
CachedImageData::new(DynamicImage::from(rgba_image))
|
CachedImageData::new(DynamicImage::from(rgba_image))
|
||||||
}
|
}
|
||||||
pub fn paragraph<'a>(&self, inner: Rect, preview_block: Block<'a>) -> Paragraph<'a> {
|
pub fn paragraph<'a>(
|
||||||
|
&self,
|
||||||
|
inner: Rect,
|
||||||
|
preview_block: Block<'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 image_rgba = if self.image.width() > preview_width || self.image.height() > preview_height {
|
let image_rgba = if self.image.width() > preview_width
|
||||||
&self.image.resize(preview_width, preview_height, FILTER_TYPE).into_rgba8()
|
|| self.image.height() > preview_height
|
||||||
} else{
|
{
|
||||||
|
&self
|
||||||
|
.image
|
||||||
|
.resize(preview_width, preview_height, FILTER_TYPE)
|
||||||
|
.into_rgba8()
|
||||||
|
} else {
|
||||||
self.image.as_rgba8().expect("to be rgba8 image") // converted into rgba8 before being put into the cache, so it should never enter the expect
|
self.image.as_rgba8().expect("to be rgba8 image") // converted into rgba8 before being put into the cache, so it should never enter the expect
|
||||||
};
|
};
|
||||||
// transform it into text
|
// transform it into text
|
||||||
@ -68,44 +78,27 @@ impl CachedImageData {
|
|||||||
// iter over pair of rows
|
// iter over pair of rows
|
||||||
.rows()
|
.rows()
|
||||||
.step_by(2)
|
.step_by(2)
|
||||||
.zip(image_rgba.rows().skip(1).step_by(2)).enumerate()
|
.zip(image_rgba.rows().skip(1).step_by(2))
|
||||||
.map(|(double_row_y, (row_1, row_2))|
|
.enumerate()
|
||||||
Line::from_iter(row_1.into_iter().zip(row_2.into_iter()).enumerate().map(
|
.map(|(double_row_y, (row_1, row_2))| {
|
||||||
|(x, (color_up, color_down))| {
|
Line::from_iter(
|
||||||
convert_pixel_to_span(
|
row_1.into_iter().zip(row_2).enumerate().map(
|
||||||
color_up,
|
|(x, (color_up, color_down))| {
|
||||||
color_down,
|
convert_pixel_to_span(
|
||||||
(x, double_row_y),
|
color_up,
|
||||||
)}
|
color_down,
|
||||||
|
(x, double_row_y),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
)
|
)
|
||||||
))
|
})
|
||||||
.collect::<Vec<Line>>();
|
.collect::<Vec<Line>>();
|
||||||
let text_image = Text::from(lines);
|
let text_image = Text::from(lines);
|
||||||
Paragraph::new(text_image)
|
Paragraph::new(text_image)
|
||||||
.block(preview_block)
|
.block(preview_block)
|
||||||
.alignment(Alignment::Center)
|
.alignment(Alignment::Center)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn calculate_fit_dimensions(original_width: u32, original_height: u32, max_width: u32, max_height: u32) -> (u32, u32) {
|
|
||||||
if original_width <= max_width && original_height <= max_height {
|
|
||||||
return (original_width, original_height);
|
|
||||||
}
|
|
||||||
|
|
||||||
let width_ratio = f64::from(max_width) / f64::from(original_width);
|
|
||||||
let height_ratio = f64::from(max_height) / f64::from(original_height);
|
|
||||||
|
|
||||||
let scale = width_ratio.min(height_ratio);
|
|
||||||
|
|
||||||
|
|
||||||
let new_width = u32::try_from((f64::from(original_width) * scale).round() as u64)
|
|
||||||
.unwrap_or(u32::MAX);
|
|
||||||
let new_height = u32::try_from((f64::from(original_height) * scale).round() as u64)
|
|
||||||
.unwrap_or(u32::MAX);
|
|
||||||
|
|
||||||
(new_width, new_height)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn convert_pixel_to_span<'a>(
|
pub fn convert_pixel_to_span<'a>(
|
||||||
@ -141,7 +134,6 @@ pub fn convert_pixel_to_span<'a>(
|
|||||||
let color_up = convert_image_color_to_ratatui_color(color_up);
|
let color_up = convert_image_color_to_ratatui_color(color_up);
|
||||||
let color_down = convert_image_color_to_ratatui_color(color_down);
|
let color_down = convert_image_color_to_ratatui_color(color_down);
|
||||||
|
|
||||||
|
|
||||||
let style = Style::default().fg(color_up).bg(color_down);
|
let style = Style::default().fg(color_up).bg(color_down);
|
||||||
Span::styled(String::from(PIXEL), style)
|
Span::styled(String::from(PIXEL), style)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user