Skip to content

Commit

Permalink
fix: resize issues (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndd7xv authored Sep 1, 2022
1 parent c6bbba8 commit c19c341
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Application {
/// Renders the display. This is a wrapper around [`ScreenHandler`'s
/// render](ScreenHandler::render) method.
fn render_display(&mut self) -> Result<(), Box<dyn Error>> {
self.display.render(&self.data, &self.labels, self.key_handler.as_ref())?;
self.display.render(&mut self.data, &self.labels, self.key_handler.as_ref())?;
Ok(())
}

Expand Down
31 changes: 18 additions & 13 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ impl ScreenHandler {
.map(|i| Spans::from(format!("{:08X?}\n", (start_address + i * bytes_per_line))))
.collect::<Vec<Spans>>();

let cursor_row = offset / bytes_per_line;

// Highlight the address row that the cursor is in for visibility
if cursor_row >= start_row && cursor_row < start_row + lines_per_screen {
address_text[cursor_row - start_row].0[0].style =
Style::default().bg(Color::White).fg(Color::Black);
}

// Display hex - chunks the bytes into rows and formats them into hex
let mut hex_text = contents[start_address..]
.chunks(bytes_per_line)
Expand Down Expand Up @@ -220,10 +212,16 @@ impl ScreenHandler {
})
.collect::<Vec<Spans>>();

let cursor_row = offset / bytes_per_line;
let cursor_col = offset % bytes_per_line;

// Style the selected byte that the cursor is on
let cursor_byte = contents[offset];
let cursor_col = offset % bytes_per_line;
if cursor_row >= start_row && cursor_row < start_row + lines_per_screen {
// Highlight the address row that the cursor is in for visibility
address_text[cursor_row - start_row].0[0].style =
Style::default().bg(Color::White).fg(Color::Black);

// Highlight the selected nibble in the Hex table
let byte = format!("{:02X?}", cursor_byte);
let mut byte = byte.chars();
Expand Down Expand Up @@ -265,18 +263,25 @@ impl ScreenHandler {
/// [`calculate_dimensions`](Self::calculate_dimensions).
pub(crate) fn render(
&mut self,
app_info: &AppData,
app_info: &mut AppData,
labels: &LabelHandler,
window: &dyn KeyHandler,
) -> Result<(), Box<dyn Error>> {
self.terminal.draw(|f| {
// We check if we need to recompute the terminal size in the case that the saved off variable
// differs from the current frame, which can occur when a terminal is resized between an event
// handling and a rendering.
// We check if we need to recompute the terminal size in the case that the saved off
// variable differs from the current frame, which can occur when a terminal is resized
// between an event handling and a rendering.
let size = f.size();
if size != self.terminal_size {
self.terminal_size = size;
self.comp_layouts = Self::calculate_dimensions(self.terminal_size, window);

// We change the start_address here to ensure that 0 is ALWAYS the first start
// address. We round to preventing constant resizing always moving to 0.
app_info.start_address = (app_info.start_address
+ (self.comp_layouts.bytes_per_line / 2))
/ self.comp_layouts.bytes_per_line
* self.comp_layouts.bytes_per_line;
}

// Check if terminal is large enough
Expand Down
21 changes: 5 additions & 16 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,14 @@ pub(crate) fn adjust_offset(
display: &mut ScreenHandler,
labels: &mut LabelHandler,
) {
let line_adjustment = if app.offset <= app.start_address {
app.start_address - app.offset + display.comp_layouts.bytes_per_line - 1
} else {
app.offset - app.start_address
} / display.comp_layouts.bytes_per_line;

let bytes_per_screen =
display.comp_layouts.bytes_per_line * display.comp_layouts.lines_per_screen;
let bytes_per_line = display.comp_layouts.bytes_per_line;
let bytes_per_screen = bytes_per_line * display.comp_layouts.lines_per_screen;

if app.offset < app.start_address {
app.start_address = (app.offset / bytes_per_line) * bytes_per_line;
} else if app.offset >= app.start_address + (bytes_per_screen) {
app.start_address =
app.start_address.saturating_sub(display.comp_layouts.bytes_per_line * line_adjustment);
} else if app.offset >= app.start_address + (bytes_per_screen)
&& app.start_address + display.comp_layouts.bytes_per_line < app.contents.len()
{
app.start_address = app.start_address.saturating_add(
display.comp_layouts.bytes_per_line
* (line_adjustment + 1 - display.comp_layouts.lines_per_screen),
);
(app.offset / bytes_per_line) * bytes_per_line - bytes_per_screen + bytes_per_line;
}

labels.offset = format!("{:#X}", app.offset);
Expand Down

0 comments on commit c19c341

Please sign in to comment.