Skip to content

Commit

Permalink
Maximize width for the Hex viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
ztroop committed Feb 25, 2024
1 parent 053329a commit fdf8b11
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct FileDiffViewer {
diffs: Vec<(usize, u8)>,
cursor_pos: usize,
scroll: usize,
bytes_per_line: usize,
}

impl FileDiffViewer {
Expand All @@ -25,6 +26,7 @@ impl FileDiffViewer {
diffs,
cursor_pos: 0,
scroll: 0,
bytes_per_line: 0,
}
}

Expand All @@ -47,9 +49,9 @@ impl FileDiffViewer {
match key.code {
KeyCode::Char('q') => break 'mainloop,
KeyCode::Down => self.move_cursor_down(&terminal.size()?),
KeyCode::Up => self.move_cursor_up(&terminal.size()?),
KeyCode::Up => self.move_cursor_up(),
KeyCode::Right => self.move_cursor_right(&terminal.size()?),
KeyCode::Left => self.move_cursor_left(&terminal.size()?),
KeyCode::Left => self.move_cursor_left(),
_ => {}
}
}
Expand All @@ -66,9 +68,17 @@ impl FileDiffViewer {
Ok(())
}

fn draw(&self, f: &mut Frame) {
fn draw(&mut self, f: &mut Frame) {
let size = f.size();
let bytes_per_line = (size.width / 6) as usize;

let hex_section_width = (size.width as f32 * 0.7).floor() as usize;
let padding_and_borders = 4;
let adjusted_width = hex_section_width - padding_and_borders;
self.bytes_per_line = adjusted_width / 3;

let hex_width = (self.bytes_per_line * 3 + 2) as u16;
let ascii_width = (self.bytes_per_line + 2) as u16;

let lines = (size.height - 3) as usize;

let hex_chunks = Layout::default()
Expand All @@ -79,18 +89,19 @@ impl FileDiffViewer {
])
.split(size);

// Adjusted to reflect different width requirements for Hex and ASCII sections
let hex_ascii_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(70), // Hex view
Constraint::Percentage(30), // ASCII view
Constraint::Length(hex_width), // Hex view
Constraint::Length(ascii_width), // ASCII view
])
.split(hex_chunks[0]);

// Prepare hex and ASCII lines
let hex_lines = self
.diffs
.chunks(bytes_per_line)
.chunks(self.bytes_per_line)
.skip(self.scroll)
.take(lines)
.enumerate()
Expand All @@ -99,7 +110,7 @@ impl FileDiffViewer {
.iter()
.enumerate()
.map(|(idx, &(_, byte))| {
let pos = (line_idx + self.scroll) * bytes_per_line + idx;
let pos = (line_idx + self.scroll) * self.bytes_per_line + idx;
let style = if pos == self.cursor_pos {
Style::default()
.fg(Color::White)
Expand All @@ -116,7 +127,7 @@ impl FileDiffViewer {

let ascii_lines = self
.diffs
.chunks(bytes_per_line)
.chunks(self.bytes_per_line)
.skip(self.scroll)
.take(lines)
.enumerate()
Expand All @@ -125,7 +136,7 @@ impl FileDiffViewer {
.iter()
.enumerate()
.map(|(idx, &(_, byte))| {
let pos = (line_idx + self.scroll) * bytes_per_line + idx;
let pos = (line_idx + self.scroll) * self.bytes_per_line + idx;
let style = if pos == self.cursor_pos {
Style::default()
.fg(Color::White)
Expand Down Expand Up @@ -164,39 +175,36 @@ impl FileDiffViewer {
}

fn move_cursor_down(&mut self, terminal_size: &Rect) {
let bytes_per_line = (terminal_size.width / 6) as usize;
let lines = (terminal_size.height - 5) as usize;
let max_cursor_pos = self.diffs.len().saturating_sub(1);

// Increment cursor position if not at the end of diffs
if self.cursor_pos < max_cursor_pos {
self.cursor_pos += bytes_per_line;
self.cursor_pos += self.bytes_per_line;
self.cursor_pos = self.cursor_pos.min(max_cursor_pos);
}

// Adjust scrolling if cursor moves beyond the visible area
if (self.cursor_pos / bytes_per_line) >= (self.scroll + lines)
&& (self.scroll + lines) < ((self.diffs.len() + bytes_per_line - 1) / bytes_per_line)
if (self.cursor_pos / self.bytes_per_line) >= (self.scroll + lines)
&& (self.scroll + lines)
< ((self.diffs.len() + self.bytes_per_line - 1) / self.bytes_per_line)
{
self.scroll += 1;
}
}

fn move_cursor_up(&mut self, _terminal_size: &Rect) {
let bytes_per_line = (_terminal_size.width / 6) as usize;

if self.cursor_pos >= bytes_per_line {
self.cursor_pos = self.cursor_pos.saturating_sub(bytes_per_line);
fn move_cursor_up(&mut self) {
if self.cursor_pos >= self.bytes_per_line {
self.cursor_pos = self.cursor_pos.saturating_sub(self.bytes_per_line);
}

// Adjust scrolling if cursor moves above the visible area
if self.cursor_pos / bytes_per_line < self.scroll {
if self.cursor_pos / self.bytes_per_line < self.scroll {
self.scroll = self.scroll.saturating_sub(1);
}
}

fn move_cursor_right(&mut self, terminal_size: &Rect) {
let bytes_per_line = (terminal_size.width / 6) as usize;
let lines = (terminal_size.height - 5) as usize;
let max_cursor_pos = self.diffs.len().saturating_sub(1);

Expand All @@ -206,22 +214,20 @@ impl FileDiffViewer {
}

// Special handling for bottom right movement
let cursor_line = self.cursor_pos / bytes_per_line;
let cursor_line = self.cursor_pos / self.bytes_per_line;
if cursor_line >= self.scroll + lines {
self.scroll = cursor_line + 1 - lines;
}
}

fn move_cursor_left(&mut self, terminal_size: &Rect) {
let bytes_per_line = (terminal_size.width / 6) as usize;

fn move_cursor_left(&mut self) {
// Move cursor left if not at the start
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
}

// Special handling for top left movement
let cursor_line = self.cursor_pos / bytes_per_line;
let cursor_line = self.cursor_pos / self.bytes_per_line;
if cursor_line < self.scroll {
self.scroll = cursor_line;
}
Expand Down

0 comments on commit fdf8b11

Please sign in to comment.