Skip to content

Commit f54f671

Browse files
mtrajanostefanhaller
authored andcommitted
Add ability to configure branch color patterns
1 parent cdc324f commit f54f671

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

docs/Config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,15 @@ You can customize the color of branches based on the branch prefix:
836836

837837
```yaml
838838
gui:
839+
# NOTE: that this configuration will be deprecated in favor of using branchColorPatterns below
839840
branchColors:
840841
'docs': '#11aaff' # use a light blue for branches beginning with 'docs/'
842+
843+
# alternatively you can use a regex pattern as your coloring rules
844+
# NOTE: this configuration overwrites the one above, if you would like to set a similar rule see the example below
845+
branchColorPatterns:
846+
'docs/.+': '#11aaff' # similar to the previous configuration above, setting branches that begin with 'docs/'
847+
'ISSUE-\d+': '#ff5733' # use a bright orange for branches beginning with 'ISSUE-'
841848
```
842849

843850
## Example Coloring

pkg/config/user_config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ 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+
// NOTE: BranchColors is being deprecated in favor of BranchColorPatterns
56+
BranchColors map[string]string `yaml:"branchColors"`
57+
BranchColorPatterns map[string]string `yaml:"branchColorPatterns"`
5658
// The number of lines you scroll by when scrolling the main window
5759
ScrollHeight int `yaml:"scrollHeight" jsonschema:"minimum=1"`
5860
// If true, allow scrolling past the bottom of the content in the main window

pkg/gui/gui.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,13 @@ 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 len(userConfig.Gui.BranchColorPatterns) > 0 {
459+
presentation.SetCustomBranches(userConfig.Gui.BranchColorPatterns, true)
460+
} else {
461+
// The alternative is to match on branch types with the branchColors config which will be deprecated in the future
462+
presentation.SetCustomBranches(userConfig.Gui.BranchColors, false)
463+
}
458464

459465
return nil
460466
}

pkg/gui/presentation/branches.go

Lines changed: 32 additions & 7 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 // NOTE: this value is needed only until branchColors is deprecated and only regex color patterns are used
25+
}
26+
27+
var branchPrefixColorCache *colorMatcher
2228

2329
func GetBranchListDisplayStrings(
2430
branches []*models.Branch,
@@ -125,15 +131,31 @@ func getBranchDisplayStrings(
125131

126132
// GetBranchTextStyle branch color
127133
func GetBranchTextStyle(name string) style.TextStyle {
128-
branchType := strings.Split(name, "/")[0]
129-
130-
if value, ok := branchPrefixColorCache[branchType]; ok {
131-
return value
134+
if style, ok := branchPrefixColorCache.match(name); ok {
135+
return *style
132136
}
133137

134138
return theme.DefaultTextColor
135139
}
136140

141+
func (m *colorMatcher) match(name string) (*style.TextStyle, bool) {
142+
if m.isRegex {
143+
for pattern, style := range m.patterns {
144+
if matched, _ := regexp.MatchString(pattern, name); matched {
145+
return &style, true
146+
}
147+
}
148+
} else {
149+
// old behavior using the deprecated branchColors behavior matching on branch type
150+
branchType := strings.Split(name, "/")[0]
151+
if value, ok := m.patterns[branchType]; ok {
152+
return &value, true
153+
}
154+
}
155+
156+
return nil, false
157+
}
158+
137159
func BranchStatus(
138160
branch *models.Branch,
139161
itemOperation types.ItemOperation,
@@ -180,6 +202,9 @@ func BranchStatus(
180202
return result
181203
}
182204

183-
func SetCustomBranches(customBranchColors map[string]string) {
184-
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
205+
func SetCustomBranches(customBranchColors map[string]string, isRegex bool) {
206+
branchPrefixColorCache = &colorMatcher{
207+
patterns: utils.SetCustomColors(customBranchColors),
208+
isRegex: isRegex,
209+
}
185210
}

0 commit comments

Comments
 (0)