From 79848087bccd5c87af1dbb44a39753aad1346f8b Mon Sep 17 00:00:00 2001 From: mjarkk Date: Tue, 27 Jul 2021 15:00:37 +0200 Subject: Switch to github.com/gookit/color for terminal colors --- pkg/theme/theme.go | 137 ++++++++++-------------------------------------- pkg/theme/theme_test.go | 63 ---------------------- 2 files changed, 27 insertions(+), 173 deletions(-) delete mode 100644 pkg/theme/theme_test.go (limited to 'pkg/theme') diff --git a/pkg/theme/theme.go b/pkg/theme/theme.go index 218ee4b19..0eafa84b2 100644 --- a/pkg/theme/theme.go +++ b/pkg/theme/theme.go @@ -1,19 +1,14 @@ package theme import ( - "encoding/hex" - - "github.com/fatih/color" + "github.com/gookit/color" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/gui/style" + "github.com/jesseduffield/lazygit/pkg/utils" ) var ( - // DefaultTextColor is the default text color - DefaultTextColor = color.FgWhite - // DefaultHiTextColor is the default highlighted text color - DefaultHiTextColor = color.FgHiWhite - // GocuiDefaultTextColor does the same as DefaultTextColor but this one only colors gocui default text colors GocuiDefaultTextColor gocui.Attribute @@ -23,69 +18,55 @@ var ( // InactiveBorderColor is the border color of the inactive active frames InactiveBorderColor gocui.Attribute - // SelectedLineBgColor is the background color for the selected line - SelectedLineBgColor color.Attribute - - // SelectedRangeBgColor is the background color of the selected range of lines - SelectedRangeBgColor color.Attribute - // GocuiSelectedLineBgColor is the background color for the selected line in gocui GocuiSelectedLineBgColor gocui.Attribute - OptionsFgColor color.Attribute - OptionsColor gocui.Attribute - DiffTerminalColor = color.FgMagenta + // DefaultTextColor is the default text color + DefaultTextColor = style.New(color.FgWhite, 0) + + // DefaultHiTextColor is the default highlighted text color + DefaultHiTextColor = style.New(color.FgLightWhite, 0) + + // SelectedLineBgColor is the background color for the selected line + SelectedLineBgColor = style.New(0, 0) + + // SelectedRangeBgColor is the background color of the selected range of lines + SelectedRangeBgColor = style.New(0, 0) + + OptionsFgColor = style.New(0, 0) + + DiffTerminalColor = style.New(color.FgMagenta, 0) ) // UpdateTheme updates all theme variables func UpdateTheme(themeConfig config.ThemeConfig) { ActiveBorderColor = GetGocuiColor(themeConfig.ActiveBorderColor) InactiveBorderColor = GetGocuiColor(themeConfig.InactiveBorderColor) - SelectedLineBgColor = GetBgColor(themeConfig.SelectedLineBgColor) - SelectedRangeBgColor = GetBgColor(themeConfig.SelectedRangeBgColor) + SelectedLineBgColor = style.SetConfigStyles(SelectedLineBgColor, themeConfig.SelectedLineBgColor, true) + SelectedRangeBgColor = style.SetConfigStyles(SelectedRangeBgColor, themeConfig.SelectedRangeBgColor, true) GocuiSelectedLineBgColor = GetGocuiColor(themeConfig.SelectedLineBgColor) OptionsColor = GetGocuiColor(themeConfig.OptionsTextColor) - OptionsFgColor = GetFgColor(themeConfig.OptionsTextColor) + OptionsFgColor = style.SetConfigStyles(OptionsFgColor, themeConfig.OptionsTextColor, false) isLightTheme := themeConfig.LightTheme if isLightTheme { - DefaultTextColor = color.FgBlack - DefaultHiTextColor = color.FgHiBlack + DefaultTextColor = style.FgBlack + DefaultHiTextColor = style.FgBlackLighter GocuiDefaultTextColor = gocui.ColorBlack } else { - DefaultTextColor = color.FgWhite - DefaultHiTextColor = color.FgHiWhite + DefaultTextColor = style.FgWhite + DefaultHiTextColor = style.FgLightWhite GocuiDefaultTextColor = gocui.ColorWhite } } -// getHexColorValues returns the rgb values of a hex color -func getHexColorValues(v string) (r int32, g int32, b int32, valid bool) { - if len(v) == 4 { - v = string([]byte{v[0], v[1], v[1], v[2], v[2], v[3], v[3]}) - } else if len(v) != 7 { - return - } - - if v[0] != '#' { - return - } - - rgb, err := hex.DecodeString(v[1:]) - if err != nil { - return - } - - return int32(rgb[0]), int32(rgb[1]), int32(rgb[2]), true -} - // GetAttribute gets the gocui color attribute from the string func GetGocuiAttribute(key string) gocui.Attribute { - r, g, b, validHexColor := getHexColorValues(key) + r, g, b, validHexColor := utils.GetHexColorValues(key) if validHexColor { - return gocui.NewRGBColor(r, g, b) + return gocui.NewRGBColor(int32(r), int32(g), int32(b)) } colorMap := map[string]gocui.Attribute{ @@ -109,52 +90,6 @@ func GetGocuiAttribute(key string) gocui.Attribute { return gocui.ColorWhite } -// GetFgAttribute gets the color foreground attribute from the string -func GetFgAttribute(key string) color.Attribute { - colorMap := map[string]color.Attribute{ - "default": color.FgWhite, - "black": color.FgBlack, - "red": color.FgRed, - "green": color.FgGreen, - "yellow": color.FgYellow, - "blue": color.FgBlue, - "magenta": color.FgMagenta, - "cyan": color.FgCyan, - "white": color.FgWhite, - "bold": color.Bold, - "reverse": color.ReverseVideo, - "underline": color.Underline, - } - value, present := colorMap[key] - if present { - return value - } - return color.FgWhite -} - -// GetBgAttribute gets the color background attribute from the string -func GetBgAttribute(key string) color.Attribute { - colorMap := map[string]color.Attribute{ - "default": color.BgWhite, - "black": color.BgBlack, - "red": color.BgRed, - "green": color.BgGreen, - "yellow": color.BgYellow, - "blue": color.BgBlue, - "magenta": color.BgMagenta, - "cyan": color.BgCyan, - "white": color.BgWhite, - "bold": color.Bold, - "reverse": color.ReverseVideo, - "underline": color.Underline, - } - value, present := colorMap[key] - if present { - return value - } - return color.FgWhite -} - // GetGocuiColor bitwise OR's a list of attributes obtained via the given keys func GetGocuiColor(keys []string) gocui.Attribute { var attribute gocui.Attribute @@ -163,21 +98,3 @@ func GetGocuiColor(keys []string) gocui.Attribute { } return attribute } - -// GetColor bitwise OR's a list of attributes obtained via the given keys -func GetBgColor(keys []string) color.Attribute { - var attribute color.Attribute - for _, key := range keys { - attribute |= GetBgAttribute(key) - } - return attribute -} - -// GetColor bitwise OR's a list of attributes obtained via the given keys -func GetFgColor(keys []string) color.Attribute { - var attribute color.Attribute - for _, key := range keys { - attribute |= GetFgAttribute(key) - } - return attribute -} diff --git a/pkg/theme/theme_test.go b/pkg/theme/theme_test.go deleted file mode 100644 index 311289408..000000000 --- a/pkg/theme/theme_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package theme - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGetHexColorValues(t *testing.T) { - scenarios := []struct { - name string - hexColor string - rgb []int32 - valid bool - }{ - { - name: "valid uppercase hex color", - hexColor: "#00FF00", - rgb: []int32{0, 255, 0}, - valid: true, - }, - { - name: "valid lowercase hex color", - hexColor: "#00ff00", - rgb: []int32{0, 255, 0}, - valid: true, - }, - { - name: "valid short hex color", - hexColor: "#0bf", - rgb: []int32{0, 187, 255}, - valid: true, - }, - { - name: "invalid hex value", - hexColor: "#zz00ff", - valid: false, - }, - { - name: "invalid length hex color", - hexColor: "#", - valid: false, - }, - { - name: "invalid length hex color", - hexColor: "#aaaaaaaaaaa", - valid: false, - }, - } - - for _, s := range scenarios { - s := s - t.Run(s.name, func(t *testing.T) { - r, g, b, valid := getHexColorValues(s.hexColor) - assert.EqualValues(t, s.valid, valid, s.hexColor) - if valid { - assert.EqualValues(t, s.rgb[0], r, s.hexColor) - assert.EqualValues(t, s.rgb[1], g, s.hexColor) - assert.EqualValues(t, s.rgb[2], b, s.hexColor) - } - }) - } -} -- cgit v1.2.3