Skip to content

Commit

Permalink
Make it discoverable
Browse files Browse the repository at this point in the history
  • Loading branch information
ethteck committed Oct 16, 2024
1 parent c96af95 commit 05078b0
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 90 deletions.
78 changes: 49 additions & 29 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ impl BdiffApp {
match ret.open_file(&file.path) {
Ok(fv) => {
if let Some(map) = file.map.as_ref() {
fv.mt.load_file(map);
fv.st.load_file(map);
}
fv.file.endianness = file.endianness; // TODO hook up endianness saving
}
Err(e) => {
log::error!("Failed to open file: {}", e);
Expand Down Expand Up @@ -240,11 +241,36 @@ impl BdiffApp {
self.global_view_pos = 0.max(self.global_view_pos as isize + delta) as usize;
}

fn move_view(&mut self, ctx: &Context) {
let diffing = self.file_views.len() > 1 && self.settings.diff_state_enabled;
let bytes_per_screen = self.bytes_per_row * self.num_rows;
fn move_global_pos_enter(&mut self, longest_file_len: usize, bytes_per_screen: usize) {
if self.is_diffing() {
let last_byte = self.global_view_pos + bytes_per_screen;

if last_byte < longest_file_len {
match self.diff_state.get_next_diff(last_byte) {
Some(next_diff) => {
// Move to the next diff
let new_pos = next_diff - (next_diff % self.bytes_per_row);
self.set_global_pos(new_pos);
}
None => {
// Move to the end of the file
self.set_global_pos(0.max(longest_file_len - bytes_per_screen))
}
}
}
} else {
// Move one screen down
self.move_global_pos(bytes_per_screen as isize);
}
}

fn is_diffing(&self) -> bool {
self.file_views.len() > 1 && self.settings.diff_enabled
}

fn move_view(&mut self, ctx: &Context) {
let longest_file_len = self.file_views.iter().map(|fv| fv.file.data.len()).max().unwrap();
let bytes_per_screen = self.bytes_per_row * self.num_rows;

// Keys
if ctx.input(|i| i.key_pressed(egui::Key::Home)) {
Expand Down Expand Up @@ -272,26 +298,7 @@ impl BdiffApp {
self.move_global_pos(self.bytes_per_row as isize);
}
if ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
if diffing {
let last_byte = self.global_view_pos + bytes_per_screen;

if last_byte < longest_file_len {
match self.diff_state.get_next_diff(last_byte) {
Some(next_diff) => {
// Move to the next diff
let new_pos = next_diff - (next_diff % self.bytes_per_row);
self.set_global_pos(new_pos);
}
None => {
// Move to the end of the file
self.set_global_pos(0.max(longest_file_len - bytes_per_screen))
}
}
}
} else {
// Move one screen down
self.move_global_pos(bytes_per_screen as isize);
}
self.move_global_pos_enter(longest_file_len, bytes_per_screen);
}

let scroll_y = ctx.input(|i| i.raw_scroll_delta.y);
Expand Down Expand Up @@ -497,24 +504,35 @@ impl eframe::App for BdiffApp {
goto_modal.open();
ui.close_menu();
}

let enter_text = match self.is_diffing() {
true => "Jump to next diff (Enter)",
false => "Scroll one screen (Enter)",
};

if ui.button(enter_text).clicked() && self.file_views.len() > 1 {
let longest_file_len = self.file_views.iter().map(|fv| fv.file.data.len()).max().unwrap();
let bytes_per_screen = self.bytes_per_row * self.num_rows;

self.move_global_pos_enter(longest_file_len, bytes_per_screen);
}
});

ui.menu_button("Options", |ui| {
let diff_checkbox = Checkbox::new(&mut self.settings.diff_state_enabled, "Display diff");
let diff_checkbox = Checkbox::new(&mut self.settings.diff_enabled, "Display diff");
let mirror_selection_checkbox = Checkbox::new(
&mut self.options.mirror_selection,
"Mirror selection across files",
);

ui.label("Behavior");


ui.add_enabled(self.file_views.len() > 1, mirror_selection_checkbox);

if ui
.add_enabled(self.file_views.len() > 1, diff_checkbox)
.clicked()
&& self.settings.diff_state_enabled
&& self.settings.diff_enabled
{
self.recalculate_diffs()
}
Expand Down Expand Up @@ -562,6 +580,8 @@ impl eframe::App for BdiffApp {
cursor_state,
can_selection_change,
self.global_view_pos,
self.bytes_per_row,
self.num_rows,
);

if fv.closed {
Expand Down Expand Up @@ -648,8 +668,8 @@ impl eframe::App for BdiffApp {
}
}

if fv.mt.map_file.is_some() {
let map_file = fv.mt.map_file.as_mut().unwrap();
if fv.st.map_file.is_some() {
let map_file = fv.st.map_file.as_mut().unwrap();
if map_file.modified.swap(false, Ordering::Relaxed) {
match map_file.reload() {
Ok(_) => {
Expand Down
6 changes: 3 additions & 3 deletions app/src/bin_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::{
sync::{atomic::AtomicBool, Arc},
};

use anyhow::Error;

use crate::watcher::create_watcher;
use anyhow::Error;
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
pub enum Endianness {
Little,
#[default]
Expand Down
46 changes: 36 additions & 10 deletions app/src/file_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct FileView {
pub hv: HexView,
sv: StringViewer,
dv: DataViewer,
pub mt: SymbolTool,
pub st: SymbolTool,
pub closed: bool,
}

Expand All @@ -40,7 +40,7 @@ impl FileView {
hv: HexView::new(id, bytes_per_row, num_rows),
sv: StringViewer::default(),
dv: DataViewer::default(),
mt: SymbolTool::default(),
st: SymbolTool::default(),
closed: false,
}
}
Expand All @@ -61,6 +61,26 @@ impl FileView {
Ok(())
}

pub fn get_display_bytes(&self, global_offset: usize, num_bytes: usize) -> Vec<Option<u8>> {
let pos: isize = global_offset as isize - self.cur_pos as isize;

if pos > 0 && (pos as usize) > self.file.data.len() {
vec![None; num_bytes]
} else {
let mut bytes = Vec::with_capacity(num_bytes);
for i in 0..num_bytes {
let idx = pos + i as isize;
if idx >= 0 && (idx as usize) < self.file.data.len() {
bytes.push(Some(self.file.data[idx as usize]));
} else {
bytes.push(None);
}
}
bytes
}
}


pub fn show(
&mut self,
settings: &Settings,
Expand All @@ -69,6 +89,8 @@ impl FileView {
cursor_state: CursorState,
can_selection_change: bool,
global_view_pos: usize,
bytes_per_row: usize,
num_rows: usize,
) {
egui::Window::new(self.file.path.to_str().unwrap())
.id(Id::new(format!("hex_view_window_{}", self.id)))
Expand Down Expand Up @@ -135,7 +157,7 @@ impl FileView {
ui.checkbox(&mut self.show_cursor_info, "Cursor info");
ui.checkbox(&mut self.dv.show, "Data viewer");
ui.checkbox(&mut self.sv.show, "String viewer");
ui.checkbox(&mut self.mt.show, "Symbols");
ui.checkbox(&mut self.st.show, "Symbols");
});

if ui.button("X").on_hover_text("Close").clicked() {
Expand All @@ -149,18 +171,22 @@ impl FileView {
|ui: &mut egui::Ui| {
ui.vertical(|ui| {
ui.group(|ui| {
let diffs = match settings.diff_state_enabled {
let diffs = match settings.diff_enabled {
true => Some(&diff_state.diffs[..]),
false => None
};

let display_data = self.get_display_bytes(global_view_pos, bytes_per_row * num_rows);

self.hv.show(
ui,
&self.file.data,
global_view_pos as isize - self.cur_pos as isize, // TODO pass just the slice of data we care about, don't keep track of global pos inside hv
&display_data,
&diffs,
global_view_pos,
self.cur_pos,
cursor_state,
can_selection_change,
settings.byte_grouping.into(),
diffs,
);
});

Expand All @@ -172,7 +198,7 @@ impl FileView {
let end = self.hv.selection.end();
let length = end - start + 1;

let map_entry = match self.mt.map_file {
let map_entry = match self.st.map_file {
Some(ref map_file) => {
map_file.get_entry(start, end + 1)
}
Expand Down Expand Up @@ -210,7 +236,7 @@ impl FileView {
if self.show_cursor_info {
let hover_text = match self.hv.cursor_pos {
Some(pos) => {
let map_entry = match self.mt.map_file {
let map_entry = match self.st.map_file {
Some(ref map_file) => map_file.get_entry(pos, pos + 1),
None => None,
};
Expand Down Expand Up @@ -246,7 +272,7 @@ impl FileView {
self.hv.get_selected_bytes(&self.file.data),
self.file.endianness,
);
self.mt.display(ui);
self.st.display(ui);
});
},
);
Expand Down
2 changes: 1 addition & 1 deletion app/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use theme::show_theme_settings;
#[derive(Deserialize, Serialize, Default, PartialEq, PartialOrd, Clone)]
pub struct Settings {
pub byte_grouping: ByteGrouping,
pub diff_state_enabled: bool,
pub diff_enabled: bool,
pub theme: ThemeSettings,
}

Expand Down
7 changes: 4 additions & 3 deletions app/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ use std::{
path::{Path, PathBuf},
};

use crate::bin_file::Endianness;
use anyhow::{Context, Error};
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
pub struct WorkspaceFile {
pub path: PathBuf,
pub map: Option<PathBuf>,
pub endianness: Endianness,
}

impl From<PathBuf> for WorkspaceFile {
fn from(path: PathBuf) -> Self {
Self { path, map: None }
Self { path, map: None, endianness: Endianness::Big }
}
}

impl From<&Path> for WorkspaceFile {
fn from(path: &Path) -> Self {
let path: PathBuf = path.into();
Self { path, map: None }
Self { path, map: None, endianness: Endianness::Big }
}
}

Expand All @@ -37,7 +39,6 @@ pub fn read_json_config(config_path: &Path) -> Result<Workspace, Error> {
Ok(serde_json::from_reader(&mut reader)?)
}

#[allow(dead_code)]
pub fn write_json_config<P: Into<PathBuf>>(config_path: P, config: &Workspace) -> Result<(), Error> {
let path: PathBuf = config_path.into();
let mut oo = OpenOptions::new();
Expand Down
Loading

0 comments on commit 05078b0

Please sign in to comment.