mirror of
https://github.com/extrawurst/gitui.git
synced 2024-11-23 20:52:54 +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)]
|
#[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 {
|
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_lines = Vec::new();
|
||||||
let mut current_hunk: Option<HunkHeader> = None;
|
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| {
|
let mut put = |hunk: Option<DiffHunk>, line: git2::DiffLine| {
|
||||||
if let Some(hunk) = hunk {
|
if let Some(hunk) = hunk {
|
||||||
let hunk_header = HunkHeader::from(hunk);
|
let hunk_header = HunkHeader::from(hunk);
|
||||||
@ -101,7 +106,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
|
|||||||
match current_hunk {
|
match current_hunk {
|
||||||
None => current_hunk = Some(hunk_header),
|
None => current_hunk = Some(hunk_header),
|
||||||
Some(h) if h != hunk_header => {
|
Some(h) if h != hunk_header => {
|
||||||
res.0.push(Hunk(current_lines.clone()));
|
adder(¤t_lines);
|
||||||
current_lines.clear();
|
current_lines.clear();
|
||||||
current_hunk = Some(hunk_header)
|
current_hunk = Some(hunk_header)
|
||||||
}
|
}
|
||||||
@ -170,7 +175,7 @@ pub fn get_diff(p: String, stage: bool) -> Diff {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !current_lines.is_empty() {
|
if !current_lines.is_empty() {
|
||||||
res.0.push(Hunk(current_lines))
|
adder(¤t_lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
|
@ -4,7 +4,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use asyncgit::{hash, Diff, DiffLine, DiffLineType};
|
use asyncgit::{hash, Diff, DiffLine, DiffLineType};
|
||||||
use crossterm::event::{Event, KeyCode};
|
use crossterm::event::{Event, KeyCode};
|
||||||
use std::borrow::Cow;
|
use std::{borrow::Cow, cmp};
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::Backend,
|
backend::Backend,
|
||||||
layout::{Alignment, Rect},
|
layout::{Alignment, Rect},
|
||||||
@ -27,7 +27,7 @@ pub struct DiffComponent {
|
|||||||
impl DiffComponent {
|
impl DiffComponent {
|
||||||
///
|
///
|
||||||
fn can_scroll(&self) -> bool {
|
fn can_scroll(&self) -> bool {
|
||||||
self.diff.0.len() > 1
|
self.diff.1 > 1
|
||||||
}
|
}
|
||||||
///
|
///
|
||||||
pub fn current(&self) -> (String, bool) {
|
pub fn current(&self) -> (String, bool) {
|
||||||
@ -57,8 +57,10 @@ impl DiffComponent {
|
|||||||
|
|
||||||
fn scroll(&mut self, inc: bool) {
|
fn scroll(&mut self, inc: bool) {
|
||||||
if inc {
|
if inc {
|
||||||
self.scroll =
|
self.scroll = cmp::min(
|
||||||
self.scroll.checked_add(1).unwrap_or(self.scroll);
|
self.diff.1.saturating_sub(1),
|
||||||
|
self.scroll.saturating_add(1),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
self.scroll = self.scroll.saturating_sub(1);
|
self.scroll = self.scroll.saturating_sub(1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user