Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskim06 committed Feb 23, 2023
1 parent 0188e04 commit fe3ad66
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
3 changes: 2 additions & 1 deletion internal/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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...")
Expand Down
11 changes: 7 additions & 4 deletions internal/ui/graphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()),
}
}

Expand All @@ -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))
}
Expand Down Expand Up @@ -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),
}
Expand Down
7 changes: 4 additions & 3 deletions internal/ui/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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),
}
}
Expand All @@ -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())
}
Expand Down
16 changes: 8 additions & 8 deletions internal/ui/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}

Expand All @@ -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),
}
}

Expand All @@ -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))
Expand All @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down
21 changes: 13 additions & 8 deletions internal/ui/utils.go → internal/ui/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ui
package utils

import (
"bytes"
Expand All @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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), "…")
}

0 comments on commit fe3ad66

Please sign in to comment.