diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 12:26:30 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 20:14:41 +1100 |
commit | c7a629c4401ae0d4aad06767c88ce1e9e418dbf3 (patch) | |
tree | 2ff5599041030a423e02214989f07129772337c6 /pkg/gui/controllers | |
parent | dde30fa104347ab9c01f82b7886864b473e6f51c (diff) |
make more use of generics
Diffstat (limited to 'pkg/gui/controllers')
-rw-r--r-- | pkg/gui/controllers/global_controller.go | 8 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/cherry_pick_helper.go | 36 |
2 files changed, 22 insertions, 22 deletions
diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 487c1e10b..c45e98d85 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -1,9 +1,11 @@ package controllers import ( + "github.com/jesseduffield/generics/list" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type GlobalController struct { @@ -36,9 +38,7 @@ func (self *GlobalController) customCommand() error { FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(), HandleConfirm: func(command string) error { self.c.GetAppState().CustomCommandsHistory = utils.Limit( - utils.Uniq( - append(self.c.GetAppState().CustomCommandsHistory, command), - ), + lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)), 1000, ) @@ -57,7 +57,7 @@ func (self *GlobalController) customCommand() error { func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { // reversing so that we display the latest command first - history := utils.Reverse(self.c.GetAppState().CustomCommandsHistory) + history := list.Reverse(self.c.GetAppState().CustomCommandsHistory) return helpers.FuzzySearchFunc(history) } diff --git a/pkg/gui/controllers/helpers/cherry_pick_helper.go b/pkg/gui/controllers/helpers/cherry_pick_helper.go index badbf0dfe..e4a9b3e81 100644 --- a/pkg/gui/controllers/helpers/cherry_pick_helper.go +++ b/pkg/gui/controllers/helpers/cherry_pick_helper.go @@ -1,11 +1,13 @@ package helpers import ( + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) type CherryPickHelper struct { @@ -63,13 +65,13 @@ func (self *CherryPickHelper) CopyRange(selectedIndex int, commitsList []*models return err } - commitShaMap := self.CherryPickedCommitShaMap() + commitSet := self.CherryPickedCommitShaSet() // find the last commit that is copied that's above our position // if there are none, startIndex = 0 startIndex := 0 for index, commit := range commitsList[0:selectedIndex] { - if commitShaMap[commit.Sha] { + if commitSet.Includes(commit.Sha) { startIndex = index } } @@ -105,25 +107,23 @@ func (self *CherryPickHelper) Reset() error { return self.rerender() } -func (self *CherryPickHelper) CherryPickedCommitShaMap() map[string]bool { - commitShaMap := map[string]bool{} - for _, commit := range self.getData().CherryPickedCommits { - commitShaMap[commit.Sha] = true - } - return commitShaMap +func (self *CherryPickHelper) CherryPickedCommitShaSet() *set.Set[string] { + shas := lo.Map(self.getData().CherryPickedCommits, func(commit *models.Commit, _ int) string { + return commit.Sha + }) + return set.NewFromSlice(shas) } func (self *CherryPickHelper) add(selectedCommit *models.Commit, commitsList []*models.Commit) { - commitShaMap := self.CherryPickedCommitShaMap() - commitShaMap[selectedCommit.Sha] = true - - newCommits := []*models.Commit{} - for _, commit := range commitsList { - if commitShaMap[commit.Sha] { - // duplicating just the things we need to put in the rebase TODO list - newCommits = append(newCommits, &models.Commit{Name: commit.Name, Sha: commit.Sha}) - } - } + commitSet := self.CherryPickedCommitShaSet() + commitSet.Add(selectedCommit.Sha) + + commitsInSet := lo.Filter(commitsList, func(commit *models.Commit, _ int) bool { + return commitSet.Includes(commit.Sha) + }) + newCommits := lo.Map(commitsInSet, func(commit *models.Commit, _ int) *models.Commit { + return &models.Commit{Name: commit.Name, Sha: commit.Sha} + }) self.getData().CherryPickedCommits = newCommits } |