diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-24 13:06:42 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-30 18:51:23 +1000 |
commit | e33fe37a99d4b3866970b1b2f40169d1bc9afa2e (patch) | |
tree | 0027f612fb72b2603044d04389da0236df28d864 | |
parent | ea54cb6e9c07ebca4a53e691fcb3cf993c793a50 (diff) |
Standardise on using lo for slice functions
We've been sometimes using lo and sometimes using my slices package, and we need to pick one
for consistency. Lo is more extensive and better maintained so we're going with that.
My slices package was a superset of go's own slices package so in some places I've just used
the official one (the methods were just wrappers anyway).
I've also moved the remaining methods into the utils package.
58 files changed, 212 insertions, 732 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go index 2b7a8457d..f34611b34 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -13,7 +13,6 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/afero" - "github.com/jesseduffield/generics/slices" appTypes "github.com/jesseduffield/lazygit/pkg/app/types" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" @@ -267,7 +266,11 @@ func (app *App) Run(startArgs appTypes.StartArgs) error { // Close closes any resources func (app *App) Close() error { - return slices.TryForEach(app.closers, func(closer io.Closer) error { - return closer.Close() - }) + for _, closer := range app.closers { + if err := closer.Close(); err != nil { + return err + } + } + + return nil } diff --git a/pkg/app/daemon/rebase.go b/pkg/app/daemon/rebase.go index 8702f0f69..50494113a 100644 --- a/pkg/app/daemon/rebase.go +++ b/pkg/app/daemon/rebase.go @@ -6,10 +6,10 @@ import ( "strings" "github.com/fsmiamoto/git-todo-parser/todo" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/env" + "github.com/samber/lo" ) type TodoLine struct { @@ -26,11 +26,11 @@ func (self *TodoLine) ToString() string { } func TodoLinesToString(todoLines []TodoLine) string { - lines := slices.Map(todoLines, func(todoLine TodoLine) string { + lines := lo.Map(todoLines, func(todoLine TodoLine, _ int) string { return todoLine.ToString() }) - return strings.Join(slices.Reverse(lines), "") + return strings.Join(lo.Reverse(lines), "") } type ChangeTodoAction struct { diff --git a/pkg/app/errors.go b/pkg/app/errors.go index 1556e58fe..2561fccb8 100644 --- a/pkg/app/errors.go +++ b/pkg/app/errors.go @@ -3,8 +3,8 @@ package app import ( "strings" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/samber/lo" ) type errorMapping struct { @@ -18,7 +18,7 @@ func knownError(tr *i18n.TranslationSet, err error) (string, bool) { knownErrorMessages := []string{tr.MinGitVersionError} - if slices.Contains(knownErrorMessages, errorMessage) { + if lo.Contains(knownErrorMessages, errorMessage) { return errorMessage, true } @@ -29,7 +29,7 @@ func knownError(tr *i18n.TranslationSet, err error) (string, bool) { }, } - if mapping, ok := slices.Find(mappings, func(mapping errorMapping) bool { + if mapping, ok := lo.Find(mappings, func(mapping errorMapping) bool { return strings.Contains(errorMessage, mapping.originalError) }); ok { return mapping.newError, true diff --git a/pkg/cheatsheet/generate.go b/pkg/cheatsheet/generate.go index 112e4c4d5..184e84a04 100644 --- a/pkg/cheatsheet/generate.go +++ b/pkg/cheatsheet/generate.go @@ -15,7 +15,6 @@ import ( "strings" "github.com/jesseduffield/generics/maps" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazycore/pkg/utils" "github.com/jesseduffield/lazygit/pkg/app" "github.com/jesseduffield/lazygit/pkg/config" @@ -23,6 +22,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/samber/lo" + "golang.org/x/exp/slices" ) type bindingSection struct { @@ -129,7 +129,7 @@ func localisedTitle(tr *i18n.TranslationSet, str string) string { func getBindingSections(bindings []*types.Binding, tr *i18n.TranslationSet) []*bindingSection { excludedViews := []string{"stagingSecondary", "patchBuildingSecondary"} - bindingsToDisplay := slices.Filter(bindings, func(binding *types.Binding) bool { + bindingsToDisplay := lo.Filter(bindings, func(binding *types.Binding, _ int) bool { if lo.Contains(excludedViews, binding.ViewName) { return false } @@ -162,7 +162,7 @@ func getBindingSections(bindings []*types.Binding, tr *i18n.TranslationSet) []*b return a.header.title < b.header.title }) - return slices.Map(bindingGroups, func(hb headerWithBindings) *bindingSection { + return lo.Map(bindingGroups, func(hb headerWithBindings, _ int) *bindingSection { return &bindingSection{ title: hb.header.title, bindings: hb.bindings, diff --git a/pkg/commands/git_commands/bisect_info.go b/pkg/commands/git_commands/bisect_info.go index ea20d0d38..67293803a 100644 --- a/pkg/commands/git_commands/bisect_info.go +++ b/pkg/commands/git_commands/bisect_info.go @@ -2,7 +2,7 @@ package git_commands import ( "github.com/jesseduffield/generics/maps" - "github.com/jesseduffield/generics/slices" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -97,5 +97,5 @@ func (self *BisectInfo) Bisecting() bool { return false } - return slices.Contains(maps.Values(self.statusMap), BisectStatusOld) + return lo.Contains(maps.Values(self.statusMap), BisectStatusOld) } diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index 7c91c1cbc..094354cad 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -6,13 +6,13 @@ 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/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" + "golang.org/x/exp/slices" ) // context: @@ -75,7 +75,7 @@ outer: if strings.EqualFold(reflogBranch.Name, branch.Name) { branch.Recency = reflogBranch.Recency branchesWithRecency = append(branchesWithRecency, branch) - branches = slices.Remove(branches, j) + branches = utils.Remove(branches, j) continue outer } } @@ -87,14 +87,14 @@ outer: return a.Name < b.Name }) - branches = slices.Prepend(branches, branchesWithRecency...) + branches = utils.Prepend(branches, branchesWithRecency...) foundHead := false for i, branch := range branches { if branch.Head { foundHead = true branch.Recency = " *" - branches = slices.Move(branches, i, 0) + branches = utils.Move(branches, i, 0) break } } @@ -103,7 +103,7 @@ outer: if err != nil { return nil, err } - branches = slices.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Recency: " *"}) + branches = utils.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Recency: " *"}) } configBranches, err := self.config.Branches() @@ -131,7 +131,7 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { trimmedOutput := strings.TrimSpace(output) outputLines := strings.Split(trimmedOutput, "\n") - return slices.FilterMap(outputLines, func(line string) (*models.Branch, bool) { + return lo.FilterMap(outputLines, func(line string, _ int) (*models.Branch, bool) { if line == "" { return nil, false } diff --git a/pkg/commands/git_commands/commit_file_loader.go b/pkg/commands/git_commands/commit_file_loader.go index d89c3c578..7abdc74c5 100644 --- a/pkg/commands/git_commands/commit_file_loader.go +++ b/pkg/commands/git_commands/commit_file_loader.go @@ -3,7 +3,6 @@ package git_commands import ( "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" @@ -52,7 +51,7 @@ func getCommitFilesFromFilenames(filenames string) []*models.CommitFile { } // typical result looks like 'A my_file' meaning my_file was added - return slices.Map(lo.Chunk(lines, 2), func(chunk []string) *models.CommitFile { + return lo.Map(lo.Chunk(lines, 2), func(chunk []string, _ int) *models.CommitFile { return &models.CommitFile{ ChangeStatus: chunk[0], Name: chunk[1], diff --git a/pkg/commands/git_commands/commit_loader.go b/pkg/commands/git_commands/commit_loader.go index ac0f19363..f138adfaf 100644 --- a/pkg/commands/git_commands/commit_loader.go +++ b/pkg/commands/git_commands/commit_loader.go @@ -11,7 +11,6 @@ import ( "sync" "github.com/fsmiamoto/git-todo-parser/todo" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" @@ -233,7 +232,7 @@ func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode return nil, nil } - commitShas := slices.FilterMap(commits, func(commit *models.Commit) (string, bool) { + commitShas := lo.FilterMap(commits, func(commit *models.Commit, _ int) (string, bool) { return commit.Sha, commit.Sha != "" }) @@ -379,7 +378,7 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err // Command does not have a commit associated, skip continue } - commits = slices.Prepend(commits, &models.Commit{ + commits = utils.Prepend(commits, &models.Commit{ Sha: t.Commit, Name: t.Msg, Status: models.StatusRebasing, diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 66bfbf82d..83c3fbe09 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -7,7 +7,6 @@ import ( "github.com/fsmiamoto/git-todo-parser/todo" "github.com/go-errors/errors" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/app/daemon" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" @@ -158,7 +157,7 @@ func (self *RebaseCommands) EditRebase(branchRef string) error { } func logTodoChanges(changes []daemon.ChangeTodoAction) string { - changeTodoStr := strings.Join(slices.Map(changes, func(c daemon.ChangeTodoAction) string { + changeTodoStr := strings.Join(lo.Map(changes, func(c daemon.ChangeTodoAction, _ int) string { return fmt.Sprintf("%s:%s", c.Sha, c.NewAction) }), "\n") return fmt.Sprintf("Changing TODO actions: %s", changeTodoStr) diff --git a/pkg/commands/git_commands/remote_loader.go b/pkg/commands/git_commands/remote_loader.go index c2a8da025..6551ecb25 100644 --- a/pkg/commands/git_commands/remote_loader.go +++ b/pkg/commands/git_commands/remote_loader.go @@ -4,12 +4,13 @@ import ( "strings" "sync" - "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/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" + "golang.org/x/exp/slices" ) type RemoteLoader struct { @@ -53,7 +54,7 @@ func (self *RemoteLoader) GetRemotes() ([]*models.Remote, error) { return nil, remoteBranchesErr } - remotes := slices.Map(goGitRemotes, func(goGitRemote *gogit.Remote) *models.Remote { + remotes := lo.Map(goGitRemotes, func(goGitRemote *gogit.Remote, _ int) *models.Remote { remoteName := goGitRemote.Config().Name branches := remoteBranchesByRemoteName[remoteName] diff --git a/pkg/commands/git_commands/stash_loader.go b/pkg/commands/git_commands/stash_loader.go index e5ea1da49..1ab0e0ad0 100644 --- a/pkg/commands/git_commands/stash_loader.go +++ b/pkg/commands/git_commands/stash_loader.go @@ -5,11 +5,11 @@ 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" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type StashLoader struct { @@ -69,7 +69,7 @@ func (self *StashLoader) getUnfilteredStashEntries() []*models.StashEntry { cmdArgs := NewGitCmd("stash").Arg("list", "-z", "--pretty=%gs").ToArgv() rawString, _ := self.cmd.New(cmdArgs).DontLog().RunWithOutput() - return slices.MapWithIndex(utils.SplitNul(rawString), func(line string, index int) *models.StashEntry { + return lo.Map(utils.SplitNul(rawString), func(line string, index int) *models.StashEntry { return self.stashEntryFromLine(line, index) }) } diff --git a/pkg/commands/git_commands/tag_loader.go b/pkg/commands/git_commands/tag_loader.go index f69ebf79a..bd05fe4b4 100644 --- a/pkg/commands/git_commands/tag_loader.go +++ b/pkg/commands/git_commands/tag_loader.go @@ -3,11 +3,11 @@ package git_commands import ( "regexp" - "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" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type TagLoader struct { @@ -38,7 +38,7 @@ func (self *TagLoader) GetTags() ([]*models.Tag, error) { lineRegex := regexp.MustCompile(`^([^\s]+)(\s+)?(.*)$`) - tags := slices.Map(split, func(line string) *models.Tag { + tags := lo.Map(split, func(line string, _ int) *models.Tag { matches := lineRegex.FindStringSubmatch(line) tagName := matches[1] message := "" diff --git a/pkg/commands/hosting_service/hosting_service.go b/pkg/commands/hosting_service/hosting_service.go index dd06e1cb7..e6f9bef79 100644 --- a/pkg/commands/hosting_service/hosting_service.go +++ b/pkg/commands/hosting_service/hosting_service.go @@ -8,9 +8,10 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" - "github.com/jesseduffield/generics/slices" + "golang.org/x/exp/slices" ) // This package is for handling logic specific to a git hosting service like github, gitlab, bitbucket, gitea, etc. @@ -111,7 +112,7 @@ func (self *HostingServiceMgr) getCandidateServiceDomains() []ServiceDomain { serviceDefinition, ok := serviceDefinitionByProvider[provider] if !ok { - providerNames := slices.Map(serviceDefinitions, func(serviceDefinition ServiceDefinition) string { + providerNames := lo.Map(serviceDefinitions, func(serviceDefinition ServiceDefinition, _ int) string { return serviceDefinition.provider }) diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 3e305a96f..4a77310b5 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -13,7 +13,6 @@ import ( "github.com/samber/lo" "github.com/atotto/clipboard" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/kill" "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" @@ -189,7 +188,7 @@ func (c *OSCommand) FileExists(path string) (bool, error) { // PipeCommands runs a heap of commands and pipes their inputs/outputs together like A | B | C func (c *OSCommand) PipeCommands(cmdObjs ...ICmdObj) error { - cmds := slices.Map(cmdObjs, func(cmdObj ICmdObj) *exec.Cmd { + cmds := lo.Map(cmdObjs, func(cmdObj ICmdObj, _ int) *exec.Cmd { return cmdObj.GetCmd() }) diff --git a/pkg/commands/patch/patch_builder.go b/pkg/commands/patch/patch_builder.go index b73891911..2f350a40b 100644 --- a/pkg/commands/patch/patch_builder.go +++ b/pkg/commands/patch/patch_builder.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/jesseduffield/generics/maps" - "github.com/jesseduffield/generics/slices" "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -209,10 +208,10 @@ func (p *PatchBuilder) renderEachFilePatch(plain bool) []string { filenames := maps.Keys(p.fileInfoMap) sort.Strings(filenames) - patches := slices.Map(filenames, func(filename string) string { + patches := lo.Map(filenames, func(filename string, _ int) string { return p.RenderPatchForFile(filename, plain, false) }) - output := slices.Filter(patches, func(patch string) bool { + output := lo.Filter(patches, func(patch string, _ int) bool { return patch != "" }) diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 90bb78768..d845265a3 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -4,9 +4,9 @@ import ( "errors" "sync" - "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" ) @@ -134,7 +134,7 @@ func (self *ContextMgr) pushToContextStack(c types.Context) ([]types.Context, ty (topContext.GetKind() == types.MAIN_CONTEXT && c.GetKind() == types.MAIN_CONTEXT) { contextsToDeactivate = append(contextsToDeactivate, topContext) - _, self.ContextStack = slices.Pop(self.ContextStack) + _, self.ContextStack = utils.Pop(self.ContextStack) } self.ContextStack = append(self.ContextStack, c) @@ -154,7 +154,7 @@ func (self *ContextMgr) Pop() error { } var currentContext types.Context - currentContext, self.ContextStack = slices.Pop(se |