Skip to content

Commit f85a192

Browse files
committed
Store TextStyle in Pipe struct as pointer
The instances are held by the AuthorStyle cache.
1 parent d49a18d commit f85a192

File tree

7 files changed

+97
-96
lines changed

7 files changed

+97
-96
lines changed

pkg/gui/presentation/authors/authors.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type authorNameCacheKey struct {
2121
var (
2222
authorInitialCache = make(map[string]string)
2323
authorNameCache = make(map[authorNameCacheKey]string)
24-
authorStyleCache = make(map[string]style.TextStyle)
24+
authorStyleCache = make(map[string]*style.TextStyle)
2525
)
2626

2727
const authorNameWildcard = "*"
@@ -73,7 +73,7 @@ func AuthorWithLength(authorName string, length int) string {
7373
return LongAuthor(authorName, length)
7474
}
7575

76-
func AuthorStyle(authorName string) style.TextStyle {
76+
func AuthorStyle(authorName string) *style.TextStyle {
7777
if value, ok := authorStyleCache[authorName]; ok {
7878
return value
7979
}
@@ -85,9 +85,9 @@ func AuthorStyle(authorName string) style.TextStyle {
8585

8686
value := trueColorStyle(authorName)
8787

88-
authorStyleCache[authorName] = value
88+
authorStyleCache[authorName] = &value
8989

90-
return value
90+
return &value
9191
}
9292

9393
func trueColorStyle(str string) style.TextStyle {

pkg/gui/presentation/branches.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
type colorMatcher struct {
23-
patterns map[string]style.TextStyle
23+
patterns map[string]*style.TextStyle
2424
isRegex bool // NOTE: this value is needed only until the deprecated branchColors config is removed and only regex color patterns are used
2525
}
2626

@@ -142,14 +142,14 @@ func (m *colorMatcher) match(name string) (*style.TextStyle, bool) {
142142
if m.isRegex {
143143
for pattern, style := range m.patterns {
144144
if matched, _ := regexp.MatchString(pattern, name); matched {
145-
return &style, true
145+
return style, true
146146
}
147147
}
148148
} else {
149149
// old behavior using the deprecated branchColors behavior matching on branch type
150150
branchType := strings.Split(name, "/")[0]
151151
if value, ok := m.patterns[branchType]; ok {
152-
return &value, true
152+
return value, true
153153
}
154154
}
155155

pkg/gui/presentation/commits.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func loadPipesets(commits []*models.Commit) [][]graph.Pipe {
258258
if !ok {
259259
// pipe sets are unique to a commit head. and a commit count. Sometimes we haven't loaded everything for that.
260260
// so let's just cache it based on that.
261-
getStyle := func(commit *models.Commit) style.TextStyle {
261+
getStyle := func(commit *models.Commit) *style.TextStyle {
262262
return authors.AuthorStyle(commit.AuthorName)
263263
}
264264
pipeSets = graph.GetPipeSets(commits, getStyle)

pkg/gui/presentation/graph/cell.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Cell struct {
2525
up, down, left, right bool
2626
cellType cellType
2727
rightStyle *style.TextStyle
28-
style style.TextStyle
28+
style *style.TextStyle
2929
}
3030

3131
func (cell *Cell) render(writer io.StringWriter) {
@@ -44,7 +44,7 @@ func (cell *Cell) render(writer io.StringWriter) {
4444

4545
var rightStyle *style.TextStyle
4646
if cell.rightStyle == nil {
47-
rightStyle = &cell.style
47+
rightStyle = cell.style
4848
} else {
4949
rightStyle = cell.rightStyle
5050
}
@@ -59,7 +59,7 @@ func (cell *Cell) render(writer io.StringWriter) {
5959
styledSecondChar = cachedSprint(*rightStyle, second)
6060
}
6161

62-
_, _ = writer.WriteString(cachedSprint(cell.style, adjustedFirst))
62+
_, _ = writer.WriteString(cachedSprint(*cell.style, adjustedFirst))
6363
_, _ = writer.WriteString(styledSecondChar)
6464
}
6565

@@ -104,19 +104,19 @@ func (cell *Cell) reset() {
104104
cell.right = false
105105
}
106106

107-
func (cell *Cell) setUp(style style.TextStyle) *Cell {
107+
func (cell *Cell) setUp(style *style.TextStyle) *Cell {
108108
cell.up = true
109109
cell.style = style
110110
return cell
111111
}
112112

113-
func (cell *Cell) setDown(style style.TextStyle) *Cell {
113+
func (cell *Cell) setDown(style *style.TextStyle) *Cell {
114114
cell.down = true
115115
cell.style = style
116116
return cell
117117
}
118118

119-
func (cell *Cell) setLeft(style style.TextStyle) *Cell {
119+
func (cell *Cell) setLeft(style *style.TextStyle) *Cell {
120120
cell.left = true
121121
if !cell.up && !cell.down {
122122
// vertical trumps left
@@ -126,15 +126,15 @@ func (cell *Cell) setLeft(style style.TextStyle) *Cell {
126126
}
127127

128128
//nolint:unparam
129-
func (cell *Cell) setRight(style style.TextStyle, override bool) *Cell {
129+
func (cell *Cell) setRight(style *style.TextStyle, override bool) *Cell {
130130
cell.right = true
131131
if cell.rightStyle == nil || override {
132-
cell.rightStyle = &style
132+
cell.rightStyle = style
133133
}
134134
return cell
135135
}
136136

137-
func (cell *Cell) setStyle(style style.TextStyle) *Cell {
137+
func (cell *Cell) setStyle(style *style.TextStyle) *Cell {
138138
cell.style = style
139139
return cell
140140
}

pkg/gui/presentation/graph/graph.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Pipe struct {
2828
fromHash *string
2929
toHash *string
3030
kind PipeKind
31-
style style.TextStyle
31+
style *style.TextStyle
3232
}
3333

3434
var (
@@ -45,7 +45,7 @@ func (self Pipe) right() int {
4545
return max(self.fromPos, self.toPos)
4646
}
4747

48-
func RenderCommitGraph(commits []*models.Commit, selectedCommitHash *string, getStyle func(c *models.Commit) style.TextStyle) []string {
48+
func RenderCommitGraph(commits []*models.Commit, selectedCommitHash *string, getStyle func(c *models.Commit) *style.TextStyle) []string {
4949
pipeSets := GetPipeSets(commits, getStyle)
5050
if len(pipeSets) == 0 {
5151
return nil
@@ -56,12 +56,12 @@ func RenderCommitGraph(commits []*models.Commit, selectedCommitHash *string, get
5656
return lines
5757
}
5858

59-
func GetPipeSets(commits []*models.Commit, getStyle func(c *models.Commit) style.TextStyle) [][]Pipe {
59+
func GetPipeSets(commits []*models.Commit, getStyle func(c *models.Commit) *style.TextStyle) [][]Pipe {
6060
if len(commits) == 0 {
6161
return nil
6262
}
6363

64-
pipes := []Pipe{{fromPos: 0, toPos: 0, fromHash: &StartCommitHash, toHash: commits[0].HashPtr(), kind: STARTS, style: style.FgDefault}}
64+
pipes := []Pipe{{fromPos: 0, toPos: 0, fromHash: &StartCommitHash, toHash: commits[0].HashPtr(), kind: STARTS, style: &style.FgDefault}}
6565

6666
return lo.Map(commits, func(commit *models.Commit, _ int) []Pipe {
6767
pipes = getNextPipes(pipes, commit, getStyle)
@@ -106,7 +106,7 @@ func RenderAux(pipeSets [][]Pipe, commits []*models.Commit, selectedCommitHash *
106106
return lo.Flatten(chunks)
107107
}
108108

109-
func getNextPipes(prevPipes []Pipe, commit *models.Commit, getStyle func(c *models.Commit) style.TextStyle) []Pipe {
109+
func getNextPipes(prevPipes []Pipe, commit *models.Commit, getStyle func(c *models.Commit) *style.TextStyle) []Pipe {
110110
maxPos := 0
111111
for _, pipe := range prevPipes {
112112
if pipe.toPos > maxPos {
@@ -293,10 +293,10 @@ func renderPipeSet(
293293
isMerge := startCount > 1
294294

295295
cells := lo.Map(lo.Range(maxPos+1), func(i int, _ int) *Cell {
296-
return &Cell{cellType: CONNECTION, style: style.FgDefault}
296+
return &Cell{cellType: CONNECTION, style: &style.FgDefault}
297297
})
298298

299-
renderPipe := func(pipe *Pipe, style style.TextStyle, overrideRightStyle bool) {
299+
renderPipe := func(pipe *Pipe, style *style.TextStyle, overrideRightStyle bool) {
300300
left := pipe.left()
301301
right := pipe.right()
302302

@@ -352,9 +352,9 @@ func renderPipeSet(
352352
}
353353
}
354354
for _, pipe := range selectedPipes {
355-
renderPipe(&pipe, highlightStyle, true)
355+
renderPipe(&pipe, &highlightStyle, true)
356356
if pipe.toPos == commitPos {
357-
cells[pipe.toPos].setStyle(highlightStyle)
357+
cells[pipe.toPos].setStyle(&highlightStyle)
358358
}
359359
}
360360

0 commit comments

Comments
 (0)