From 02fef3136f27a0a93352ee62f47d4924e54389dd Mon Sep 17 00:00:00 2001 From: mjarkk Date: Fri, 18 Oct 2019 09:48:37 +0200 Subject: [PATCH] Added light theme option to the settings --- docs/Config.md | 1 + pkg/commands/branch.go | 4 ++- pkg/commands/commit.go | 7 ++-- pkg/config/app_config.go | 1 + pkg/gui/confirmation_panel.go | 3 +- pkg/gui/gui.go | 24 ++++++------- pkg/gui/menu_panel.go | 3 +- pkg/gui/merge_panel.go | 3 +- pkg/gui/theme.go | 14 ++++---- pkg/theme/theme.go | 68 +++++++++++++++++++++++++++++++++++ 10 files changed, 103 insertions(+), 25 deletions(-) create mode 100644 pkg/theme/theme.go diff --git a/docs/Config.md b/docs/Config.md index d9c3e2369..57077e852 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -8,6 +8,7 @@ scrollHeight: 2 # how many lines you scroll by scrollPastBottom: true # enable scrolling past the bottom theme: + lightTheme: false # For terminals with a light background activeBorderColor: - white - bold diff --git a/pkg/commands/branch.go b/pkg/commands/branch.go index cbcab2712..d1cafdfb4 100644 --- a/pkg/commands/branch.go +++ b/pkg/commands/branch.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" + "github.com/jesseduffield/lazygit/pkg/theme" + "github.com/fatih/color" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -38,7 +40,7 @@ func (b *Branch) GetColor() color.Attribute { case "hotfix": return color.FgRed default: - return color.FgWhite + return theme.DefaultTextColor } } diff --git a/pkg/commands/commit.go b/pkg/commands/commit.go index e8a0a52e7..4102af4c0 100644 --- a/pkg/commands/commit.go +++ b/pkg/commands/commit.go @@ -2,6 +2,7 @@ package commands import ( "github.com/fatih/color" + "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -22,7 +23,7 @@ func (c *Commit) GetDisplayStrings(isFocused bool) []string { green := color.New(color.FgGreen) blue := color.New(color.FgBlue) cyan := color.New(color.FgCyan) - white := color.New(color.FgWhite) + defaultColor := color.New(theme.DefaultTextColor) magenta := color.New(color.FgMagenta) // for some reason, setting the background to blue pads out the other commits @@ -43,7 +44,7 @@ func (c *Commit) GetDisplayStrings(isFocused bool) []string { case "selected": shaColor = magenta default: - shaColor = white + shaColor = defaultColor } if c.Copied { @@ -55,5 +56,5 @@ func (c *Commit) GetDisplayStrings(isFocused bool) []string { actionString = cyan.Sprint(utils.WithPadding(c.Action, 7)) + " " } - return []string{shaColor.Sprint(c.Sha), actionString + white.Sprint(c.Name)} + return []string{shaColor.Sprint(c.Sha), actionString + defaultColor.Sprint(c.Name)} } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 50cb68056..ca17efd3d 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -244,6 +244,7 @@ func GetDefaultConfig() []byte { scrollPastBottom: true mouseEvents: false # will default to true when the feature is complete theme: + lightTheme: false activeBorderColor: - white - bold diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 51ad711cb..bffda8517 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -12,6 +12,7 @@ import ( "github.com/fatih/color" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/theme" ) func (gui *Gui) wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.View) error) func(*gocui.Gui, *gocui.View) error { @@ -81,7 +82,7 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt confirmationView.HasLoader = hasLoader confirmationView.Title = title confirmationView.Wrap = true - confirmationView.FgColor = gocui.ColorWhite + confirmationView.FgColor = theme.GocuiDefaultTextColor } gui.g.Update(func(g *gocui.Gui) error { return gui.switchFocus(gui.g, currentView, confirmationView) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 20c247fa1..6430b19f7 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -24,6 +24,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/updates" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" @@ -374,6 +375,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { _, _ = g.SetViewOnBottom("limit") g.DeleteView("limit") + textColor := theme.GocuiDefaultTextColor v, err := g.SetView("main", leftSideWidth+panelSpacing, 0, width-1, height-2, gocui.LEFT) if err != nil { if err.Error() != "unknown view" { @@ -381,7 +383,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } v.Title = gui.Tr.SLocalize("DiffTitle") v.Wrap = true - v.FgColor = gocui.ColorWhite + v.FgColor = textColor } if v, err := g.SetView("status", 0, 0, leftSideWidth, vHeights["status"]-1, gocui.BOTTOM|gocui.RIGHT); err != nil { @@ -389,7 +391,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } v.Title = gui.Tr.SLocalize("StatusTitle") - v.FgColor = gocui.ColorWhite + v.FgColor = textColor } filesView, err := g.SetViewBeneath("files", "status", vHeights["files"]) @@ -399,7 +401,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } filesView.Highlight = true filesView.Title = gui.Tr.SLocalize("FilesTitle") - v.FgColor = gocui.ColorWhite + v.FgColor = textColor } branchesView, err := g.SetViewBeneath("branches", "files", vHeights["branches"]) @@ -408,7 +410,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } branchesView.Title = gui.Tr.SLocalize("BranchesTitle") - branchesView.FgColor = gocui.ColorWhite + branchesView.FgColor = textColor } if v, err := g.SetViewBeneath("commitFiles", "branches", vHeights["commits"]); err != nil { @@ -416,7 +418,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } v.Title = gui.Tr.SLocalize("CommitFiles") - v.FgColor = gocui.ColorWhite + v.FgColor = textColor } commitsView, err := g.SetViewBeneath("commits", "branches", vHeights["commits"]) @@ -425,7 +427,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } commitsView.Title = gui.Tr.SLocalize("CommitsTitle") - commitsView.FgColor = gocui.ColorWhite + commitsView.FgColor = textColor } stashView, err := g.SetViewBeneath("stash", "commits", vHeights["stash"]) @@ -434,7 +436,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } stashView.Title = gui.Tr.SLocalize("StashTitle") - stashView.FgColor = gocui.ColorWhite + stashView.FgColor = textColor } if v, err := g.SetView("options", appStatusOptionsBoundary-1, height-2, optionsVersionBoundary-1, height, 0); err != nil { @@ -442,9 +444,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } v.Frame = false - if v.FgColor, err = gui.GetOptionsPanelTextColor(); err != nil { - return err - } + v.FgColor = gui.GetOptionsPanelTextColor() } if gui.getCommitMessageView() == nil { @@ -455,7 +455,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } g.SetViewOnBottom("commitMessage") commitMessageView.Title = gui.Tr.SLocalize("CommitMessage") - commitMessageView.FgColor = gocui.ColorWhite + commitMessageView.FgColor = textColor commitMessageView.Editable = true } } @@ -471,7 +471,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { return err } credentialsView.Title = gui.Tr.SLocalize("CredentialsUsername") - credentialsView.FgColor = gocui.ColorWhite + credentialsView.FgColor = textColor credentialsView.Editable = true } } diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index 66dabd03e..2d08a010b 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -62,7 +63,7 @@ func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handl x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, false, list) menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0) menuView.Title = title - menuView.FgColor = gocui.ColorWhite + menuView.FgColor = theme.GocuiDefaultTextColor menuView.Clear() fmt.Fprint(menuView, list) gui.State.Panels.Menu.SelectedLine = 0 diff --git a/pkg/gui/merge_panel.go b/pkg/gui/merge_panel.go index 1b4f1a88f..5b08ddbdc 100644 --- a/pkg/gui/merge_panel.go +++ b/pkg/gui/merge_panel.go @@ -14,6 +14,7 @@ import ( "github.com/golang-collections/collections/stack" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -50,7 +51,7 @@ func (gui *Gui) coloredConflictFile(content string, conflicts []commands.Conflic conflict, remainingConflicts := gui.shiftConflict(conflicts) var outputBuffer bytes.Buffer for i, line := range utils.SplitLines(content) { - colourAttr := color.FgWhite + colourAttr := theme.DefaultTextColor if i == conflict.Start || i == conflict.Middle || i == conflict.End { colourAttr = color.FgRed } diff --git a/pkg/gui/theme.go b/pkg/gui/theme.go index 1f87e325e..d2341bbf3 100644 --- a/pkg/gui/theme.go +++ b/pkg/gui/theme.go @@ -2,6 +2,7 @@ package gui import ( "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/theme" ) // GetAttribute gets the gocui color attribute from the string @@ -37,18 +38,19 @@ func (gui *Gui) GetColor(keys []string) gocui.Attribute { } // GetOptionsPanelTextColor gets the color of the options panel text -func (gui *Gui) GetOptionsPanelTextColor() (gocui.Attribute, error) { +func (gui *Gui) GetOptionsPanelTextColor() gocui.Attribute { userConfig := gui.Config.GetUserConfig() optionsColor := userConfig.GetStringSlice("gui.theme.optionsTextColor") - return gui.GetColor(optionsColor), nil + return gui.GetColor(optionsColor) } // SetColorScheme sets the color scheme for the app based on the user config func (gui *Gui) SetColorScheme() error { userConfig := gui.Config.GetUserConfig() - activeBorderColor := userConfig.GetStringSlice("gui.theme.activeBorderColor") - inactiveBorderColor := userConfig.GetStringSlice("gui.theme.inactiveBorderColor") - gui.g.FgColor = gui.GetColor(inactiveBorderColor) - gui.g.SelFgColor = gui.GetColor(activeBorderColor) + theme.UpdateTheme(userConfig) + + gui.g.FgColor = theme.InactiveBorderColor + gui.g.SelFgColor = theme.ActiveBorderColor + return nil } diff --git a/pkg/theme/theme.go b/pkg/theme/theme.go new file mode 100644 index 000000000..4b2c0e9b2 --- /dev/null +++ b/pkg/theme/theme.go @@ -0,0 +1,68 @@ +package theme + +import ( + "github.com/fatih/color" + "github.com/jesseduffield/gocui" + "github.com/spf13/viper" +) + +var ( + // DefaultTextColor is the default text color + DefaultTextColor = color.FgWhite + + // GocuiDefaultTextColor does the same as DefaultTextColor but this one only colors gocui default text colors + GocuiDefaultTextColor gocui.Attribute + + // ActiveBorderColor is the border color of the active frame + ActiveBorderColor gocui.Attribute + + // InactiveBorderColor is the border color of the inactive active frames + InactiveBorderColor gocui.Attribute +) + +// UpdateTheme updates all theme variables +func UpdateTheme(userConfig *viper.Viper) { + ActiveBorderColor = getColor(userConfig.GetStringSlice("gui.theme.activeBorderColor")) + InactiveBorderColor = getColor(userConfig.GetStringSlice("gui.theme.inactiveBorderColor")) + + isLightTheme := userConfig.GetBool("gui.theme.lightTheme") + if isLightTheme { + DefaultTextColor = color.FgBlack + GocuiDefaultTextColor = gocui.ColorBlack + } else { + DefaultTextColor = color.FgWhite + GocuiDefaultTextColor = gocui.ColorWhite + } +} + +// getAttribute gets the gocui color attribute from the string +func getAttribute(key string) gocui.Attribute { + colorMap := map[string]gocui.Attribute{ + "default": gocui.ColorDefault, + "black": gocui.ColorBlack, + "red": gocui.ColorRed, + "green": gocui.ColorGreen, + "yellow": gocui.ColorYellow, + "blue": gocui.ColorBlue, + "magenta": gocui.ColorMagenta, + "cyan": gocui.ColorCyan, + "white": gocui.ColorWhite, + "bold": gocui.AttrBold, + "reverse": gocui.AttrReverse, + "underline": gocui.AttrUnderline, + } + value, present := colorMap[key] + if present { + return value + } + return gocui.ColorWhite +} + +// getColor bitwise OR's a list of attributes obtained via the given keys +func getColor(keys []string) gocui.Attribute { + var attribute gocui.Attribute + for _, key := range keys { + attribute |= getAttribute(key) + } + return attribute +}