From 6924612e2493ac78f895b1acf395e59faaaa74bc Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Sat, 2 Mar 2019 23:41:05 +0000 Subject: [PATCH] Fix offsets with long utf sequences --- src/main.rs | 15 +++++---------- src/state.rs | 4 ++-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index aeef1b4..ba50d11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,7 +84,7 @@ fn main() { let output = String::from_utf8_lossy(&execution.stdout); let lines = output.split("\n").collect::>(); - let mut state = state::State::new(lines, alphabet); + let mut state = state::State::new(&lines, alphabet); let mut paste = false; { @@ -125,15 +125,10 @@ fn main() { foreground_color }; - // TODO: Find long utf sequences and extract it from mat.x - // let re = regex::bytes::Regex::new(r"127").unwrap(); - // let line = lines[mat.y as usize]; - // let extra = re - // .find_iter(line.as_bytes()) - // .fold(0, |sum, item| sum + item.as_bytes().len()); - - let extra = 0; - + // Find long utf sequences and extract it from mat.x + let line = &lines[mat.y as usize]; + let prefix = &line[0..mat.x as usize]; + let extra = prefix.len() - prefix.chars().count(); let offset = (mat.x as usize) - extra; rustbox.print(offset, mat.y as usize, rustbox::RB_NORMAL, selected_color, background_color, mat.text); diff --git a/src/state.rs b/src/state.rs index 6b7d644..e002f58 100644 --- a/src/state.rs +++ b/src/state.rs @@ -29,13 +29,13 @@ impl<'a> PartialEq for Match<'a> { } pub struct State<'a> { - pub lines: Vec<&'a str>, + pub lines: &'a Vec<&'a str>, alphabet: &'a str, pub skip: usize, } impl<'a> State<'a> { - pub fn new(lines: Vec<&'a str>, alphabet: &'a str) -> State<'a> { + pub fn new(lines: &'a Vec<&'a str>, alphabet: &'a str) -> State<'a> { State{ lines: lines, alphabet: alphabet,