Skip to content

Commit

Permalink
add help
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskim06 committed Feb 23, 2023
1 parent 5a4c1f2 commit 9924085
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
5 changes: 5 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package metrics

import (
"log"
"strings"

"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions internal/metrics/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions internal/ui/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui

import (
"fmt"
"time"

"github.com/charmbracelet/bubbles/spinner"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ 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{
resource: resource,
conf: conf,
content: itemList,
focused: true,
style: border.Copy(),
style: border.Copy().Padding(0, 1),
}
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/list/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 39 additions & 10 deletions internal/ui/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -49,8 +50,7 @@ type Model struct {
showTitle bool
showPagination bool

itemNameSingular string
itemNamePlural string
ItemNamePlural string

Title string
Styles Styles
Expand All @@ -61,6 +61,7 @@ type Model struct {
width int
height int
Paginator paginator.Model
Help help.Model
cursor int
offset int

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()))

Expand Down Expand Up @@ -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...)
}
Expand Down Expand Up @@ -440,14 +465,18 @@ 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()

var b strings.Builder

// 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]
Expand Down
2 changes: 2 additions & 0 deletions internal/ui/list/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Styles struct {
NoItems lipgloss.Style

PaginationStyle lipgloss.Style
HelpStyle lipgloss.Style

// Styled characters.
ActivePaginationDot lipgloss.Style
Expand Down Expand Up @@ -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"}).
Expand Down

0 comments on commit 9924085

Please sign in to comment.