diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 19:12:58 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 20:14:41 +1100 |
commit | 1b75ed37403ac2997cb6a5ede92d87f1a1eb96b1 (patch) | |
tree | 6a1b70201901725cd689c90f32a75fe34e77a603 /pkg/gui/presentation | |
parent | bf4f06ab4e6ceefe388e0efefcc553526f3d96c2 (diff) |
many more generics
Diffstat (limited to 'pkg/gui/presentation')
-rw-r--r-- | pkg/gui/presentation/branches.go | 13 | ||||
-rw-r--r-- | pkg/gui/presentation/graph/graph.go | 69 | ||||
-rw-r--r-- | pkg/gui/presentation/reflog_commits.go | 15 | ||||
-rw-r--r-- | pkg/gui/presentation/remote_branches.go | 13 | ||||
-rw-r--r-- | pkg/gui/presentation/remotes.go | 13 | ||||
-rw-r--r-- | pkg/gui/presentation/stash_entries.go | 13 | ||||
-rw-r--r-- | pkg/gui/presentation/submodules.go | 11 | ||||
-rw-r--r-- | pkg/gui/presentation/suggestions.go | 11 | ||||
-rw-r--r-- | pkg/gui/presentation/tags.go | 13 |
9 files changed, 67 insertions, 104 deletions
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 := pipe.toPos for i := pipe.toPos; i > pos; i-- { - if takenSpots[i] || traversedSpots[i] { + if takenSpots.Includes(i) || traversedSpots.Includes(i) { break } else { last = i @@ -297,10 +293,9 @@ func renderPipeSet( } isMerge := startCount > 1 - cells := make([]*Cell, maxPos+1) - for i := range cells { - cells[i] = &Cell{cellType: CONNECTION, style: style.FgDefault} - } + cells := slices.Map(lo.Range(maxPos+1), func(i int) *Cell { + return &Cell{cellType: CONNECTION, style: style.FgDefault} + }) renderPipe := func(pipe *Pipe, style style.TextStyle, overrideRightStyle bool) { left := pipe.left() @@ -336,17 +331,9 @@ func renderPipeSet( // so we have our commit pos again, now it's time to build the cells. // we'll handle the one that's sourced from our selected commit last so that it can override the other cells. - selectedPipes := []*Pipe{} - // pre-allocating this one because most of the time we'll only have non-selected pipes - nonSelectedPipes := make([]*Pipe, 0, len(pipes)) - - for _, pipe := range pipes { - if highlight && equalHashes(pipe.fromSha, selectedCommitSha) { - selectedPipes = append(selectedPipes, pipe) - } else { - nonSelectedPipes = append(nonSelectedPipes, pipe) - } - } + selectedPipes, nonSelectedPipes := slices.Partition(pipes, func(pipe *Pipe) bool { + return highlight && equalHashes(pipe.fromSha, selectedCommitSha) + }) for _, pipe := range nonSelectedPipes { if pipe.kind == STARTS { diff --git a/pkg/gui/presentation/reflog_commits.go b/pkg/gui/presentation/reflog_commits.go index 72bb80ef6..95124c867 100644 --- a/pkg/gui/presentation/reflog_commits.go +++ b/pkg/gui/presentation/reflog_commits.go @@ -2,6 +2,7 @@ package presentation import ( "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/theme" @@ -10,8 +11,6 @@ import ( ) 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 if fullDescription { displayFunc = getFullDescriptionDisplayStringsForReflogCommit @@ -19,13 +18,11 @@ func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription displayFunc = getDisplayStringsForReflogCommit } - for i := range commits { - diffed := commits[i].Sha == diffName - cherryPicked := cherryPickedCommitShaSet.Includes(commits[i].Sha) - lines[i] = displayFunc(commits[i], cherryPicked, diffed, parseEmoji) - } - - return lines + return slices.Map(commits, func(commit *models.Commit) []string { + diffed := commit.Sha == diffName + cherryPicked := cherryPickedCommitShaSet.Includes(commit.Sha) + return displayFunc(commit, cherryPicked, diffed, parseEmoji) + }) } func reflogShaColor(cherryPicked, diffed bool) style.TextStyle { diff --git a/pkg/gui/presentation/remote_branches.go b/pkg/gui/presentation/remote_branches.go index d8439acfe..c5c54dfcb 100644 --- a/pkg/gui/presentation/remote_branches.go +++ b/pkg/gui/presentation/remote_branches.go @@ -1,19 +1,16 @@ package presentation import ( + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/theme" ) func GetRemoteBranchListDisplayStrings(branches []*models.RemoteBranch, diffName string) [][]string { - lines := make([][]string, len(branches)) - - for i := range branches { - diffed := branches[i].FullName() == diffName - lines[i] = getRemoteBranchDisplayStrings(branches[i], diffed) - } - - return lines + return slices.Map(branches, func(branch *models.RemoteBranch) []string { + diffed := branch.FullName() == diffName + return getRemoteBranchDisplayStrings(branch, diffed) + }) } // getRemoteBranchDisplayStrings returns the display string of branch diff --git a/pkg/gui/presentation/remotes.go b/pkg/gui/presentation/remotes.go index a1e50fe2f..9b26cbfae 100644 --- a/pkg/gui/presentation/remotes.go +++ b/pkg/gui/presentation/remotes.go @@ -1,20 +1,17 @@ package presentation import ( + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" ) func GetRemoteListDisplayStrings(remotes []*models.Remote, diffName string) [][]string { - lines := make([][]string, len(remotes)) - - for i := range remotes { - diffed := remotes[i].Name == diffName - lines[i] = getRemoteDisplayStrings(remotes[i], diffed) - } - - return lines + return slices.Map(remotes, func(remote *models.Remote) []string { + diffed := remote.Name == diffName + return getRemoteDisplayStrings(remote, diffed) + }) } // getRemoteDisplayStrings returns the display string of branch diff --git a/pkg/gui/presentation/stash_entries.go b/pkg/gui/presentation/stash_entries.go index f15b35a9c..54b39c636 100644 --- a/pkg/gui/presentation/stash_entries.go +++ b/pkg/gui/presentation/stash_entries.go @@ -1,19 +1,16 @@ package presentation import ( + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/theme" ) func GetStashEntryListDisplayStrings(stashEntries []*models.StashEntry, diffName string) [][]string { - lines := make([][]string, len(stashEntries)) - - for i := range stashEntries { - diffed := stashEntries[i].RefName() == diffName - lines[i] = getStashEntryDisplayStrings(stashEntries[i], diffed) - } - - return lines + return slices.Map(stashEntries, func(stashEntry *models.StashEntry) []string { + diffed := stashEntry.RefName() == diffName + return getStashEntryDisplayStrings(stashEntry, diffed) + }) } // getStashEntryDisplayStrings returns the display string of branch diff --git a/pkg/gui/presentation/submodules.go b/pkg/gui/presentation/submodules.go index 2d131ed8f..0fb057ef0 100644 --- a/pkg/gui/presentation/submodules.go +++ b/pkg/gui/presentation/submodules.go @@ -1,18 +1,15 @@ package presentation import ( + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/theme" ) func GetSubmoduleListDisplayStrings(submodules []*models.SubmoduleConfig) [][]string { - lines := make([][]string, len(submodules)) - - for i := range submodules { - lines[i] = getSubmoduleDisplayStrings(submodules[i]) - } - - return lines + return slices.Map(submodules, func(submodule *models.SubmoduleConfig) []string { + return getSubmoduleDisplayStrings(submodule) + }) } func getSubmoduleDisplayStrings(s *models.SubmoduleConfig) []string { diff --git a/pkg/gui/presentation/suggestions.go b/pkg/gui/presentation/suggestions.go index 81c6a3a3d..5319b40f7 100644 --- a/pkg/gui/presentation/suggestions.go +++ b/pkg/gui/presentation/suggestions.go @@ -1,17 +1,14 @@ package presentation import ( + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/gui/types" ) func GetSuggestionListDisplayStrings(suggestions []*types.Suggestion) [][]string { - lines := make([][]string, len(suggestions)) - - for i := range suggestions { - lines[i] = getSuggestionDisplayStrings(suggestions[i]) - } - - return lines + return slices.Map(suggestions, func(suggestion *types.Suggestion) []string { + return getSuggestionDisplayStrings(suggestion) + }) } func getSuggestionDisplayStrings(suggestion *types.Suggestion) []string { diff --git a/pkg/gui/presentation/tags.go b/pkg/gui/presentation/tags.go index 4754c4bef..2157e29c9 100644 --- a/pkg/gui/presentation/tags.go +++ b/pkg/gui/presentation/tags.go @@ -1,19 +1,16 @@ package presentation import ( + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/theme" ) func GetTagListDisplayStrings(tags []*models.Tag, diffName string) [][]string { - lines := make([][]string, len(tags)) - - for i := range tags { - diffed := tags[i].Name == diffName - lines[i] = getTagDisplayStrings(tags[i], diffed) - } - - return lines + return slices.Map(tags, func(tag *models.Tag) []string { + diffed := tag.Name == diffName + return getTagDisplayStrings(tag, diffed) + }) } // getTagDisplayStrings returns the display string of branch |