Skip to content

Commit 7071456

Browse files
author
Stephan Dilly
committed
Merge branch 'master' into #3-stashing
2 parents cf0358b + dd49d68 commit 7071456

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/tabs/revlog/mod.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
},
88
keys,
99
strings::commands,
10+
ui::calc_scroll_top,
1011
};
1112
use asyncgit::{sync, AsyncLog, AsyncNotification, CWD};
1213
use crossbeam_channel::Sender;
@@ -55,6 +56,7 @@ pub struct Revlog {
5556
scroll_state: (Instant, f32),
5657
tags: Tags,
5758
current_size: (u16, u16),
59+
scroll_top: usize,
5860
}
5961

6062
impl Revlog {
@@ -70,6 +72,7 @@ impl Revlog {
7072
scroll_state: (Instant::now(), 0_f32),
7173
tags: Tags::new(),
7274
current_size: (0, 0),
75+
scroll_top: 0,
7376
}
7477
}
7578

@@ -233,22 +236,12 @@ impl Revlog {
233236

234237
assert_eq!(txt.len() - count_before, ELEMENTS_PER_LINE);
235238
}
236-
}
237-
238-
impl DrawableComponent for Revlog {
239-
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect) {
240-
self.current_size = (
241-
area.width.saturating_sub(2),
242-
area.height.saturating_sub(2),
243-
);
244239

245-
let height = area.height as usize;
246-
let selection =
247-
self.selection.saturating_sub(self.items.index_offset);
248-
let height_d2 = height as usize / 2;
249-
let min = selection.saturating_sub(height_d2);
240+
fn get_text(&self) -> Vec<Text> {
241+
let selection = self.relative_selection();
250242

251243
let mut txt = Vec::new();
244+
252245
for (idx, e) in self.items.items.iter().enumerate() {
253246
let tag = if let Some(tags) = self.tags.get(&e.hash) {
254247
Some(tags.join(" "))
@@ -258,16 +251,41 @@ impl DrawableComponent for Revlog {
258251
Self::add_entry(e, idx == selection, &mut txt, tag);
259252
}
260253

254+
txt
255+
}
256+
257+
fn relative_selection(&self) -> usize {
258+
self.selection.saturating_sub(self.items.index_offset)
259+
}
260+
}
261+
262+
impl DrawableComponent for Revlog {
263+
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect) {
264+
self.current_size = (
265+
area.width.saturating_sub(2),
266+
area.height.saturating_sub(2),
267+
);
268+
269+
let height_in_lines = self.current_size.1 as usize;
270+
let selection = self.relative_selection();
271+
272+
self.scroll_top = calc_scroll_top(
273+
self.scroll_top,
274+
height_in_lines,
275+
selection,
276+
);
277+
261278
let title = format!(
262279
"commit {}/{}",
263-
self.selection, self.selection_max
280+
self.selection, self.selection_max,
264281
);
265282

266283
f.render_widget(
267284
Paragraph::new(
268-
txt.iter()
269-
.skip(min * ELEMENTS_PER_LINE)
270-
.take(height * ELEMENTS_PER_LINE),
285+
self.get_text()
286+
.iter()
287+
.skip(self.scroll_top * ELEMENTS_PER_LINE)
288+
.take(height_in_lines * ELEMENTS_PER_LINE),
271289
)
272290
.block(
273291
Block::default()

src/ui/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ use tui::{
99
Frame,
1010
};
1111

12+
/// return the scroll position (line) necessary to have the `selection` in view if it is not already
13+
pub fn calc_scroll_top(
14+
current_top: usize,
15+
height_in_lines: usize,
16+
selection: usize,
17+
) -> usize {
18+
if current_top + height_in_lines <= selection {
19+
selection.saturating_sub(height_in_lines) + 1
20+
} else if current_top > selection {
21+
selection
22+
} else {
23+
current_top
24+
}
25+
}
26+
1227
/// use layouts to create a rects that
1328
/// centers inside `r` and sizes `percent_x`/`percent_x` of `r`
1429
pub fn centered_rect(

0 commit comments

Comments
 (0)