diff --git a/pkg/gui/context/commit_list_goto_bottom.go b/pkg/gui/context/commit_list_goto_bottom.go new file mode 100644 index 000000000..f05e9dffc --- /dev/null +++ b/pkg/gui/context/commit_list_goto_bottom.go @@ -0,0 +1,23 @@ +package context + +import ( + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/samber/lo" +) + +// Returns the index of the commit that the "go to bottom" command selects in a +// list of commits +func indexForGotoBottomInCommits(commits []*models.Commit, selectedIdx int) int { + if selectedIdx >= 0 && selectedIdx < len(commits)-1 { + if commits[selectedIdx+1].Status != models.StatusMerged { + _, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool { + return c.Status == models.StatusMerged + }) + if found { + return idx - 1 + } + } + } + + return len(commits) - 1 +} diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index e9faf3911..a00783147 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -520,18 +520,5 @@ func searchModelCommits(caseSensitive bool, commits []*models.Commit, columnPosi } func (self *LocalCommitsContext) IndexForGotoBottom() int { - commits := self.GetCommits() - selectedIdx := self.GetSelectedLineIdx() - if selectedIdx >= 0 && selectedIdx < len(commits)-1 { - if commits[selectedIdx+1].Status != models.StatusMerged { - _, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool { - return c.Status == models.StatusMerged - }) - if found { - return idx - 1 - } - } - } - - return self.list.Len() - 1 + return indexForGotoBottomInCommits(self.GetCommits(), self.GetSelectedLineIdx()) } diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index 73d3022b5..c6bad0f45 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -269,18 +269,5 @@ func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitiv } func (self *SubCommitsContext) IndexForGotoBottom() int { - commits := self.GetCommits() - selectedIdx := self.GetSelectedLineIdx() - if selectedIdx >= 0 && selectedIdx < len(commits)-1 { - if commits[selectedIdx+1].Status != models.StatusMerged { - _, idx, found := lo.FindIndexOf(commits, func(c *models.Commit) bool { - return c.Status == models.StatusMerged - }) - if found { - return idx - 1 - } - } - } - - return self.list.Len() - 1 + return indexForGotoBottomInCommits(self.GetCommits(), self.GetSelectedLineIdx()) }