Skip to content

Commit

Permalink
Add shared function for loading map of custom colors
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-cles authored and jesseduffield committed Feb 1, 2022
1 parent 4df7646 commit 9adf4a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
"github.com/jesseduffield/lazygit/pkg/gui/modes/filtering"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/authors"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/authors"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/graph"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
Expand Down Expand Up @@ -493,7 +493,7 @@ func NewGui(
gui.PopupHandler = &RealPopupHandler{gui: gui}

authors.SetCustomAuthors(gui.UserConfig.Gui.AuthorColors)
presentation.SetCustomBranchColors(gui.UserConfig.Gui.BranchColors)
presentation.SetCustomBranches(gui.UserConfig.Gui.BranchColors)

return gui, nil
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/gui/presentation/authors/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,5 @@ func getFirstRune(str string) rune {
}

func SetCustomAuthors(customAuthorColors map[string]string) {
for authorName, colorSequence := range customAuthorColors {
style := style.New().SetFg(style.NewRGBColor(color.HEX(colorSequence, false)))
authorStyleCache[authorName] = style
}
authorStyleCache = utils.SetCustomColors(customAuthorColors)
}
13 changes: 5 additions & 8 deletions pkg/gui/presentation/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"fmt"
"strings"

"github.com/gookit/color"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils"
)

var branchPrefixColors = make(map[string]style.TextStyle)
var branchPrefixColorCache = make(map[string]style.TextStyle)

func GetBranchListDisplayStrings(branches []*models.Branch, fullDescription bool, diffName string) [][]string {
lines := make([][]string, len(branches))
Expand Down Expand Up @@ -61,7 +61,7 @@ func getBranchDisplayStrings(b *models.Branch, fullDescription bool, diffed bool
func GetBranchTextStyle(name string) style.TextStyle {
branchType := strings.Split(name, "/")[0]

if value, ok := branchPrefixColors[branchType]; ok {
if value, ok := branchPrefixColorCache[branchType]; ok {
return value
}

Expand Down Expand Up @@ -92,9 +92,6 @@ func BranchStatus(branch *models.Branch) string {
return fmt.Sprintf("↑%s↓%s", branch.Pushables, branch.Pullables)
}

func SetCustomBranchColors(customBranchColors map[string]string) {
for branchPrefix, colorSequence := range customBranchColors {
style := style.New().SetFg(style.NewRGBColor(color.HEX(colorSequence, false)))
branchPrefixColors[branchPrefix] = style
}
func SetCustomBranches(customBranchColors map[string]string) {
branchPrefixColorCache = utils.SetCustomColors(customBranchColors)
}
12 changes: 12 additions & 0 deletions pkg/utils/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package utils
import (
"regexp"
"sync"

"github.com/gookit/color"
"github.com/jesseduffield/lazygit/pkg/gui/style"
)

var decoloriseCache = make(map[string]string)
Expand Down Expand Up @@ -48,3 +51,12 @@ func IsValidHexValue(v string) bool {

return true
}

func SetCustomColors(customColors map[string]string) map[string]style.TextStyle {
colors := make(map[string]style.TextStyle)
for key, colorSequence := range customColors {
style := style.New().SetFg(style.NewRGBColor(color.HEX(colorSequence, false)))
colors[key] = style
}
return colors
}

0 comments on commit 9adf4a1

Please sign in to comment.