From fe3ad6697fc81427e456a0b1fa9ea6c0bb73944d Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Thu, 23 Feb 2023 09:32:12 -0800 Subject: [PATCH] update --- internal/ui/app.go | 3 ++- internal/ui/graphs.go | 11 +++++++---- internal/ui/info.go | 7 ++++--- internal/ui/items.go | 16 ++++++++-------- internal/ui/list/list.go | 4 ++-- internal/ui/{ => utils}/utils.go | 21 +++++++++++++-------- 6 files changed, 36 insertions(+), 26 deletions(-) rename internal/ui/{ => utils}/utils.go (79%) diff --git a/internal/ui/app.go b/internal/ui/app.go index 2a9ffe1..2130263 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -10,6 +10,7 @@ import ( "github.com/chriskim06/asciigraph" "github.com/chriskim06/kubectl-topui/internal/config" "github.com/chriskim06/kubectl-topui/internal/metrics" + "github.com/chriskim06/kubectl-topui/internal/ui/utils" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/kubectl/pkg/cmd/top" ) @@ -162,7 +163,7 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (a App) View() string { if a.err != nil { - return lipgloss.Place(a.width, a.height, lipgloss.Center, lipgloss.Center, errStyle.Width(a.width/2).Height(a.height/2).Render("ERROR:\n\n"+a.err.Error())) + return lipgloss.Place(a.width, a.height, lipgloss.Center, lipgloss.Center, utils.ErrStyle.Width(a.width/2).Height(a.height/2).Render("ERROR:\n\n"+a.err.Error())) } if !a.ready || !a.sizeReady { return lipgloss.Place(a.width, a.height, lipgloss.Center, lipgloss.Center, a.loading.View()+"Initializing...") diff --git a/internal/ui/graphs.go b/internal/ui/graphs.go index 0eb4296..0392d90 100644 --- a/internal/ui/graphs.go +++ b/internal/ui/graphs.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/chriskim06/asciigraph" "github.com/chriskim06/kubectl-topui/internal/config" + "github.com/chriskim06/kubectl-topui/internal/ui/utils" ) type Graphs struct { @@ -29,7 +30,7 @@ func NewGraphs(conf config.Colors, graphColor asciigraph.AnsiColor) *Graphs { return &Graphs{ conf: conf, graphColor: graphColor, - style: border.Copy().Align(lipgloss.Top).BorderForeground(adaptive.Copy().GetForeground()), + style: utils.Border.Copy().Align(lipgloss.Top).BorderForeground(utils.Adaptive.Copy().GetForeground()), } } @@ -50,6 +51,10 @@ func (g *Graphs) View() string { memColors := asciigraph.SeriesColors(asciigraph.ColorNames[string(g.conf.MemLimit)], asciigraph.ColorNames[string(g.conf.MemUsage)]) cpuPlot := g.plot(g.cpuData[g.name], "CPU", asciigraph.Min(g.cpuMin), asciigraph.Max(g.cpuMax), cpuColors) memPlot := g.plot(g.memData[g.name], "MEM", asciigraph.Min(g.memMin), asciigraph.Max(g.memMax), memColors) + cpuTitle := utils.Truncate(fmt.Sprintf("CPU - %s", g.name), g.Width/2-2) + memTitle := utils.Truncate(fmt.Sprintf("MEM - %s", g.name), g.Width/2-2) + cpuPlot = fmt.Sprintf("%s\n%s", cpuTitle, cpuPlot) + memPlot = fmt.Sprintf("%s\n%s", memTitle, memPlot) g.style = g.style.MaxWidth(g.Width / 2).MaxHeight(g.Height).Width(g.Width/2 - 2) return lipgloss.JoinHorizontal(lipgloss.Top, g.style.Render(cpuPlot), g.style.Render(memPlot)) } @@ -84,10 +89,8 @@ func (g *Graphs) updateData(name string, cpuData, memData map[string][][]float64 func (g Graphs) plot(data [][]float64, caption string, o ...asciigraph.Option) string { options := []asciigraph.Option{ - asciigraph.Height(g.Height - 6), asciigraph.Width(0), - asciigraph.Caption(fmt.Sprintf("%s - %s", caption, g.name)), - asciigraph.CaptionColor(g.graphColor), + asciigraph.Height(g.Height - 7), asciigraph.AxisColor(g.graphColor), asciigraph.LabelColor(g.graphColor), } diff --git a/internal/ui/info.go b/internal/ui/info.go index 16e2955..896697c 100644 --- a/internal/ui/info.go +++ b/internal/ui/info.go @@ -8,6 +8,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/chriskim06/kubectl-topui/internal/config" + "github.com/chriskim06/kubectl-topui/internal/ui/utils" "github.com/muesli/reflow/padding" "github.com/muesli/reflow/wrap" ) @@ -25,7 +26,7 @@ type Info struct { func NewInfo(conf config.Colors) *Info { return &Info{ conf: conf, - style: border.Copy().Padding(0), + style: utils.Border.Copy().Padding(0), content: viewport.New(0, 0), } } @@ -46,9 +47,9 @@ func (i *Info) Update(msg tea.Msg) (Info, tea.Cmd) { func (i Info) View() string { if i.focused { - i.style.BorderForeground(toColor(string(i.conf.Selected))) + i.style.BorderForeground(utils.ToColor(string(i.conf.Selected))) } else { - i.style.BorderForeground(adaptive.Copy().GetForeground()) + i.style.BorderForeground(utils.Adaptive.Copy().GetForeground()) } return i.style.Render(i.content.View()) } diff --git a/internal/ui/items.go b/internal/ui/items.go index 5ccbb9b..df3fcb6 100644 --- a/internal/ui/items.go +++ b/internal/ui/items.go @@ -10,7 +10,7 @@ import ( "github.com/chriskim06/kubectl-topui/internal/config" "github.com/chriskim06/kubectl-topui/internal/metrics" "github.com/chriskim06/kubectl-topui/internal/ui/list" - "github.com/muesli/reflow/truncate" + "github.com/chriskim06/kubectl-topui/internal/ui/utils" ) type listItem string @@ -33,12 +33,12 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, item list.Ite line = "" } else { line = line[m.GetOffset():] - line = truncate.StringWithTail(line, uint(m.Width()), "…") + line = utils.Truncate(line, m.Width()) } if index == m.Index() { - fmt.Fprintf(w, adaptive.Copy().Background(lipgloss.Color("245")).Bold(true).Render(line)) + fmt.Fprintf(w, utils.Adaptive.Copy().Background(lipgloss.Color("245")).Bold(true).Render(line)) } else { - fmt.Fprintf(w, adaptive.Copy().Render(line)) + fmt.Fprintf(w, utils.Adaptive.Copy().Render(line)) } } @@ -62,7 +62,7 @@ func NewList(resource metrics.Resource, conf config.Colors) *List { conf: conf, content: itemList, focused: true, - style: border.Copy().Padding(0, 1), + style: utils.Border.Copy().Padding(0, 1), } } @@ -76,7 +76,7 @@ func (l *List) Update(msg tea.Msg) (List, tea.Cmd) { case tea.KeyMsg: l.content, cmd = l.content.Update(msg) case tickMsg: - header, items := tabStrings(msg.m, l.resource) + header, items := utils.TabStrings(msg.m, l.resource) listItems := []list.Item{} for _, item := range items { listItems = append(listItems, listItem(item)) @@ -89,9 +89,9 @@ func (l *List) Update(msg tea.Msg) (List, tea.Cmd) { func (l List) View() string { if l.focused { - l.style.BorderForeground(toColor(string(l.conf.Selected))) + l.style.BorderForeground(utils.ToColor(string(l.conf.Selected))) } else { - l.style.BorderForeground(adaptive.Copy().GetForeground()) + l.style.BorderForeground(utils.Adaptive.Copy().GetForeground()) } return l.style.Render(l.content.View()) } diff --git a/internal/ui/list/list.go b/internal/ui/list/list.go index 8cd4f6e..6bfb17d 100644 --- a/internal/ui/list/list.go +++ b/internal/ui/list/list.go @@ -11,8 +11,8 @@ import ( "github.com/charmbracelet/bubbles/paginator" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/chriskim06/kubectl-topui/internal/ui/utils" "github.com/muesli/reflow/ansi" - "github.com/muesli/reflow/truncate" ) // Item is an item that appears in the list. @@ -433,7 +433,7 @@ func (m Model) titleView() string { } else { titleStr = titleStr[m.offset:] } - titleStr = truncate.StringWithTail(titleStr, uint(m.width), "…") + titleStr = utils.Truncate(titleStr, m.width) view += m.Styles.Title.Render(titleStr) } diff --git a/internal/ui/utils.go b/internal/ui/utils/utils.go similarity index 79% rename from internal/ui/utils.go rename to internal/ui/utils/utils.go index 8c1c62f..0ea77d7 100644 --- a/internal/ui/utils.go +++ b/internal/ui/utils/utils.go @@ -1,4 +1,4 @@ -package ui +package utils import ( "bytes" @@ -9,10 +9,11 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/chriskim06/asciigraph" "github.com/chriskim06/kubectl-topui/internal/metrics" + "github.com/muesli/reflow/truncate" "k8s.io/cli-runtime/pkg/printers" ) -const helpText = `This app shows metrics for pods and nodes! The graphs display the limit and usage for the cpu and memory of whichever item is selected. +const HelpText = `This app shows metrics for pods and nodes! The graphs display the limit and usage for the cpu and memory of whichever item is selected. Keyboard Shortcuts - j: move selection down or scroll down spec @@ -21,16 +22,16 @@ Keyboard Shortcuts - ?: open/close this help menu` var ( - adaptive = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "0", Dark: "15"}) - border = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()) - errStyle = lipgloss.NewStyle().BorderStyle(lipgloss.DoubleBorder()).BorderForeground(lipgloss.Color("9")) + Adaptive = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "0", Dark: "15"}) + Border = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()) + ErrStyle = lipgloss.NewStyle().BorderStyle(lipgloss.DoubleBorder()).BorderForeground(lipgloss.Color("9")) headers = map[metrics.Resource]string{ metrics.POD: "NAMESPACE\tNAME\tREADY\tSTATUS\tNODE\tCPU USAGE\tCPU LIMIT\tMEM USAGE\tMEM LIMIT\tRESTARTS\tAGE", metrics.NODE: "NAME\tCPU USAGE\tCPU AVAILABLE\tCPU PERCENT\tMEM USAGE\tMEM AVAILABLE\tMEM PERCENT", } ) -func tabStrings(data []metrics.MetricValue, resource metrics.Resource) (string, []string) { +func TabStrings(data []metrics.MetricValue, resource metrics.Resource) (string, []string) { var b bytes.Buffer w := printers.GetNewTabWriter(&b) fmt.Fprintln(w, headers[resource]) @@ -73,10 +74,14 @@ func writeMetric(w io.Writer, m metrics.MetricValue, resource metrics.Resource) } } -func toColor(s string) lipgloss.Color { +func ToColor(s string) lipgloss.Color { b, ok := asciigraph.ColorNames[s] if !ok { - return adaptive.GetForeground().(lipgloss.Color) + return Adaptive.GetForeground().(lipgloss.Color) } return lipgloss.Color(fmt.Sprintf("%d", b)) } + +func Truncate(s string, width int) string { + return truncate.StringWithTail(s, uint(width), "…") +}