Skip to content

Commit

Permalink
allow configuring colors
Browse files Browse the repository at this point in the history
Fixes #174
Fixes #141
  • Loading branch information
tzneal committed Oct 20, 2023
1 parent 06a27fe commit 6c93804
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Usage of ./eks-node-viewer:
Sort order for the nodes, either 'creation' or a label name. The sort order can be controlled by appending =asc or =dsc to the value. (default "creation")
-resources string
List of comma separated resources to monitor (default "cpu")
-style string
Three color to use for styling 'good','ok' and 'bad' values. These are also used in the gradients displayed from bad -> good. (default "#04B575,#FFFF00,#FF0000")
-v Display eks-node-viewer version
-version
Display eks-node-viewer version
Expand Down
4 changes: 4 additions & 0 deletions cmd/eks-node-viewer/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Flags struct {
NodeSelector string
ExtraLabels string
NodeSort string
Style string
Kubeconfig string
Resources string
DisablePricing bool
Expand Down Expand Up @@ -76,6 +77,9 @@ func ParseFlags() (Flags, error) {
nodeSort := cfg.getValue("node-sort", "creation=dsc")
flagSet.StringVar(&flags.NodeSort, "node-sort", nodeSort, "Sort order for the nodes, either 'creation' or a label name. The sort order defaults to ascending and can be controlled by appending =asc or =dsc to the value.")

style := cfg.getValue("style", "#04B575,#FFFF00,#FF0000")
flagSet.StringVar(&flags.Style, "style", style, "Three color to use for styling 'good','ok' and 'bad' values. These are also used in the gradients displayed from bad -> good.")

// flag overrides env. var. and env. var. overrides config file
kubeconfigDefault := getStringEnv("KUBECONFIG", cfg.getValue("kubeconfig", filepath.Join(homeDir, ".kube", "config")))
flagSet.StringVar(&flags.Kubeconfig, "kubeconfig", kubeconfigDefault, "Absolute path to the kubeconfig file")
Expand Down
6 changes: 5 additions & 1 deletion cmd/eks-node-viewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func main() {

defaults.SharedCredentialsFilename()
pprov := pricing.NewStaticProvider()
m := model.NewUIModel(strings.Split(flags.ExtraLabels, ","), flags.NodeSort)
style, err := model.ParseStyle(flags.Style)
if err != nil {
log.Fatalf("creating style, %s", err)
}
m := model.NewUIModel(strings.Split(flags.ExtraLabels, ","), flags.NodeSort, style)
m.SetResources(strings.FieldsFunc(flags.Resources, func(r rune) bool { return r == ',' }))

if !flags.DisablePricing {
Expand Down
30 changes: 30 additions & 0 deletions pkg/model/style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package model

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/lipgloss"
)

type Style struct {
green func(strs ...string) string
yellow func(strs ...string) string
red func(strs ...string) string
gradient progress.Option
}

func ParseStyle(style string) (*Style, error) {
colors := strings.Split(style, ",")
if len(colors) != 3 {
return nil, fmt.Errorf("three colors must be provided for the style, found %d (%q)", len(colors), style)
}
s := &Style{}
s.green = lipgloss.NewStyle().Foreground(lipgloss.Color(colors[0])).Render
s.yellow = lipgloss.NewStyle().Foreground(lipgloss.Color(colors[1])).Render
s.red = lipgloss.NewStyle().Foreground(lipgloss.Color(colors[2])).Render

s.gradient = progress.WithGradient(colors[2], colors[0])
return s, nil
}
16 changes: 7 additions & 9 deletions pkg/model/uimodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ type UIModel struct {
paginator paginator.Model
height int
nodeSorter func(lhs, rhs *Node) bool
style *Style
}

func NewUIModel(extraLabels []string, nodeSort string) *UIModel {
func NewUIModel(extraLabels []string, nodeSort string, style *Style) *UIModel {
pager := paginator.New()
pager.Type = paginator.Dots
pager.ActiveDot = activeDot
pager.InactiveDot = inactiveDot
return &UIModel{
// red to green
progress: progress.New(progress.WithGradient("#ff0000", "#04B575")),
progress: progress.New(style.gradient),
cluster: NewCluster(),
extraLabels: extraLabels,
paginator: pager,
nodeSorter: makeNodeSorter(nodeSort),
style: style,
}
}

Expand All @@ -73,10 +75,6 @@ func (u *UIModel) Init() tea.Cmd {
return nil
}

var green = lipgloss.NewStyle().Foreground(lipgloss.Color("#04B575")).Render
var yellow = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFF00")).Render
var red = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")).Render

func (u *UIModel) View() string {
b := strings.Builder{}

Expand Down Expand Up @@ -209,11 +207,11 @@ func (u *UIModel) writeClusterSummary(resources []v1.ResourceName, stats Stats,
}
pctUsedStr := fmt.Sprintf("%0.1f%%", pctUsed)
if pctUsed > 90 {
pctUsedStr = green(pctUsedStr)
pctUsedStr = u.style.green(pctUsedStr)
} else if pctUsed > 60 {
pctUsedStr = yellow(pctUsedStr)
pctUsedStr = u.style.yellow(pctUsedStr)
} else {
pctUsedStr = red(pctUsedStr)
pctUsedStr = u.style.red(pctUsedStr)
}

u.progress.ShowPercentage = false
Expand Down

0 comments on commit 6c93804

Please sign in to comment.