1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
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},
queue,
style::{Attribute, Print},
terminal,
};
mod epub;
// XXX assumes a char is i unit wide
fn wrap(text: &str, width: usize) -> Vec<(usize, String)> {
let mut lines = Vec::new();
// bytes
let mut start = 0;
let mut end = 0;
// chars after the break
let mut after = 0;
// chars in unbroken line
let mut len = 0;
// are we breaking on whitespace?
let mut skip = false;
for (i, c) in text.char_indices() {
len += 1;
match c {
'\n' => {
after = 0;
end = i;
skip = true;
len = width + 1;
}
' ' => {
after = 0;
end = i;
skip = true;
}
'-' | '—' if len <= width => {
after = 0;
end = i + c.len_utf8();
skip = false;
}
_ => after += 1,
}
if len > width {
if len == after {
after = 1;
end = i;
skip = false;
}
lines.push((start, String::from(&text[start..end])));
start = end;
if skip {
start += 1;
}
len = after;
}
}
lines
}
#[derive(Clone)]
enum Direction {
Forward,
Backward,
}
trait View {
fn run(&self, bk: &mut Bk, kc: KeyCode);
fn render(&self, bk: &Bk) -> Vec<String>;
}
// TODO render something useful?
struct Mark;
impl View for Mark {
fn run(&self, bk: &mut Bk, kc: KeyCode) {
if let KeyCode::Char(c) = kc {
bk.mark(c)
}
bk.view = Some(&Page)
}
fn render(&self, bk: &Bk) -> Vec<String> {
Page::render(&Page, bk)
}
}
struct Jump;
impl View for Jump {
fn run(&self, bk: &mut Bk, kc: KeyCode) {
if let KeyCode::Char(c) = kc {
bk.jump(c)
}
bk.view = Some(&Page);
}
fn render(&self, bk: &Bk) -> Vec<String> {
Page::render(&Page, bk)
}
}
struct Help;
impl View for Help {
fn run(&self, bk: &mut Bk, _: KeyCode) {
bk.view = Some(&Page);
}
fn render(&self, _: &Bk) -> Vec<String> {
let text = r#"
Esc q Quit
Fn Help
/ Search Forward
? Search Backward
Tab Table of Contents
PageDown Right Space f l Page Down
PageUp Left b h Page Up
d Half Page Down
u Half Page Up
Down j Line Down
Up k Line Up
Home g Chapter Start
End G Chapter End
[ Previous Chapter
] Next Chapter
n Repeat search forward
N Repeat search backward
mx Set mark x
'x Jump to mark x
"#;
text.lines().map(String::from).collect()
}
}
struct Nav;
impl View for Nav {
fn run(&self, bk: &mut Bk, kc: KeyCode) {
match kc {
KeyCode::Esc
| KeyCode::Tab
| KeyCode::Left
| KeyCode::Char('h')
| KeyCode::Char('q') => {
bk.jump_reset();
bk.view = Some(&Page);
}
KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => {
bk.line = 0;
bk.view = Some(&Page);
}
KeyCode::Down | KeyCode::Char('j') => {
if bk.chapter < bk.chapters.len() - 1 {
bk.chapter += 1;
if bk.chapter == bk.nav_top + bk.rows {
bk.nav_top += 1;
}
}
}
KeyCode::Up | KeyCode::Char('k') => {
if bk.chapter > 0 {
if bk.chapter == bk.nav_top {
bk.nav_top -= 1;
}
bk.chapter -= 1;
}
}
KeyCode::Home | KeyCode::Char('g') => {
bk.chapter = 0;
bk.nav_top = 0;
}
KeyCode::End | KeyCode::Char('G') => {
bk.chapter = bk.chapters.len() - 1;
bk.nav_top = bk.chapters.len().saturating_sub(bk.rows);
}
_ => (),
}
}
fn render(&self, bk: &Bk) -> Vec<String> {
let end = min(bk.nav_top + bk.rows, bk.chapters.len());
bk.chapters[bk.nav_top..end]
.iter()
.enumerate()
.map(|(i, chapter)| {
if bk.chapter == bk.nav_top + i {
format!(
"{}{}{}",
Attribute::Reverse,
chapter.title,
Attribute::Reset
)
} else {
chapter.title.to_string()
}
})
.collect()
}
}
struct Page;
impl View for Page {
fn run(&self, bk: &mut Bk, kc: KeyCode) {
match kc {
KeyCode::Esc | KeyCode::Char('q') => bk.view = None,
KeyCode::Tab => {
bk.nav_top = bk.chapter.saturating_sub(bk.rows - 1);
bk.mark('\'');
bk.view = Some(&Nav);
}
KeyCode::F(_) => bk.view = Some(&Help),
KeyCode::Char('?') => bk.start_search(Direction::Backward),
KeyCode::Char('/') => bk.start_search(Direction::Forward),
KeyCode::Char('m') => bk.view = Some(&Mark),
KeyCode::Char('\'') => bk.view = Some(&Jump),
KeyCode::Char('N') => {
bk.search(Direction::Backward);
}
KeyCode::Char('n') => {
// FIXME
bk.scroll_down(1);
bk.search(Direction::Forward);
}
KeyCode::End | KeyCode::Char('G') => {
bk.mark('\'');
bk.line = bk.lines().len().saturating_sub(bk.rows);
}
KeyCode::Home | KeyCode::Char('g') => {
bk.mark('\'');
bk.line = 0;
}
KeyCode::Char('d') => {
bk.scroll_down(bk.rows / 2);
}
KeyCode::Char('u') => {
bk.scroll_up(bk.rows / 2);
}
KeyCode::Up | KeyCode::Char('k') => {
bk.scroll_up(2);
}
KeyCode::Left | KeyCode::PageUp | KeyCode::Char('b') | KeyCode::Char('h') => {
bk.scroll_up(bk.rows);
}
KeyCode::Down | KeyCode::Char('j') => {
bk.scroll_down(2);
}
KeyCode::Right
| KeyCode::PageDown
| KeyCode::Char('f')
| KeyCode::Char('l')
| KeyCode::Char(' ') => {
bk.scroll_down(bk.rows);
}
KeyCode::Char('[') => bk.prev_chapter(),
KeyCode::Char(']') => bk.next_chapter(),
_ => (),
}
}
fn render(&self, bk: &Bk) -> Vec<String> {
let end = min(bk.line + bk.rows, bk.lines().len());
bk.lines()[bk.line..end].iter().map(String::from).collect()
}
}
struct Search;
impl View for Search {
fn run(&self, bk: &mut Bk, kc: KeyCode) {
match kc {
KeyCode::Esc => {
bk.jump_reset();
bk.view = Some(&Page);
}
KeyCode::Enter => {
bk.view = Some(&Page);
}
KeyCode::Backspace => {
bk.query.pop();
bk.jump_reset();
bk.search(bk.dir.clone());
}
KeyCode::Char(c) => {
bk.query.push(c);
if !bk.search(bk.dir.clone()) {
bk.jump_reset();
}
}
_ => (),
}
}
fn render(&self, bk: &Bk) -> Vec<String> {
let end = min(bk.line + bk.rows - 1, bk.lines().len());
let mut buf = Vec::with_capacity(bk.rows);
for line in bk.lines()[bk.line..end].iter() {
if let Some(i) = line.find(&bk.query) {
buf.push(format!(
"{}{}{}{}{}",
&line[..i],
Attribute::Reverse,
&bk.query,
Attribute::Reset,
&line[i + bk.query.len()..],
));
} else {
buf.push(String::from(line));
}
}
for _ in buf.len()..bk.rows - 1 {
buf.push(String::new());
}
let prefix = match bk.dir {
Direction::Forward => '/',
Direction::Backward => '?',
};
buf.push(format!("{}{}", prefix, bk.query));
buf
}
}
// search the text to find the byte index of the query, then find the containing line
// ideally we could use string slices as pointers, but self referential structs are hard
struct Chapter {
title: String,
text: String,
bytes: Vec<usize>,
lines: Vec<String>,
}
struct Bk<'a> {
chapters: Vec<Chapter>,
// position in the book
chapter: usize,
line: usize,
mark: HashMap<char, (usize, usize)>,
// terminal
cols: u16,
rows: usize,
// user config
max_width: u16,
// view state
view: Option<&'a dyn View>,
dir: Direction,
nav_top: usize,
query: String,
}
impl Bk<'_> {
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());
for (title, text) in epub.chapters {
let title = if title.chars().count() > width {
title
.chars()
.take(width - 1)
.chain(iter::once('…'))
.collect()
} else {
title
};
let (bytes, lines) = wrap(&text, width).into_iter().unzip();
chapters.push(Chapter {
title,
text,
lines,
bytes,
});
}
Bk {
line: min(
args.line,
chapters[args.chapter]
.lines
.len()
.saturating_sub(rows as usize),
),
chapters,
chapter: args.chapter,
mark: HashMap::new(),
cols,
rows: rows as usize,
max_width: args.width,
view: Some(&Page),
dir: Direction::Forward,
nav_top: 0,
query: String::new(),
}
}
fn run(&mut self) -> crossterm::Result<()> {
let mut stdout = stdout();
queue!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;
terminal::enable_raw_mode()?;
while let Some(view) = self.view {
let pad = self.cols.saturating_sub(self.max_width) / 2;
queue!(stdout, terminal::Clear(terminal::ClearType::All))?;
for (i, line) in view.render(self).iter().enumerate() {
queue!(stdout, cursor::MoveTo(pad, i as u16), Print(line))?;
}
stdout.flush().unwrap();
match event::read()? {
Event::Key(e) => view.run(self, e.code),
Event::Resize(cols, rows) => {
self.cols = cols;
self.rows = rows as usize;
}
// TODO
Event::Mouse(_) => (),
}
}
queue!(stdout, terminal::LeaveAlternateScreen, cursor::Show)?;
terminal::disable_raw_mode()
}
fn mark(&mut self, c: char) {
self.mark.insert(c, (self.chapter, self.line));
}
fn jump(&mut self, c: char) {
if let Some(&(c, l)) = self.mark.get(&c) {
let jump = (self.chapter, self.line);
self.chapter = c;
self.line = l;
self.mark.insert('\'', jump);
}
}
fn jump_reset(&mut self) {
let &(c, l) = self.mark.get(&'\'').unwrap();
self.chapter = c;
self.line = l;
}
fn lines(&self) -> &Vec<String> {
&self.chapters[self.chapter].lines
}
fn next_chapter(&mut self) {
if self.chapter < self.chapters.len() - 1 {
self.chapter += 1;
self.line = 0;
}
}
fn prev_chapter(&mut self) {
if self.chapter > 0 {
self.chapter -= 1;
self.line = 0;
}
}
fn scroll_down(&mut self, n: usize) {
if self.line + self.rows < self.lines().len() {
self.line += n;
} else {
self.next_chapter();
}
}
fn scroll_up(&mut self, n: usize) {
if self.line > 0 {
self.line = self.line.saturating_sub(n);
} else {
self.prev_chapter();
self.line = self.lines().len().saturating_sub(self.rows);
}
}
fn start_search(&mut self, dir: Direction) {
self.mark('\'');
self.query.clear();
self.dir = dir;
self.view = Some(&Search);
}
fn search(&mut self, dir: Direction) -> bool {
// https://doc.rust-lang.org/std/vec/struct.Vec.html#method.binary_search
// If the value is not found then Result::Err is returned, containing the index where a matching element
// could be inserted while maintaining sorted order.
let get_line = |bytes: &Vec<usize>, byte: usize| -> usize {
match bytes.binary_search(&byte) {
Ok(n) => n,
Err(n) => n - 1,
}
};
let head = (self.chapter, self.chapters[self.chapter].bytes[self.line]);
match dir {
Direction::Forward => {
let tail = (self.chapter + 1..self.chapters.len() - 1).map(|n| (n, 0));
for (c, byte) in iter::once(head).chain(tail) {
if let Some(index) = self.chapters[c].text[byte..].find(&self.query) {
self.line = get_line(&self.chapters[c].bytes, index + byte);
self.chapter = c;
return true;
}
}
false
}
Direction::Backward => {
let tail = (0..self.chapter - 1)
.rev()
.map(|c| (c, self.chapters[c].text.len()));
for (c, byte) in iter::once(head).chain(tail) {
if let Some(index) = self.chapters[c].text[..byte].rfind(&self.query) {
self.line = get_line(&self.chapters[c].bytes, index);
self.chapter = c;
return true;
}
}
false
}
}
}
}
#[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, Props)> {
let args: Args = argh::from_env();
let width = args.width;
// XXX we shouldn't panic, but it gives a useful error, and we
// don't want to load the saved path if an invalid path is given
let path = args
.path
.map(|s| 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 (path, chapter, line) = match (save, path) {
(Err(_), None) => return None,
(Err(_), Some(path)) => (path, 0, 0),
(Ok(save), None) => save,
(Ok(save), Some(path)) => {
if save.0 == path {
save
} else {
(path, 0, 0)
}
}
};
Some((
path,
Props {
chapter,
line,
width,
},
))
}
fn main() {
let save_path = if cfg!(windows) {
format!("{}\\bk", env::var("APPDATA").unwrap())
} else {
format!("{}/.local/share/bk", env::var("HOME").unwrap())
};
let (path, args) = restore(&save_path).unwrap_or_else(|| {
println!("error: need a path");
exit(1);
});
let epub = epub::Epub::new(&path).unwrap_or_else(|e| {
println!("error reading epub: {}", e);
exit(1);
});
let mut bk = Bk::new(epub, args);
// 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 state: {}", e);
exit(1);
});
}
|