Skip to content

Commit a367238

Browse files
author
Stephan Dilly
committed
support page up/down in diff (#43)
1 parent 70e5201 commit a367238

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/components/diff.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct Current {
2929
pub struct DiffComponent {
3030
diff: FileDiff,
3131
scroll: u16,
32+
current_height: u16,
3233
focused: bool,
3334
current: Current,
3435
selected_hunk: Option<u16>,
@@ -45,6 +46,7 @@ impl DiffComponent {
4546
selected_hunk: None,
4647
diff: FileDiff::default(),
4748
scroll: 0,
49+
current_height: 0,
4850
}
4951
}
5052
///
@@ -92,21 +94,20 @@ impl DiffComponent {
9294

9395
let scroll_max = self.diff.lines.saturating_sub(1);
9496

95-
match scroll {
96-
ScrollType::Down => {
97-
self.scroll = cmp::min(
98-
scroll_max,
99-
self.scroll.saturating_add(1),
100-
);
101-
}
102-
ScrollType::Up => {
103-
self.scroll = self.scroll.saturating_sub(1);
104-
}
105-
ScrollType::Home => self.scroll = 0,
106-
ScrollType::End => self.scroll = scroll_max,
107-
ScrollType::PageDown => (),
108-
ScrollType::PageUp => (),
109-
}
97+
self.scroll = match scroll {
98+
ScrollType::Down => self.scroll.saturating_add(1),
99+
ScrollType::Up => self.scroll.saturating_sub(1),
100+
ScrollType::Home => 0,
101+
ScrollType::End => scroll_max,
102+
ScrollType::PageDown => self.scroll.saturating_add(
103+
self.current_height.saturating_sub(1),
104+
),
105+
ScrollType::PageUp => self.scroll.saturating_sub(
106+
self.current_height.saturating_sub(1),
107+
),
108+
};
109+
110+
self.scroll = cmp::min(scroll_max, self.scroll);
110111

111112
if old != self.scroll {
112113
self.selected_hunk =
@@ -297,6 +298,7 @@ impl DiffComponent {
297298

298299
impl DrawableComponent for DiffComponent {
299300
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, r: Rect) {
301+
self.current_height = r.height.saturating_sub(2);
300302
let mut style_border = Style::default().fg(Color::DarkGray);
301303
let mut style_title = Style::default();
302304
if self.focused {
@@ -368,16 +370,22 @@ impl Component for DiffComponent {
368370
self.scroll(ScrollType::End);
369371
true
370372
}
371-
372373
keys::HOME | keys::SHIFT_UP => {
373374
self.scroll(ScrollType::Home);
374375
true
375376
}
376-
377377
keys::MOVE_UP => {
378378
self.scroll(ScrollType::Up);
379379
true
380380
}
381+
keys::PAGE_UP => {
382+
self.scroll(ScrollType::PageUp);
383+
true
384+
}
385+
keys::PAGE_DOWN => {
386+
self.scroll(ScrollType::PageDown);
387+
true
388+
}
381389
keys::ENTER => {
382390
self.add_hunk();
383391
true

0 commit comments

Comments
 (0)