mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
Keep the same commit selected when exiting filtering mode
When exiting filtering mode, we currently keep the selection index the same in the commits panel. This doesn't make sense at all, since the index in the filtered view has no relation to the index in the unfiltered view. I often use filtering mode (either by path or by author) to find a given commit faster than I would otherwise be able to. When exiting filtering mode, it's useful to keep the same commit selected, so that I can look at the surrounding commits, see which branch it was a part of, etc. So reselect the commit again after exiting filtering mode. Sometimes this is not possible, most likely when the commit is so long ago that it's outside of the initial 300 range. In that case, at least select the commit again that was selected before I entered filtering; this is still better than arbitrarily keeping the same selection index.
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type LocalCommitsContext struct {
|
||||
@ -125,6 +126,29 @@ func (self *LocalCommitsContext) GetSelectedRef() types.Ref {
|
||||
return commit
|
||||
}
|
||||
|
||||
// Returns the commit hash of the selected commit, or an empty string if no
|
||||
// commit is selected
|
||||
func (self *LocalCommitsContext) GetSelectedCommitHash() string {
|
||||
commit := self.GetSelected()
|
||||
if commit == nil {
|
||||
return ""
|
||||
}
|
||||
return commit.Sha
|
||||
}
|
||||
|
||||
func (self *LocalCommitsContext) SelectCommitByHash(hash string) bool {
|
||||
if hash == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if _, idx, found := lo.FindIndexOf(self.GetItems(), func(c *models.Commit) bool { return c.Sha == hash }); found {
|
||||
self.SetSelection(idx)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *LocalCommitsContext) GetDiffTerminals() []string {
|
||||
itemId := self.GetSelectedItemId()
|
||||
|
||||
|
Reference in New Issue
Block a user