Skip to content

Commit

Permalink
width config
Browse files Browse the repository at this point in the history
  • Loading branch information
aeosynth committed Jun 13, 2020
1 parent 2e3f499 commit fab97b4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 52 deletions.
15 changes: 11 additions & 4 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
2 changes: 1 addition & 1 deletion src/epub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
87 changes: 47 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
});

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

0 comments on commit fab97b4

Please sign in to comment.