diff options
author | James Campos <james.r.campos@gmail.com> | 2020-07-17 09:54:43 -0700 |
---|---|---|
committer | James Campos <james.r.campos@gmail.com> | 2020-07-17 09:54:43 -0700 |
commit | 1136a1f5dcea65490197b6b3f69d8a59848e4e53 (patch) | |
tree | a0ff38514d3c82031fd99740debb3485aada9eac /src/main.rs | |
parent | 163f4e6a8acedfd0842ac40cc3d4f72c3fe36b66 (diff) | |
download | bk-1136a1f5dcea65490197b6b3f69d8a59848e4e53.tar.gz |
fragments
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs index 09eb5b5..6bee84e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use crossterm::{ }; use serde::{Deserialize, Serialize}; use std::{ - cmp::{Ordering, min}, + cmp::{min, Ordering}, collections::HashMap, env, fs, io::{stdout, Write}, @@ -75,6 +75,13 @@ fn wrap(text: &str, max_cols: usize) -> Vec<(usize, usize)> { lines } +fn get_line (lines: &Vec<(usize, usize)>, byte: usize) -> usize { + match lines.binary_search_by_key(&byte, |&(a, _)| a) { + Ok(n) => n, + Err(n) => n - 1, + } +} + struct SearchArgs { dir: Direction, skip: bool, @@ -270,9 +277,10 @@ impl View for Page { } }); if let Ok(i) = r { - let path = &c.links[i].2; - let &pos = bk.links.get(path).unwrap(); - bk.jump(pos); + let url = &c.links[i].2; + let &(chapter, byte) = bk.links.get(url).unwrap(); + let line = get_line(&bk.chapters[chapter].lines, byte); + bk.jump((chapter, line)); } } MouseEvent::ScrollDown(_, _, _) => bk.scroll_down(3), @@ -522,13 +530,7 @@ impl Bk<'_> { } } - let line = match chapters[args.chapter] - .lines - .binary_search_by_key(&args.byte, |&(a, _)| a) - { - Ok(n) => n, - Err(n) => n - 1, - }; + let line = get_line(&chapters[args.chapter].lines, args.byte); let mut mark = HashMap::new(); let view: &dyn View = if args.toc { @@ -653,12 +655,6 @@ impl Bk<'_> { self.view = Some(&Search); } fn search(&mut self, args: SearchArgs) -> bool { - let get_line = |lines: &Vec<(usize, usize)>, byte: usize| -> usize { - match lines.binary_search_by_key(&byte, |&(a, _)| a) { - Ok(n) => n, - Err(n) => n - 1, - } - }; let (start, end) = self.chap().lines[self.line]; match args.dir { Direction::Next => { |