Skip to content

Commit

Permalink
pico -> argh
Browse files Browse the repository at this point in the history
  • Loading branch information
aeosynth committed Jun 14, 2020
1 parent fab97b4 commit 1b8a8b2
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 21 deletions.
91 changes: 82 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 11,8 @@ readme = "README.md"
repository = "https://github.com/aeosynth/bk"

[dependencies]
argh = "*"
crossterm = "*"
pico-args = "*"
roxmltree = "*"

[dependencies.zip]
Expand Down
33 changes: 22 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -544,7 555,7 @@ fn restore(save_path: &str) -> Option<(String, Args)> {

Some((
path,
Args {
Props {
chapter,
line,
width,
Expand All @@ -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);
});

Expand All @@ -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);
});
}

0 comments on commit 1b8a8b2

Please sign in to comment.