diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 15:36:46 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 20:14:41 +1100 |
commit | eda8f4a5d4302691d99efd066f9851809c984bc0 (patch) | |
tree | 0cb13f767ca52682a62d09314f8d9f2f2c1cd4a1 /pkg/commands | |
parent | c7a629c4401ae0d4aad06767c88ce1e9e418dbf3 (diff) |
lots more generics
Diffstat (limited to 'pkg/commands')
-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 |
3 files changed, 22 insertions, 24 deletions
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 |