Skip to content

Commit 925e161

Browse files
committed
Add ability to configure branch color patterns
1 parent 5e9ba82 commit 925e161

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

docs/Config.md

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

830830
```yaml
831831
gui:
832+
# NOTE: that this configuration will be deprecated in favor of using branchColorPatterns below
832833
branchColors:
833834
'docs': '#11aaff' # use a light blue for branches beginning with 'docs/'
835+
836+
# alternatively you can use a regex pattern as your coloring rules
837+
# NOTE: this configuration overwrites the one above, if you would like to set a similar rule see the example below
838+
branchColorPatterns:
839+
'docs/.+': '#11aaff' # similar to the previous configuration above, setting branches that begin with 'docs/'
840+
'ISSUE-\d+': '#ff5733' # use a bright orange for branches beginning with 'ISSUE-'
834841
```
835842

836843
## 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 userConfig.Gui.BranchColorPatterns != nil {
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: 34 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,12 +131,12 @@ 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

138+
// Default colors for common branch types
139+
branchType := strings.Split(name, "/")[0]
134140
switch branchType {
135141
case "feature":
136142
return style.FgGreen
@@ -143,6 +149,24 @@ func GetBranchTextStyle(name string) style.TextStyle {
143149
}
144150
}
145151

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+
// old behavior using the deprecated branchColors behavior matching on branch type
161+
branchType := strings.Split(name, "/")[0]
162+
if value, ok := m.patterns[branchType]; ok {
163+
return &value, true
164+
}
165+
}
166+
167+
return nil, false
168+
}
169+
146170
func BranchStatus(
147171
branch *models.Branch,
148172
itemOperation types.ItemOperation,
@@ -189,6 +213,9 @@ func BranchStatus(
189213
return result
190214
}
191215

192-
func SetCustomBranches(customBranchColors map[string]string) {
193-
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
216+
func SetCustomBranches(customBranchColors map[string]string, isRegex bool) {
217+
branchPrefixColorCache = &colorMatcher{
218+
patterns: utils.SetCustomColors(customBranchColors),
219+
isRegex: isRegex,
220+
}
194221
}

0 commit comments

Comments
 (0)