Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to configure branch color patterns #4130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,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
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,13 @@ func (gui *Gui) onUserConfigLoaded() error {
} else if userConfig.Gui.ShowIcons {
icons.SetNerdFontsVersion("2")
}
presentation.SetCustomBranches(userConfig.Gui.BranchColors)

if userConfig.Gui.BranchColorPatterns != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to make a difference in practice, but I'd find it clearer to say 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
}
Expand Down
41 changes: 34 additions & 7 deletions pkg/gui/presentation/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package presentation

import (
"fmt"
"regexp"
"strings"
"time"

Expand All @@ -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 branchPrefixColorCache *colorMatcher

func GetBranchListDisplayStrings(
branches []*models.Branch,
Expand Down Expand Up @@ -125,12 +131,12 @@ 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 := branchPrefixColorCache.match(name); ok {
return *style
}

// Default colors for common branch types
branchType := strings.Split(name, "/")[0]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping the old default behavior

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jesseduffield How do you feel about removing this default behavior? I find it surprising and annoying. I don't want it, but it's not obvious how to disable it; previously I had to use this to disable it:

gui:
  branchColors:
    feature: default
    bugfix: default
    hotfix: default

but if I want to use the new regex patterns now, I have to say

gui:
  branchColorPatterns:
    "mypattern": "#ff0000"
    "^feature/": default
    "^bugfix/": default
    "^hotfix/": default

This is very non-obvious, and I feel like I shouldn't have to do that.

switch branchType {
case "feature":
return style.FgGreen
Expand All @@ -143,6 +149,24 @@ func GetBranchTextStyle(name string) style.TextStyle {
}
}

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the implicit anchoring surprising, and would remove it. If users want it, they can easily add it themselves. For the record, we don't use it for commit prefix patterns either.

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(
branch *models.Branch,
itemOperation types.ItemOperation,
Expand Down Expand Up @@ -189,6 +213,9 @@ func BranchStatus(
return result
}

func SetCustomBranches(customBranchColors map[string]string) {
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
func SetCustomBranches(customBranchColors map[string]string, isRegex bool) {
branchPrefixColorCache = &colorMatcher{
patterns: utils.SetCustomColors(customBranchColors),
isRegex: isRegex,
}
}
Loading