@@ -2,6 +2,7 @@ package presentation
22
33import (
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
2329func GetBranchListDisplayStrings (
2430 branches []* models.Branch ,
@@ -125,12 +131,12 @@ func getBranchDisplayStrings(
125131
126132// GetBranchTextStyle branch color
127133func 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+
146170func 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