mirror of
https://github.com/gitui-org/gitui
synced 2026-05-24 09:28:21 +00:00
support home/end to jump up/down (#43)
This commit is contained in:
parent
13979c1013
commit
6bb54cfa8c
2 changed files with 55 additions and 13 deletions
|
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
strings,
|
strings,
|
||||||
};
|
};
|
||||||
use asyncgit::{hash, DiffLine, DiffLineType, FileDiff};
|
use asyncgit::{hash, DiffLine, DiffLineType, FileDiff};
|
||||||
use crossterm::event::{Event, KeyCode};
|
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||||
use std::{borrow::Cow, cmp, convert::TryFrom};
|
use std::{borrow::Cow, cmp, convert::TryFrom};
|
||||||
use strings::commands;
|
use strings::commands;
|
||||||
use tui::{
|
use tui::{
|
||||||
|
|
@ -24,6 +24,13 @@ struct Current {
|
||||||
hash: u64,
|
hash: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ScrollType {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Home,
|
||||||
|
End,
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
pub struct DiffComponent {
|
pub struct DiffComponent {
|
||||||
diff: FileDiff,
|
diff: FileDiff,
|
||||||
|
|
@ -86,15 +93,25 @@ impl DiffComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scroll(&mut self, inc: bool) {
|
fn scroll(&mut self, scroll: ScrollType) {
|
||||||
let old = self.scroll;
|
let old = self.scroll;
|
||||||
if inc {
|
|
||||||
self.scroll = cmp::min(
|
let scroll_max = self.diff.lines.saturating_sub(1);
|
||||||
self.diff.lines.saturating_sub(1),
|
|
||||||
self.scroll.saturating_add(1),
|
match scroll {
|
||||||
);
|
ScrollType::Down => {
|
||||||
} else {
|
self.scroll = cmp::min(
|
||||||
self.scroll = self.scroll.saturating_sub(1);
|
scroll_max,
|
||||||
|
self.scroll.saturating_add(1),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScrollType::Up => {
|
||||||
|
self.scroll = self.scroll.saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScrollType::Home => self.scroll = 0,
|
||||||
|
ScrollType::End => self.scroll = scroll_max,
|
||||||
}
|
}
|
||||||
|
|
||||||
if old != self.scroll {
|
if old != self.scroll {
|
||||||
|
|
@ -322,6 +339,15 @@ impl Component for DiffComponent {
|
||||||
self.focused,
|
self.focused,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
out.push(
|
||||||
|
CommandInfo::new(
|
||||||
|
commands::DIFF_HOME_END,
|
||||||
|
self.can_scroll(),
|
||||||
|
self.focused,
|
||||||
|
)
|
||||||
|
.hidden(),
|
||||||
|
);
|
||||||
|
|
||||||
let cmd_text = if self.current.is_stage {
|
let cmd_text = if self.current.is_stage {
|
||||||
commands::DIFF_HUNK_REMOVE
|
commands::DIFF_HUNK_REMOVE
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -340,13 +366,23 @@ impl Component for DiffComponent {
|
||||||
fn event(&mut self, ev: Event) -> bool {
|
fn event(&mut self, ev: Event) -> bool {
|
||||||
if self.focused {
|
if self.focused {
|
||||||
if let Event::Key(e) = ev {
|
if let Event::Key(e) = ev {
|
||||||
|
let has_shift =
|
||||||
|
e.modifiers.contains(KeyModifiers::SHIFT);
|
||||||
return match e.code {
|
return match e.code {
|
||||||
KeyCode::Down => {
|
KeyCode::Down if !has_shift => {
|
||||||
self.scroll(true);
|
self.scroll(ScrollType::Down);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
KeyCode::Up => {
|
KeyCode::End | KeyCode::Down if has_shift => {
|
||||||
self.scroll(false);
|
self.scroll(ScrollType::End);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
KeyCode::Home | KeyCode::Up if has_shift => {
|
||||||
|
self.scroll(ScrollType::Home);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
KeyCode::Up if !has_shift => {
|
||||||
|
self.scroll(ScrollType::Up);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,12 @@ pub mod commands {
|
||||||
CMD_GROUP_GENERAL,
|
CMD_GROUP_GENERAL,
|
||||||
);
|
);
|
||||||
///
|
///
|
||||||
|
pub static DIFF_HOME_END: CommandText = CommandText::new(
|
||||||
|
"Scroll [Home,End]",
|
||||||
|
"scroll to top or bottom",
|
||||||
|
CMD_GROUP_DIFF,
|
||||||
|
);
|
||||||
|
///
|
||||||
pub static DIFF_HUNK_ADD: CommandText = CommandText::new(
|
pub static DIFF_HUNK_ADD: CommandText = CommandText::new(
|
||||||
"Add hunk [enter]",
|
"Add hunk [enter]",
|
||||||
"adds selected hunk to stage",
|
"adds selected hunk to stage",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue