diff options
author | James Campos <james.r.campos@gmail.com> | 2020-06-12 23:33:13 -0700 |
---|---|---|
committer | James Campos <james.r.campos@gmail.com> | 2020-06-12 23:33:13 -0700 |
commit | fab97b4d390a1a755b72d24a3d93eb2085b4f441 (patch) | |
tree | 79ff7a590d6787d749cca8299b0209d2ea4da871 | |
parent | 2e3f4993c77b5cd986ca535d22770810701c1ad4 (diff) | |
download | bk-fab97b4d390a1a755b72d24a3d93eb2085b4f441.tar.gz |
width config
-rw-r--r-- | Cargo.lock | 15 | ||||
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | src/epub.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 87 |
5 files changed, 68 insertions, 52 deletions
@@ -8,9 +8,9 @@ checksum = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2" [[package]] name = "arc-swap" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b585a98a234c46fc563103e9278c9391fde1f4e6850334da895d27edb9580f62" +checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" [[package]] name = "bitflags" @@ -23,6 +23,7 @@ name = "bk" version = "0.1.1" dependencies = [ "crossterm", + "pico-args", "roxmltree", "zip", ] @@ -155,9 +156,9 @@ dependencies = [ [[package]] name = "miniz_oxide" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa679ff6578b1cddee93d7e82e263b94a575e0bfced07284eb0c037c1d2416a5" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" dependencies = [ "adler32", ] @@ -229,6 +230,12 @@ dependencies = [ ] [[package]] +name = "pico-args" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc" + +[[package]] name = "podio" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -11,10 +11,11 @@ readme = "README.md" repository = "https://github.com/aeosynth/bk" [dependencies] -crossterm = "0.17" -roxmltree = "0.11" +crossterm = "*" +pico-args = "*" +roxmltree = "*" [dependencies.zip] -version = "0.5" +version = "*" default-features = false features = ["deflate"] @@ -22,18 +22,19 @@ or from github: then run: - bk path/to/epub + bk [flags] [path] -Type any function key (eg <kbd>F1</kbd>) to see the commands. +Running `bk` without a path will load the most recent Epub. + +The `-w` flag sets the line width. -Running `bk` without an argument will load the most recent Epub. +Type any function key (eg <kbd>F1</kbd>) to see the commands. # TODO - configuration - better html support - better unicode support - mobi? -- images? - css? - gui? diff --git a/src/epub.rs b/src/epub.rs index 795f4d1..f850add 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -53,7 +53,7 @@ impl Epub { } buf.push_str("\x1b\x5b0m\n"); } - "blockquote" | "p" => { + "blockquote" | "p" | "tr" => { buf.push('\n'); for c in n.children() { Self::render(buf, c); diff --git a/src/main.rs b/src/main.rs index c66bf64..4911551 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::io::{stdout, Write}; -use std::{cmp::min, collections::HashMap, env, iter, process::exit}; +use std::{cmp::min, collections::HashMap, env, fs, iter, process::exit}; use crossterm::{ cursor, @@ -349,9 +349,9 @@ struct Bk<'a> { } impl Bk<'_> { - fn new(epub: epub::Epub, chapter: usize, line: usize, max_width: u16) -> Self { + fn new(epub: epub::Epub, args: Args) -> Self { let (cols, rows) = terminal::size().unwrap(); - let width = min(cols, max_width) as usize; + let width = min(cols, args.width) as usize; let mut chapters = Vec::with_capacity(epub.chapters.len()); for (title, text) in epub.chapters { @@ -375,12 +375,12 @@ impl Bk<'_> { Bk { chapters, - chapter, - line, + chapter: args.chapter, + line: args.line, mark: HashMap::new(), cols, rows: rows as usize, - max_width, + max_width: args.width, view: Some(&Page), dir: Direction::Forward, nav_top: 0, @@ -507,41 +507,49 @@ impl Bk<'_> { } } -fn restore(save_path: &str) -> Option<(String, usize, usize)> { - let path = env::args().nth(1); - let save = std::fs::read_to_string(save_path); +struct Args { + chapter: usize, + line: usize, + width: u16, +} - let get_save = |s: String| { +fn restore(save_path: &str) -> Option<(String, Args)> { + let mut args = pico_args::Arguments::from_env(); + let width = args.opt_value_from_str("-w").unwrap().unwrap_or(75); + let free = args.free().unwrap(); + let path = free + .first() + .and_then(|s| Some(fs::canonicalize(s).unwrap().to_str().unwrap().to_string())); + let save = fs::read_to_string(save_path).and_then(|s| { let mut lines = s.lines(); - ( + Ok(( lines.next().unwrap().to_string(), lines.next().unwrap().parse::<usize>().unwrap(), lines.next().unwrap().parse::<usize>().unwrap(), - ) - }; - - let canon = |s: String| { - std::fs::canonicalize(s) - .unwrap() - .to_str() - .unwrap() - .to_string() - }; + )) + }); - // XXX move errors when i try to refactor - match (save, path) { - (Err(_), None) => None, - (Err(_), Some(path)) => Some((canon(path), 0, 0)), - (Ok(save), None) => Some(get_save(save)), + let (path, chapter, line) = match (save, path) { + (Err(_), None) => return None, + (Err(_), Some(path)) => (path, 0, 0), + (Ok(save), None) => save, (Ok(save), Some(path)) => { - let save = get_save(save); - if path == save.0 { - Some(save) + if save.0 == path { + save } else { - Some((canon(path), 0, 0)) + (path, 0, 0) } } - } + }; + + Some(( + path, + Args { + chapter, + line, + width, + }, + )) } fn main() { @@ -550,8 +558,9 @@ fn main() { } else { format!("{}/.local/share/bk", env::var("HOME").unwrap()) }; - let (path, chapter, line) = restore(&save_path).unwrap_or_else(|| { - println!("usage: bk path"); + + let (path, args) = restore(&save_path).unwrap_or_else(|| { + println!("usage: bk [flags] [path]"); exit(1); }); @@ -560,14 +569,12 @@ fn main() { exit(1); }); - let mut bk = Bk::new(epub, chapter, line, 75); + let mut bk = Bk::new(epub, args); // crossterm really shouldn't error bk.run().unwrap(); - std::fs::write(save_path, format!("{}\n{}\n{}", path, bk.chapter, bk.line)).unwrap_or_else( - |e| { - println!("error saving position: {}", e); - exit(1); - }, - ); + fs::write(save_path, format!("{}\n{}\n{}", path, bk.chapter, bk.line)).unwrap_or_else(|e| { + println!("error saving position: {}", e); + exit(1); + }); } |