mirror of
https://github.com/itsjunetime/tdf.git
synced 2026-06-02 08:01:47 -04:00
Format and fix clippy unnecessary 'as'
This commit is contained in:
+23
-16
@@ -1,7 +1,10 @@
|
|||||||
use image::ImageFormat;
|
use image::ImageFormat;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ratatui_image::{picker::Picker, protocol::Protocol, Resize};
|
use ratatui_image::{picker::Picker, protocol::Protocol, Resize};
|
||||||
use tokio::sync::mpsc::{error::{SendError, TryRecvError}, UnboundedReceiver, UnboundedSender};
|
use tokio::sync::mpsc::{
|
||||||
|
error::{SendError, TryRecvError},
|
||||||
|
UnboundedReceiver, UnboundedSender
|
||||||
|
};
|
||||||
|
|
||||||
use crate::renderer::{fill_default, PageInfo, RenderError};
|
use crate::renderer::{fill_default, PageInfo, RenderError};
|
||||||
|
|
||||||
@@ -34,7 +37,7 @@ pub async fn run_conversion_loop(
|
|||||||
iteration: &mut usize
|
iteration: &mut usize
|
||||||
) -> Result<Option<ConvertedPage>, RenderError> {
|
) -> Result<Option<ConvertedPage>, RenderError> {
|
||||||
if images.is_empty() || *iteration >= MAX_ITER {
|
if images.is_empty() || *iteration >= MAX_ITER {
|
||||||
return Ok(None)
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This kinda mimics the way the renderer alternates between going above and below the
|
// This kinda mimics the way the renderer alternates between going above and below the
|
||||||
@@ -50,20 +53,28 @@ pub async fn run_conversion_loop(
|
|||||||
.skip(*iteration)
|
.skip(*iteration)
|
||||||
.find_map(|(i_idx, p_idx)| images[p_idx].take().map(|p| (p, i_idx)))
|
.find_map(|(i_idx, p_idx)| images[p_idx].take().map(|p| (p, i_idx)))
|
||||||
else {
|
else {
|
||||||
return Ok(None)
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let img_area = page_info.img_data.area;
|
let img_area = page_info.img_data.area;
|
||||||
|
|
||||||
let dyn_img = image::load_from_memory_with_format(&page_info.img_data.data, ImageFormat::Png)
|
let dyn_img =
|
||||||
.map_err(|e| RenderError::Render(format!("Couldn't convert Vec<u8> to DynamicImage: {e}")))?;
|
image::load_from_memory_with_format(&page_info.img_data.data, ImageFormat::Png)
|
||||||
|
.map_err(|e| {
|
||||||
|
RenderError::Render(format!("Couldn't convert Vec<u8> to DynamicImage: {e}"))
|
||||||
|
})?;
|
||||||
|
|
||||||
// We don't actually want to Crop this image, but we've already
|
// We don't actually want to Crop this image, but we've already
|
||||||
// verified (with the ImageSurface stuff) that the image is the correct
|
// verified (with the ImageSurface stuff) that the image is the correct
|
||||||
// size for the area given, so to save ratatui the work of having to
|
// size for the area given, so to save ratatui the work of having to
|
||||||
// resize it, we tell them to crop it to fit.
|
// resize it, we tell them to crop it to fit.
|
||||||
let txt_img = picker.new_protocol(dyn_img, img_area, Resize::Crop)
|
let txt_img = picker
|
||||||
.map_err(|e| RenderError::Render(format!("Couldn't convert DynamicImage to ratatui image: {e}")))?;
|
.new_protocol(dyn_img, img_area, Resize::Crop)
|
||||||
|
.map_err(|e| {
|
||||||
|
RenderError::Render(format!(
|
||||||
|
"Couldn't convert DynamicImage to ratatui image: {e}"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
// update the iteration to the iteration that we stole this image from
|
// update the iteration to the iteration that we stole this image from
|
||||||
*iteration = new_iter;
|
*iteration = new_iter;
|
||||||
@@ -75,21 +86,17 @@ pub async fn run_conversion_loop(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_notif(
|
fn handle_notif(msg: ConverterMsg, images: &mut Vec<Option<PageInfo>>, page: &mut usize) {
|
||||||
msg: ConverterMsg,
|
|
||||||
images: &mut Vec<Option<PageInfo>>,
|
|
||||||
page: &mut usize
|
|
||||||
) {
|
|
||||||
match msg {
|
match msg {
|
||||||
ConverterMsg::AddImg(img) => {
|
ConverterMsg::AddImg(img) => {
|
||||||
let page_num = img.page;
|
let page_num = img.page;
|
||||||
images[page_num] = Some(img);
|
images[page_num] = Some(img);
|
||||||
},
|
}
|
||||||
ConverterMsg::NumPages(n_pages) => {
|
ConverterMsg::NumPages(n_pages) => {
|
||||||
fill_default(images, n_pages);
|
fill_default(images, n_pages);
|
||||||
*page = (*page).min(n_pages - 1);
|
*page = (*page).min(n_pages - 1);
|
||||||
},
|
}
|
||||||
ConverterMsg::GoToPage(new_page) => *page = new_page,
|
ConverterMsg::GoToPage(new_page) => *page = new_page
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +107,7 @@ pub async fn run_conversion_loop(
|
|||||||
Ok(msg) => {
|
Ok(msg) => {
|
||||||
handle_notif(msg, &mut images, &mut page);
|
handle_notif(msg, &mut images, &mut page);
|
||||||
continue 'outer;
|
continue 'outer;
|
||||||
},
|
}
|
||||||
Err(TryRecvError::Empty) => (),
|
Err(TryRecvError::Empty) => (),
|
||||||
Err(TryRecvError::Disconnected) => panic!("Disconnected :(")
|
Err(TryRecvError::Disconnected) => panic!("Disconnected :(")
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-8
@@ -6,17 +6,17 @@ use converter::{run_conversion_loop, ConvertedPage, ConverterMsg};
|
|||||||
use crossterm::{
|
use crossterm::{
|
||||||
execute,
|
execute,
|
||||||
terminal::{
|
terminal::{
|
||||||
disable_raw_mode, enable_raw_mode, window_size, EndSynchronizedUpdate, EnterAlternateScreen, LeaveAlternateScreen
|
disable_raw_mode, enable_raw_mode, window_size, EndSynchronizedUpdate,
|
||||||
|
EnterAlternateScreen, LeaveAlternateScreen
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
use futures_util::stream::StreamExt;
|
use futures_util::{stream::StreamExt, FutureExt};
|
||||||
use glib::{LogField, LogLevel, LogWriterOutput};
|
use glib::{LogField, LogLevel, LogWriterOutput};
|
||||||
use notify::{RecursiveMode, Watcher};
|
use notify::{RecursiveMode, Watcher};
|
||||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
use ratatui::{backend::CrosstermBackend, Terminal};
|
||||||
use ratatui_image::picker::Picker;
|
use ratatui_image::picker::Picker;
|
||||||
use renderer::{RenderInfo, RenderNotif};
|
use renderer::{RenderInfo, RenderNotif};
|
||||||
use tui::{InputAction, Tui};
|
use tui::{InputAction, Tui};
|
||||||
use futures_util::FutureExt;
|
|
||||||
|
|
||||||
mod converter;
|
mod converter;
|
||||||
mod renderer;
|
mod renderer;
|
||||||
@@ -39,9 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// This shouldn't fail to send unless the receiver gets disconnected. If that's happened,
|
// This shouldn't fail to send unless the receiver gets disconnected. If that's happened,
|
||||||
// then like the main thread has panicked or something, so it doesn't matter if this panics
|
// then like the main thread has panicked or something, so it doesn't matter if this panics
|
||||||
// as well
|
// as well
|
||||||
watch_tx
|
watch_tx.send(renderer::RenderNotif::Reload).unwrap();
|
||||||
.send(renderer::RenderNotif::Reload)
|
|
||||||
.unwrap();
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// We're making this nonrecursive 'cause we're just watching a single file, so there's nothing
|
// We're making this nonrecursive 'cause we're just watching a single file, so there's nothing
|
||||||
@@ -55,13 +53,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
// We need to create `picker` on this thread because if we create it on the `renderer` thread,
|
// We need to create `picker` on this thread because if we create it on the `renderer` thread,
|
||||||
// it messes up something with user input. Input never makes it to the crossterm thing
|
// it messes up something with user input. Input never makes it to the crossterm thing
|
||||||
let mut picker = Picker::new((window_size.width / window_size.columns, window_size.height / window_size.rows));
|
let mut picker = Picker::new((
|
||||||
|
window_size.width / window_size.columns,
|
||||||
|
window_size.height / window_size.rows
|
||||||
|
));
|
||||||
picker.guess_protocol();
|
picker.guess_protocol();
|
||||||
|
|
||||||
// then we want to spawn off the rendering task
|
// then we want to spawn off the rendering task
|
||||||
// We need to use the thread::spawn API so that this exists in a thread not owned by tokio,
|
// We need to use the thread::spawn API so that this exists in a thread not owned by tokio,
|
||||||
// since the methods we call in `start_rendering` will panic if called in an async context
|
// since the methods we call in `start_rendering` will panic if called in an async context
|
||||||
std::thread::spawn(move || renderer::start_rendering(file_path, render_tx, render_rx, window_size));
|
std::thread::spawn(move || {
|
||||||
|
renderer::start_rendering(file_path, render_tx, render_rx, window_size)
|
||||||
|
});
|
||||||
|
|
||||||
let mut ev_stream = crossterm::event::EventStream::new();
|
let mut ev_stream = crossterm::event::EventStream::new();
|
||||||
|
|
||||||
|
|||||||
+12
-7
@@ -89,9 +89,7 @@ pub fn start_rendering(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let n_pages = doc.n_pages() as usize;
|
let n_pages = doc.n_pages() as usize;
|
||||||
sender
|
sender.send(Ok(RenderInfo::NumPages(n_pages))).unwrap();
|
||||||
.send(Ok(RenderInfo::NumPages(n_pages)))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// We're using this vec of bools to indicate which page numbers have already been rendered,
|
// We're using this vec of bools to indicate which page numbers have already been rendered,
|
||||||
// to support people jumping to specific pages and having quick rendering results. We
|
// to support people jumping to specific pages and having quick rendering results. We
|
||||||
@@ -204,7 +202,14 @@ pub fn start_rendering(
|
|||||||
rendered.successful && rendered.contained_term == Some(false);
|
rendered.successful && rendered.contained_term == Some(false);
|
||||||
|
|
||||||
// render the page
|
// render the page
|
||||||
match render_single_page(page, area, num, &search_term, rendered_with_no_results, &size) {
|
match render_single_page(
|
||||||
|
page,
|
||||||
|
area,
|
||||||
|
num,
|
||||||
|
&search_term,
|
||||||
|
rendered_with_no_results,
|
||||||
|
&size
|
||||||
|
) {
|
||||||
// If we've already rendered it just fine and we don't need to render it again,
|
// If we've already rendered it just fine and we don't need to render it again,
|
||||||
// just continue. We're all good
|
// just continue. We're all good
|
||||||
Ok(None) => (),
|
Ok(None) => (),
|
||||||
@@ -216,7 +221,7 @@ pub fn start_rendering(
|
|||||||
rendered.contained_term = Some(img.search_results > 0);
|
rendered.contained_term = Some(img.search_results > 0);
|
||||||
rendered.successful = true;
|
rendered.successful = true;
|
||||||
sender.send(Ok(RenderInfo::Page(img))).unwrap()
|
sender.send(Ok(RenderInfo::Page(img))).unwrap()
|
||||||
},
|
}
|
||||||
// And if we got an error, then obviously we need to propagate that
|
// And if we got an error, then obviously we need to propagate that
|
||||||
Err(e) => sender.send(Err(RenderError::Render(e))).unwrap()
|
Err(e) => sender.send(Err(RenderError::Render(e))).unwrap()
|
||||||
}
|
}
|
||||||
@@ -279,9 +284,9 @@ fn render_single_page(
|
|||||||
// scale the height to fit perfectly. The dimension that _is not_ scaled to fit perfectly
|
// scale the height to fit perfectly. The dimension that _is not_ scaled to fit perfectly
|
||||||
// is scaled by the same factor as the dimension that _is_ scaled perfectly.
|
// is scaled by the same factor as the dimension that _is_ scaled perfectly.
|
||||||
let scale_factor = if p_aspect_ratio > area_aspect_ratio {
|
let scale_factor = if p_aspect_ratio > area_aspect_ratio {
|
||||||
area_full_w as f64 / p_width
|
area_full_w / p_width
|
||||||
} else {
|
} else {
|
||||||
area_full_h as f64 / p_height
|
area_full_h / p_height
|
||||||
};
|
};
|
||||||
|
|
||||||
let surface_width = p_width * scale_factor;
|
let surface_width = p_width * scale_factor;
|
||||||
|
|||||||
+5
-2
@@ -348,10 +348,13 @@ impl Tui {
|
|||||||
term.push(c);
|
term.push(c);
|
||||||
Some(InputAction::Redraw)
|
Some(InputAction::Redraw)
|
||||||
}
|
}
|
||||||
KeyCode::Backspace if let BottomMessage::Input(InputCommand::Search(ref mut term)) = self.bottom_msg => {
|
KeyCode::Backspace
|
||||||
|
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
|
||||||
|
self.bottom_msg =>
|
||||||
|
{
|
||||||
term.pop();
|
term.pop();
|
||||||
Some(InputAction::Redraw)
|
Some(InputAction::Redraw)
|
||||||
},
|
}
|
||||||
KeyCode::Char(c)
|
KeyCode::Char(c)
|
||||||
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
|
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
|
||||||
self.bottom_msg =>
|
self.bottom_msg =>
|
||||||
|
|||||||
Reference in New Issue
Block a user