Skip to content

Commit

Permalink
Avoid color hardcoding and use the ones in skin.yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
placintaalexandru committed Jun 8, 2023
1 parent c4ef600 commit 7483a68
Show file tree
Hide file tree
Showing 34 changed files with 168 additions and 239 deletions.
66 changes: 52 additions & 14 deletions internal/config/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ type (

// Body tracks body styles.
Body struct {
FgColor Color `yaml:"fgColor"`
BgColor Color `yaml:"bgColor"`
LogoColor Color `yaml:"logoColor"`
LogoColorMsg Color `yaml:"logoColorMsg"`
LogoColorInfo Color `yaml:"logoColorInfo"`
LogoColorWarn Color `yaml:"logoColorWarn"`
LogoColorError Color `yaml:"logoColorError"`
FgColor Color `yaml:"fgColor"`
BgColor Color `yaml:"bgColor"`
LogoColor Color `yaml:"logoColor"`
LogoColorMsg Color `yaml:"logoColorMsg"`
LogoColorInfo Color `yaml:"logoColorInfo"`
LogoColorWarn Color `yaml:"logoColorWarn"`
LogoColorError Color `yaml:"logoColorError"`
FlashColorOk Color `yaml:"flashColorOk"`
FlashColorWarn Color `yaml:"flashColorWarn"`
FlashColorError Color `yaml:"flashColorError"`
}

// Dialog tracks dialog styles.
Expand All @@ -80,6 +83,7 @@ type (
ButtonFocusBgColor Color `yaml:"buttonFocusBgColor"`
LabelFgColor Color `yaml:"labelFgColor"`
FieldFgColor Color `yaml:"fieldFgColor"`
ErrorColor Color `yaml:"errorColor"`
}

// Frame tracks frame styles.
Expand All @@ -97,6 +101,7 @@ type (
Xray Xray `yaml:"xray"`
Charts Charts `yaml:"charts"`
Yaml Yaml `yaml:"yaml"`
Picker Picker `yaml:"picker"`
Log Log `yaml:"logs"`
}

Expand All @@ -112,6 +117,12 @@ type (
CompletedColor Color `yaml:"completedColor"`
}

// Picker tracks color when selecting containers
Picker struct {
MainColor Color `yaml:"mainColor"`
FocusColor Color `yaml:"focusColor"`
}

// Log tracks Log styles.
Log struct {
FgColor Color `yaml:"fgColor"`
Expand Down Expand Up @@ -279,6 +290,7 @@ func newDialog() Dialog {
ButtonFocusFgColor: "black",
LabelFgColor: "white",
FieldFgColor: "white",
ErrorColor: "orangered",
}
}

Expand Down Expand Up @@ -310,6 +322,7 @@ func newViews() Views {
Xray: newXray(),
Charts: newCharts(),
Yaml: newYaml(),
Picker: newPicker(),
Log: newLog(),
}
}
Expand All @@ -336,13 +349,16 @@ func newHelp() Help {

func newBody() Body {
return Body{
FgColor: "cadetblue",
BgColor: "black",
LogoColor: "orange",
LogoColorMsg: "white",
LogoColorInfo: "green",
LogoColorWarn: "mediumvioletred",
LogoColorError: "red",
FgColor: "cadetblue",
BgColor: "black",
LogoColor: "orange",
LogoColorMsg: "white",
LogoColorInfo: "green",
LogoColorWarn: "mediumvioletred",
LogoColorError: "red",
FlashColorOk: "green",
FlashColorWarn: "yellow",
FlashColorError: "red",
}
}

Expand All @@ -359,6 +375,13 @@ func newStatus() Status {
}
}

func newPicker() Picker {
return Picker{
MainColor: "white",
FocusColor: "aqua",
}
}

func newLog() Log {
return Log{
FgColor: "lightskyblue",
Expand Down Expand Up @@ -480,6 +503,21 @@ func (s *Styles) BgColor() tcell.Color {
return s.Body().BgColor.Color()
}

// FlashColorOk returns the color in combination with happy emoji.
func (s *Styles) FlashColorOk() tcell.Color {
return s.Body().FlashColorOk.Color()
}

// FlashColorWarn returns the color in combination with warn emoji.
func (s *Styles) FlashColorWarn() tcell.Color {
return s.Body().FlashColorWarn.Color()
}

// FlashColorWarn returns the color in combination with angry emoji.
func (s *Styles) FlashColorError() tcell.Color {
return s.Body().FlashColorError.Color()
}

// AddListener registers a new listener.
func (s *Styles) AddListener(l StyleListener) {
s.listeners = append(s.listeners, l)
Expand Down
8 changes: 1 addition & 7 deletions internal/render/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/tcell/v2"
"github.com/derailed/tview"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -30,12 +29,7 @@ type Benchmark struct {

// ColorerFunc colors a resource row.
func (b Benchmark) ColorerFunc() ColorerFunc {
return func(ns string, h Header, re RowEvent) tcell.Color {
if !Happy(ns, h, re.Row) {
return ErrColor
}
return tcell.ColorPaleGreen
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 1 addition & 4 deletions internal/render/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"

"github.com/derailed/tcell/v2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -19,9 +18,7 @@ func (Dir) IsGeneric() bool {

// ColorerFunc colors a resource row.
func (Dir) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorCadetBlue
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
9 changes: 1 addition & 8 deletions internal/render/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strconv"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/tcell/v2"
"helm.sh/helm/v3/pkg/release"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -22,13 +21,7 @@ func (Helm) IsGeneric() bool {

// ColorerFunc colors a resource row.
func (Helm) ColorerFunc() ColorerFunc {
return func(ns string, h Header, re RowEvent) tcell.Color {
if !Happy(ns, h, re.Row) {
return ErrColor
}

return tcell.ColorMediumSpringGreen
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 1 addition & 4 deletions internal/render/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/tcell/v2"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -31,9 +30,7 @@ type Policy struct {

// ColorerFunc colors a resource row.
func (Policy) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorMediumSpringGreen
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 1 addition & 4 deletions internal/render/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/tcell/v2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand Down Expand Up @@ -35,9 +34,7 @@ type PortForward struct {

// ColorerFunc colors a resource row.
func (PortForward) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorSkyblue
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 1 addition & 4 deletions internal/render/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/derailed/k9s/internal/client"
"github.com/derailed/tcell/v2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -16,9 +15,7 @@ type Reference struct {

// ColorerFunc colors a resource row.
func (Reference) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorCadetBlue
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 1 addition & 4 deletions internal/render/screen_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"time"

"github.com/derailed/tcell/v2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -18,9 +17,7 @@ type ScreenDump struct {

// ColorerFunc colors a resource row.
func (ScreenDump) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorNavajoWhite
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 1 addition & 4 deletions internal/render/subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package render
import (
"fmt"

"github.com/derailed/tcell/v2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -15,9 +14,7 @@ type Subject struct {

// ColorerFunc colors a resource row.
func (Subject) ColorerFunc() ColorerFunc {
return func(ns string, _ Header, re RowEvent) tcell.Color {
return tcell.ColorMediumSpringGreen
}
return DefaultColorer
}

// Header returns a header row.
Expand Down
5 changes: 2 additions & 3 deletions internal/ui/dialog/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tcell/v2"
"github.com/derailed/tview"
)

Expand All @@ -18,7 +17,7 @@ func ShowError(styles config.Dialog, pages *ui.Pages, msg string) {
SetButtonBackgroundColor(styles.ButtonBgColor.Color()).
SetButtonTextColor(styles.ButtonFgColor.Color()).
SetLabelColor(styles.LabelFgColor.Color()).
SetFieldTextColor(tcell.ColorIndianRed)
SetFieldTextColor(styles.FieldFgColor.Color())
f.AddButton("Dismiss", func() {
dismiss(pages)
})
Expand All @@ -29,7 +28,7 @@ func ShowError(styles config.Dialog, pages *ui.Pages, msg string) {
f.SetFocus(0)
modal := tview.NewModalForm("<error>", f)
modal.SetText(cowTalk(msg))
modal.SetTextColor(tcell.ColorOrangeRed)
modal.SetTextColor(styles.ErrorColor.Color())
modal.SetDoneFunc(func(int, string) {
dismiss(pages)
})
Expand Down
14 changes: 8 additions & 6 deletions internal/ui/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewFlash(app *App) *Flash {
app: app,
TextView: tview.NewTextView(),
}
f.SetTextColor(tcell.ColorAqua)

f.SetDynamicColors(true)
f.SetTextAlign(tview.AlignCenter)
f.SetBorderPadding(0, 0, 1, 1)
Expand Down Expand Up @@ -70,7 +70,7 @@ func (f *Flash) SetMessage(m model.LevelMessage) {
f.Clear()
return
}
f.SetTextColor(flashColor(m.Level))
f.SetTextColor(f.flashColor(m.Level))
f.SetText(f.flashEmoji(m.Level) + " " + m.Text)
}

Expand Down Expand Up @@ -98,14 +98,16 @@ func (f *Flash) flashEmoji(l model.FlashLevel) string {

// Helpers...

func flashColor(l model.FlashLevel) tcell.Color {
func (f *Flash) flashColor(l model.FlashLevel) tcell.Color {
styles := f.app.Styles

// nolint:exhaustive
switch l {
case model.FlashWarn:
return tcell.ColorOrange
return styles.FlashColorWarn()
case model.FlashErr:
return tcell.ColorOrangeRed
return styles.FlashColorError()
default:
return tcell.ColorNavajoWhite
return styles.FlashColorOk()
}
}
3 changes: 1 addition & 2 deletions internal/ui/indicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/tcell/v2"
"github.com/derailed/tview"
)

Expand All @@ -30,7 +29,7 @@ func NewStatusIndicator(app *App, styles *config.Styles) *StatusIndicator {
styles: styles,
}
s.SetTextAlign(tview.AlignCenter)
s.SetTextColor(tcell.ColorWhite)
s.SetTextColor(styles.FgColor())
s.SetBackgroundColor(styles.BgColor())
s.SetDynamicColors(true)
styles.AddListener(&s)
Expand Down
15 changes: 1 addition & 14 deletions internal/ui/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (p *Prompt) BufferActive(activate bool, kind model.BufferKind) {
p.ShowCursor(true)
p.SetBorder(true)
p.SetTextColor(p.styles.FgColor())
p.SetBorderColor(colorFor(kind))
p.SetBorderColor(p.styles.Frame().Border.FgColor.Color().TrueColor())
p.icon = p.iconFor(kind)
p.activate()
return
Expand All @@ -256,16 +256,3 @@ func (p *Prompt) iconFor(k model.BufferKind) rune {
return '🐩'
}
}

// ----------------------------------------------------------------------------
// Helpers...

func colorFor(k model.BufferKind) tcell.Color {
// nolint:exhaustive
switch k {
case model.CommandBuffer:
return tcell.ColorAqua
default:
return tcell.ColorSeaGreen
}
}
Loading

0 comments on commit 7483a68

Please sign in to comment.