aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorJames Campos <james.r.campos@gmail.com>2020-06-14 12:22:10 -0700
committerJames Campos <james.r.campos@gmail.com>2020-06-14 12:22:10 -0700
commit1b8a8b27f7e79d68583e8eef5d15c33f7dd9a955 (patch)
tree00c9452adf19126331f54db343a8d8a3add12f8e /src/main.rs
parentfab97b4d390a1a755b72d24a3d93eb2085b4f441 (diff)
downloadbk-1b8a8b27f7e79d68583e8eef5d15c33f7dd9a955.tar.gz
pico -> argh
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 4911551..1d16440 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,8 @@
use std::io::{stdout, Write};
use std::{cmp::min, collections::HashMap, env, fs, iter, process::exit};
+use argh::FromArgs;
+
use crossterm::{
cursor,
event::{self, Event, KeyCode},
@@ -349,7 +351,7 @@ struct Bk<'a> {
}
impl Bk<'_> {
- fn new(epub: epub::Epub, args: Args) -> Self {
+ fn new(epub: epub::Epub, args: Props) -> Self {
let (cols, rows) = terminal::size().unwrap();
let width = min(cols, args.width) as usize;
let mut chapters = Vec::with_capacity(epub.chapters.len());
@@ -507,18 +509,27 @@ impl Bk<'_> {
}
}
+#[derive(FromArgs)]
+/// read a book
struct Args {
+ #[argh(positional)]
+ path: Option<String>,
+
+ /// characters per line
+ #[argh(option, short = 'w', default = "75")]
+ width: u16,
+}
+
+struct Props {
chapter: usize,
line: usize,
width: u16,
}
-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()
+fn restore(save_path: &str) -> Option<(String, Props)> {
+ let args: Args = argh::from_env();
+ let width = args.width;
+ let path = args.path
.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();
@@ -544,7 +555,7 @@ fn restore(save_path: &str) -> Option<(String, Args)> {
Some((
path,
- Args {
+ Props {
chapter,
line,
width,
@@ -560,7 +571,7 @@ fn main() {
};
let (path, args) = restore(&save_path).unwrap_or_else(|| {
- println!("usage: bk [flags] [path]");
+ println!("error: need a path");
exit(1);
});
@@ -570,11 +581,11 @@ fn main() {
});
let mut bk = Bk::new(epub, args);
- // crossterm really shouldn't error
+ // i have never seen crossterm error
bk.run().unwrap();
fs::write(save_path, format!("{}\n{}\n{}", path, bk.chapter, bk.line)).unwrap_or_else(|e| {
- println!("error saving position: {}", e);
+ println!("error saving state: {}", e);
exit(1);
});
}