Skip to content

Commit fe3ad66

Browse files
committed
update
1 parent 0188e04 commit fe3ad66

File tree

6 files changed

+36
-26
lines changed

6 files changed

+36
-26
lines changed

internal/ui/app.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/chriskim06/asciigraph"
1111
"github.com/chriskim06/kubectl-topui/internal/config"
1212
"github.com/chriskim06/kubectl-topui/internal/metrics"
13+
"github.com/chriskim06/kubectl-topui/internal/ui/utils"
1314
"k8s.io/cli-runtime/pkg/genericclioptions"
1415
"k8s.io/kubectl/pkg/cmd/top"
1516
)
@@ -162,7 +163,7 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
162163

163164
func (a App) View() string {
164165
if a.err != nil {
165-
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()))
166+
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()))
166167
}
167168
if !a.ready || !a.sizeReady {
168169
return lipgloss.Place(a.width, a.height, lipgloss.Center, lipgloss.Center, a.loading.View()+"Initializing...")

internal/ui/graphs.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/charmbracelet/lipgloss"
99
"github.com/chriskim06/asciigraph"
1010
"github.com/chriskim06/kubectl-topui/internal/config"
11+
"github.com/chriskim06/kubectl-topui/internal/ui/utils"
1112
)
1213

1314
type Graphs struct {
@@ -29,7 +30,7 @@ func NewGraphs(conf config.Colors, graphColor asciigraph.AnsiColor) *Graphs {
2930
return &Graphs{
3031
conf: conf,
3132
graphColor: graphColor,
32-
style: border.Copy().Align(lipgloss.Top).BorderForeground(adaptive.Copy().GetForeground()),
33+
style: utils.Border.Copy().Align(lipgloss.Top).BorderForeground(utils.Adaptive.Copy().GetForeground()),
3334
}
3435
}
3536

@@ -50,6 +51,10 @@ func (g *Graphs) View() string {
5051
memColors := asciigraph.SeriesColors(asciigraph.ColorNames[string(g.conf.MemLimit)], asciigraph.ColorNames[string(g.conf.MemUsage)])
5152
cpuPlot := g.plot(g.cpuData[g.name], "CPU", asciigraph.Min(g.cpuMin), asciigraph.Max(g.cpuMax), cpuColors)
5253
memPlot := g.plot(g.memData[g.name], "MEM", asciigraph.Min(g.memMin), asciigraph.Max(g.memMax), memColors)
54+
cpuTitle := utils.Truncate(fmt.Sprintf("CPU - %s", g.name), g.Width/2-2)
55+
memTitle := utils.Truncate(fmt.Sprintf("MEM - %s", g.name), g.Width/2-2)
56+
cpuPlot = fmt.Sprintf("%s\n%s", cpuTitle, cpuPlot)
57+
memPlot = fmt.Sprintf("%s\n%s", memTitle, memPlot)
5358
g.style = g.style.MaxWidth(g.Width / 2).MaxHeight(g.Height).Width(g.Width/2 - 2)
5459
return lipgloss.JoinHorizontal(lipgloss.Top, g.style.Render(cpuPlot), g.style.Render(memPlot))
5560
}
@@ -84,10 +89,8 @@ func (g *Graphs) updateData(name string, cpuData, memData map[string][][]float64
8489

8590
func (g Graphs) plot(data [][]float64, caption string, o ...asciigraph.Option) string {
8691
options := []asciigraph.Option{
87-
asciigraph.Height(g.Height - 6),
8892
asciigraph.Width(0),
89-
asciigraph.Caption(fmt.Sprintf("%s - %s", caption, g.name)),
90-
asciigraph.CaptionColor(g.graphColor),
93+
asciigraph.Height(g.Height - 7),
9194
asciigraph.AxisColor(g.graphColor),
9295
asciigraph.LabelColor(g.graphColor),
9396
}

internal/ui/info.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
tea "github.com/charmbracelet/bubbletea"
99
"github.com/charmbracelet/lipgloss"
1010
"github.com/chriskim06/kubectl-topui/internal/config"
11+
"github.com/chriskim06/kubectl-topui/internal/ui/utils"
1112
"github.com/muesli/reflow/padding"
1213
"github.com/muesli/reflow/wrap"
1314
)
@@ -25,7 +26,7 @@ type Info struct {
2526
func NewInfo(conf config.Colors) *Info {
2627
return &Info{
2728
conf: conf,
28-
style: border.Copy().Padding(0),
29+
style: utils.Border.Copy().Padding(0),
2930
content: viewport.New(0, 0),
3031
}
3132
}
@@ -46,9 +47,9 @@ func (i *Info) Update(msg tea.Msg) (Info, tea.Cmd) {
4647

4748
func (i Info) View() string {
4849
if i.focused {
49-
i.style.BorderForeground(toColor(string(i.conf.Selected)))
50+
i.style.BorderForeground(utils.ToColor(string(i.conf.Selected)))
5051
} else {
51-
i.style.BorderForeground(adaptive.Copy().GetForeground())
52+
i.style.BorderForeground(utils.Adaptive.Copy().GetForeground())
5253
}
5354
return i.style.Render(i.content.View())
5455
}

internal/ui/items.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/chriskim06/kubectl-topui/internal/config"
1111
"github.com/chriskim06/kubectl-topui/internal/metrics"
1212
"github.com/chriskim06/kubectl-topui/internal/ui/list"
13-
"github.com/muesli/reflow/truncate"
13+
"github.com/chriskim06/kubectl-topui/internal/ui/utils"
1414
)
1515

1616
type listItem string
@@ -33,12 +33,12 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, item list.Ite
3333
line = ""
3434
} else {
3535
line = line[m.GetOffset():]
36-
line = truncate.StringWithTail(line, uint(m.Width()), "…")
36+
line = utils.Truncate(line, m.Width())
3737
}
3838
if index == m.Index() {
39-
fmt.Fprintf(w, adaptive.Copy().Background(lipgloss.Color("245")).Bold(true).Render(line))
39+
fmt.Fprintf(w, utils.Adaptive.Copy().Background(lipgloss.Color("245")).Bold(true).Render(line))
4040
} else {
41-
fmt.Fprintf(w, adaptive.Copy().Render(line))
41+
fmt.Fprintf(w, utils.Adaptive.Copy().Render(line))
4242
}
4343
}
4444

@@ -62,7 +62,7 @@ func NewList(resource metrics.Resource, conf config.Colors) *List {
6262
conf: conf,
6363
content: itemList,
6464
focused: true,
65-
style: border.Copy().Padding(0, 1),
65+
style: utils.Border.Copy().Padding(0, 1),
6666
}
6767
}
6868

@@ -76,7 +76,7 @@ func (l *List) Update(msg tea.Msg) (List, tea.Cmd) {
7676
case tea.KeyMsg:
7777
l.content, cmd = l.content.Update(msg)
7878
case tickMsg:
79-
header, items := tabStrings(msg.m, l.resource)
79+
header, items := utils.TabStrings(msg.m, l.resource)
8080
listItems := []list.Item{}
8181
for _, item := range items {
8282
listItems = append(listItems, listItem(item))
@@ -89,9 +89,9 @@ func (l *List) Update(msg tea.Msg) (List, tea.Cmd) {
8989

9090
func (l List) View() string {
9191
if l.focused {
92-
l.style.BorderForeground(toColor(string(l.conf.Selected)))
92+
l.style.BorderForeground(utils.ToColor(string(l.conf.Selected)))
9393
} else {
94-
l.style.BorderForeground(adaptive.Copy().GetForeground())
94+
l.style.BorderForeground(utils.Adaptive.Copy().GetForeground())
9595
}
9696
return l.style.Render(l.content.View())
9797
}

internal/ui/list/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/charmbracelet/bubbles/paginator"
1212
tea "github.com/charmbracelet/bubbletea"
1313
"github.com/charmbracelet/lipgloss"
14+
"github.com/chriskim06/kubectl-topui/internal/ui/utils"
1415
"github.com/muesli/reflow/ansi"
15-
"github.com/muesli/reflow/truncate"
1616
)
1717

1818
// Item is an item that appears in the list.
@@ -433,7 +433,7 @@ func (m Model) titleView() string {
433433
} else {
434434
titleStr = titleStr[m.offset:]
435435
}
436-
titleStr = truncate.StringWithTail(titleStr, uint(m.width), "…")
436+
titleStr = utils.Truncate(titleStr, m.width)
437437
view += m.Styles.Title.Render(titleStr)
438438
}
439439

internal/ui/utils.go renamed to internal/ui/utils/utils.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ui
1+
package utils
22

33
import (
44
"bytes"
@@ -9,10 +9,11 @@ import (
99
"github.com/charmbracelet/lipgloss"
1010
"github.com/chriskim06/asciigraph"
1111
"github.com/chriskim06/kubectl-topui/internal/metrics"
12+
"github.com/muesli/reflow/truncate"
1213
"k8s.io/cli-runtime/pkg/printers"
1314
)
1415

15-
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.
16+
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.
1617
1718
Keyboard Shortcuts
1819
- j: move selection down or scroll down spec
@@ -21,16 +22,16 @@ Keyboard Shortcuts
2122
- ?: open/close this help menu`
2223

2324
var (
24-
adaptive = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "0", Dark: "15"})
25-
border = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder())
26-
errStyle = lipgloss.NewStyle().BorderStyle(lipgloss.DoubleBorder()).BorderForeground(lipgloss.Color("9"))
25+
Adaptive = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "0", Dark: "15"})
26+
Border = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder())
27+
ErrStyle = lipgloss.NewStyle().BorderStyle(lipgloss.DoubleBorder()).BorderForeground(lipgloss.Color("9"))
2728
headers = map[metrics.Resource]string{
2829
metrics.POD: "NAMESPACE\tNAME\tREADY\tSTATUS\tNODE\tCPU USAGE\tCPU LIMIT\tMEM USAGE\tMEM LIMIT\tRESTARTS\tAGE",
2930
metrics.NODE: "NAME\tCPU USAGE\tCPU AVAILABLE\tCPU PERCENT\tMEM USAGE\tMEM AVAILABLE\tMEM PERCENT",
3031
}
3132
)
3233

33-
func tabStrings(data []metrics.MetricValue, resource metrics.Resource) (string, []string) {
34+
func TabStrings(data []metrics.MetricValue, resource metrics.Resource) (string, []string) {
3435
var b bytes.Buffer
3536
w := printers.GetNewTabWriter(&b)
3637
fmt.Fprintln(w, headers[resource])
@@ -73,10 +74,14 @@ func writeMetric(w io.Writer, m metrics.MetricValue, resource metrics.Resource)
7374
}
7475
}
7576

76-
func toColor(s string) lipgloss.Color {
77+
func ToColor(s string) lipgloss.Color {
7778
b, ok := asciigraph.ColorNames[s]
7879
if !ok {
79-
return adaptive.GetForeground().(lipgloss.Color)
80+
return Adaptive.GetForeground().(lipgloss.Color)
8081
}
8182
return lipgloss.Color(fmt.Sprintf("%d", b))
8283
}
84+
85+
func Truncate(s string, width int) string {
86+
return truncate.StringWithTail(s, uint(width), "…")
87+
}

0 commit comments

Comments
 (0)