mirror of
https://github.com/extrawurst/gitui.git
synced 2024-11-27 10:15:07 +03:00
clamp scroll position to valid range
This commit is contained in:
parent
8ade74f201
commit
b1f8914cba
@ -61,7 +61,7 @@ pub struct Hunk(pub Vec<DiffLine>);
|
||||
|
||||
///
|
||||
#[derive(Default, Clone, Hash)]
|
||||
pub struct Diff(pub Vec<Hunk>);
|
||||
pub struct Diff(pub Vec<Hunk>, pub u16);
|
||||
|
||||
///
|
||||
pub fn get_diff(p: String, stage: bool) -> Diff {
|
||||
@ -94,6 +94,11 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
|
||||
let mut current_lines = Vec::new();
|
||||
let mut current_hunk: Option<HunkHeader> = None;
|
||||
|
||||
let mut adder = |lines: &Vec<DiffLine>| {
|
||||
res.0.push(Hunk(lines.clone()));
|
||||
res.1 += lines.len() as u16;
|
||||
};
|
||||
|
||||
let mut put = |hunk: Option<DiffHunk>, line: git2::DiffLine| {
|
||||
if let Some(hunk) = hunk {
|
||||
let hunk_header = HunkHeader::from(hunk);
|
||||
@ -101,7 +106,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
|
||||
match current_hunk {
|
||||
None => current_hunk = Some(hunk_header),
|
||||
Some(h) if h != hunk_header => {
|
||||
res.0.push(Hunk(current_lines.clone()));
|
||||
adder(¤t_lines);
|
||||
current_lines.clear();
|
||||
current_hunk = Some(hunk_header)
|
||||
}
|
||||
@ -170,7 +175,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
|
||||
}
|
||||
|
||||
if !current_lines.is_empty() {
|
||||
res.0.push(Hunk(current_lines))
|
||||
adder(¤t_lines);
|
||||
}
|
||||
|
||||
res
|
||||
|
@ -4,7 +4,7 @@ use crate::{
|
||||
};
|
||||
use asyncgit::{hash, Diff, DiffLine, DiffLineType};
|
||||
use crossterm::event::{Event, KeyCode};
|
||||
use std::borrow::Cow;
|
||||
use std::{borrow::Cow, cmp};
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::{Alignment, Rect},
|
||||
@ -27,7 +27,7 @@ pub struct DiffComponent {
|
||||
impl DiffComponent {
|
||||
///
|
||||
fn can_scroll(&self) -> bool {
|
||||
self.diff.0.len() > 1
|
||||
self.diff.1 > 1
|
||||
}
|
||||
///
|
||||
pub fn current(&self) -> (String, bool) {
|
||||
@ -57,8 +57,10 @@ impl DiffComponent {
|
||||
|
||||
fn scroll(&mut self, inc: bool) {
|
||||
if inc {
|
||||
self.scroll =
|
||||
self.scroll.checked_add(1).unwrap_or(self.scroll);
|
||||
self.scroll = cmp::min(
|
||||
self.diff.1.saturating_sub(1),
|
||||
self.scroll.saturating_add(1),
|
||||
);
|
||||
} else {
|
||||
self.scroll = self.scroll.saturating_sub(1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user