From 284c53425102dba89164aa4363441caf71402114 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 18 Aug 2018 13:53:58 +1000 Subject: [PATCH] user configurable border colors --- pkg/config/app_config.go | 5 +++++ pkg/gui/gui.go | 5 +++-- pkg/gui/theme.go | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 pkg/gui/theme.go diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index c9ab1b1d0..6276a6afa 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -150,6 +150,11 @@ func getDefaultConfig() []byte { gui: ## stuff relating to the UI scrollHeight: 2 + activeBorderColor: + - white + - bold + inactiveBorderColor: + - white git: # stuff relating to git os: diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 21a7f5ebc..71ba255a8 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -140,7 +140,6 @@ func max(a, b int) int { // layout is called for every screen re-render e.g. when the screen is resized func (gui *Gui) layout(g *gocui.Gui) error { g.Highlight = true - g.SelFgColor = gocui.ColorWhite | gocui.AttrBold width, height := g.Size() leftSideWidth := width / 3 statusFilesBoundary := 2 @@ -315,7 +314,9 @@ func (gui *Gui) Run() error { gui.g = g // TODO: always use gui.g rather than passing g around everywhere - g.FgColor = gocui.ColorDefault + if err := gui.SetColorScheme(); err != nil { + return err + } gui.goEvery(g, time.Second*60, gui.fetch) gui.goEvery(g, time.Second*10, gui.refreshFiles) diff --git a/pkg/gui/theme.go b/pkg/gui/theme.go new file mode 100644 index 000000000..25f5f8dc0 --- /dev/null +++ b/pkg/gui/theme.go @@ -0,0 +1,46 @@ +package gui + +import ( + "github.com/jesseduffield/gocui" +) + +// GetAttribute gets the gocui color attribute from the string +func (gui *Gui) 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 (gui *Gui) GetColor(keys []string) gocui.Attribute { + var attribute gocui.Attribute + for _, key := range keys { + attribute = attribute | gui.GetAttribute(key) + } + return attribute +} + +// SetColorScheme sets the color scheme for the app based on the user config +func (gui *Gui) SetColorScheme() error { + activeBorderColor := gui.Config.GetUserConfig().GetStringSlice("gui.activeBorderColor") + inactiveBorderColor := gui.Config.GetUserConfig().GetStringSlice("gui.inactiveBorderColor") + gui.g.FgColor = gui.GetColor(inactiveBorderColor) + gui.g.SelFgColor = gui.GetColor(activeBorderColor) + return nil +}