From c6db91eddda42687a24859373869bef5df997cc0 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sun, 15 Oct 2017 03:30:24 -0700 Subject: give render_bytes enough information to right-justify --- main.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.rs b/main.rs index 848c041..e47efda 100644 --- a/main.rs +++ b/main.rs @@ -166,7 +166,7 @@ impl Edits { } trait EditMode { - fn render_bytes(&self, cursor: u64, start: u64, bytes: std::slice::Iter) -> String; + fn render_bytes(&self, cursor: u64, width: u16, start: u64, bytes: std::slice::Iter) -> String; // translates from the width of the display (terminal) // to the number of bytes this edit mode can display fn element_width(&self, display_width: u16) -> u64; @@ -179,7 +179,7 @@ struct BinaryMode { } impl EditMode for BinaryMode { - fn render_bytes(&self, cursor: u64, start: u64, bytes: std::slice::Iter) -> String { + fn render_bytes(&self, cursor: u64, width: u16, start: u64, bytes: std::slice::Iter) -> String { let mut text = "".to_owned(); // let mut ascii_text = "".to_owned(); let col_size = 1; @@ -222,7 +222,7 @@ struct HexMode { } impl EditMode for HexMode { - fn render_bytes(&self, cursor: u64, start: u64, bytes: std::slice::Iter) -> String { + fn render_bytes(&self, cursor: u64, width: u16, start: u64, bytes: std::slice::Iter) -> String { let mut text = "".to_owned(); let mut ascii_text = "".to_owned(); let col_size = 4; @@ -253,7 +253,15 @@ impl EditMode for HexMode { } i = i + 1; } - format!("0x{:08x}: {} {}", start, text, ascii_text).to_owned() + /* + * 12 == "
: " + * i / col_size for the extra space between columns + * i * 3 for 3 characters per byte + * + 1 for padding.. + */ + let hex_line_width = 12 + i / col_size + i * 3 + 1; + let padding = format!("{: >line_width$}", "", line_width = width as usize - hex_line_width - i); + format!("0x{:08x}: {}{}{}", start, text, padding, ascii_text).to_owned() } fn element_width(&self, display_width: u16) -> u64 { (display_width as u64 - 13) / 17 * 4 @@ -623,7 +631,7 @@ fn render_interface(state: &mut Program) { let view = &state.edit_views[j]; let slice_start = width * i; let slice_end = slice_start + width; - write!(iface, "{}", view.render_bytes(state.cursor, start as u64 + slice_start, buffer[(slice_start as usize)..(slice_end as usize)].iter())).unwrap(); + write!(iface, "{}", view.render_bytes(state.cursor, state.width, start as u64 + slice_start, buffer[(slice_start as usize)..(slice_end as usize)].iter())).unwrap(); if i < state.lines_to_draw() - 1 || j < (state.edit_views.len() - 1) { write!(iface, "\n").unwrap(); } -- cgit v1.1