summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-03-19 19:12:58 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-03-24 20:14:41 +1100
commit1b75ed37403ac2997cb6a5ede92d87f1a1eb96b1 (patch)
tree6a1b70201901725cd689c90f32a75fe34e77a603 /pkg
parentbf4f06ab4e6ceefe388e0efefcc553526f3d96c2 (diff)
many more generics
Diffstat (limited to 'pkg')
-rw-r--r--pkg/gui/context.go14
-rw-r--r--pkg/gui/context/menu_context.go24
-rw-r--r--pkg/gui/controllers/helpers/merge_and_rebase_helper.go10
-rw-r--r--pkg/gui/controllers/helpers/refs_helper.go9
-rw-r--r--pkg/gui/controllers/helpers/suggestions_helper.go65
-rw-r--r--pkg/gui/files_panel_test.go11
-rw-r--r--pkg/gui/filetree/commit_file_node.go33
-rw-r--r--pkg/gui/filetree/file_node.go34
-rw-r--r--pkg/gui/filetree/file_tree.go9
-rw-r--r--pkg/gui/list_context_config.go19
-rw-r--r--pkg/gui/options_menu_panel.go10
-rw-r--r--pkg/gui/presentation/branches.go13
-rw-r--r--pkg/gui/presentation/graph/graph.go69
-rw-r--r--pkg/gui/presentation/reflog_commits.go15
-rw-r--r--pkg/gui/presentation/remote_branches.go13
-rw-r--r--pkg/gui/presentation/remotes.go13
-rw-r--r--pkg/gui/presentation/stash_entries.go13
-rw-r--r--pkg/gui/presentation/submodules.go11
-rw-r--r--pkg/gui/presentation/suggestions.go11
-rw-r--r--pkg/gui/presentation/tags.go13
-rw-r--r--pkg/gui/recent_repos_panel.go11
-rw-r--r--pkg/gui/refresh.go10
-rw-r--r--pkg/gui/services/custom_commands/handler_creator.go21
-rw-r--r--pkg/gui/services/custom_commands/keybinding_creator.go9
-rw-r--r--pkg/gui/status_panel.go14
-rw-r--r--pkg/utils/formatting.go31
-rw-r--r--pkg/utils/fuzzy_search.go10
27 files changed, 201 insertions, 314 deletions
diff --git a/pkg/gui/context.go b/pkg/gui/context.go
index 53e29e246..2c30218bc 100644
--- a/pkg/gui/context.go
+++ b/pkg/gui/context.go
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/jesseduffield/generics/maps"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -413,16 +414,9 @@ func (gui *Gui) changeMainViewsContext(c types.Context) {
func (gui *Gui) viewTabNames(viewName string) []string {
tabContexts := gui.State.ViewTabContextMap[viewName]
- if len(tabContexts) == 0 {
- return nil
- }
-
- result := make([]string, len(tabContexts))
- for i, tabContext := range tabContexts {
- result[i] = tabContext.Tab
- }
-
- return result
+ return slices.Map(tabContexts, func(tabContext context.TabContext) string {
+ return tabContext.Tab
+ })
}
func (gui *Gui) setViewTabForContext(c types.Context) {
diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go
index 67d6b126a..1f5654902 100644
--- a/pkg/gui/context/menu_context.go
+++ b/pkg/gui/context/menu_context.go
@@ -1,6 +1,7 @@
package context
import (
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -77,19 +78,16 @@ func (self *MenuViewModel) SetMenuItems(items []*types.MenuItem) {
}
// TODO: move into presentation package
-func (self *MenuViewModel) GetDisplayStrings(startIdx int, length int) [][]string {
- stringArrays := make([][]string, len(self.menuItems))
- for i, item := range self.menuItems {
- if item.DisplayStrings == nil {
- styledStr := item.DisplayString
- if item.OpensMenu {
- styledStr = presentation.OpensMenuStyle(styledStr)
- }
- stringArrays[i] = []string{styledStr}
- } else {
- stringArrays[i] = item.DisplayStrings
+func (self *MenuViewModel) GetDisplayStrings(_startIdx int, _length int) [][]string {
+ return slices.Map(self.menuItems, func(item *types.MenuItem) []string {
+ if item.DisplayStrings != nil {
+ return item.DisplayStrings
}
- }
- return stringArrays
+ styledStr := item.DisplayString
+ if item.OpensMenu {
+ styledStr = presentation.OpensMenuStyle(styledStr)
+ }
+ return []string{styledStr}
+ })
}
diff --git a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go
index 477c5c64f..636c1c5fe 100644
--- a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go
+++ b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go
@@ -4,6 +4,7 @@ import (
"fmt"
"strings"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
@@ -51,17 +52,14 @@ func (self *MergeAndRebaseHelper) CreateRebaseOptionsMenu() error {
options = append(options, REBASE_OPTION_SKIP)
}
- menuItems := make([]*types.MenuItem, len(options))
- for i, option := range options {
- // note to self. Never, EVER, close over loop variables in a function
- option := option
- menuItems[i] = &types.MenuItem{
+ menuItems := slices.Map(options, func(option string) *types.MenuItem {
+ return &types.MenuItem{
DisplayString: option,
OnPress: func() error {
return self.genericMergeCommand(option)
},
}
- }
+ })
var title string
if self.git.Status.WorkingTreeState() == enums.REBASE_MODE_MERGING {
diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go
index 65c01d4a7..0838dd6f0 100644
--- a/pkg/gui/controllers/helpers/refs_helper.go
+++ b/pkg/gui/controllers/helpers/refs_helper.go
@@ -4,6 +4,7 @@ import (
"fmt"
"strings"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
@@ -134,10 +135,8 @@ func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string
func (self *RefsHelper) CreateGitResetMenu(ref string) error {
strengths := []string{"soft", "mixed", "hard"}
- menuItems := make([]*types.MenuItem, len(strengths))
- for i, strength := range strengths {
- strength := strength
- menuItems[i] = &types.MenuItem{
+ menuItems := slices.Map(strengths, func(strength string) *types.MenuItem {
+ return &types.MenuItem{
DisplayStrings: []string{
fmt.Sprintf("%s reset", strength),
style.FgRed.Sprintf("reset --%s %s", strength, ref),
@@ -147,7 +146,7 @@ func (self *RefsHelper) CreateGitResetMenu(ref string) error {
return self.ResetToRef(ref, strength, []string{})
},
}
- }
+ })
return self.c.Menu(types.CreateMenuOptions{
Title: fmt.Sprintf("%s %s", self.c.Tr.LcResetTo, ref),
diff --git a/pkg/gui/controllers/helpers/suggestions_helper.go b/pkg/gui/controllers/helpers/suggestions_helper.go
index a48e325b1..52ccf9d96 100644
--- a/pkg/gui/controllers/helpers/suggestions_helper.go
+++ b/pkg/gui/controllers/helpers/suggestions_helper.go
@@ -4,6 +4,8 @@ import (
"fmt"
"os"
+ "github.com/jesseduffield/generics/slices"
+ "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
@@ -51,22 +53,18 @@ func NewSuggestionsHelper(
}
func (self *SuggestionsHelper) getRemoteNames() []string {
- result := make([]string, len(self.model.Remotes))
- for i, remote := range self.model.Remotes {
- result[i] = remote.Name
- }
- return result
+ return slices.Map(self.model.Remotes, func(remote *models.Remote) string {
+ return remote.Name
+ })
}
func matchesToSuggestions(matches []string) []*types.Suggestion {
- suggestions := make([]*types.Suggestion, len(matches))
- for i, match := range matches {
- suggestions[i] = &types.Suggestion{
+ return slices.Map(matches, func(match string) *types.Suggestion {
+ return &types.Suggestion{
Value: match,
Label: match,
}
- }
- return suggestions
+ })
}
func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion {
@@ -76,11 +74,9 @@ func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.
}
func (self *SuggestionsHelper) getBranchNames() []string {
- result := make([]string, len(self.model.Branches))
- for i, branch := range self.model.Branches {
- result[i] = branch.Name
- }
- return result
+ return slices.Map(self.model.Branches, func(branch *models.Branch) string {
+ return branch.Name
+ })
}
func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*types.Suggestion {
@@ -94,15 +90,12 @@ func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*ty
matchingBranchNames = utils.FuzzySearch(input, branchNames)
}
- suggestions := make([]*types.Suggestion, len(matchingBranchNames))
- for i, branchName := range matchingBranchNames {
- suggestions[i] = &types.Suggestion{
+ return slices.Map(matchingBranchNames, func(branchName string) *types.Suggestion {
+ return &types.Suggestion{
Value: branchName,
Label: presentation.GetBranchTextStyle(branchName).Sprint(branchName),
}
- }
-
- return suggestions
+ })
}
}
@@ -148,26 +141,16 @@ func (self *SuggestionsHelper) GetFilePathSuggestionsFunc() func(string) []*type
// doing another fuzzy search for good measure
matchingNames = utils.FuzzySearch(input, matchingNames)
- suggestions := make([]*types.Suggestion, len(matchingNames))
- for i, name := range matchingNames {
- suggestions[i] = &types.Suggestion{
- Value: name,
- Label: name,
- }
- }
-
- return suggestions
+ return matchesToSuggestions(matchingNames)
}
}
func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string {
- result := []string{}
- for _, remote := range self.model.Remotes {
- for _, branch := range remote.Branches {
- result = append(result, fmt.Sprintf("%s%s%s", remote.Name, separator, branch.Name))
- }
- }
- return result
+ return slices.FlatMap(self.model.Remotes, func(remote *models.Remote) []string {
+ return slices.Map(remote.Branches, func(branch *models.RemoteBranch) string {
+ return fmt.Sprintf("%s%s%s", remote.Name, separator, branch.Name)
+ })
+ })
}
func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
@@ -175,11 +158,9 @@ func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string
}
func (self *SuggestionsHelper) getTagNames() []string {
- result := make([]string, len(self.model.Tags))
- for i, tag := range self.model.Tags {
- result[i] = tag.Name
- }
- return result
+ return slices.Map(self.model.Tags, func(tag *models.Tag) string {
+ return tag.Name
+ })
}
func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Suggestion {
diff --git a/pkg/gui/files_panel_test.go b/pkg/gui/files_panel_test.go
index 8946898e5..08d5d8838 100644
--- a/pkg/gui/files_panel_test.go
+++ b/pkg/gui/files_panel_test.go
@@ -3,6 +3,7 @@ package gui
import (
"testing"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/stretchr/testify/assert"
)
@@ -24,11 +25,7 @@ func TestGetSuggestedRemote(t *testing.T) {
}
func mkRemoteList(names ...string) []*models.Remote {
- result := make([]*models.Remote, 0, len(names))
-
- for _, name := range names {
- result = append(result, &models.Remote{Name: name})
- }
-
- return result
+ return slices.Map(names, func(name string) *models.Remote {
+ return &models.Remote{Name: name}
+ })
}
diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go
index ac2057da5..ad794c0c2 100644
--- a/pkg/gui/filetree/commit_file_node.go
+++ b/pkg/gui/filetree/commit_file_node.go
@@ -1,6 +1,7 @@
package filetree
import (
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
@@ -40,19 +41,15 @@ func (s *CommitFileNode) GetPath() string {
}
func (s *CommitFileNode) GetChildren() []INode {
- result := make([]INode, len(s.Children))
- for i, child := range s.Children {
- result[i] = child
- }
-
- return result
+ return slices.Map(s.Children, func(child *CommitFileNode) INode {
+ return child
+ })
}
func (s *CommitFileNode) SetChildren(children []INode) {
- castChildren := make([]*CommitFileNode, len(children))
- for i, child := range children {
- castChildren[i] = child.(*CommitFileNode)
- }
+ castChildren := slices.Map(children, func(child INode) *CommitFileNode {
+ return child.(*CommitFileNode)
+ })
s.Children = castChildren
}
@@ -102,12 +99,10 @@ func (s *CommitFileNode) EveryFile(test func(file *models.CommitFile) bool) bool
func (n *CommitFileNode) Flatten(collapsedPaths *CollapsedPaths) []*CommitFileNode {
results := flatten(n, collapsedPaths)
- nodes := make([]*CommitFileNode, len(results))
- for i, result := range results {
- nodes[i] = result.(*CommitFileNode)
- }
- return nodes
+ return slices.Map(results, func(result INode) *CommitFileNode {
+ return result.(*CommitFileNode)
+ })
}
func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *CommitFileNode {
@@ -149,12 +144,10 @@ func (s *CommitFileNode) Compress() {
func (s *CommitFileNode) GetLeaves() []*CommitFileNode {
leaves := getLeaves(s)
- castLeaves := make([]*CommitFileNode, len(leaves))
- for i := range leaves {
- castLeaves[i] = leaves[i].(*CommitFileNode)
- }
- return castLeaves
+ return slices.Map(leaves, func(leaf INode) *CommitFileNode {
+ return leaf.(*CommitFileNode)
+ })
}
// extra methods
diff --git a/pkg/gui/filetree/file_node.go b/pkg/gui/filetree/file_node.go
index e73504321..69663b000 100644
--- a/pkg/gui/filetree/file_node.go
+++ b/pkg/gui/filetree/file_node.go
@@ -1,6 +1,7 @@
package filetree
import (
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
@@ -42,19 +43,15 @@ func (s *FileNode) GetPath() string {
}
func (s *FileNode) GetChildren() []INode {
- result := make([]INode, len(s.Children))
- for i, child := range s.Children {
- result[i] = child
- }
-
- return result
+ return slices.Map(s.Children, func(child *FileNode) INode {
+ return child
+ })
}
func (s *FileNode) SetChildren(children []INode) {
- castChildren := make([]*FileNode, len(children))
- for i, child := range children {
- castChildren[i] = child.(*FileNode)
- }
+ castChildren := slices.Map(children, func(child INode) *FileNode {
+ return child.(*FileNode)
+ })
s.Children = castChildren
}
@@ -89,12 +86,9 @@ func (s *FileNode) Any(test func(node *FileNode) bool) bool {
func (n *FileNode) Flatten(collapsedPaths *CollapsedPaths) []*FileNode {
results := flatten(n, collapsedPaths)
- nodes := make([]*FileNode, len(results))
- for i, result := range results {
- nodes[i] = result.(*FileNode)
- }
-
- return nodes
+ return slices.Map(results, func(result INode) *FileNode {
+ return result.(*FileNode)
+ })
}
func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *FileNode {
@@ -146,12 +140,10 @@ func (node *FileNode) GetFilePathsMatching(test func(*models.File) bool) []strin
func (s *FileNode) GetLeaves() []*FileNode {
leaves := getLeaves(s)
- castLeaves := make([]*FileNode, len(leaves))
- for i := range leaves {
- castLeaves[i] = leaves[i].(*FileNode)
- }
- return castLeaves
+ return slices.Map(leaves, func(leaf INode) *FileNode {
+ return leaf.(*FileNode)
+ })
}
// extra methods
diff --git a/pkg/gui/filetree/file_tree.go b/pkg/gui/filetree/file_tree.go
index 47d7f32f2..d4bb8e596 100644
--- a/pkg/gui/filetree/file_tree.go
+++ b/pkg/gui/filetree/file_tree.go
@@ -3,6 +3,7 @@ package filetree
import (
"fmt"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/sirupsen/logrus"
)
@@ -85,13 +86,7 @@ func (self *FileTree) getFilesForDisplay() []*models.File {
}
func (self *FileTree) FilterFiles(test func(*models.File) bool) []*models.File {
- result := make([]*models.File, 0)
- for _, file := range self.getFiles() {
- if test(file) {
- result = append(result, file)
- }
- }
- return result
+ return slices.Filter(self.getFiles(), test)
}
func (self *FileTree) SetFilter(filter FileTreeDisplayFilter) {
diff --git a/pkg/gui/list_context_config.go b/pkg/gui/list_context_config.go
index dcea5a936..5a3f172f0 100644
--- a/pkg/gui/list_context_config.go
+++ b/pkg/gui/list_context_config.go
@@ -3,6 +3,7 @@ package gui
import (
"log"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
@@ -28,12 +29,9 @@ func (gui *Gui) filesListContext() *context.WorkingTreeContext {
gui.Views.Files,
func(startIdx int, length int) [][]string {
lines := presentation.RenderFileTree(gui.State.Contexts.Files.FileTreeViewModel, gui.State.Modes.Diffing.Ref, gui.State.Model.Submodules)
- mappedLines := make([][]string, len(lines))
- for i, line := range lines {
- mappedLines[i] = []string{line}
- }
-
- return mappedLines
+ return slices.Map(lines, func(line string) []string {
+ return []string{line}
+ })
},
OnFocusWrapper(gui.onFocusFile),
OnFocusWrapper(gui.withDiffModeCheck(gui.filesRenderToMain)),
@@ -235,12 +233,9 @@ func (gui *Gui) commitFilesListContext() *context.CommitFilesContext {
}
lines := presentation.RenderCommitFileTree(gui.State.Contexts.CommitFiles.CommitFileTreeViewModel, gui.State.Modes.Diffing.Ref, gui.git.Patch.PatchManager)
- mappedLines := make([][]string, len(lines))
- for i, line := range lines {
- mappedLines[i] = []string{line}
- }
-
- return mappedLines
+ return slices.Map(lines, func(line string) []string {
+ return []string{line}
+ })
},
OnFocusWrapper(gui.onCommitFileFocus),
OnFocusWrapper(gui.withDiffModeCheck(gui.commitFilesRenderToMain)),
diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go
index 54f08ed50..c21a9dce3 100644
--- a/pkg/gui/options_menu_panel.go
+++ b/pkg/gui/options_menu_panel.go
@@ -4,6 +4,7 @@ import (
"log"
"strings"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
@@ -62,11 +63,8 @@ func (gui *Gui) handleCreateOptionsMenu() error {
context := gui.currentContext()
bindings := gui.getBindings(context)
- menuItems := make([]*types.MenuItem, len(bindings))
-
- for i, binding := range bindings {
- binding := binding // note to self, never close over loop variables
- menuItems[i] = &types.MenuItem{
+ menuItems := slices.Map(bindings, func(binding *types.Binding) *types.MenuItem {
+ return &types.MenuItem{
DisplayStrings: []string{GetKeyDisplay(binding.Key), gui.displayDescription(binding)},
OnPress: func() error {
if binding.Key == nil {
@@ -78,7 +76,7 @@ func (gui *Gui) handleCreateOptionsMenu() error {
return binding.Handler()
},
}
- }
+ })
return gui.c.Menu(types.CreateMenuOptions{
Title: strings.Title(gui.c.Tr.LcMenu),
diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go
index 9062eface..b97ef6a5f 100644
--- a/pkg/gui/presentation/branches.go
+++ b/pkg/gui/presentation/branches.go
@@ -4,6 +4,7 @@ import (
"fmt"
"strings"
+ "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/i18n"
@@ -14,14 +15,10 @@ import (
var branchPrefixColorCache = make(map[string]style.TextStyle)
func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string, tr *i18n.TranslationSet) [][]string {
- lines := make([][]string, len(branches))
-
- for i := range branches {
- diffed := branches[i].Name == diffName
- lines[i] = getBranchDisplayStrings(branches[i], fullDescription, diffed, tr)
- }
-
- return lines
+ return slices.Map(branches, func(branch *models.Branch) []string {
+ diffed := branch.Name == diffName
+ return getBranchDisplayStrings(branch, fullDescription, diffed, tr)
+ })
}
// getBranchDisplayStrings returns the display string of branch
diff --git a/pkg/gui/presentation/graph/graph.go b/pkg/gui/presentation/graph/graph.go
index 70ab53079..de90d3e7a 100644
--- a/pkg/gui/presentation/graph/graph.go
+++ b/pkg/gui/presentation/graph/graph.go
@@ -5,10 +5,12 @@ import (
"strings"
"sync"
+ "github.com/jesseduffield/generics/set"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/utils"
+ "github.com/samber/lo"
)
type PipeKind uint8
@@ -77,7 +79,6 @@ func GetPipeSets(commits []*models.Commit, getStyle func(c *models.Commit) style
func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitSha string) []string {
maxProcs := runtime.GOMAXPROCS(0)
- lines := make([]string, 0, len(pipeSets))
// splitting up the rendering of the graph into multiple goroutines allows us to render the graph in parallel
chunks := make([][]string, maxProcs)
perProc := len(pipeSets) / maxProcs
@@ -110,24 +111,19 @@ func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitSha s
wg.Wait()
- for _, chunk := range chunks {
- lines = append(lines, chunk...)
- }
-
- return lines
+ return slices.Flatten(chunks)
}
func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *models.Commit) style.TextStyle) []*Pipe {
- currentPipes := make([]*Pipe, 0, len(prevPipes))
- maxPos := 0
- for _, pipe := range prevPipes {
- // a pipe that terminated in the previous line has no bearing on the current line
- // so we'll filter those out
- if pipe.kind != TERMINATES {
- currentPipes = append(currentPipes, pipe)
- }
- maxPos = utils.Max(maxPos, pipe.toPos)
- }
+ maxPos := lo.Max(
+ slices.Map(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
+ currentPipes := slices.Filter(prevPipes, func(pipe *Pipe) bool {
+ return pipe.kind != TERMINATES
+ })
newPipes := make([]*Pipe, 0, len(currentPipes)+len(commit.Parents))
// start by assuming that we've got a brand new commit not related to any preceding commit.
@@ -142,9 +138,9 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
}
// a taken spot is one where a current pipe is ending on
- takenSpots := make(map[int]bool)
+ takenSpots := set.New[int]()
// a traversed spot is one where a current pipe is starting on, ending on, or passing through
- traversedSpots := make(map[int]bool)
+ traversedSpots := set.New[int]()
if len(commit.Parents) > 0 {
newPipes = append(newPipes, &Pipe{
@@ -157,17 +153,17 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
})
}
- traversedSpotsForContinuingPipes := make(map[int]bool)
+ traversedSpotsForContinuingPipes := set.New[int]()
for _, pipe := range currentPipes {
if !equalHashes(pipe.toSha, commit.Sha) {
- traversedSpotsForContinuingPipes[pipe.toPos] = true
+ traversedSpotsForContinuingPipes.Add(pipe.toPos)
}
}
getNextAvailablePosForContinuingPipe := func() int {
i := 0
for {
- if !traversedSpots[i] {
+ if !traversedSpots.Includes(i) {
return i
}
i++
@@ -179,7 +175,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
for {
// a newly created pipe is not allowed to end on a spot that's already taken,
// nor on a spot that's been traversed by a continuing pipe.
- if !takenSpots[i] && !traversedSpotsForContinuingPipes[i] {
+ if !takenSpots.Includes(i) && !traversedSpotsForContinuingPipes.Includes(i) {
return i
}
i++
@@ -192,9 +188,9 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
left, right = right, left
}
for i := left; i <= right; i++ {
- traversedSpots[i] = true
+ traversedSpots.Add(i)
}
- takenSpots[to] = true
+ takenSpots.Add(to)
}
for _, pipe := range currentPipes {
@@ -237,7 +233,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
style: getStyle(commit),
})
- takenSpots[availablePos] = true
+ takenSpots.Add(availablePos)
}
}
@@ -246,7 +242,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
// continuing on, potentially moving left to fill in a blank spot
last