Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Reload and force reload will now highlight changed bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed May 27, 2019
1 parent 3341245 commit 91f7e8f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/hex_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct HexReader {
pub window_pos: (u64,u64),
pub window_size: (u16,u16),
capture: Vec<u8>,
before_image: Vec<u8>,
highlight: BTreeMap<u64,Highlight>,
pub vis_mode: VisualMode,
}
Expand All @@ -58,6 +59,7 @@ impl HexReader {
window_pos: (0,0),
window_size: (16,32),
capture: Vec::new(),
before_image: Vec::new(),
highlight: BTreeMap::new(),
vis_mode: VisualMode::Unicode
})
Expand All @@ -78,22 +80,61 @@ impl HexReader {
pub fn get_length(&self) -> u64 {
self.reader.get_length()
}

pub fn get_row_offsets_width(&self) -> usize {
if self.reader.use_large_addresses() { 16 + 2 } else { 8 + 2 }
}

pub fn get_lines_in_file(&self) -> u64 {
self.reader.get_length() / self.line_width
}

pub fn capture(&mut self) -> Result<()> {
let (x, y) = self.window_pos;
let (w, h) = self.window_size;
self.capture.clear();
// xxx Possible optimisation, since 'capture' is a Vec of u8 where drop is a no-op.
// unsafe { self.capture.set_len(0) };
self.reader.get_window((x, y, w, h), self.line_width, &mut self.capture)
self.reader.get_window((x, y, w, h), self.line_width, &mut self.capture)?;

if !self.before_image.is_empty() {
self.compute_window_diff();
self.before_image.clear();
}

Ok(())
}

pub fn get_row_offsets_width(&self) -> usize {
if self.reader.use_large_addresses() { 16 + 2 } else { 8 + 2 }
fn compute_window_diff(&mut self) {
let line_cap = u64::from(self.window_size.0);
let line_width = self.line_width;
let mut line_offset = line_width * self.window_pos.1 + self.window_pos.0;

let mut before_itr = self.before_image.iter();
let mut after_itr = self.capture.iter();
let mut i = 0;
while let Some(a) = after_itr.next() {
let offset = line_offset + i;

if let Some(b) = before_itr.next() {
if a != b {
self.highlight.insert(offset, Highlight::Negative);
}
} else {
break;
}

i += 1;
if i == line_cap {
i = 0;
line_offset += line_width;
}
}
}

pub fn get_lines_in_file(&self) -> u64 {
self.reader.get_length() / self.line_width
pub fn capture_before_image(&mut self) {
self.before_image.clear();
self.before_image.clone_from(&self.capture);
}

pub fn clear_highlights(&mut self) {
Expand Down
2 changes: 2 additions & 0 deletions src/hex_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ impl HexView {
}

fn reload_data(&mut self) -> EventResult {
self.reader.clear_highlights();
self.reader.capture_before_image();
self.invalidated_data_changed = true;
EventResult::Consumed(None)
}
Expand Down

0 comments on commit 91f7e8f

Please sign in to comment.