Skip to content

Commit

Permalink
add selected byte information
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Nov 14, 2023
1 parent 4bb5630 commit 31dd53d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/binocle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::view::View;

pub struct Binocle {
pub settings: Settings,
buffer: Buffer,
pub buffer: Buffer,
}

impl Binocle {
Expand Down
24 changes: 24 additions & 0 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,30 @@ pub fn run(options: CliOptions) -> Result<()> {
}

if let Some((x, y)) = input.mouse() {
if let Ok((x, y)) = pixels.window_pos_to_pixel((x, y)) {
let px_size = 2usize.pow((settings.zoom - 1) as u32);
let x_px = x / px_size;
let y_px = y / px_size;
let px_window_selected = x_px + (y_px * settings.width as usize);
let offset = settings.offset + settings.offset_fine;
let byte_data_selected = px_window_selected + offset as usize;
let data = binocle.buffer.data();
let start = data.get(byte_data_selected..);
let end = start.map(|start| start.get(..16)).flatten();
let bytes = match (start, end) {
(None, _) => None,
(Some(partial_data), None) => {
let mut data = 0u128.to_ne_bytes();
data[..partial_data.len()].copy_from_slice(partial_data);
Some(data)
}
(_, Some(data)) => Some(data.try_into().unwrap()),
};
if let Some(bytes) = bytes {
settings.selected_byte = byte_data_selected;
settings.selected_data = bytes;
}
}
if input.mouse_pressed(0) {
if input.held_shift() {
mouse_drag_action = MouseDragAction::ControlOffsetFine {
Expand Down
30 changes: 30 additions & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ impl Gui {
ui.checkbox(&mut settings.hex_view_visible, "hex view");
ui.separator();

ui.add(egui::Label::new("Data information").heading());
let data_u128 = u128::from_le_bytes(settings.selected_data);
let data_u8 = data_u128 as u8;
let data_u16 = data_u128 as u16;
let data_u32 = data_u128 as u32;
let data_u64 = data_u128 as u64;
let mut to_label = |x: String| {
ui.add(egui::Label::new(x).monospace().wrap(false));
};
to_label(format!("byte: {}", settings.selected_byte));
to_label(format!("u8: {data_u8}"));
to_label(format!("i8: {}", data_u8 as i8));
to_label(format!("u16le: {data_u16}"));
to_label(format!("u16be: {}", data_u16.to_be()));
to_label(format!("i16le: {}", data_u16 as i16));
to_label(format!("i16be: {}", data_u16.to_be() as i16));
to_label(format!("u32le: {data_u32}"));
to_label(format!("u32be: {}", data_u32.to_be()));
to_label(format!("i32le: {}", data_u32 as i32));
to_label(format!("i32be: {}", data_u32.to_be() as i32));
to_label(format!("u64le: {data_u64}"));
to_label(format!("u64be: {}", data_u64.to_be()));
to_label(format!("i64le: {}", data_u64 as i64));
to_label(format!("i64be: {}", data_u64.to_be() as i64));
to_label(format!("f32le: {:e}", f32::from_bits(data_u32)));
to_label(format!("f32be: {:e}", f32::from_bits(data_u32.to_be())));
to_label(format!("f64le: {:e}", f64::from_bits(data_u64)));
to_label(format!("f64be: {:e}", f64::from_bits(data_u64.to_be())));

ui.separator();
ui.add(egui::Label::new("Information").heading());
let file_size = settings
.buffer_length
Expand Down
5 changes: 5 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub struct Settings {

pub gui_wants_keyboard: bool,
pub gui_wants_mouse: bool,

pub selected_byte: usize,
pub selected_data: [u8; 16],
}

impl Settings {
Expand Down Expand Up @@ -103,6 +106,8 @@ impl Default for Settings {
hex_ascii: "".into(),
gui_wants_keyboard: false,
gui_wants_mouse: false,
selected_byte: 0,
selected_data: [0; 16],
}
}
}

0 comments on commit 31dd53d

Please sign in to comment.