summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2017-10-15 03:30:24 -0700
committeriximeow <me@iximeow.net>2017-10-15 03:30:34 -0700
commitc6db91eddda42687a24859373869bef5df997cc0 (patch)
treead51eee33271beb55c1431dc82e553941e6921df
parente242083d38edae25f332c672a45b31d6fb7f56f5 (diff)
give render_bytes enough information to right-justify
-rw-r--r--main.rs18
1 files 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<u8>) -> String;
+ fn render_bytes(&self, cursor: u64, width: u16, start: u64, bytes: std::slice::Iter<u8>) -> 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<u8>) -> String {
+ fn render_bytes(&self, cursor: u64, width: u16, start: u64, bytes: std::slice::Iter<u8>) -> 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<u8>) -> String {
+ fn render_bytes(&self, cursor: u64, width: u16, start: u64, bytes: std::slice::Iter<u8>) -> 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 == "<address>: "
+ * 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();
}