From 0c7e26c49103ddc6cb835c7faaf1923c66d724f2 Mon Sep 17 00:00:00 2001 From: James Campos Date: Thu, 30 Apr 2020 23:09:47 -0700 Subject: internal --- README.md | 2 +- src/main.rs | 39 ++++++++++++++++++++------------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 45fc5c8..5c00a80 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # bk -bk is a WIP terminal epub reader +bk is a WIP terminal epub reader. currently only tested on linux. # install diff --git a/src/main.rs b/src/main.rs index fc8d305..dbfd284 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,13 +12,6 @@ use crossterm::{ use roxmltree::Document; -enum Mode { - Help, - Nav, - Read, - Search, -} - struct Epub { container: zip::ZipArchive, } @@ -38,7 +31,7 @@ impl Epub { let last = acc.last_mut().unwrap(); last.push(text); } - return + return; } match n.tag_name().name() { @@ -181,7 +174,7 @@ impl Epub { .into_iter() .enumerate() .map(|(i, path)| { - let title = toc.remove(path).unwrap_or(i.to_string()); + let title = toc.remove(path).unwrap_or_else(|| i.to_string()); let path = rootdir.join(path).to_str().unwrap().to_string(); (title, path) }) @@ -218,6 +211,20 @@ fn wrap(text: String, width: u16) -> Vec { wrapped } +struct Position(String, usize, usize); + +enum Direction { + Forward, + Backward, +} + +enum Mode { + Help, + Nav, + Read, + Search, +} + struct Bk { mode: Mode, epub: Epub, @@ -233,16 +240,9 @@ struct Bk { search: String, } -struct Position(String, usize, usize); -enum Direction { - Forward, - Backward, -} - impl Bk { - fn new(pos: &Position, pad: u16) -> Result { + fn new(mut epub: Epub, pos: &Position, pad: u16) -> Self { let (cols, rows) = terminal::size().unwrap(); - let mut epub = Epub::new(&pos.0)?; let mut bk = Bk { mode: Mode::Read, chapter: Vec::new(), @@ -258,7 +258,7 @@ impl Bk { search: String::new(), }; bk.get_chapter(pos.1); - Ok(bk) + bk } fn run(&mut self) -> crossterm::Result<()> { let mut stdout = stdout(); @@ -575,11 +575,12 @@ fn main() { std::process::exit(1); }); - let mut bk = Bk::new(&pos, 3).unwrap_or_else(|e| { + let epub = Epub::new(&pos.0).unwrap_or_else(|e| { println!("error reading epub: {}", e); std::process::exit(1); }); + let mut bk = Bk::new(epub, &pos, 3); bk.run().unwrap(); std::fs::write( -- cgit v1.2.3