Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New table dependency #63

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/dashboards/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ panels:
version: v1
kind: Pod
type: table
pageSize: 3
columns:
- header: Namespace
path: metadata.namespace
width: 15
- header: Name
path: metadata.name
width: 15
- header: Labels
path: metadata.labels
width: 30
- header: Ready Condition
path: status.conditions.#(type==Ready)
width: 30
- header: Phase
path: status.phase
width: 10
- header: Containers
path: spec.containers.#.name
width: 20
- name: Kube-System Pods with label tier=control-plane
group: ""
version: v1
Expand Down Expand Up @@ -81,4 +88,4 @@ panels:
type: logs
key:
namespace: kube-system
name: coredns
name: coredns
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/everettraven/buoy
go 1.21

require (
github.com/calyptia/go-bubble-table v0.2.1
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.26.1
github.com/charmbracelet/lipgloss v0.10.0
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/evertras/bubble-table v0.16.0
github.com/sahilm/fuzzy v0.1.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
Expand Down Expand Up @@ -47,9 +47,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand Down
14 changes: 2 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/calyptia/go-bubble-table v0.2.1 h1:NWcVRyGCLuP7QIA29uUFSY+IjmWcmUWHjy5J/CPb0Rk=
github.com/calyptia/go-bubble-table v0.2.1/go.mod h1:gJvzUOUzfQeA9JmgLumyJYWJMtuRQ7WxxTwc9tjEiGw=
github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=
github.com/charmbracelet/bubbles v0.16.1/go.mod h1:2QCp9LFlEsBQMvIYERr7Ww2H2bA7xen1idUDIzm/+Xc=
github.com/charmbracelet/bubbletea v0.26.1 h1:xujcQeF73rh4jwu3+zhfQsvV18x+7zIjlw7/CYbzGJ0=
Expand All @@ -27,6 +25,8 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evertras/bubble-table v0.16.0 h1:Bt2cPukoP8DSWVpBcPSq2E3W2fs7R0mf5BaIFQxFPfM=
github.com/evertras/bubble-table v0.16.0/go.mod h1:SPOZKbIpyYWPHBNki3fyNpiPBQkvkULAtOT7NTD5fKY=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -66,8 +66,6 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc h1:ZQrgZFsLzkw7o3CoDzsfBhx0bf/1rVBXrLy8dXKRe8o=
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc/go.mod h1:PyXUpnI3olx3bsPcHt98FGPX/KCFZ1Fi+hw1XLI6384=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand All @@ -81,13 +79,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lunixbochs/vtclean v1.0.0 h1:xu2sLAri4lGiovBDQKxl5mrXyESr3gUr5m5SM5+LVb8=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.10/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
Expand Down Expand Up @@ -174,10 +167,7 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
Expand Down
66 changes: 46 additions & 20 deletions pkg/charm/models/panels/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import (
"sync"

"github.com/alecthomas/chroma/quick"
tbl "github.com/calyptia/go-bubble-table"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
buoytypes "github.com/everettraven/buoy/pkg/types"
tbl "github.com/evertras/bubble-table/table"
"github.com/tidwall/gjson"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)

const (
modeView = "view"
modeTable = "table"
// TODO: These default sizes should probably
// be configurable
defaultPageSize = 5
defaultColumnWidth = 20
)

type KeyMap struct {
ViewModeToggle key.Binding
}
Expand All @@ -36,7 +45,9 @@ func (k KeyMap) ShortHelp() []key.Binding {
// key.Map interface.
func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.ViewModeToggle},
{
k.ViewModeToggle,
},
}
}

Expand All @@ -47,11 +58,6 @@ var DefaultKeys = KeyMap{
),
}

const (
modeView = "view"
modeTable = "table"
)

type RowInfo struct {
Row tbl.Row
Identifier *types.NamespacedName
Expand All @@ -60,14 +66,13 @@ type RowInfo struct {

type Styles struct {
SelectedRow lipgloss.Style
TextAlignment lipgloss.Style
SyntaxHighlightDark string
SyntaxHighlightLight string
}

type ViewActionFunc func(row *RowInfo) (string, error)

// TODO: Can some of the action logic be decoupled from this model?

// Model is a tea.Model implementation
// that represents a table panel
type Model struct {
Expand All @@ -86,15 +91,32 @@ type Model struct {
}

func New(keys KeyMap, table *buoytypes.Table, styles Styles) *Model {
tblColumns := []string{}
tblColumns := []tbl.Column{}
width := 0
for _, column := range table.Columns {
tblColumns = append(tblColumns, column.Header)
width += column.Width
if column.Width > 0 {
tblColumns = append(tblColumns, tbl.NewColumn(column.Header, column.Header, column.Width))
width += column.Width
} else {
tblColumns = append(tblColumns, tbl.NewFlexColumn(column.Header, column.Header, 1))
width += defaultColumnWidth
}
}

tab := tbl.New(tblColumns, width, 10)
tab.Styles.SelectedRow = styles.SelectedRow
pageSize := table.PageSize
if pageSize <= 0 {
pageSize = defaultPageSize
}

tab := tbl.New(tblColumns).
SelectableRows(false).
HighlightStyle(styles.SelectedRow).
WithBaseStyle(styles.TextAlignment).
WithPageSize(pageSize).
WithHorizontalFreezeColumnCount(1).
WithMultiline(true).
WithTargetWidth(width).
BorderRounded()

return &Model{
tableModel: tab,
Expand All @@ -117,7 +139,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.tableModel.SetSize(msg.Width, msg.Height/2)
m.tableModel = m.tableModel.WithMaxTotalWidth(msg.Width)
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height / 2
case tea.KeyMsg:
Expand All @@ -126,7 +148,8 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.mode {
case modeTable:
m.mode = modeView
row := m.FetchRowForIndex(m.tableModel.Cursor())
m.tableModel = m.tableModel.Focused(false)
row := m.FetchRowForIndex(m.tableModel.GetHighlightedRowIndex())
vpContent, err := m.viewAction(row)
if err != nil {
m.viewport.SetContent(err.Error())
Expand All @@ -136,17 +159,19 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case modeView:
m.mode = modeTable
m.viewport.SetContent("")
m.tableModel = m.tableModel.Focused(true)
}
}
}

if len(m.tempRows) > 0 {
m.tableModel.SetRows(m.tempRows)
m.tableModel = m.tableModel.WithRows(m.tempRows)
m.tempRows = []tbl.Row{}
}

switch m.mode {
case modeTable:
m.tableModel = m.tableModel.Focused(true)
m.tableModel, cmd = m.tableModel.Update(msg)
case modeView:
m.viewport, cmd = m.viewport.Update(msg)
Expand All @@ -172,16 +197,17 @@ func (m *Model) AddOrUpdate(u *unstructured.Unstructured) {
m.mutex.Lock()
defer m.mutex.Unlock()
uid := u.GetUID()
row := tbl.SimpleRow{}
rowData := tbl.RowData{}
for _, column := range m.Columns() {
val, err := getDotNotationValue(u.Object, column.Path)
if err != nil {
m.SetError(err)
break
}

row = append(row, fmt.Sprint(val))
rowData[column.Header] = val
}
row := tbl.NewRow(rowData)
row = row.WithStyle(m.styles.TextAlignment)

m.rows[uid] = &RowInfo{
Row: row,
Expand Down
1 change: 1 addition & 0 deletions pkg/types/panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Table struct {
Columns []Column `json:"columns" yaml:"columns"`
Namespace string `json:"namespace" yaml:"namespace"`
LabelSelector map[string]string `json:"labelSelector" yaml:"labelSelector"`
PageSize int `json:"pageSize" yaml:"pageSize"`
}

type Column struct {
Expand Down
Loading