diff --git a/docs/Config.md b/docs/Config.md index ed39cd4883e..b72d4c8311d 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -836,8 +836,15 @@ You can customize the color of branches based on the branch prefix: ```yaml gui: + # NOTE: that this configuration will be deprecated in favor of using branchColorPatterns below branchColors: 'docs': '#11aaff' # use a light blue for branches beginning with 'docs/' + + # alternatively you can use a regex pattern as your coloring rules + # NOTE: this configuration overwrites the one above, if you would like to set a similar rule see the example below + branchColorPatterns: + 'docs/.+': '#11aaff' # similar to the previous configuration above, setting branches that begin with 'docs/' + 'ISSUE-\d+': '#ff5733' # use a bright orange for branches beginning with 'ISSUE-' ``` ## Example Coloring diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 36dc02a68a7..36230ba85d7 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -52,7 +52,9 @@ type GuiConfig struct { // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-author-color AuthorColors map[string]string `yaml:"authorColors"` // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color - BranchColors map[string]string `yaml:"branchColors"` + // NOTE: BranchColors is being deprecated in favor of BranchColorPatterns + BranchColors map[string]string `yaml:"branchColors"` + BranchColorPatterns map[string]string `yaml:"branchColorPatterns"` // The number of lines you scroll by when scrolling the main window ScrollHeight int `yaml:"scrollHeight" jsonschema:"minimum=1"` // If true, allow scrolling past the bottom of the content in the main window diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 7600c955bbe..dc8ee534a4e 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -454,7 +454,13 @@ func (gui *Gui) onUserConfigLoaded() error { } else if userConfig.Gui.ShowIcons { icons.SetNerdFontsVersion("2") } - presentation.SetCustomBranches(userConfig.Gui.BranchColors) + + if len(userConfig.Gui.BranchColorPatterns) > 0 { + presentation.SetCustomBranches(userConfig.Gui.BranchColorPatterns, true) + } else { + // The alternative is to match on branch types with the branchColors config which will be deprecated in the future + presentation.SetCustomBranches(userConfig.Gui.BranchColors, false) + } return nil } diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index b75dfc95b72..3fd14458ec3 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -2,6 +2,7 @@ package presentation import ( "fmt" + "regexp" "strings" "time" @@ -18,7 +19,12 @@ import ( "github.com/samber/lo" ) -var branchPrefixColorCache = make(map[string]style.TextStyle) +type colorMatcher struct { + patterns map[string]style.TextStyle + isRegex bool // NOTE: this value is needed only until branchColors is deprecated and only regex color patterns are used +} + +var colorPatterns *colorMatcher func GetBranchListDisplayStrings( branches []*models.Branch, @@ -125,22 +131,29 @@ func getBranchDisplayStrings( // GetBranchTextStyle branch color func GetBranchTextStyle(name string) style.TextStyle { - branchType := strings.Split(name, "/")[0] - - if value, ok := branchPrefixColorCache[branchType]; ok { - return value + if style, ok := colorPatterns.match(name); ok { + return *style } - switch branchType { - case "feature": - return style.FgGreen - case "bugfix": - return style.FgYellow - case "hotfix": - return style.FgRed - default: - return theme.DefaultTextColor + return theme.DefaultTextColor +} + +func (m *colorMatcher) match(name string) (*style.TextStyle, bool) { + if m.isRegex { + for pattern, style := range m.patterns { + if matched, _ := regexp.MatchString(pattern, name); matched { + return &style, true + } + } + } else { + // old behavior using the deprecated branchColors behavior matching on branch type + branchType := strings.Split(name, "/")[0] + if value, ok := m.patterns[branchType]; ok { + return &value, true + } } + + return nil, false } func BranchStatus( @@ -189,6 +202,9 @@ func BranchStatus( return result } -func SetCustomBranches(customBranchColors map[string]string) { - branchPrefixColorCache = utils.SetCustomColors(customBranchColors) +func SetCustomBranches(customBranchColors map[string]string, isRegex bool) { + colorPatterns = &colorMatcher{ + patterns: utils.SetCustomColors(customBranchColors), + isRegex: isRegex, + } } diff --git a/pkg/gui/presentation/branches_test.go b/pkg/gui/presentation/branches_test.go index d917846748c..02377e8bcfa 100644 --- a/pkg/gui/presentation/branches_test.go +++ b/pkg/gui/presentation/branches_test.go @@ -321,6 +321,7 @@ func Test_getBranchDisplayStrings(t *testing.T) { defer color.ForceSetColorLevel(oldColorLevel) c := utils.NewDummyCommon() + SetCustomBranches(c.UserConfig().Gui.BranchColorPatterns, true) for i, s := range scenarios { icons.SetNerdFontsVersion(lo.Ternary(s.useIcons, "3", "")) diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index e777bdc9619..65f166d8d9c 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -2006,6 +2006,8 @@ keybinding: gui: filterMode: 'fuzzy' `, + "0.44.0": `- The gui.branchColors config option is deprecated; it will be removed in a future version. Please use gui.branchColorPatterns instead. +- 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.`, }, } } diff --git a/schema/config.json b/schema/config.json index ee6f37ca508..5e66561d23b 100644 --- a/schema/config.json +++ b/schema/config.json @@ -16,7 +16,13 @@ "type": "string" }, "type": "object", - "description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color" + "description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color\nNOTE: BranchColors is being deprecated in favor of BranchColorPatterns" + }, + "branchColorPatterns": { + "additionalProperties": { + "type": "string" + }, + "type": "object" }, "scrollHeight": { "type": "integer",