From 94a53484a183bb32b066dc51fb90948dead634a1 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 19 Mar 2022 19:51:48 +1100 Subject: would you believe that I'm adding even more generics --- pkg/commands/hosting_service/hosting_service.go | 8 ++++---- pkg/commands/loaders/commits.go | 5 +++-- pkg/commands/loaders/remotes.go | 5 ++--- pkg/commands/loaders/stash.go | 9 ++++----- pkg/commands/patch/patch_parser.go | 2 +- pkg/gui/app_status_manager.go | 11 ++++------- pkg/gui/context.go | 16 ++++++++-------- pkg/gui/controllers/helpers/cherry_pick_helper.go | 3 +-- pkg/gui/filetree/inode.go | 9 +++------ pkg/gui/presentation/graph/graph.go | 15 ++++++--------- 10 files changed, 36 insertions(+), 47 deletions(-) (limited to 'pkg') diff --git a/pkg/commands/hosting_service/hosting_service.go b/pkg/commands/hosting_service/hosting_service.go index 15fc244ba..091da3ebb 100644 --- a/pkg/commands/hosting_service/hosting_service.go +++ b/pkg/commands/hosting_service/hosting_service.go @@ -111,10 +111,10 @@ func (self *HostingServiceMgr) getCandidateServiceDomains() []ServiceDomain { serviceDefinition, ok := serviceDefinitionByProvider[provider] if !ok { - providerNames := []string{} - for _, serviceDefinition := range serviceDefinitions { - providerNames = append(providerNames, serviceDefinition.provider) - } + providerNames := slices.Map(serviceDefinitions, func(serviceDefinition ServiceDefinition) string { + return serviceDefinition.provider + }) + self.log.Errorf("Unknown git service type: '%s'. Expected one of %s", provider, strings.Join(providerNames, ", ")) continue } diff --git a/pkg/commands/loaders/commits.go b/pkg/commands/loaders/commits.go index 20721be42..c370cc059 100644 --- a/pkg/commands/loaders/commits.go +++ b/pkg/commands/loaders/commits.go @@ -307,6 +307,7 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err commits := []*models.Commit{} lines := strings.Split(string(bytesContent), "\n") + for _, line := range lines { if line == "" || line == "noop" { return commits, nil @@ -315,12 +316,12 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err continue } splitLine := strings.Split(line, " ") - commits = append([]*models.Commit{{ + commits = slices.Prepend(commits, &models.Commit{ Sha: splitLine[1], Name: strings.Join(splitLine[2:], " "), Status: "rebasing", Action: splitLine[0], - }}, commits...) + }) } return commits, nil diff --git a/pkg/commands/loaders/remotes.go b/pkg/commands/loaders/remotes.go index 3cd57d9a2..1323560f5 100644 --- a/pkg/commands/loaders/remotes.go +++ b/pkg/commands/loaders/remotes.go @@ -10,7 +10,6 @@ import ( "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 { @@ -43,12 +42,12 @@ func (self *RemoteLoader) GetRemotes() ([]*models.Remote, error) { } // first step is to get our remotes from go-git - remotes := lo.Map(goGitRemotes, func(goGitRemote *gogit.Remote, _ int) *models.Remote { + remotes := slices.Map(goGitRemotes, func(goGitRemote *gogit.Remote) *models.Remote { remoteName := goGitRemote.Config().Name re := regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%s\/([\S]+)`, remoteName)) matches := re.FindAllStringSubmatch(remoteBranchesStr, -1) - branches := lo.Map(matches, func(match []string, _ int) *models.RemoteBranch { + branches := slices.Map(matches, func(match []string) *models.RemoteBranch { return &models.RemoteBranch{ Name: match[1], RemoteName: remoteName, diff --git a/pkg/commands/loaders/stash.go b/pkg/commands/loaders/stash.go index 689bf30ce..66cfeaa3e 100644 --- a/pkg/commands/loaders/stash.go +++ b/pkg/commands/loaders/stash.go @@ -5,6 +5,7 @@ import ( "strconv" "strings" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" @@ -65,11 +66,9 @@ outer: func (self *StashLoader) getUnfilteredStashEntries() []*models.StashEntry { rawString, _ := self.cmd.New("git stash list --pretty='%gs'").DontLog().RunWithOutput() - stashEntries := []*models.StashEntry{} - for i, line := range utils.SplitLines(rawString) { - stashEntries = append(stashEntries, self.stashEntryFromLine(line, i)) - } - return stashEntries + return slices.MapWithIndex(utils.SplitLines(rawString), func(line string, index int) *models.StashEntry { + return self.stashEntryFromLine(line, index) + }) } func (c *StashLoader) stashEntryFromLine(line string, index int) *models.StashEntry { diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index 097f01329..fa730afe7 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -191,7 +191,7 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic return "" } - renderedLines := lo.Map(p.PatchLines, func(patchLine *PatchLine, index int) string { + renderedLines := slices.MapWithIndex(p.PatchLines, func(patchLine *PatchLine, index int) string { selected := index >= firstLineIndex && index <= lastLineIndex included := lo.Contains(incLineIndices, index) return patchLine.render(selected, included) diff --git a/pkg/gui/app_status_manager.go b/pkg/gui/app_status_manager.go index 4c32f79b5..34c97a5f4 100644 --- a/pkg/gui/app_status_manager.go +++ b/pkg/gui/app_status_manager.go @@ -4,6 +4,7 @@ import ( "sync" "time" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -23,13 +24,9 @@ func (m *statusManager) removeStatus(id int) { m.mutex.Lock() defer m.mutex.Unlock() - newStatuses := []appStatus{} - for _, status := range m.statuses { - if status.id != id { - newStatuses = append(newStatuses, status) - } - } - m.statuses = newStatuses + m.statuses = slices.Filter(m.statuses, func(status appStatus) bool { + return status.id != id + }) } func (m *statusManager) addWaitingStatus(message string) int { diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 2c30218bc..13b55e342 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -14,14 +14,14 @@ import ( ) func (gui *Gui) popupViewNames() []string { - result := []string{} - for _, context := range gui.State.Contexts.Flatten() { - if context.GetKind() == types.PERSISTENT_POPUP || context.GetKind() == types.TEMPORARY_POPUP { - result = append(result, context.GetViewName()) - } - } - - return result + return slices.FilterThenMap(gui.State.Contexts.Flatten(), + func(c types.Context) bool { + return c.GetKind() == types.PERSISTENT_POPUP || c.GetKind() == types.TEMPORARY_POPUP + }, + func(c types.Context) string { + return c.GetViewName() + }, + ) } func (gui *Gui) currentContextKeyIgnoringPopups() types.ContextKey { diff --git a/pkg/gui/controllers/helpers/cherry_pick_helper.go b/pkg/gui/controllers/helpers/cherry_pick_helper.go index c433655d0..9117f24f6 100644 --- a/pkg/gui/controllers/helpers/cherry_pick_helper.go +++ b/pkg/gui/controllers/helpers/cherry_pick_helper.go @@ -8,7 +8,6 @@ import ( "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 { @@ -109,7 +108,7 @@ func (self *CherryPickHelper) Reset() error { } func (self *CherryPickHelper) CherryPickedCommitShaSet() *set.Set[string] { - shas := lo.Map(self.getData().CherryPickedCommits, func(commit *models.Commit, _ int) string { + shas := slices.Map(self.getData().CherryPickedCommits, func(commit *models.Commit) string { return commit.Sha }) return set.NewFromSlice(shas) diff --git a/pkg/gui/filetree/inode.go b/pkg/gui/filetree/inode.go index 48cdc3be3..d59315b28 100644 --- a/pkg/gui/filetree/inode.go +++ b/pkg/gui/filetree/inode.go @@ -200,10 +200,7 @@ func getLeaves(node INode) []INode { return []INode{node} } - output := []INode{} - for _, child := range node.GetChildren() { - output = append(output, getLeaves(child)...) - } - - return output + return slices.FlatMap(node.GetChildren(), func(child INode) []INode { + return getLeaves(child) + }) } diff --git a/pkg/gui/presentation/graph/graph.go b/pkg/gui/presentation/graph/graph.go index de90d3e7a..392af8984 100644 --- a/pkg/gui/presentation/graph/graph.go +++ b/pkg/gui/presentation/graph/graph.go @@ -67,13 +67,10 @@ func GetPipeSets(commits []*models.Commit, getStyle func(c *models.Commit) style pipes := []*Pipe{{fromPos: 0, toPos: 0, fromSha: "START", toSha: commits[0].Sha, kind: STARTS, style: style.FgDefault}} - pipeSets := [][]*Pipe{} - for _, commit := range commits { + return slices.Map(commits, func(commit *models.Commit) []*Pipe { pipes = getNextPipes(pipes, commit, getStyle) - pipeSets = append(pipeSets, pipes) - } - - return pipeSets + return pipes + }) } func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitSha string) []string { @@ -115,9 +112,9 @@ func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitSha s } func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *models.Commit) style.TextStyle) []*Pipe { - maxPos := lo.Max( - slices.Map(prevPipes, func(pipe *Pipe) int { return pipe.toPos }), - ) + maxPos := slices.MaxBy(prevPipes, func(pipe *Pipe) int { + return pipe.toPos + }) // a pipe that terminated in the previous line has no bearing on the current line // so we'll filter those out -- cgit v1.2.3