diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index c0a6e19..0bb87e6 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -17,6 +17,7 @@ package metrics import ( "log" + "strings" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -28,6 +29,10 @@ import ( type Resource string +func (r Resource) LowerCase() string { + return strings.ToLower(string(r)) +} + const ( POD Resource = "PODS" NODE Resource = "NODES" diff --git a/internal/metrics/pod.go b/internal/metrics/pod.go index 2b8b8e1..3270cec 100644 --- a/internal/metrics/pod.go +++ b/internal/metrics/pod.go @@ -47,6 +47,7 @@ type resourceLimits struct { func (m *MetricsClient) GetPodMetrics(o *top.TopPodOptions) ([]MetricValue, error) { o.MetricsClient = m.m o.PodClient = m.k.CoreV1() + o.Namespace = m.ns versionedMetrics := &metricsv1beta1api.PodMetricsList{} mc := o.MetricsClient.MetricsV1beta1() diff --git a/internal/ui/app.go b/internal/ui/app.go index a3e49f1..da99379 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -1,6 +1,7 @@ package ui import ( + "fmt" "time" "github.com/charmbracelet/bubbles/spinner" @@ -148,6 +149,9 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case spinner.TickMsg: if a.ready && a.sizeReady { a.loading = nil + half := a.height / 2 + thirdRounded := (a.width / 3) + (a.width % 3) + a.itemsPane.SetSize(a.width-thirdRounded-5, half) return a, nil } *a.loading, cmd = a.loading.Update(msg) @@ -190,6 +194,7 @@ func (a *App) updateData() tea.Msg { m, err = a.client.GetNodeMetrics(a.options.(*top.TopNodeOptions)) } if err != nil { + fmt.Println(err) return tickMsg{err: err} } for _, metric := range m { diff --git a/internal/ui/items.go b/internal/ui/items.go index 1faa4fc..143e4a8 100644 --- a/internal/ui/items.go +++ b/internal/ui/items.go @@ -54,6 +54,7 @@ type List struct { func NewList(resource metrics.Resource, conf config.Colors) *List { itemList := list.New([]list.Item{}, itemDelegate{}, 0, 0) + itemList.ItemNamePlural = resource.LowerCase() itemList.Styles.Title = lipgloss.NewStyle().Bold(true).Padding(0) itemList.Styles.TitleBar = lipgloss.NewStyle().Padding(0) return &List{ @@ -61,7 +62,7 @@ func NewList(resource metrics.Resource, conf config.Colors) *List { conf: conf, content: itemList, focused: true, - style: border.Copy(), + style: border.Copy().Padding(0, 1), } } @@ -98,7 +99,7 @@ func (l List) View() string { func (l *List) SetSize(width, height int) { l.Width = width l.Height = height - l.style = l.style.Width(l.Width).Height(l.Height).Padding(0, 1) + l.style = l.style.Width(l.Width).Height(l.Height) v, h := l.style.GetFrameSize() l.content.Styles.TitleBar.Width(l.Width - h).MaxWidth(l.Width - h) l.content.SetSize(l.Width-h, l.Height-v) diff --git a/internal/ui/list/keys.go b/internal/ui/list/keys.go index e5617f6..7705923 100644 --- a/internal/ui/list/keys.go +++ b/internal/ui/list/keys.go @@ -44,19 +44,19 @@ func DefaultKeyMap() KeyMap { ), PrevPage: key.NewBinding( key.WithKeys("shift+tab", "pgup"), - key.WithHelp("←/shift+tab/pgup", "prev page"), + key.WithHelp("shift+tab", "prev page"), ), NextPage: key.NewBinding( key.WithKeys("tab", "pgdown"), - key.WithHelp("→/tab/pgdn", "next page"), + key.WithHelp("tab", "next page"), ), GoToStart: key.NewBinding( key.WithKeys("home", "g"), - key.WithHelp("g/home", "go to start"), + key.WithHelp("g", "go to start"), ), GoToEnd: key.NewBinding( key.WithKeys("end", "G"), - key.WithHelp("G/end", "go to end"), + key.WithHelp("G", "go to end"), ), // Quitting. diff --git a/internal/ui/list/list.go b/internal/ui/list/list.go index bdbb993..8cd4f6e 100644 --- a/internal/ui/list/list.go +++ b/internal/ui/list/list.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/paginator" tea "github.com/charmbracelet/bubbletea" @@ -49,8 +50,7 @@ type Model struct { showTitle bool showPagination bool - itemNameSingular string - itemNamePlural string + ItemNamePlural string Title string Styles Styles @@ -61,6 +61,7 @@ type Model struct { width int height int Paginator paginator.Model + Help help.Model cursor int offset int @@ -83,19 +84,19 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model { p.InactiveDot = styles.InactivePaginationDot.String() m := Model{ - showTitle: true, - showPagination: true, - itemNameSingular: "item", - itemNamePlural: "items", - KeyMap: DefaultKeyMap(), - Styles: styles, - Title: "List", + showTitle: true, + showPagination: true, + ItemNamePlural: "items", + KeyMap: DefaultKeyMap(), + Styles: styles, + Title: "List", width: width, height: height, delegate: delegate, items: items, Paginator: p, + Help: help.New(), } m.updatePagination() @@ -265,9 +266,28 @@ func (m *Model) SetHeight(v int) { func (m *Model) setSize(width, height int) { m.width = width m.height = height + m.Help.Width = width m.updatePagination() } +func (m Model) ShortHelp() []key.Binding { + return []key.Binding{ + m.KeyMap.CursorUp, + m.KeyMap.CursorDown, + m.KeyMap.NextPage, + m.KeyMap.PrevPage, + m.KeyMap.CursorLeft, + m.KeyMap.CursorRight, + m.KeyMap.GoToStart, + m.KeyMap.GoToEnd, + m.KeyMap.Quit, + } +} + +func (m Model) FullHelp() [][]key.Binding { + return [][]key.Binding{m.ShortHelp()} +} + // Update pagination according to the amount of items for the current state. func (m *Model) updatePagination() { index := m.Index() @@ -280,6 +300,7 @@ func (m *Model) updatePagination() { if m.showPagination { availHeight -= lipgloss.Height(m.paginationView()) } + availHeight -= lipgloss.Height(m.helpView()) m.Paginator.PerPage = max(1, availHeight/(m.delegate.Height()+m.delegate.Spacing())) @@ -384,12 +405,16 @@ func (m Model) View() string { availHeight -= lipgloss.Height(pagination) } + help := m.helpView() + availHeight -= lipgloss.Height(help) + content := lipgloss.NewStyle().Height(availHeight).Render(m.populatedView()) sections = append(sections, content) if m.showPagination { sections = append(sections, pagination) } + sections = append(sections, help) return lipgloss.JoinVertical(lipgloss.Left, sections...) } @@ -440,6 +465,10 @@ func (m Model) paginationView() string { return style.Render(s) } +func (m Model) helpView() string { + return m.Styles.HelpStyle.Render(m.Help.View(m)) +} + func (m Model) populatedView() string { items := m.VisibleItems() @@ -447,7 +476,7 @@ func (m Model) populatedView() string { // Empty states if len(items) == 0 { - return m.Styles.NoItems.Render("No " + m.itemNamePlural + " found.") + return m.Styles.NoItems.Render("No " + m.ItemNamePlural + " found.") } else { start, end := m.Paginator.GetSliceBounds(len(items)) docs := items[start:end] diff --git a/internal/ui/list/style.go b/internal/ui/list/style.go index 44ddd4a..8f90570 100644 --- a/internal/ui/list/style.go +++ b/internal/ui/list/style.go @@ -18,6 +18,7 @@ type Styles struct { NoItems lipgloss.Style PaginationStyle lipgloss.Style + HelpStyle lipgloss.Style // Styled characters. ActivePaginationDot lipgloss.Style @@ -45,6 +46,7 @@ func DefaultStyles() (s Styles) { s.ArabicPagination = lipgloss.NewStyle().Foreground(subduedColor) s.PaginationStyle = lipgloss.NewStyle().PaddingLeft(2) //nolint:gomnd + s.HelpStyle = lipgloss.NewStyle().Padding(1, 0, 0, 2) s.ActivePaginationDot = lipgloss.NewStyle(). Foreground(lipgloss.AdaptiveColor{Light: "#847A85", Dark: "#979797"}).