Skip to content

Commit 39a4bda

Browse files
authored
small cleanups (#129)
- don't parse color strings for defaults - fix comment misstating type of vec - impl size_hint and ExactSizeIterator for PopOnNext
1 parent 19030f7 commit 39a4bda

2 files changed

Lines changed: 46 additions & 19 deletions

File tree

‎src/main.rs‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use tdf::{
3939
PrerenderLimit,
4040
converter::{ConvertedPage, ConverterMsg, run_conversion_loop},
4141
kitty::{KittyDisplay, display_kitty_images, do_shms_work, run_action},
42-
renderer::{self, RenderError, RenderInfo, RenderNotif},
42+
renderer::{self, MUPDF_BLACK, MUPDF_WHITE, RenderError, RenderInfo, RenderNotif},
4343
tui::{BottomMessage, InputAction, MessageSetting, Tui}
4444
};
4545

@@ -128,21 +128,37 @@ async fn inner_main() -> Result<(), WrappedErr> {
128128
.canonicalize()
129129
.map_err(|e| WrappedErr(format!("Cannot canonicalize provided file: {e}").into()))?;
130130

131-
let black =
132-
parse_color_to_i32(flags.black_color.as_deref().unwrap_or("000000")).map_err(|e| {
133-
WrappedErr(
134-
format!("Couldn't parse black color: {e} - is it formatted like a CSS color?")
131+
let black = flags
132+
.black_color
133+
.as_deref()
134+
.map(|color| {
135+
parse_color_to_i32(color).map_err(|e| {
136+
WrappedErr(
137+
format!(
138+
"Couldn't parse black color {color:?}: {e} - is it formatted like a CSS color?"
139+
)
135140
.into()
136-
)
137-
})?;
138-
139-
let white =
140-
parse_color_to_i32(flags.white_color.as_deref().unwrap_or("FFFFFF")).map_err(|e| {
141-
WrappedErr(
142-
format!("Couldn't parse white color: {e} - is it formatted like a CSS color?")
141+
)
142+
})
143+
})
144+
.transpose()?
145+
.unwrap_or(MUPDF_BLACK);
146+
147+
let white = flags
148+
.white_color
149+
.as_deref()
150+
.map(|color| {
151+
parse_color_to_i32(color).map_err(|e| {
152+
WrappedErr(
153+
format!(
154+
"Couldn't parse white color {color:?}: {e} - is it formatted like a CSS color?"
155+
)
143156
.into()
144-
)
145-
})?;
157+
)
158+
})
159+
})
160+
.transpose()?
161+
.unwrap_or(MUPDF_WHITE);
146162

147163
// need to keep it around throughout the lifetime of the program, but don't rly need to use it.
148164
// Just need to make sure it doesn't get dropped yet.

‎src/renderer.rs‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ struct PrevRender {
5757
num_search_found: Option<usize>
5858
}
5959

60-
const MUPDF_BLACK: i32 = 0;
61-
const MUPDF_WHITE: i32 = i32::from_be_bytes([0, 0xff, 0xff, 0xff]);
60+
pub const MUPDF_BLACK: i32 = 0;
61+
pub const MUPDF_WHITE: i32 = i32::from_be_bytes([0, 0xff, 0xff, 0xff]);
6262

6363
#[inline]
6464
pub fn fill_default<T: Default>(vec: &mut Vec<T>, size: usize) {
@@ -152,8 +152,8 @@ pub fn start_rendering(
152152

153153
sender.send(Ok(RenderInfo::NumPages(n_pages.get())))?;
154154

155-
// We're using this vec of bools to indicate which page numbers have already been rendered,
156-
// to support people jumping to specific pages and having quick rendering results. We
155+
// We're using this vec to indicate which page numbers have already been rendered, to
156+
// support people jumping to specific pages and having quick rendering results. We
157157
// `split_at_mut` at 0 initially (which bascially makes `right == rendered && left == []`),
158158
// doing basically nothing, but if we get a notification that something has been jumped to,
159159
// then we can split at that page and render at both sides of it
@@ -277,7 +277,7 @@ pub fn start_rendering(
277277
// we only want to continue if one of the following is met:
278278
// 1. It failed to render last time (we want to retry)
279279
// 2. The `contained_term` is set to Unknown, meaning that we need to at least
280-
// check if it contains the current term to see if it needs a re-render
280+
// check if it contains the current term to see if it needs a re-render
281281
if rendered.successful && rendered.num_search_found.is_some() {
282282
continue;
283283
}
@@ -570,4 +570,15 @@ impl Iterator for PopOnNext<'_> {
570570
fn next(&mut self) -> Option<Self::Item> {
571571
self.inner.pop_front()
572572
}
573+
574+
fn size_hint(&self) -> (usize, Option<usize>) {
575+
let l = self.len();
576+
(l, Some(l))
577+
}
578+
}
579+
580+
impl ExactSizeIterator for PopOnNext<'_> {
581+
fn len(&self) -> usize {
582+
self.inner.len()
583+
}
573584
}

0 commit comments

Comments
 (0)