Skip to content

Commit ce827b3

Browse files
committed
Overwrite branchColors config
1 parent 95042fb commit ce827b3

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

docs/Config.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,12 @@ You can customize the color of branches based on the branch prefix:
814814
gui:
815815
branchColors:
816816
'docs': '#11aaff' # use a light blue for branches beginning with 'docs/'
817-
'ISSUE-*': '#ff5733' # use a bright orange for branches beginning with 'ISSUE-'
817+
818+
# alternatively you can use a regex pattern as your coloring rules
819+
# NOTE: this configuration overwrites the one above, if you would like to set a similar rule see the example below
820+
branchColorPatterns:
821+
'docs/.+': '#11aaff' # similar to the previous configuration above, setting branches that begin with 'docs/'
822+
'ISSUE-\d+': '#ff5733' # use a bright orange for branches beginning with 'ISSUE-'
818823
```
819824

820825
## Example Coloring

pkg/config/user_config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ type GuiConfig struct {
5252
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-author-color
5353
AuthorColors map[string]string `yaml:"authorColors"`
5454
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color
55-
BranchColors map[string]string `yaml:"branchColors"`
55+
BranchColors map[string]string `yaml:"branchColors"`
56+
BranchColorPatterns map[string]string `yaml:"branchColorPatterns"`
5657
// The number of lines you scroll by when scrolling the main window
5758
ScrollHeight int `yaml:"scrollHeight" jsonschema:"minimum=1"`
5859
// If true, allow scrolling past the bottom of the content in the main window

pkg/gui/gui.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,12 @@ func (gui *Gui) onUserConfigLoaded() error {
454454
} else if userConfig.Gui.ShowIcons {
455455
icons.SetNerdFontsVersion("2")
456456
}
457-
presentation.SetCustomBranches(userConfig.Gui.BranchColors)
457+
458+
if userConfig.Gui.BranchColorPatterns != nil {
459+
presentation.SetCustomBranches(userConfig.Gui.BranchColorPatterns, true)
460+
} else {
461+
presentation.SetCustomBranches(userConfig.Gui.BranchColors, false)
462+
}
458463

459464
return nil
460465
}

pkg/gui/presentation/branches.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package presentation
22

33
import (
44
"fmt"
5+
"regexp"
56
"strings"
67
"time"
78

@@ -18,7 +19,12 @@ import (
1819
"github.com/samber/lo"
1920
)
2021

21-
var branchPrefixColorCache = make(map[string]style.TextStyle)
22+
type colorMatcher struct {
23+
patterns map[string]style.TextStyle
24+
isRegex bool
25+
}
26+
27+
var branchPrefixColorCache *colorMatcher
2228

2329
func GetBranchListDisplayStrings(
2430
branches []*models.Branch,
@@ -123,31 +129,14 @@ func getBranchDisplayStrings(
123129
return res
124130
}
125131

126-
func matchColorPrefix(name string) (style.TextStyle, bool) {
127-
for possiblePattern, color := range branchPrefixColorCache {
128-
if strings.Contains(possiblePattern, "*") {
129-
prefix := strings.TrimSuffix(possiblePattern, "*")
130-
if strings.HasPrefix(name, prefix) {
131-
return color, true
132-
}
133-
}
134-
}
135-
136-
return style.TextStyle{}, false
137-
}
138-
139132
// GetBranchTextStyle branch color
140133
func GetBranchTextStyle(name string) style.TextStyle {
141-
branchType := strings.Split(name, "/")[0]
142-
143-
if value, ok := branchPrefixColorCache[branchType]; ok {
144-
return value
145-
}
146-
147-
if value, ok := matchColorPrefix(name); ok {
148-
return value
134+
if style, ok := branchPrefixColorCache.match(name); ok {
135+
return *style
149136
}
150137

138+
// Default colors for common branch types
139+
branchType := strings.Split(name, "/")[0]
151140
switch branchType {
152141
case "feature":
153142
return style.FgGreen
@@ -160,6 +149,23 @@ func GetBranchTextStyle(name string) style.TextStyle {
160149
}
161150
}
162151

152+
func (m *colorMatcher) match(name string) (*style.TextStyle, bool) {
153+
if m.isRegex {
154+
for pattern, style := range m.patterns {
155+
if matched, _ := regexp.MatchString("^"+pattern+"$", name); matched {
156+
return &style, true
157+
}
158+
}
159+
} else {
160+
branchType := strings.Split(name, "/")[0]
161+
if value, ok := m.patterns[branchType]; ok {
162+
return &value, true
163+
}
164+
}
165+
166+
return nil, false
167+
}
168+
163169
func BranchStatus(
164170
branch *models.Branch,
165171
itemOperation types.ItemOperation,
@@ -206,6 +212,9 @@ func BranchStatus(
206212
return result
207213
}
208214

209-
func SetCustomBranches(customBranchColors map[string]string) {
210-
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
215+
func SetCustomBranches(customBranchColors map[string]string, isRegex bool) {
216+
branchPrefixColorCache = &colorMatcher{
217+
patterns: utils.SetCustomColors(customBranchColors),
218+
isRegex: isRegex,
219+
}
211220
}

0 commit comments

Comments
 (0)