many more generics

This commit is contained in:
Jesse Duffield
2022-03-19 19:12:58 +11:00
parent bf4f06ab4e
commit 1b75ed3740
31 changed files with 278 additions and 320 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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