diff options
author | James Campos <james.r.campos@gmail.com> | 2020-07-18 02:44:34 -0700 |
---|---|---|
committer | James Campos <james.r.campos@gmail.com> | 2020-07-18 02:44:34 -0700 |
commit | 4cb47493b0f7362b06f87a3c49419087ee18ab54 (patch) | |
tree | 02d801484ad49061238b6dedae15831017481c01 /src | |
parent | 1136a1f5dcea65490197b6b3f69d8a59848e4e53 (diff) | |
download | bk-4cb47493b0f7362b06f87a3c49419087ee18ab54.tar.gz |
unicode
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 6bee84e..90a60cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -262,20 +262,34 @@ impl View for Page { } let c = bk.chap(); let (start, end) = c.lines[bk.line + row as usize]; - // FIXME unicode width - let byte = start + (col - bk.pad()) as usize; - if byte > end { + let line_col = (col - bk.pad()) as usize; + + let mut cols = 0; + let mut found = false; + let mut byte = start; + for (i, c) in c.text[start..end].char_indices() { + cols += c.width().unwrap(); + if cols > line_col { + byte += i; + found = true; + break; + } + } + + if !found { return; } + let r = c.links.binary_search_by(|&(start, end, _)| { if start > byte { Ordering::Greater - } else if end < byte { + } else if end <= byte { Ordering::Less } else { Ordering::Equal } }); + if let Ok(i) = r { let url = &c.links[i].2; let &(chapter, byte) = bk.links.get(url).unwrap(); |