Skip to content

Commit 71304cd

Browse files
committed
Improve code for selecting bytes
1 parent 163c351 commit 71304cd

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/hex/selection.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,31 @@ impl Selection {
3535
self.end = 0;
3636
self.direction = None;
3737
}
38-
pub fn select_left(&mut self, offset: usize) {
39-
// unset direction if at the selection origin
40-
if self.start == self.end {
41-
self.direction = None;
42-
}
43-
38+
pub fn select_left(&mut self, step: usize) {
4439
match self.direction {
4540
None => {
4641
self.direction = Some(Direction::Left);
47-
self.start = offset;
42+
self.start = self.start.saturating_sub(step);
4843
}
49-
Some(Direction::Left) => self.start = offset,
50-
Some(Direction::Right) => self.end = offset.saturating_sub(1),
44+
Some(Direction::Left) => self.start = self.start.saturating_sub(step),
45+
Some(Direction::Right) => self.end = self.end.saturating_sub(step),
5146
}
52-
}
53-
pub fn select_right(&mut self, offset: usize) {
54-
// unset direction if at the selection origin
47+
5548
if self.start == self.end {
56-
self.direction = None;
49+
self.direction = Some(Direction::Left);
5750
}
58-
51+
}
52+
pub fn select_right(&mut self, offset_max: usize, step: usize) {
5953
match self.direction {
6054
None => {
6155
self.direction = Some(Direction::Right);
62-
self.end = offset;
56+
self.end = (self.start + step).min(offset_max);
6357
}
64-
Some(Direction::Left) => self.start = offset + 1,
65-
Some(Direction::Right) => self.end = offset,
58+
Some(Direction::Left) => self.start = (self.start + step).min(offset_max),
59+
Some(Direction::Right) => self.end = (self.end + step).min(offset_max),
60+
}
61+
if self.start == self.end {
62+
self.direction = Some(Direction::Right);
6663
}
6764
}
6865
}
@@ -80,7 +77,7 @@ pub fn select_events(app: &mut App, key: KeyEvent) -> Result<bool> {
8077
KeyCode::Left | KeyCode::Char('h') => {
8178
let new_offset = app.hex_view.offset.saturating_sub(1);
8279

83-
app.hex_view.selection.select_left(new_offset);
80+
app.hex_view.selection.select_left(1);
8481
app.goto(new_offset);
8582
}
8683
KeyCode::Right | KeyCode::Char('l') => {
@@ -91,7 +88,7 @@ pub fn select_events(app: &mut App, key: KeyEvent) -> Result<bool> {
9188
return Ok(true);
9289
}
9390

94-
app.hex_view.selection.select_right(new_offset);
91+
app.hex_view.selection.select_right(app.file_info.size, 1);
9592
app.goto(new_offset);
9693
}
9794
KeyCode::Up | KeyCode::Char('k') => {
@@ -105,7 +102,9 @@ pub fn select_events(app: &mut App, key: KeyEvent) -> Result<bool> {
105102
return Ok(true);
106103
}
107104

108-
app.hex_view.selection.select_left(new_offset);
105+
app.hex_view
106+
.selection
107+
.select_left(app.config.hex_mode_bytes_per_line);
109108
app.goto(new_offset);
110109
}
111110
KeyCode::Down | KeyCode::Char('j') => {
@@ -115,7 +114,9 @@ pub fn select_events(app: &mut App, key: KeyEvent) -> Result<bool> {
115114
.saturating_add(app.config.hex_mode_bytes_per_line)
116115
.min(app.file_info.size - 1);
117116

118-
app.hex_view.selection.select_right(new_offset);
117+
app.hex_view
118+
.selection
119+
.select_right(app.file_info.size, app.config.hex_mode_bytes_per_line);
119120
app.goto(new_offset);
120121
}
121122

0 commit comments

Comments
 (0)