diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/cheatsheet/generate.go | 8 | ||||
-rw-r--r-- | pkg/commands/hosting_service/hosting_service.go | 2 | ||||
-rw-r--r-- | pkg/commands/loaders/branches.go | 19 | ||||
-rw-r--r-- | pkg/commands/loaders/remotes.go | 25 | ||||
-rw-r--r-- | pkg/gui/controllers/global_controller.go | 4 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/cherry_pick_helper.go | 13 | ||||
-rw-r--r-- | pkg/gui/filetree/inode.go | 16 | ||||
-rw-r--r-- | pkg/gui/presentation/graph/graph.go | 10 |
8 files changed, 46 insertions, 51 deletions
diff --git a/pkg/cheatsheet/generate.go b/pkg/cheatsheet/generate.go index c7c2b0d37..d88f3d733 100644 --- a/pkg/cheatsheet/generate.go +++ b/pkg/cheatsheet/generate.go @@ -12,8 +12,8 @@ import ( "fmt" "log" "os" - "sort" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/app" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui" @@ -180,9 +180,9 @@ outer: groupedBindings = append(groupedBindings, groupedBindingsType{contextAndView: contextAndView, bindings: contextBindings}) } - sort.Slice(groupedBindings, func(i, j int) bool { - first := groupedBindings[i].contextAndView - second := groupedBindings[j].contextAndView + slices.SortFunc(groupedBindings, func(a, b groupedBindingsType) bool { + first := a.contextAndView + second := b.contextAndView if first.title == "" { return true } diff --git a/pkg/commands/hosting_service/hosting_service.go b/pkg/commands/hosting_service/hosting_service.go index b448e3925..15fc244ba 100644 --- a/pkg/commands/hosting_service/hosting_service.go +++ b/pkg/commands/hosting_service/hosting_service.go @@ -10,7 +10,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" - "golang.org/x/exp/slices" + "github.com/jesseduffield/generics/slices" ) // This package is for handling logic specific to a git hosting service like github, gitlab, bitbucket, etc. diff --git a/pkg/commands/loaders/branches.go b/pkg/commands/loaders/branches.go index 9fa1e80f4..90480ca9a 100644 --- a/pkg/commands/loaders/branches.go +++ b/pkg/commands/loaders/branches.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/jesseduffield/generics/set" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/go-git/v5/config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/common" @@ -78,8 +79,7 @@ outer: if branch.Head { foundHead = true branch.Recency = " *" - branches = append(branches[0:i], branches[i+1:]...) - branches = append([]*models.Branch{branch}, branches...) + branches = slices.Move(branches, i, 0) break } } @@ -88,7 +88,7 @@ outer: if err != nil { return nil, err } - branches = append([]*models.Branch{{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}}, branches...) + branches = slices.Prepend(branches, &models.Branch{Name: currentBranchName, DisplayName: currentBranchDisplayName, Head: true, Recency: " *"}) } configBranches, err := self.config.Branches() @@ -158,10 +158,10 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { trimmedOutput := strings.TrimSpace(output) outputLines := strings.Split(trimmedOutput, "\n") - branches := make([]*models.Branch, 0, len(outputLines)) - for _, line := range outputLines { + + branches := slices.FilterMap(outputLines, func(line string) (*models.Branch, bool) { if line == "" { - continue + return nil, false } split := strings.Split(line, SEPARATION_CHAR) @@ -169,12 +169,11 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { // Ignore line if it isn't separated into 4 parts // This is probably a warning message, for more info see: // https://github.com/jesseduffield/lazygit/issues/1385#issuecomment-885580439 - continue + return nil, false } - branch := obtainBranch(split) - branches = append(branches, branch) - } + return obtainBranch(split), true + }) return branches } diff --git a/pkg/commands/loaders/remotes.go b/pkg/commands/loaders/remotes.go index bd1fe0b6a..3cd57d9a2 100644 --- a/pkg/commands/loaders/remotes.go +++ b/pkg/commands/loaders/remotes.go @@ -3,13 +3,14 @@ package loaders import ( "fmt" "regexp" - "sort" "strings" + "github.com/jesseduffield/generics/slices" gogit "github.com/jesseduffield/go-git/v5" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" + "github.com/samber/lo" ) type RemoteLoader struct { @@ -42,37 +43,35 @@ func (self *RemoteLoader) GetRemotes() ([]*models.Remote, error) { } // first step is to get our remotes from go-git - remotes := make([]*models.Remote, len(goGitRemotes)) - for i, goGitRemote := range goGitRemotes { + remotes := lo.Map(goGitRemotes, func(goGitRemote *gogit.Remote, _ int) *models.Remote { remoteName := goGitRemote.Config().Name re := regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%s\/([\S]+)`, remoteName)) matches := re.FindAllStringSubmatch(remoteBranchesStr, -1) - branches := make([]*models.RemoteBranch, len(matches)) - for j, match := range matches { - branches[j] = &models.RemoteBranch{ + branches := lo.Map(matches, func(match []string, _ int) *models.RemoteBranch { + return &models.RemoteBranch{ Name: match[1], RemoteName: remoteName, } - } + }) - remotes[i] = &models.Remote{ + return &models.Remote{ Name: goGitRemote.Config().Name, Urls: goGitRemote.Config().URLs, Branches: branches, } - } + }) // now lets sort our remotes by name alphabetically - sort.Slice(remotes, func(i, j int) bool { + slices.SortFunc(remotes, func(a, b *models.Remote) bool { // we want origin at the top because we'll be most likely to want it - if remotes[i].Name == "origin" { + if a.Name == "origin" { return true } - if remotes[j].Name == "origin" { + if b.Name == "origin" { return false } - return strings.ToLower(remotes[i].Name) < strings.ToLower(remotes[j].Name) + return strings.ToLower(a.Name) < strings.ToLower(b.Name) }) return remotes, nil diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index c45e98d85..e59231739 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -1,7 +1,7 @@ package controllers import ( - "github.com/jesseduffield/generics/list" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" @@ -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 := list.Reverse(self.c.GetAppState().CustomCommandsHistory) + history := slices.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 e4a9b3e81..c433655d0 100644 --- a/pkg/gui/controllers/helpers/cherry_pick_helper.go +++ b/pkg/gui/controllers/helpers/cherry_pick_helper.go @@ -2,6 +2,7 @@ package helpers import ( "github.com/jesseduffield/generics/set" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/context" @@ -118,12 +119,12 @@ func (self *CherryPickHelper) add(selectedCommit *models.Commit, commitsList []* 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} - }) + newCommits := slices.FilterThenMap(commitsList, + func(commit *models.Commit) bool { return commitSet.Includes(commit.Sha) }, + func(commit *models.Commit) *models.Commit { + return &models.Commit{Name: commit.Name, Sha: commit.Sha} + }, + ) self.getData().CherryPickedCommits = newCommits } diff --git a/pkg/gui/filetree/inode.go b/pkg/gui/filetree/inode.go index 7c8b9fb75..48cdc3be3 100644 --- a/pkg/gui/filetree/inode.go +++ b/pkg/gui/filetree/inode.go @@ -1,8 +1,6 @@ package filetree -import ( - "sort" -) +import "github.com/jesseduffield/generics/slices" type INode interface { IsNil() bool @@ -27,19 +25,17 @@ func sortChildren(node INode) { return } - children := node.GetChildren() - sortedChildren := make([]INode, len(children)) - copy(sortedChildren, children) + sortedChildren := slices.Clone(node.GetChildren()) - sort.Slice(sortedChildren, func(i, j int) bool { - if !sortedChildren[i].IsLeaf() && sortedChildren[j].IsLeaf() { + slices.SortFunc(sortedChildren, func(a, b INode) bool { + if !a.IsLeaf() && b.IsLeaf() { return true } - if sortedChildren[i].IsLeaf() && !sortedChildren[j].IsLeaf() { + if a.IsLeaf() && !b.IsLeaf() { return false } - return sortedChildren[i].GetPath() < sortedChildren[j].GetPath() + return a.GetPath() < b.GetPath() }) // TODO: think about making this in-place diff --git a/pkg/gui/presentation/graph/graph.go b/pkg/gui/presentation/graph/graph.go index 0e193cba8..70ab53079 100644 --- a/pkg/gui/presentation/graph/graph.go +++ b/pkg/gui/presentation/graph/graph.go @@ -2,10 +2,10 @@ package graph import ( "runtime" - "sort" "strings" "sync" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/utils" @@ -265,11 +265,11 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod } // not efficient but doing it for now: sorting my pipes by toPos, then by kind - sort.Slice(newPipes, func(i, j int) bool { - if newPipes[i].toPos == newPipes[j].toPos { - return newPipes[i].kind < newPipes[j].kind + slices.SortFunc(newPipes, func(a, b *Pipe) bool { + if a.toPos == b.toPos { + return a.kind < b.kind } - return newPipes[i].toPos < newPipes[j].toPos + return a.toPos < b.toPos }) return newPipes |