Skip to content

Commit a1a8cd1

Browse files
authored
Add ability to configure branch color patterns using regex (#4130)
- **PR Description** Add ability to specify color patterns in the `branchColorPatterns` config using regex, ex. `JIRA-\d+` would match all branch names in the form `JIRA-456`. Example config: ```yaml gui: branchColorPatterns: 'docs/.+': 'black' # make all branches prefixed with docs/ have a black color 'feature/collapse-all': 'red' # make a specfic branch name red 'IDEA-\d+': 'blue' # make all branches with the prefix `IDEA-` followed by a digit, blue ```
2 parents 6da99a4 + c64a790 commit a1a8cd1

File tree

7 files changed

+58
-20
lines changed

7 files changed

+58
-20
lines changed

docs/Config.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,14 +832,17 @@ gui:
832832

833833
## Custom Branch Color
834834

835-
You can customize the color of branches based on the branch prefix:
835+
You can customize the color of branches based on branch patterns (regular expressions):
836836

837837
```yaml
838838
gui:
839-
branchColors:
840-
'docs': '#11aaff' # use a light blue for branches beginning with 'docs/'
839+
branchColorPatterns:
840+
'^docs/': '#11aaff' # use a light blue for branches beginning with 'docs/'
841+
'ISSUE-\d+': '#ff5733' # use a bright orange for branches containing 'ISSUE-<some-number>'
841842
```
842843

844+
Note that the regular expressions are not implicitly anchored to the beginning/end of the branch name. If you want to do that, add leading `^` and/or trailing `$` as needed.
845+
843846
## Example Coloring
844847

845848
![border example](../../assets/colored-border-example.png)

pkg/config/user_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ 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+
// Deprecated: use branchColorPatterns instead
5556
BranchColors map[string]string `yaml:"branchColors"`
57+
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color
58+
BranchColorPatterns map[string]string `yaml:"branchColorPatterns"`
5659
// The number of lines you scroll by when scrolling the main window
5760
ScrollHeight int `yaml:"scrollHeight" jsonschema:"minimum=1"`
5861
// 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
@@ -455,7 +455,13 @@ func (gui *Gui) onUserConfigLoaded() error {
455455
} else if userConfig.Gui.ShowIcons {
456456
icons.SetNerdFontsVersion("2")
457457
}
458-
presentation.SetCustomBranches(userConfig.Gui.BranchColors)
458+
459+
if len(userConfig.Gui.BranchColorPatterns) > 0 {
460+
presentation.SetCustomBranches(userConfig.Gui.BranchColorPatterns, true)
461+
} else {
462+
// Fall back to the deprecated branchColors config
463+
presentation.SetCustomBranches(userConfig.Gui.BranchColors, false)
464+
}
459465

460466
return nil
461467
}

pkg/gui/presentation/branches.go

Lines changed: 32 additions & 16 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 the deprecated branchColors config is removed and only regex color patterns are used
25+
}
26+
27+
var colorPatterns *colorMatcher
2228

2329
func GetBranchListDisplayStrings(
2430
branches []*models.Branch,
@@ -125,22 +131,29 @@ 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 := colorPatterns.match(name); ok {
135+
return *style
132136
}
133137

134-
switch branchType {
135-
case "feature":
136-
return style.FgGreen
137-
case "bugfix":
138-
return style.FgYellow
139-
case "hotfix":
140-
return style.FgRed
141-
default:
142-
return theme.DefaultTextColor
138+
return theme.DefaultTextColor
139+
}
140+
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+
}
143154
}
155+
156+
return nil, false
144157
}
145158

146159
func BranchStatus(
@@ -189,6 +202,9 @@ func BranchStatus(
189202
return result
190203
}
191204

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

pkg/gui/presentation/branches_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ func Test_getBranchDisplayStrings(t *testing.T) {
321321
defer color.ForceSetColorLevel(oldColorLevel)
322322

323323
c := utils.NewDummyCommon()
324+
SetCustomBranches(c.UserConfig().Gui.BranchColorPatterns, true)
324325

325326
for i, s := range scenarios {
326327
icons.SetNerdFontsVersion(lo.Ternary(s.useIcons, "3", ""))

pkg/i18n/english.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,8 @@ keybinding:
20062006
gui:
20072007
filterMode: 'fuzzy'
20082008
`,
2009+
"0.44.0": `- The gui.branchColors config option is deprecated; it will be removed in a future version. Please use gui.branchColorPatterns instead.
2010+
- The automatic coloring of branches starting with "feature/", "bugfix/", or "hotfix/" has been removed; if you want this, it's easy to set up using the new gui.branchColorPatterns option.`,
20092011
},
20102012
}
20112013
}

schema/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
"description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-author-color"
1313
},
1414
"branchColors": {
15+
"additionalProperties": {
16+
"type": "string"
17+
},
18+
"type": "object",
19+
"description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color\nDeprecated: use branchColorPatterns instead"
20+
},
21+
"branchColorPatterns": {
1522
"additionalProperties": {
1623
"type": "string"
1724
},

0 commit comments

Comments
 (0)