From c7a629c4401ae0d4aad06767c88ce1e9e418dbf3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 19 Mar 2022 12:26:30 +1100 Subject: make more use of generics --- go.mod | 2 + go.sum | 5 + pkg/commands/git_commands/rebase_test.go | 4 +- pkg/commands/loaders/branches.go | 7 +- pkg/commands/loaders/files.go | 10 +- pkg/commands/patch/hunk.go | 3 +- pkg/commands/patch/patch_manager.go | 6 +- pkg/commands/patch/patch_parser.go | 3 +- pkg/gui/controllers/global_controller.go | 8 +- pkg/gui/controllers/helpers/cherry_pick_helper.go | 36 +- pkg/gui/filetree/collapsed_paths.go | 32 +- pkg/gui/filetree/commit_file_node.go | 8 +- pkg/gui/filetree/commit_file_tree.go | 6 +- pkg/gui/filetree/file_node.go | 8 +- pkg/gui/filetree/file_tree.go | 8 +- pkg/gui/filetree/inode.go | 18 +- pkg/gui/list_context_config.go | 6 +- pkg/gui/options_menu_panel.go | 18 +- pkg/gui/patch_building_panel.go | 6 +- pkg/gui/presentation/commits.go | 13 +- pkg/gui/presentation/commits_test.go | 5 +- pkg/gui/presentation/files.go | 2 +- pkg/gui/presentation/reflog_commits.go | 5 +- pkg/gui/refresh.go | 27 +- pkg/utils/slice.go | 83 -- pkg/utils/slice_test.go | 76 -- vendor/github.com/jesseduffield/generics/LICENSE | 21 + .../jesseduffield/generics/hashmap/functions.go | 35 + .../jesseduffield/generics/list/comparable_list.go | 49 + .../jesseduffield/generics/list/functions.go | 72 ++ .../github.com/jesseduffield/generics/list/list.go | 117 +++ .../github.com/jesseduffield/generics/set/set.go | 49 + vendor/github.com/samber/lo/.gitignore | 36 + vendor/github.com/samber/lo/CHANGELOG.md | 71 ++ vendor/github.com/samber/lo/Dockerfile | 8 + vendor/github.com/samber/lo/LICENSE | 21 + vendor/github.com/samber/lo/Makefile | 51 ++ vendor/github.com/samber/lo/README.md | 981 +++++++++++++++++++++ vendor/github.com/samber/lo/condition.go | 99 +++ vendor/github.com/samber/lo/constraints.go | 6 + vendor/github.com/samber/lo/docker-compose.yml | 9 + vendor/github.com/samber/lo/drop.go | 65 ++ vendor/github.com/samber/lo/find.go | 157 ++++ vendor/github.com/samber/lo/intersect.go | 131 +++ vendor/github.com/samber/lo/map.go | 72 ++ vendor/github.com/samber/lo/pointers.go | 19 + vendor/github.com/samber/lo/retry.go | 19 + vendor/github.com/samber/lo/slice.go | 242 +++++ vendor/github.com/samber/lo/tuples.go | 413 +++++++++ vendor/github.com/samber/lo/types.go | 83 ++ vendor/github.com/samber/lo/util.go | 50 ++ vendor/modules.txt | 8 + 52 files changed, 3014 insertions(+), 275 deletions(-) create mode 100644 vendor/github.com/jesseduffield/generics/LICENSE create mode 100644 vendor/github.com/jesseduffield/generics/hashmap/functions.go create mode 100644 vendor/github.com/jesseduffield/generics/list/comparable_list.go create mode 100644 vendor/github.com/jesseduffield/generics/list/functions.go create mode 100644 vendor/github.com/jesseduffield/generics/list/list.go create mode 100644 vendor/github.com/jesseduffield/generics/set/set.go create mode 100644 vendor/github.com/samber/lo/.gitignore create mode 100644 vendor/github.com/samber/lo/CHANGELOG.md create mode 100644 vendor/github.com/samber/lo/Dockerfile create mode 100644 vendor/github.com/samber/lo/LICENSE create mode 100644 vendor/github.com/samber/lo/Makefile create mode 100644 vendor/github.com/samber/lo/README.md create mode 100644 vendor/github.com/samber/lo/condition.go create mode 100644 vendor/github.com/samber/lo/constraints.go create mode 100644 vendor/github.com/samber/lo/docker-compose.yml create mode 100644 vendor/github.com/samber/lo/drop.go create mode 100644 vendor/github.com/samber/lo/find.go create mode 100644 vendor/github.com/samber/lo/intersect.go create mode 100644 vendor/github.com/samber/lo/map.go create mode 100644 vendor/github.com/samber/lo/pointers.go create mode 100644 vendor/github.com/samber/lo/retry.go create mode 100644 vendor/github.com/samber/lo/slice.go create mode 100644 vendor/github.com/samber/lo/tuples.go create mode 100644 vendor/github.com/samber/lo/types.go create mode 100644 vendor/github.com/samber/lo/util.go diff --git a/go.mod b/go.mod index 3e7598384..afdf377ed 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/gookit/color v1.4.2 github.com/imdario/mergo v0.3.11 github.com/integrii/flaggy v1.4.0 + github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 github.com/jesseduffield/gocui v0.3.1-0.20220227022729-69f0c798eec8 github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e @@ -25,6 +26,7 @@ require ( github.com/mgutz/str v1.2.0 github.com/pmezard/go-difflib v1.0.0 github.com/sahilm/fuzzy v0.1.0 + github.com/samber/lo v1.10.1 github.com/sanity-io/litter v1.5.2 github.com/sirupsen/logrus v1.4.2 github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad diff --git a/go.sum b/go.sum index fc2d2400d..c06b0aef1 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/integrii/flaggy v1.4.0 h1:A1x7SYx4jqu5NSrY14z8Z+0UyX2S5ygfJJrfolWR3zM github.com/integrii/flaggy v1.4.0/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f h1:9USuZttmg5ioHsjFyXboiGSbncpAqcKkq9qb4ga5PD0= +github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk= github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 h1:GOQrmaE8i+KEdB8NzAegKYd4tPn/inM0I1uo0NXFerg= github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o= github.com/jesseduffield/gocui v0.3.1-0.20220227022729-69f0c798eec8 h1:9N08i5kjvOfkzMj6THmIM110wPTQLdVYEOHMHT2DFiI= @@ -129,6 +131,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +github.com/samber/lo v1.10.1 h1:0D3h7i0U3hRAbaCeQ82DLe67n0A7Bbl0/cEoWqFGp+U= +github.com/samber/lo v1.10.1/go.mod h1:2I7tgIv8Q1SG2xEIkRq0F2i2zgxVpnyPOP0d3Gj2r+A= github.com/sanity-io/litter v1.5.2 h1:AnC8s9BMORWH5a4atZ4D6FPVvKGzHcnc5/IVTa87myw= github.com/sanity-io/litter v1.5.2/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= @@ -146,6 +150,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= diff --git a/pkg/commands/git_commands/rebase_test.go b/pkg/commands/git_commands/rebase_test.go index 56df77a86..4e7b5c2c6 100644 --- a/pkg/commands/git_commands/rebase_test.go +++ b/pkg/commands/git_commands/rebase_test.go @@ -8,7 +8,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/stretchr/testify/assert" ) @@ -64,7 +64,7 @@ func TestRebaseSkipEditorCommand(t *testing.T) { "^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$", } { regexStr := regexStr - foundMatch := utils.IncludesStringFunc(envVars, func(envVar string) bool { + foundMatch := lo.ContainsBy(envVars, func(envVar string) bool { return regexp.MustCompile(regexStr).MatchString(envVar) }) if !foundMatch { diff --git a/pkg/commands/loaders/branches.go b/pkg/commands/loaders/branches.go index 1f78908a8..9fa1e80f4 100644 --- a/pkg/commands/loaders/branches.go +++ b/pkg/commands/loaders/branches.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/go-git/v5/config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/common" @@ -181,15 +182,15 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { // TODO: only look at the new reflog commits, and otherwise store the recencies in // int form against the branch to recalculate the time ago func (self *BranchLoader) obtainReflogBranches(reflogCommits []*models.Commit) []*models.Branch { - foundBranchesMap := map[string]bool{} + foundBranches := set.New[string]() re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) reflogBranches := make([]*models.Branch, 0, len(reflogCommits)) for _, commit := range reflogCommits { if match := re.FindStringSubmatch(commit.Name); len(match) == 3 { recency := utils.UnixToTimeAgo(commit.UnixTimestamp) for _, branchName := range match[1:] { - if !foundBranchesMap[branchName] { - foundBranchesMap[branchName] = true + if !foundBranches.Includes(branchName) { + foundBranches.Add(branchName) reflogBranches = append(reflogBranches, &models.Branch{ Recency: recency, Name: branchName, diff --git a/pkg/commands/loaders/files.go b/pkg/commands/loaders/files.go index f5becdb92..8ab727453 100644 --- a/pkg/commands/loaders/files.go +++ b/pkg/commands/loaders/files.go @@ -7,7 +7,7 @@ import ( "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 FileLoaderConfig interface { @@ -57,10 +57,10 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File change := status.Change stagedChange := change[0:1] unstagedChange := change[1:2] - untracked := utils.IncludesString([]string{"??", "A ", "AM"}, change) - hasNoStagedChanges := utils.IncludesString([]string{" ", "U", "?"}, stagedChange) - hasInlineMergeConflicts := utils.IncludesString([]string{"UU", "AA"}, change) - hasMergeConflicts := hasInlineMergeConflicts || utils.IncludesString([]string{"DD", "AU", "UA", "UD", "DU"}, change) + untracked := lo.Contains([]string{"??", "A ", "AM"}, change) + hasNoStagedChanges := lo.Contains([]string{" ", "U", "?"}, stagedChange) + hasInlineMergeConflicts := lo.Contains([]string{"UU", "AA"}, change) + hasMergeConflicts := hasInlineMergeConflicts || lo.Contains([]string{"DD", "AU", "UA", "UD", "DU"}, change) file := &models.File{ Name: status.Name, diff --git a/pkg/commands/patch/hunk.go b/pkg/commands/patch/hunk.go index bbb2d54ff..98d932126 100644 --- a/pkg/commands/patch/hunk.go +++ b/pkg/commands/patch/hunk.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type PatchHunk struct { @@ -54,7 +55,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool) []string { if line == "" { break } - isLineSelected := utils.IncludesInt(lineIndices, lineIdx) + isLineSelected := lo.Contains(lineIndices, lineIdx) firstChar, content := line[:1], line[1:] transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineSelected) diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index cbdf7b2d4..1282356f8 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -4,7 +4,7 @@ import ( "sort" "strings" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -140,7 +140,7 @@ func (p *PatchManager) AddFileLineRange(filename string, firstLineIdx, lastLineI return err } info.mode = PART - info.includedLineIndices = utils.UnionInt(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) + info.includedLineIndices = lo.Union(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) return nil } @@ -151,7 +151,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi return err } info.mode = PART - info.includedLineIndices = utils.DifferenceInt(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) + info.includedLineIndices, _ = lo.Difference(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) if len(info.includedLineIndices) == 0 { p.removeFile(info) } diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index c2be120c9..3810d8a29 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -186,7 +187,7 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic renderedLines := make([]string, len(p.PatchLines)) for index, patchLine := range p.PatchLines { selected := index >= firstLineIndex && index <= lastLineIndex - included := utils.IncludesInt(incLineIndices, index) + included := lo.Contains(incLineIndices, index) renderedLines[index] = patchLine.render(selected, included) } result := strings.Join(renderedLines, "\n") 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 } diff --git a/pkg/gui/filetree/collapsed_paths.go b/pkg/gui/filetree/collapsed_paths.go index 02c0b4303..903999b37 100644 --- a/pkg/gui/filetree/collapsed_paths.go +++ b/pkg/gui/filetree/collapsed_paths.go @@ -1,20 +1,38 @@ package filetree -type CollapsedPaths map[string]bool +import "github.com/jesseduffield/generics/set" -func (cp CollapsedPaths) ExpandToPath(path string) { +type CollapsedPaths struct { + collapsedPaths *set.Set[string] +} + +func NewCollapsedPaths() *CollapsedPaths { + return &CollapsedPaths{ + collapsedPaths: set.New[string](), + } +} + +func (self *CollapsedPaths) ExpandToPath(path string) { // need every directory along the way splitPath := split(path) for i := range splitPath { dir := join(splitPath[0 : i+1]) - cp[dir] = false + self.collapsedPaths.Remove(dir) } } -func (cp CollapsedPaths) IsCollapsed(path string) bool { - return cp[path] +func (self *CollapsedPaths) IsCollapsed(path string) bool { + return self.collapsedPaths.Includes(path) +} + +func (self *CollapsedPaths) Collapse(path string) { + self.collapsedPaths.Add(path) } -func (cp CollapsedPaths) ToggleCollapsed(path string) { - cp[path] = !cp[path] +func (self *CollapsedPaths) ToggleCollapsed(path string) { + if self.collapsedPaths.Includes(path) { + self.collapsedPaths.Remove(path) + } else { + self.collapsedPaths.Add(path) + } } diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go index a8f7d0a95..ac2057da5 100644 --- a/pkg/gui/filetree/commit_file_node.go +++ b/pkg/gui/filetree/commit_file_node.go @@ -100,7 +100,7 @@ func (s *CommitFileNode) EveryFile(test func(file *models.CommitFile) bool) bool }) } -func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNode { +func (n *CommitFileNode) Flatten(collapsedPaths *CollapsedPaths) []*CommitFileNode { results := flatten(n, collapsedPaths) nodes := make([]*CommitFileNode, len(results)) for i, result := range results { @@ -110,7 +110,7 @@ func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNo return nodes } -func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileNode { +func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *CommitFileNode { if node == nil { return nil } @@ -124,11 +124,11 @@ func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string] return result.(*CommitFileNode) } -func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) { return getIndexForPath(node, path, collapsedPaths) } -func (node *CommitFileNode) Size(collapsedPaths map[string]bool) int { +func (node *CommitFileNode) Size(collapsedPaths *CollapsedPaths) int { if node == nil { return 0 } diff --git a/pkg/gui/filetree/commit_file_tree.go b/pkg/gui/filetree/commit_file_tree.go index 055e273f3..e539c9dea 100644 --- a/pkg/gui/filetree/commit_file_tree.go +++ b/pkg/gui/filetree/commit_file_tree.go @@ -19,7 +19,7 @@ type CommitFileTree struct { tree *CommitFileNode showTree bool log *logrus.Entry - collapsedPaths CollapsedPaths + collapsedPaths *CollapsedPaths } var _ ICommitFileTree = &CommitFileTree{} @@ -29,7 +29,7 @@ func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry, getFiles: getFiles, log: log, showTree: showTree, - collapsedPaths: CollapsedPaths{}, + collapsedPaths: NewCollapsedPaths(), } } @@ -88,7 +88,7 @@ func (self *CommitFileTree) Tree() INode { return self.tree } -func (self *CommitFileTree) CollapsedPaths() CollapsedPaths { +func (self *CommitFileTree) CollapsedPaths() *CollapsedPaths { return self.collapsedPaths } diff --git a/pkg/gui/filetree/file_node.go b/pkg/gui/filetree/file_node.go index 841f723fc..e73504321 100644 --- a/pkg/gui/filetree/file_node.go +++ b/pkg/gui/filetree/file_node.go @@ -87,7 +87,7 @@ func (s *FileNode) Any(test func(node *FileNode) bool) bool { }) } -func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { +func (n *FileNode) Flatten(collapsedPaths *CollapsedPaths) []*FileNode { results := flatten(n, collapsedPaths) nodes := make([]*FileNode, len(results)) for i, result := range results { @@ -97,7 +97,7 @@ func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { return nodes } -func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileNode { +func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *FileNode { if node == nil { return nil } @@ -111,11 +111,11 @@ func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) return result.(*FileNode) } -func (node *FileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *FileNode) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) { return getIndexForPath(node, path, collapsedPaths) } -func (node *FileNode) Size(collapsedPaths map[string]bool) int { +func (node *FileNode) Size(collapsedPaths *CollapsedPaths) int { if node == nil { return 0 } diff --git a/pkg/gui/filetree/file_tree.go b/pkg/gui/filetree/file_tree.go index 0d0524470..47d7f32f2 100644 --- a/pkg/gui/filetree/file_tree.go +++ b/pkg/gui/filetree/file_tree.go @@ -27,7 +27,7 @@ type ITree interface { IsCollapsed(path string) bool ToggleCollapsed(path string) Tree() INode - CollapsedPaths() CollapsedPaths + CollapsedPaths() *CollapsedPaths } type IFileTree interface { @@ -48,7 +48,7 @@ type FileTree struct { showTree bool log *logrus.Entry filter FileTreeDisplayFilter - collapsedPaths CollapsedPaths + collapsedPaths *CollapsedPaths } func NewFileTree(getFiles func() []*models.File, log *logrus.Entry, showTree bool) *FileTree { @@ -57,7 +57,7 @@ func NewFileTree(getFiles func() []*models.File, log *logrus.Entry, showTree boo log: log, showTree: showTree, filter: DisplayAll, - collapsedPaths: CollapsedPaths{}, + collapsedPaths: NewCollapsedPaths(), } } @@ -164,7 +164,7 @@ func (self *FileTree) Tree() INode { return self.tree } -func (self *FileTree) CollapsedPaths() CollapsedPaths { +func (self *FileTree) CollapsedPaths() *CollapsedPaths { return self.collapsedPaths } diff --git a/pkg/gui/filetree/inode.go b/pkg/gui/filetree/inode.go index 7d9035fe3..7c8b9fb75 100644 --- a/pkg/gui/filetree/inode.go +++ b/pkg/gui/filetree/inode.go @@ -90,11 +90,11 @@ func every(node INode, test func(INode) bool) bool { return true } -func flatten(node INode, collapsedPaths map[string]bool) []INode { +func flatten(node INode, collapsedPaths *CollapsedPaths) []INode { result := []INode{} result = append(result, node) - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { result = append(result, flatten(child, collapsedPaths)...) } @@ -103,20 +103,20 @@ func flatten(node INode, collapsedPaths map[string]bool) []INode { return result } -func getNodeAtIndex(node INode, index int, collapsedPaths map[string]bool) INode { +func getNodeAtIndex(node INode, index int, collapsedPaths *CollapsedPaths) INode { foundNode, _ := getNodeAtIndexAux(node, index, collapsedPaths) return foundNode } -func getNodeAtIndexAux(node INode, index int, collapsedPaths map[string]bool) (INode, int) { +func getNodeAtIndexAux(node INode, index int, collapsedPaths *CollapsedPaths) (INode, int) { offset := 1 if index == 0 { return node, offset } - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { foundNode, offsetChange := getNodeAtIndexAux(child, index-offset, collapsedPaths) offset += offsetChange @@ -129,14 +129,14 @@ func getNodeAtIndexAux(node INode, index int, collapsedPaths map[string]bool) (I return nil, offset } -func getIndexForPath(node INode, path string, collapsedPaths map[string]bool) (int, bool) { +func getIndexForPath(node INode, path string, collapsedPaths *CollapsedPaths) (int, bool) { offset := 0 if node.GetPath() == path { return offset, true } - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { offsetChange, found := getIndexForPath(child, path, collapsedPaths) offset += offsetChange + 1 @@ -149,10 +149,10 @@ func getIndexForPath(node INode, path string, collapsedPaths map[string]bool) (i return offset, false } -func size(node INode, collapsedPaths map[string]bool) int { +func size(node INode, collapsedPaths *CollapsedPaths) int { output := 1 - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { output += size(child, collapsedPaths) } diff --git a/pkg/gui/list_context_config.go b/pkg/gui/list_context_config.go index 010a2f0b1..dcea5a936 100644 --- a/pkg/gui/list_context_config.go +++ b/pkg/gui/list_context_config.go @@ -123,7 +123,7 @@ func (gui *Gui) branchCommitsListContext() *context.LocalCommitsContext { return presentation.GetCommitListDisplayStrings( gui.State.Model.Commits, gui.State.ScreenMode != SCREEN_NORMAL, - gui.helpers.CherryPick.CherryPickedCommitShaMap(), + gui.helpers.CherryPick.CherryPickedCommitShaSet(), gui.State.Modes.Diffing.Ref, gui.c.UserConfig.Git.ParseEmoji, selectedCommitSha, @@ -155,7 +155,7 @@ func (gui *Gui) subCommitsListContext() *context.SubCommitsContext { return presentation.GetCommitListDisplayStrings( gui.State.Model.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, - gui.helpers.CherryPick.CherryPickedCommitShaMap(), + gui.helpers.CherryPick.CherryPickedCommitShaSet(), gui.State.Modes.Diffing.Ref, gui.c.UserConfig.Git.ParseEmoji, selectedCommitSha, @@ -199,7 +199,7 @@ func (gui *Gui) reflogCommitsListContext() *context.ReflogCommitsContext { return presentation.GetReflogCommitListDisplayStrings( gui.State.Model.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, - gui.helpers.CherryPick.CherryPickedCommitShaMap(), + gui.helpers.CherryPick.CherryPickedCommitShaSet(), gui.State.Modes.Diffing.Ref, gui.c.UserConfig.Git.ParseEmoji, ) diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go index 0073bb041..54f08ed50 100644 --- a/pkg/gui/options_menu_panel.go +++ b/pkg/gui/options_menu_panel.go @@ -7,7 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) func (gui *Gui) getBindings(context types.Context) []*types.Binding { @@ -26,7 +26,7 @@ func (gui *Gui) getBindings(context types.Context) []*types.Binding { bindingsGlobal = append(bindingsGlobal, binding) } else if binding.Tag == "navigation" { bindingsNavigation = append(bindingsNavigation, binding) - } else if utils.IncludesString(binding.Contexts, string(context.GetKey())) { + } else if lo.Contains(binding.Contexts, string(context.GetKey())) { bindingsPanel = append(bindingsPanel, binding) } } @@ -45,17 +45,9 @@ func (gui *Gui) getBindings(context types.Context) []*types.Binding { // We shouldn't really need to do this. We should define alternative keys for the same // handler in the keybinding struct. func uniqueBindings(bindings []*types.Binding) []*types.Binding { - keys := make(map[string]bool) - result := make([]*types.Binding, 0) - - for _, binding := range bindings { - if _, ok := keys[binding.Description]; !ok { - keys[binding.Description] = true - result = append(result, binding) - } - } - - return result + return lo.UniqBy(bindings, func(binding *types.Binding) string { + return binding.Description + }) } func (gui *Gui) displayDescription(binding *types.Binding) string { diff --git a/pkg/gui/patch_building_panel.go b/pkg/gui/patch_building_panel.go index e734433c4..cae6167a4 100644 --- a/pkg/gui/patch_building_panel.go +++ b/pkg/gui/patch_building_panel.go @@ -1,8 +1,6 @@ package gui -import ( - "github.com/jesseduffield/lazygit/pkg/utils" -) +import "github.com/samber/lo" func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error { if !gui.git.Patch.PatchManager.Active() { @@ -68,7 +66,7 @@ func (gui *Gui) handleToggleSelectionForPatch() error { if err != nil { return err } - currentLineIsStaged := utils.IncludesInt(includedLineIndices, state.GetSelectedLineIdx()) + currentLineIsStaged := lo.Contains(includedLineIndices, state.GetSelectedLineIdx()) if currentLineIsStaged { toggleFunc = gui.git.Patch.PatchManager.RemoveFileLineRange } diff --git a/pkg/gui/presentation/commits.go b/pkg/gui/presentation/commits.go index 2d5262e89..7ec4e78af 100644 --- a/pkg/gui/presentation/commits.go +++ b/pkg/gui/presentation/commits.go @@ -4,6 +4,7 @@ import ( "strings" "sync" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/presentation/authors" @@ -32,7 +33,7 @@ type bisectBounds struct { func GetCommitListDisplayStrings( commits []*models.Commit, fullDescription bool, - cherryPickedCommitShaMap map[string]bool, + cherryPickedCommitShaSet *set.Set[string], diffName string, parseEmoji bool, selectedCommitSha string, @@ -94,7 +95,7 @@ func GetCommitListDisplayStrings( bisectStatus = getBisectStatus(unfilteredIdx, commit.Sha, bisectInfo, bisectBounds) lines = append(lines, displayCommit( commit, - cherryPickedCommitShaMap, + cherryPickedCommitShaSet, diffName, parseEmoji, getGraphLine(unfilteredIdx), @@ -237,7 +238,7 @@ func getBisectStatusText(bisectStatus BisectStatus, bisectInfo *git_commands.Bis func displayCommit( commit *models.Commit, - cherryPickedCommitShaMap map[string]bool, + cherryPickedCommitShaSet *set.Set[string], diffName string, parseEmoji bool, graphLine string, @@ -245,7 +246,7 @@ func displayCommit( bisectStatus BisectStatus, bisectInfo *git_commands.BisectInfo, ) []string { - shaColor := getShaColor(commit, diffName, cherryPickedCommitShaMap, bisectStatus, bisectInfo) + shaColor := getShaColor(commit, diffName, cherryPickedCommitShaSet, bisectStatus, bisectInfo) bisectString := getBisectStatusText(bisectStatus, bisectInfo) actionString := "" @@ -313,7 +314,7 @@ func getBisectStatusColor(status BisectStatus) style.TextStyle { func getShaColor( commit *models.Commit, diffName string, - cherryPickedCommitShaMap map[string]bool, + cherryPickedCommitShaSet *set.Set[string], bisectStatus BisectStatus, bisectInfo *git_commands.BisectInfo, ) style.TextStyle { @@ -338,7 +339,7 @@ func getShaColor( if diffed { shaColor = theme.DiffTerminalColor - } else if cherryPickedCommitShaMap[commit.Sha] { + } else if cherryPickedCommitShaSet.Includes(commit.Sha) { shaColor = theme.CherryPickedCommitTextStyle } diff --git a/pkg/gui/presentation/commits_test.go b/pkg/gui/presentation/commits_test.go index b7fd23468..846d50d19 100644 --- a/pkg/gui/presentation/commits_test.go +++ b/pkg/gui/presentation/commits_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/gookit/color" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/utils" @@ -25,7 +26,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) { testName string commits []*models.Commit fullDescription bool - cherryPickedCommitShaMap map[string]bool + cherryPickedCommitShaSet *set.Set[string] diffName string parseEmoji bool selectedCommitSha string @@ -209,7 +210,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) { result := GetCommitListDisplayStrings( s.commits, s.fullDescription, - s.cherryPickedCommitShaMap, + s.cherryPickedCommitShaSet, s.diffName, s.parseEmoji, s.selectedCommitSha, diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index be57a3510..13b2a64b0 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -66,7 +66,7 @@ func RenderCommitFileTree( func renderAux( s filetree.INode, - collapsedPaths filetree.CollapsedPaths, + collapsedPaths *filetree.CollapsedPaths, prefix string, depth int, renderLine func(filetree.INode, int) string, diff --git a/pkg/gui/presentation/reflog_commits.go b/pkg/gui/presentation/reflog_commits.go index 5437af8b5..72bb80ef6 100644 --- a/pkg/gui/presentation/reflog_commits.go +++ b/pkg/gui/presentation/reflog_commits.go @@ -1,6 +1,7 @@ package presentation import ( + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" @@ -8,7 +9,7 @@ import ( "github.com/kyokomi/emoji/v2" ) -func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaMap map[string]bool, diffName string, parseEmoji bool) [][]string { +func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaSet *set.Set[string], diffName string, parseEmoji bool) [][]string { lines := make([][]string, len(commits)) var displayFunc func(*models.Commit, bool, bool, bool) []string @@ -20,7 +21,7 @@ func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription for i := range commits { diffed := commits[i].Sha == diffName - cherryPicked := cherryPickedCommitShaMap[commits[i].Sha] + cherryPicked := cherryPickedCommitShaSet.Includes(commits[i].Sha) lines[i] = displayFunc(commits[i], cherryPicked, diffed, parseEmoji) } diff --git a/pkg/gui/refresh.go b/pkg/gui/refresh.go index 2a47efc7a..5252d7ec9 100644 --- a/pkg/gui/refresh.go +++ b/pkg/gui/refresh.go @@ -5,6 +5,7 @@ import ( "strings" "sync" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" @@ -52,14 +53,6 @@ func getModeName(mode types.RefreshMode) string { } } -func arrToMap(arr []types.RefreshableView) map[types.RefreshableView]bool { - output := map[types.RefreshableView]bool{} - for _, el := range arr { - output[el] = true - } - return output -} - func (gui *Gui) Refresh(options types.RefreshOptions) error { if options.Scope == nil { gui.c.Log.Infof( @@ -77,9 +70,9 @@ func (gui *Gui) Refresh(options types.RefreshOptions) error { wg := sync.WaitGroup{} f := func() { - var scopeMap map[types.RefreshableView]bool + var scopeSet *set.Set[types.RefreshableView] if len(options.Scope) == 0 { - scopeMap = arrToMap([]types.RefreshableView{ + scopeSet = set.NewFromSlice([]types.RefreshableView{ types.COMMITS, types.BRANCHES, types.FILES, @@ -91,7 +84,7 @@ func (gui *Gui) Refresh(options types.RefreshOptions) error { types.BISECT_INFO, }) } else { - scopeMap = arrToMap(options.Scope) + scopeSet = set.NewFromSlice(options.Scope) } refresh := func(f func()) { @@ -106,27 +99,27 @@ func (gui *Gui) Refresh(options types.RefreshOptions) error { }() } - if scopeMap[types.COMMITS] || scopeMap[types.BRANCHES] || scopeMap[types.REFLOG] || scopeMap[types.BISECT_INFO] { + if scopeSet.Includes(types.COMMITS) || scopeSet.Includes(types.BRANCHES) || scopeSet.Includes(types.REFLOG) || scopeSet.Includes(types.BISECT_INFO) { refresh(gui.refreshCommits) - } else if scopeMap[types.REBASE_COMMITS] { + } else if scopeSet.Includes(types.REBASE_COMMITS) { // the above block handles rebase commits so we only need to call this one // if we've asked specifically for rebase commits and not those other things refresh(func() { _ = gui.refreshRebaseCommits() }) } - if scopeMap[types.FILES] || scopeMap[types.SUBMODULES] { + if scopeSet.Includes(types.FILES) || scopeSet.Includes(types.SUBMODULES) { refresh(func() { _ = gui.refreshFilesAndSubmodules() }) } - if scopeMap[types.STASH] { + if scopeSet.Includes(types.STASH) { refresh(func() { _ = gui.refreshStashEntries() }) } - if scopeMap[types.TAGS] { + if scopeSet.Includes(types.TAGS) { refresh(func() { _ = gui.refreshTags() }) } - if scopeMap[types.REMOTES] { + if scopeSet.Includes(types.REMOTES) { refresh(func() { _ = gui.refreshRemotes() }) } diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go index 123fc7df9..6971c9367 100644 --- a/pkg/utils/slice.go +++ b/pkg/utils/slice.go @@ -1,29 +1,5 @@ package utils -// IncludesString if the list contains the string -func IncludesString(list []string, a string) bool { - return IncludesStringFunc(list, func(b string) bool { return b == a }) -} - -func IncludesStringFunc(list []string, fn func(string) bool) bool { - for _, b := range list { - if fn(b) { - return true - } - } - return false -} - -// IncludesInt if the list contains the Int -func IncludesInt(list []int, a int) bool { - for _, b := range list { - if b == a { - return true - } - } - return false -} - // NextIndex returns the index of the element that comes after the given number func NextIndex(numbers []int, currentNumber int) int { for index, number := range numbers { @@ -45,44 +21,6 @@ func PrevIndex(numbers []int, currentNumber int) int { return 0 } -// UnionInt returns the union of two int arrays -func UnionInt(a, b []int) []int { - m := make(map[int]bool) - - for _, item := range a { - m[item] = true - } - - for _, item := range b { - if _, ok := m[item]; !ok { - // this does not mutate the original a slice - // though it does mutate the backing array I believe - // but that doesn't matter because if you later want to append to the - // original a it must see that the backing array has been changed - // and create a new one - a = append(a, item) - } - } - return a -} - -// DifferenceInt returns the difference of two int arrays -func DifferenceInt(a, b []int) []int { - result := []int{} - m := make(map[int]bool) - - for _, item := range b { - m[item] = true - } - - for _, item := range a { - if _, ok := m[item]; !ok { - result = append(result, item) - } - } - return result -} - // NextIntInCycle returns the next int in a slice, returning to the first index if we've reached the end func NextIntInCycle(sl []int, current int) int { for i, val := range sl { @@ -121,19 +59,6 @@ func StringArraysOverlap(strArrA []string, strArrB []string) bool { return false } -func Uniq(values []string) []string { - added := make(map[string]bool) - result := make([]string, 0, len(values)) - for _, value := range values { - if added[value] { - continue - } - added[value] = true - result = append(result, value) - } - return result -} - func Limit(values []string, limit int) []string { if len(values) > limit { return values[:limit] @@ -141,14 +66,6 @@ func Limit(values []string, limit int) []string { return values } -func Reverse(values []string) []string { - result := make([]string, len(values)) - for i, val := range values { - result[len(values)-i-1] = val - } - return result -} - func LimitStr(value string, limit int) string { n := 0 for i := range value { diff --git a/pkg/utils/slice_test.go b/pkg/utils/slice_test.go index fc80a46d7..e66edcd61 100644 --- a/pkg/utils/slice_test.go +++ b/pkg/utils/slice_test.go @@ -6,42 +6,6 @@ import ( "github.com/stretchr/testify/assert" ) -// TestIncludesString is a function. -func TestIncludesString(t *testing.T) { - type scenario struct { - list []string - element string - expected bool - } - - scenarios := []scenario{ - { - []string{"a", "b"}, - "a", - true, - }, - { - []string{"a", "b"}, - "c", - false, - }, - { - []string{"a", "b"}, - "", - false, - }, - { - []string{""}, - "", - true, - }, - } - - for _, s := range scenarios { - assert.EqualValues(t, s.expected, IncludesString(s.list, s.element)) - } -} - func TestNextIndex(t *testing.T) { type scenario struct { testName string @@ -169,26 +133,6 @@ func TestEscapeSpecialChars(t *testing.T) { } } -func TestUniq(t *testing.T) { - for _, test := range []struct { - values []string - want []string - }{ - { - values: []string{"a", "b", "c"}, - want: []string{"a", "b", "c"}, - }, - { - values: []string{"a", "b", "a", "b", "c"}, - want: []string{"a", "b", "c"}, - }, - } { - if got := Uniq(test.values); !assert.EqualValues(t, got, test.want) { - t.Errorf("Uniq(%v) = %v; want %v", test.values, got, test.want) - } - } -} - func TestLimit(t *testing.T) { for _, test := range []struct { values []string @@ -232,26 +176,6 @@ func TestLimit(t *testing.T) { } } -func TestReverse(t *testing.T) { - for _, test := range []struct { - values []string - want []string - }{ - { - values: []string{"a", "b", "c"}, - want: []string{"c", "b", "a"}, - }, - { - values: []string{}, - want: []string{}, - }, - } { - if got := Reverse(test.values); !assert.EqualValues(t, got, test.want) { - t.Errorf("Reverse(%v) = %v; want %v", test.values, got, test.want) - } - } -} - func TestLimitStr(t *testing.T) { for _, test := range []struct { values string diff --git a/vendor/github.com/jesseduffield/generics/LICENSE b/vendor/github.com/jesseduffield/generics/LICENSE new file mode 100644 index 000000000..2a7175dcc --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Jesse Duffield + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/jesseduffield/generics/hashmap/functions.go b/vendor/github.com/jesseduffield/generics/hashmap/functions.go new file mode 100644 index 000000000..526222b1f --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/hashmap/functions.go @@ -0,0 +1,35 @@ +package hashmap + +func Keys[Key comparable, Value any](m map[Key]Value) []Key { + keys := make([]Key, 0, len(m)) + for key := range m { + keys = append(keys, key) + } + return keys +} + +func Values[Key comparable, Value any](m map[Key]Value) []Value { + values := make([]Value, 0, len(m)) + for _, value := range m { + values = append(values, value) + } + return values +} + +func TransformValues[Key comparable, Value any, NewValue any]( + m map[Key]Value, fn func(Value) NewValue, +) map[Key]NewValue { + output := make(map[Key]NewValue) + for key, value := range m { + output[key] = fn(value) + } + return output +} + +func TransformKeys[Key comparable, Value any, NewKey comparable](m map[Key]Value, fn func(Key) NewKey) map[NewKey]Value { + output := make(map[NewKey]Value) + for key, value := range m { + output[fn(key)] = value + } + return output +} diff --git a/vendor/github.com/jesseduffield/generics/list/comparable_list.go b/vendor/github.com/jesseduffield/generics/list/comparable_list.go new file mode 100644 index 000000000..21d56a80b --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/comparable_list.go @@ -0,0 +1,49 @@ +package list + +import ( + "golang.org/x/exp/slices" +) + +type ComparableList[T comparable] struct { + *List[T] +} + +func NewComparable[T comparable]() *ComparableList[T] { + return &ComparableList[T]{List: New[T]()} +} + +func NewComparableFromSlice[T comparable](slice []T) *ComparableList[T] { + return &ComparableList[T]{List: NewFromSlice(slice)} +} + +func (l *ComparableList[T]) Equal(other *ComparableList[T]) bool { + return l.EqualSlice(other.ToSlice()) +} + +func (l *ComparableList[T]) EqualSlice(other []T) bool { + return slices.Equal(l.ToSlice(), other) +} + +func (l *ComparableList[T]) Compact() { + l.slice = slices.Compact(l.slice) +} + +func (l *ComparableList[T]) Index(needle T) int { + return slices.Index(l.slice, needle) +} + +func (l *ComparableList[T]) Contains(needle T) bool { + return slices.Contains(l.slice, needle) +} + +func (l *ComparableList[T]) SortFuncInPlace(test func(a T, b T) bool) { + slices.SortFunc(l.slice, test) +} + +func (l *ComparableList[T]) SortFunc(test func(a T, b T) bool) *ComparableList[T] { + newSlice := slices.Clone(l.slice) + + slices.SortFunc(newSlice, test) + + return NewComparableFromSlice(newSlice) +} diff --git a/vendor/github.com/jesseduffield/generics/list/functions.go b/vendor/github.com/jesseduffield/generics/list/functions.go new file mode 100644 index 000000000..21578b82f --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/functions.go @@ -0,0 +1,72 @@ +package list + +func Some[T any](slice []T, test func(T) bool) bool { + for _, value := range slice { + if test(value) { + return true + } + } + + return false +} + +func Every[T any](slice []T, test func(T) bool) bool { + for _, value := range slice { + if !test(value) { + return false + } + } + + return true +} + +func Map[T any, V any](slice []T, f func(T) V) []V { + result := make([]V, len(slice)) + for i, value := range slice { + result[i] = f(value) + } + + return result +} + +func MapInPlace[T any](slice []T, f func(T) T) { + for i, value := range slice { + slice[i] = f(value) + } +} + +func Filter[T any](slice []T, test func(T) bool) []T { + result := make([]T, 0) + for _, element := range slice { + if test(element) { + result = append(result, element) + } + } + return result +} + +func FilterInPlace[T any](slice []T, test func(T) bool) []T { + newLength := 0 + for _, element := range slice { + if test(element) { + slice[newLength] = element + newLength++ + } + } + + return slice[:newLength] +} + +func Reverse[T any](slice []T) []T { + result := make([]T, len(slice)) + for i := range slice { + result[i] = slice[len(slice)-1-i] + } + return result +} + +func ReverseInPlace[T any](slice []T) { + for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 { + slice[i], slice[j] = slice[j], slice[i] + } +} diff --git a/vendor/github.com/jesseduffield/generics/list/list.go b/vendor/github.com/jesseduffield/generics/list/list.go new file mode 100644 index 000000000..2b0f43010 --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/list.go @@ -0,0 +1,117 @@ +package list + +import ( + "golang.org/x/exp/slices" +) + +type List[T any] struct { + slice []T +} + +func New[T any]() *List[T] { + return &List[T]{} +} + +func NewFromSlice[T any](slice []T) *List[T] { + return &List[T]{slice: slice} +} + +func (l *List[T]) ToSlice() []T { + return l.slice +} + +// Mutative methods + +func (l *List[T]) Push(v T) { + l.slice = append(l.slice, v) +} + +func (l *List[T]) Pop() { + l.slice = l.slice[0 : len(l.slice)-1] +} + +func (l *List[T]) Insert(index int, values ...T) { + l.slice = slices.Insert(l.slice, index, values...) +} + +func (l *List[T]) Append(values ...T) { + l.slice = append(l.slice, values...) +} + +func (l *List[T]) Prepend(values ...T) { + l.slice = append(values, l.slice...) +} + +func (l *List[T]) Remove(index int) { + l.Delete(index, index+1) +} + +func (l *List[T]) Delete(from int, to int) { + l.slice = slices.Delete(l.slice, from, to) +} + +func (l *List[T]) FilterInPlace(test func(value T) bool) { + l.slice = FilterInPlace(l.slice, test) +} + +func (l *List[T]) MapInPlace(f func(value T) T) { + MapInPlace(l.slice, f) +} + +func (l *List[T]) ReverseInPlace() { + ReverseInPlace(l.slice) +} + +// Non-mutative methods + +// Similar to Append but we leave the original slice untouched and return a new list +func (l *List[T]) Concat(values ...T) *List[T] { + newSlice := make([]T, 0, len(l.slice)+len(values)) + newSlice = append(newSlice, l.slice...) + newSlice = append(newSlice, values...) + return &List[T]{slice: newSlice} +} + +func (l *List[T]) Filter(test func(value T) bool) *List[T] { + return NewFromSlice(Filter(l.slice, test)) +} + +// Unfortunately this does not support mapping from one type to another +// because Go does not yet (and may never) support methods defining their own +// type parameters. For that functionality you'll need to use the standalone +// Map function instead +func (l *List[T]) Map(f func(value T) T) *List[T] { + return NewFromSlice(Map(l.slice, f)) +} + +func (l *List[T]) Clone() *List[T] { + return NewFromSlice(slices.Clone(l.slice)) +} + +func (l *List[T]) Some(test func(value T) bool) bool { + return Some(l.slice, test) +} + +func (l *List[T]) Every(test func(value T) bool) bool { + return Every(l.slice, test) +} + +func (l *List[T]) IndexFunc(f func(T) bool) int { + return slices.IndexFunc(l.slice, f) +} + +func (l *List[T]) ContainsFunc(f func(T) bool) bool { + return l.IndexFunc(f) != -1 +} + +func (l *List[T]) Reverse() *List[T] { + return NewFromSlice(Reverse(l.slice)) +} + +func (l *List[T]) IsEmpty() bool { + return len(l.slice) == 0 +} + +func (l *List[T]) Len() int { + return len(l.slice) +} diff --git a/vendor/github.com/jesseduffield/generics/set/set.go b/vendor/github.com/jesseduffield/generics/set/set.go new file mode 100644 index 000000000..3e9b9d9bf --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/set/set.go @@ -0,0 +1,49 @@ +package set + +import "github.com/jesseduffield/generics/hashmap" + +type Set[T comparable] struct { + hashMap map[T]bool +} + +func New[T comparable]() *Set[T] { + return &Set[T]{hashMap: make(map[T]bool)} +} + +func NewFromSlice[T comparable](slice []T) *Set[T] { + hashMap := make(map[T]bool) + for _, value := range slice { + hashMap[value] = true + } + + return &Set[T]{hashMap: hashMap} +} + +func (s *Set[T]) Add(value T) { + s.hashMap[value] = true +} + +func (s *Set[T]) AddSlice(slice []T) { + for _, value := range slice { + s.Add(value) + } +} + +func (s *Set[T]) Remove(value T) { + delete(s.hashMap, value) +} + +func (s *Set[T]) RemoveSlice(slice []T) { + for _, value := range slice { + s.Remove(value) + } +} + +func (s *Set[T]) Includes(value T) bool { + return s.hashMap[value] +} + +// output slice is not necessarily in the same order that items were added +func (s *Set[T]) ToSlice() []T { + return hashmap.Keys(s.hashMap) +} diff --git a/vendor/github.com/samber/lo/.gitignore b/vendor/github.com/samber/lo/.gitignore new file mode 100644 index 000000000..3aa3a0ad4 --- /dev/null +++ b/vendor/github.com/samber/lo/.gitignore @@ -0,0 +1,36 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/go +# Edit at https://www.toptal.com/developers/gitignore?templates=go + +### Go ### +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +### Go Patch ### +/vendor/ +/Godeps/ + +# End of https://www.toptal.com/developers/gitignore/api/go + +cover.out +cover.html +.vscode diff --git a/vendor/github.com/samber/lo/CHANGELOG.md b/vendor/github.com/samber/lo/CHANGELOG.md new file mode 100644 index 000000000..aabeed120 --- /dev/null +++ b/vendor/github.com/samber/lo/CHANGELOG.md @@ -0,0 +1,71 @@ +# Changelog + +## 1.3.0 (2022-03-03) + +Last and Nth return errors + +## 1.2.0 (2022-03-03) + +Adding `lop.Map` and `lop.ForEach`. + +## 1.1.0 (2022-03-03) + +Adding `i int` param to `lo.Map()`, `lo.Filter()`, `lo.ForEach()` and `lo.Reduce()` predicates. + +## 1.0.0 (2022-03-02) + +*Initial release* + +Supported helpers for slices: + +- Filter +- Map +- Reduce +- ForEach +- Uniq +- UniqBy +- GroupBy +- Chunk +- Flatten +- Shuffle +- Reverse +- Fill +- ToMap + +Supported helpers for maps: + +- Keys +- Values +- Entries +- FromEntries +- Assign (maps merge) + +Supported intersection helpers: + +- Contains +- Every +- Some +- Intersect +- Difference + +Supported search helpers: + +- IndexOf +- LastIndexOf +- Find +- Min +- Max +- Last +- Nth + +Other functional programming helpers: + +- Ternary (1 line if/else statement) +- If / ElseIf / Else +- Switch / Case / Default +- ToPtr +- ToSlicePtr + +Constraints: + +- Clonable diff --git a/vendor/github.com/samber/lo/Dockerfile b/vendor/github.com/samber/lo/Dockerfile new file mode 100644 index 000000000..9f9f87192 --- /dev/null +++ b/vendor/github.com/samber/lo/Dockerfile @@ -0,0 +1,8 @@ + +FROM golang:1.18rc1-bullseye + +WORKDIR /go/src/github.com/samber/lo + +COPY Makefile go.* /go/src/github.com/samber/lo/ + +RUN make tools diff --git a/vendor/github.com/samber/lo/LICENSE b/vendor/github.com/samber/lo/LICENSE new file mode 100644 index 000000000..c3dc72d9a --- /dev/null +++ b/vendor/github.com/samber/lo/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Samuel Berthe + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/samber/lo/Makefile b/vendor/github.com/samber/lo/Makefile new file mode 100644 index 000000000..11b09cd25 --- /dev/null +++ b/vendor/github.com/samber/lo/Makefile @@ -0,0 +1,51 @@ + +BIN=go +# BIN=go1.18beta1 + +go1.18beta1: + go install golang.org/dl/go1.18beta1@latest + go1.18beta1 download + +build: + ${BIN} build -v ./... + +test: + go test -race -v ./... +watch-test: + reflex -R assets.go -t 50ms -s -- sh -c 'gotest -race -v ./...' + +bench: + go test -benchmem -count 3 -bench ./... +watch-bench: + reflex -R assets.go -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...' + +coverage: + ${BIN} test -v -coverprofile cover.out . + ${BIN} tool cover -html=cover.out -o cover.html + +# tools +tools: + ${BIN} install github.com/cespare/reflex@latest + ${BIN} install github.com/rakyll/gotest@latest + ${BIN} install github.com/psampaz/go-mod-outdated@latest + ${BIN} install github.com/jondot/goweight@latest + ${BIN} install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + ${BIN} get -t -u golang.org/x/tools/cmd/cover + ${BIN} get -t -u github.com/sonatype-nexus-community/nancy@latest + go mod tidy + +lint: + golangci-lint run --timeout 60s --max-same-issues 50 ./... +lint-fix: + golangci-lint run --timeout 60s --max-same-issues 50 --fix ./... + +audit: tools + ${BIN} mod tidy + ${BIN} list -json -m all | nancy sleuth + +outdated: tools + ${BIN} mod tidy + ${BIN} list -u -m -json all | go-mod-outdated -update -direct + +weight: tools + goweight diff --git a/vendor/github.com/samber/lo/README.md b/vendor/github.com/samber/lo/README.md new file mode 100644 index 000000000..48dfdc004 --- /dev/null +++ b/vendor/github.com/samber/lo/README.md @@ -0,0 +1,981 @@ +# lo + +![Build Status](https://github.com/samber/lo/actions/workflows/go.yml/badge.svg) +[![GoDoc](https://godoc.org/github.com/samber/lo?status.svg)](https://pkg.go.dev/github.com/samber/lo) +[![Go report](https://goreportcard.com/badge/github.com/samber/lo)](https://goreportcard.com/report/github.com/samber/lo) + +✨ **`lo` is a Lodash-style Go library based on Go 1.18+ Generics.** + +This project started as an experiment with the new generics implementation. It may look like [Lodash](https://github.com/lodash/lodash) in some aspects. I used to code with the fantastic ["go-funk"](https://github.com/thoas/go-funk) package, but "go-funk" uses reflection and therefore is not typesafe. + +As expected, benchmarks demonstrate that generics will be much faster than implementations based on the "reflect" package. Benchmarks also show similar performance gains compared to pure `for` loops. [See below](#-benchmark). + +In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package names `slices` and `maps`). I feel this library is legitimate and offers many more valuable abstractions. + +### Why this name? + +I wanted a **short name**, similar to "Lodash" and no Go package currently uses this name. + +## 🚀 Install + +```sh +go get github.com/samber/lo +``` + +## 💡 Usage + +You can import `lo` using: + +```go +import ( + "github.com/samber/lo" + lop "github.com/samber/lo/parallel" +) +``` + +Then use one of the helpers below: + +```go +names := lo.Uniq[string]([]string{"Samuel", "Marc", "Samuel"}) +// []string{"Samuel", "Marc"} +``` + +Most of the time, the compiler will be able to infer the type so that you can call: `lo.Uniq([]string{...})`. + +## 🤠 Spec + +GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/samber/lo) + +Supported helpers for slices: + +- Filter +- Map +- FlatMap +- Reduce +- ForEach +- Times +- Uniq +- UniqBy +- GroupBy +- Chunk +- PartitionBy +- Flatten +- Shuffle +- Reverse +- Fill +- Repeat +- KeyBy +- Drop +- DropRight +- DropWhile +- DropRightWhile + +Supported helpers for maps: + +- Keys +- Values +- Entries +- FromEntries +- Assign (merge of maps) +- MapValues + +Supported helpers for tuples: + +- Zip2 -> Zip9 +- Unzip2 -> Unzip9 + +Supported intersection helpers: + +- Contains +- ContainsBy +- Every +- Some +- Intersect +- Difference +- Union + +Supported search helpers: + +- IndexOf +- LastIndexOf +- Find +- Min +- Max +- Last +- Nth +- Sample +- Samples + +Other functional programming helpers: + +- Ternary (1 line if/else statement) +- If / ElseIf / Else +- Switch / Case / Default +- ToPtr +- ToSlicePtr +- Attempt +- Range / RangeFrom / RangeWithSteps + +Constraints: + +- Clonable + +### Map + +Manipulates a slice of one type and transforms it into a slice of another type: + +```go +import "github.com/samber/lo" + +lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string { + return strconv.FormatInt(x, 10) +}) +// []string{"1", "2", "3", "4"} +``` + +Parallel processing: like `lo.Map()`, but the mapper function is called in a goroutine. Results are returned in the same order. + +```go +import lop "github.com/samber/lo/parallel" + +lop.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string { + return strconv.FormatInt(x, 10) +}) +// []string{"1", "2", "3", "4"} +``` + +### FlatMap + +Manipulates a slice and transforms and flattens it to a slice of another type. + +```go +lo.FlatMap[int, string]([]int{0, 1, 2}, func(x int, _ int) []string { + return []string{ + strconv.FormatInt(x, 10), + strconv.FormatInt(x, 10), + } +}) +// []string{"0", "0", "1", "1", "2", "2"} +``` + +### Filter + +Iterates over a collection and returns an array of all the elements the predicate function returns `true` for. + +```go +even := lo.Filter[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool { + return x%2 == 0 +}) +// []int{2, 4} +``` + +### Contains + +Returns true if an element is present in a collection. + +```go +present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5) +// true +``` + +### Contains + +Returns true if the predicate function returns `true`. + +```go +present := lo.ContainsBy[int]([]int{0, 1, 2, 3, 4, 5}, func(x int) bool { + return x == 3 +}) +// true +``` + +### Reduce + +Reduces a collection to a single value. The value is calculated by accumulating the result of running each element in the collection through an accumulator function. Each successive invocation is supplied with the return value returned by the previous call. + +```go +sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int { + return agg + item +}, 0) +// 10 +``` + +### ForEach + +Iterates over elements of a collection and invokes the function over each element. + +```go +import "github.com/samber/lo" + +lo.ForEach[string]