Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: file and status tab support pageup and pagedown #2496

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d1645d0
feat: filetree support pageup and pagedown
Fatpandac Jan 21, 2025
12641c8
chore: update changelog
Fatpandac Jan 21, 2025
93019c4
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Jan 22, 2025
2d80047
feat: status tab suppoer pageup and pagedown
Fatpandac Jan 22, 2025
e299c81
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Feb 8, 2025
f9e9ffc
fix: use Cell replace RefCell
Fatpandac Feb 10, 2025
a430b8e
test: add test for selection_page_updown
Fatpandac Feb 10, 2025
6a5c1b9
chore: format
Fatpandac Feb 10, 2025
bfaa873
chore: update changelog
Fatpandac Feb 10, 2025
e83a699
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Feb 10, 2025
8d6d29b
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Feb 19, 2025
f623ac6
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Feb 21, 2025
85e74a0
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Feb 26, 2025
ce5182d
chore: remove .editorconfig
Fatpandac Mar 16, 2025
9dd360c
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Mar 21, 2025
bff6fba
Merge branch 'master' into feat/files_support_pageupdown
Fatpandac Mar 27, 2025
0f338b8
fix: the status tree page-down action overflows the tree length
Fatpandac Mar 27, 2025
580929f
refactor: use a more reliable method to implement page up and down.
Fatpandac Mar 28, 2025
f7adee6
fix: CI err
Fatpandac Mar 28, 2025
10b510b
chore: rename show_height to window_height
Fatpandac Mar 29, 2025
62123cb
refactor: use a more reliable method to implement page up and down in…
Fatpandac Mar 30, 2025
485a38c
chore: format
Fatpandac Mar 30, 2025
885c06b
test: test case failed
Fatpandac Mar 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.rs]
indent_style = tab
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added
* Files and status tab support pageUp and pageDown [[@fatpandac](https://github.com/fatpandac)] ([#1951](https://github.com/extrawurst/gitui/issues/1951))

### Changed
* improve syntax highlighting file detection [[@acuteenvy](https://github.com/acuteenvy)] ([#2524](https://github.com/extrawurst/gitui/pull/2524))
* After commit: jump back to unstaged area [[@tommady](https://github.com/tommady)] ([#2476](https://github.com/extrawurst/gitui/issues/2476))
Expand Down
88 changes: 85 additions & 3 deletions filetreelist/src/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::Result, filetreeitems::FileTreeItems,
tree_iter::TreeIterator, TreeItemInfo,
};
use std::{collections::BTreeSet, path::Path};
use std::{cell::Cell, collections::BTreeSet, path::Path};

///
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -30,6 +30,7 @@ pub struct FileTree {
selection: Option<usize>,
// caches the absolute selection translated to visual index
visual_selection: Option<VisualSelection>,
pub show_height: Cell<Option<usize>>,
}

impl FileTree {
Expand All @@ -42,6 +43,7 @@ impl FileTree {
items: FileTreeItems::new(list, collapsed)?,
selection: if list.is_empty() { None } else { Some(0) },
visual_selection: None,
show_height: None.into(),
};
new_self.visual_selection = new_self.calc_visual_selection();

Expand Down Expand Up @@ -112,6 +114,49 @@ impl FileTree {
}
}

fn selection_page_updown(
&self,
current_index: usize,
up: bool,
) -> Option<usize> {
let mut index = current_index;

let mut count = 0;
loop {
index = {
let new_index = if up {
index.saturating_sub(1)
} else {
index.saturating_add(1)
};

if new_index == index {
break;
}

if new_index >= self.items.len() {
break;
}

new_index
};

if self.is_visible_index(index) {
count += 1;
}

if count >= self.show_height.get().unwrap_or(0) {
break;
}
}

if index == current_index {
None
} else {
Some(index)
}
}

///
pub fn move_selection(&mut self, dir: MoveSelection) -> bool {
self.selection.is_some_and(|selection| {
Expand All @@ -130,8 +175,13 @@ impl FileTree {
Self::selection_start(selection)
}
MoveSelection::End => self.selection_end(selection),
MoveSelection::PageDown | MoveSelection::PageUp => {
None
MoveSelection::PageUp => {
Self::selection_page_updown(self, selection, true)
}
MoveSelection::PageDown => {
Self::selection_page_updown(
self, selection, false,
)
}
};

Expand Down Expand Up @@ -514,4 +564,36 @@ mod test {
assert_eq!(s.count, 3);
assert_eq!(s.index, 2);
}

#[test]
fn test_selection_page_updown() {
let items = vec![
Path::new("a/b/c"), //
Path::new("a/b/c2"), //
Path::new("a/d"), //
Path::new("a/e"), //
];

//0 a/
//1 b/
//2 c
//3 c2
//4 d
//5 e

let mut tree =
FileTree::new(&items, &BTreeSet::new()).unwrap();

tree.show_height.set(Some(2));

tree.selection = Some(0);
assert!(tree.move_selection(MoveSelection::PageDown));
assert_eq!(tree.selection, Some(2));
assert!(tree.move_selection(MoveSelection::PageDown));
assert_eq!(tree.selection, Some(4));
assert!(tree.move_selection(MoveSelection::PageUp));
assert_eq!(tree.selection, Some(2));
assert!(tree.move_selection(MoveSelection::PageUp));
assert_eq!(tree.selection, Some(0));
}
}
4 changes: 4 additions & 0 deletions src/components/revision_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ impl RevisionFilesComponent {
let tree_height = usize::from(area.height.saturating_sub(2));
let tree_width = usize::from(area.width);

self.tree
.show_height
.set(Some(tree_height.saturating_sub(1)));

self.tree.visual_selection().map_or_else(
|| {
self.scroll.reset();
Expand Down
10 changes: 10 additions & 0 deletions src/components/status_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ impl DrawableComponent for StatusTreeComponent {
.map(|idx| idx.saturating_sub(selection_offset))
.unwrap_or_default();
let tree_height = r.height.saturating_sub(2) as usize;
self.tree.window_height.set(Some(tree_height));

self.scroll_top.set(ui::calc_scroll_top(
self.scroll_top.get(),
Expand Down Expand Up @@ -504,6 +505,15 @@ impl Component for StatusTreeComponent {
|| key_match(e, self.key_config.keys.shift_down)
{
Ok(self.move_selection(MoveSelection::End).into())
} else if key_match(e, self.key_config.keys.page_up) {
Ok(self
.move_selection(MoveSelection::PageUp)
.into())
} else if key_match(e, self.key_config.keys.page_down)
{
Ok(self
.move_selection(MoveSelection::PageDown)
.into())
} else if key_match(e, self.key_config.keys.move_left)
{
Ok(self
Expand Down
43 changes: 42 additions & 1 deletion src/components/utils/statustree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::filetree::{
};
use anyhow::Result;
use asyncgit::StatusItem;
use std::{cmp, collections::BTreeSet};
use std::{cell::Cell, cmp, collections::BTreeSet};

//TODO: use new `filetreelist` crate

Expand All @@ -16,6 +16,8 @@ pub struct StatusTree {
// some folders may be folded up, this allows jumping
// over folders which are folded into their parent
pub available_selections: Vec<usize>,

pub window_height: Cell<Option<usize>>,
}

///
Expand All @@ -27,6 +29,8 @@ pub enum MoveSelection {
Right,
Home,
End,
PageDown,
PageUp,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -143,6 +147,12 @@ impl StatusTree {
}
MoveSelection::Home => SelectionChange::new(0, false),
MoveSelection::End => self.selection_end(),
MoveSelection::PageUp => {
self.selection_page_updown(selection, true)
}
MoveSelection::PageDown => {
self.selection_page_updown(selection, false)
}
};

let changed_index =
Expand Down Expand Up @@ -283,6 +293,37 @@ impl StatusTree {
SelectionChange::new(new_index, false)
}

fn selection_page_updown(
&self,
current_index: usize,
up: bool,
) -> SelectionChange {
let mut new_index = current_index;
let mut count = 0;

loop {
if up {
new_index = new_index.saturating_sub(1);
} else {
new_index = new_index.saturating_add(1);
}

if self.is_visible_index(new_index) {
count += 1;
}

if count == self.window_height.get().unwrap_or(0) {
break;
}

if new_index == 0 || new_index == self.tree.len() - 1 {
break;
}
}

SelectionChange::new(new_index, false)
}

fn is_visible_index(&self, idx: usize) -> bool {
self.tree[idx].info.visible
}
Expand Down
Loading