Skip to content

Commit 38bfc84

Browse files
committed
keep selected index in sort
1 parent 75fb648 commit 38bfc84

File tree

2 files changed

+82
-63
lines changed

2 files changed

+82
-63
lines changed

src/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,8 @@ impl App {
814814
.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS);
815815
}
816816
InternalEvent::BranchListSort(sort_by) => {
817-
self.select_branch_popup.sort(sort_by)?;
817+
self.select_branch_popup.change_sort_by(sort_by);
818+
self.select_branch_popup.sort()?;
818819
flags
819820
.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS);
820821
}

src/popups/branchlist.rs

Lines changed: 80 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ impl BranchListPopup {
401401
self.local = !self.local;
402402
self.check_remotes();
403403
self.update_branches()?;
404+
self.set_selection(0)?;
404405
}
405406
Ok(EventState::NotConsumed)
406407
}
@@ -427,9 +428,85 @@ impl BranchListPopup {
427428
}
428429
}
429430

430-
pub fn sort(&mut self, sort_by: BranchListSortBy) -> Result<()> {
431+
pub fn change_sort_by(&mut self, sort_by: BranchListSortBy) {
431432
self.sort_by = sort_by;
432-
self.update_branches()?;
433+
}
434+
435+
pub fn sort(&mut self) -> Result<()> {
436+
let pre_selected = self
437+
.branches
438+
.get(self.selection as usize)
439+
.map(|b| b.name.clone());
440+
match &self.sort_by {
441+
BranchListSortBy::LastCommitAuthorAsc => {
442+
self.branches.sort_by(|a, b| {
443+
match b
444+
.top_commit_author
445+
.cmp(&a.top_commit_author)
446+
{
447+
std::cmp::Ordering::Equal => {
448+
a.name.cmp(&b.name)
449+
}
450+
other => other,
451+
}
452+
});
453+
}
454+
BranchListSortBy::LastCommitAuthorDesc => {
455+
self.branches.sort_by(|a, b| {
456+
match a
457+
.top_commit_author
458+
.cmp(&b.top_commit_author)
459+
{
460+
std::cmp::Ordering::Equal => {
461+
a.name.cmp(&b.name)
462+
}
463+
other => other,
464+
}
465+
});
466+
}
467+
BranchListSortBy::LastCommitTimeAsc => {
468+
self.branches.sort_by(|a, b| {
469+
match a.top_commit_time.cmp(&b.top_commit_time) {
470+
std::cmp::Ordering::Equal => {
471+
a.name.cmp(&b.name)
472+
}
473+
other => other,
474+
}
475+
});
476+
}
477+
BranchListSortBy::LastCommitTimeDesc => {
478+
self.branches.sort_by(|a, b| {
479+
match b.top_commit_time.cmp(&a.top_commit_time) {
480+
std::cmp::Ordering::Equal => {
481+
a.name.cmp(&b.name)
482+
}
483+
other => other,
484+
}
485+
});
486+
}
487+
BranchListSortBy::BranchNameAsc => {
488+
self.branches.sort_by(|a, b| a.name.cmp(&b.name));
489+
}
490+
BranchListSortBy::BranchNameDesc => {
491+
self.branches.sort_by(|a, b| b.name.cmp(&a.name));
492+
}
493+
}
494+
495+
match pre_selected {
496+
Some(pre_selected) => {
497+
let next_selecttion = self
498+
.branches
499+
.iter()
500+
.position(|b| b.name == pre_selected)
501+
.unwrap_or(0);
502+
self.set_selection(
503+
next_selecttion.try_into().unwrap_or_default(),
504+
)?;
505+
}
506+
None => {
507+
self.set_selection(0)?;
508+
}
509+
}
433510

434511
Ok(())
435512
}
@@ -440,66 +517,7 @@ impl BranchListPopup {
440517
self.check_remotes();
441518
self.branches =
442519
get_branches_info(&self.repo.borrow(), self.local)?;
443-
match &self.sort_by {
444-
BranchListSortBy::LastCommitAuthorAsc => {
445-
self.branches.sort_by(|a, b| {
446-
match b
447-
.top_commit_author
448-
.cmp(&a.top_commit_author)
449-
{
450-
std::cmp::Ordering::Equal => {
451-
a.name.cmp(&b.name)
452-
}
453-
other => other,
454-
}
455-
});
456-
}
457-
BranchListSortBy::LastCommitAuthorDesc => {
458-
self.branches.sort_by(|a, b| {
459-
match a
460-
.top_commit_author
461-
.cmp(&b.top_commit_author)
462-
{
463-
std::cmp::Ordering::Equal => {
464-
a.name.cmp(&b.name)
465-
}
466-
other => other,
467-
}
468-
});
469-
}
470-
BranchListSortBy::LastCommitTimeAsc => {
471-
self.branches.sort_by(|a, b| {
472-
match a
473-
.top_commit_time
474-
.cmp(&b.top_commit_time)
475-
{
476-
std::cmp::Ordering::Equal => {
477-
a.name.cmp(&b.name)
478-
}
479-
other => other,
480-
}
481-
});
482-
}
483-
BranchListSortBy::LastCommitTimeDesc => {
484-
self.branches.sort_by(|a, b| {
485-
match b
486-
.top_commit_time
487-
.cmp(&a.top_commit_time)
488-
{
489-
std::cmp::Ordering::Equal => {
490-
a.name.cmp(&b.name)
491-
}
492-
other => other,
493-
}
494-
});
495-
}
496-
BranchListSortBy::BranchNameAsc => {
497-
self.branches.sort_by(|a, b| a.name.cmp(&b.name));
498-
}
499-
BranchListSortBy::BranchNameDesc => {
500-
self.branches.sort_by(|a, b| b.name.cmp(&a.name));
501-
}
502-
}
520+
self.sort()?;
503521
//remove remote branch called `HEAD`
504522
if !self.local {
505523
self.branches

0 commit comments

Comments
 (0)