@@ -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
25+ }
26+
27+ var branchPrefixColorCache * colorMatcher
2228
2329func GetBranchListDisplayStrings (
2430 branches []* models.Branch ,
@@ -123,31 +129,14 @@ func getBranchDisplayStrings(
123129 return res
124130}
125131
126- func matchColorPrefix (name string ) (style.TextStyle , bool ) {
127- for possiblePattern , color := range branchPrefixColorCache {
128- if strings .Contains (possiblePattern , "*" ) {
129- prefix := strings .TrimSuffix (possiblePattern , "*" )
130- if strings .HasPrefix (name , prefix ) {
131- return color , true
132- }
133- }
134- }
135-
136- return style.TextStyle {}, false
137- }
138-
139132// GetBranchTextStyle branch color
140133func GetBranchTextStyle (name string ) style.TextStyle {
141- branchType := strings .Split (name , "/" )[0 ]
142-
143- if value , ok := branchPrefixColorCache [branchType ]; ok {
144- return value
145- }
146-
147- if value , ok := matchColorPrefix (name ); ok {
148- return value
134+ if style , ok := branchPrefixColorCache .match (name ); ok {
135+ return * style
149136 }
150137
138+ // Default colors for common branch types
139+ branchType := strings .Split (name , "/" )[0 ]
151140 switch branchType {
152141 case "feature" :
153142 return style .FgGreen
@@ -160,6 +149,23 @@ func GetBranchTextStyle(name string) style.TextStyle {
160149 }
161150}
162151
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+ branchType := strings .Split (name , "/" )[0 ]
161+ if value , ok := m .patterns [branchType ]; ok {
162+ return & value , true
163+ }
164+ }
165+
166+ return nil , false
167+ }
168+
163169func BranchStatus (
164170 branch * models.Branch ,
165171 itemOperation types.ItemOperation ,
@@ -206,6 +212,9 @@ func BranchStatus(
206212 return result
207213}
208214
209- func SetCustomBranches (customBranchColors map [string ]string ) {
210- branchPrefixColorCache = utils .SetCustomColors (customBranchColors )
215+ func SetCustomBranches (customBranchColors map [string ]string , isRegex bool ) {
216+ branchPrefixColorCache = & colorMatcher {
217+ patterns : utils .SetCustomColors (customBranchColors ),
218+ isRegex : isRegex ,
219+ }
211220}
0 commit comments