From 8515f70fbfc491df9f72533d3410fc32eabc3edd Mon Sep 17 00:00:00 2001 From: derailed Date: Fri, 21 Jun 2019 22:29:49 -0600 Subject: [PATCH] refactor table --- internal/views/res_table.go | 119 +++++++++++++++++++++++++++++++ internal/views/table.go | 137 +++++------------------------------- 2 files changed, 135 insertions(+), 121 deletions(-) create mode 100644 internal/views/res_table.go diff --git a/internal/views/res_table.go b/internal/views/res_table.go new file mode 100644 index 00000000..e39526b8 --- /dev/null +++ b/internal/views/res_table.go @@ -0,0 +1,119 @@ +package views + +import ( + "time" + + "github.com/derailed/k9s/internal/config" + "github.com/derailed/k9s/internal/resource" + "github.com/derailed/tview" + "github.com/gdamore/tcell" + "k8s.io/apimachinery/pkg/util/duration" +) + +type resTable struct { + *tview.Table + + app *appView + baseTitle string + currentNS string + data resource.TableData + actions keyActions +} + +func newResTable(app *appView, title string) *resTable { + v := resTable{ + Table: tview.NewTable(), + app: app, + actions: make(keyActions), + baseTitle: title, + } + + v.SetFixed(1, 0) + v.SetBorder(true) + v.SetBackgroundColor(config.AsColor(app.styles.Table().BgColor)) + v.SetBorderColor(config.AsColor(app.styles.Table().FgColor)) + v.SetBorderFocusColor(config.AsColor(app.styles.Frame().Border.FocusColor)) + v.SetBorderAttributes(tcell.AttrBold) + v.SetBorderPadding(0, 0, 1, 1) + v.SetSelectable(true, false) + v.SetSelectedStyle( + tcell.ColorBlack, + config.AsColor(app.styles.Table().CursorColor), + tcell.AttrBold, + ) + + return &v +} + +func (v *resTable) formatCell(numerical bool, header, field string, padding int) (string, int) { + if header == "AGE" { + dur, err := time.ParseDuration(field) + if err == nil { + field = duration.HumanDuration(dur) + } + } + + if numerical || cpuRX.MatchString(header) || memRX.MatchString(header) { + return field, tview.AlignRight + } + + align := tview.AlignLeft + if isASCII(field) { + return pad(field, padding), align + } + + return field, align +} + +func (v *resTable) clearSelection() { + v.Select(0, 0) + v.ScrollToBeginning() +} + +func (v *resTable) selectFirstRow() { + if v.GetRowCount() > 0 { + v.Select(1, 0) + } +} + +func (v *resTable) setDeleted() { + r, _ := v.GetSelection() + cols := v.GetColumnCount() + for x := 0; x < cols; x++ { + v.GetCell(r, x).SetAttributes(tcell.AttrDim) + } +} + +// SetActions sets up keyboard action listener. +func (v *resTable) setActions(aa keyActions) { + for k, a := range aa { + v.actions[k] = a + } +} + +// Hints options +func (v *resTable) hints() hints { + if v.actions != nil { + return v.actions.toHints() + } + + return nil +} + +func (v *resTable) nameColIndex() int { + col := 0 + if v.currentNS == resource.AllNamespaces { + col++ + } + return col +} + +func (v *resTable) saveCmd(evt *tcell.EventKey) *tcell.EventKey { + if path, err := saveTable(v.app.config.K9s.CurrentCluster, v.baseTitle, v.data); err != nil { + v.app.flash().err(err) + } else { + v.app.flash().infof("File %s saved successfully!", path) + } + + return nil +} diff --git a/internal/views/table.go b/internal/views/table.go index 5397ca4e..5f24c89b 100644 --- a/internal/views/table.go +++ b/internal/views/table.go @@ -5,63 +5,31 @@ import ( "fmt" "regexp" "strings" - "time" "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/resource" "github.com/derailed/tview" "github.com/gdamore/tcell" "github.com/rs/zerolog/log" - "k8s.io/apimachinery/pkg/util/duration" ) -type ( - resTable struct { - *tview.Table +type tableView struct { + *resTable - app *appView - baseTitle string - currentNS string - data resource.TableData - actions keyActions - } - - tableView struct { - *resTable - - cmdBuff *cmdBuff - colorerFn colorerFn - sortFn sortFn - cleanseFn cleanseFn - filterFn func(string) - sortCol sortColumn - } -) + cmdBuff *cmdBuff + colorerFn colorerFn + sortFn sortFn + cleanseFn cleanseFn + filterFn func(string) + sortCol sortColumn +} func newTableView(app *appView, title string) *tableView { v := tableView{ - resTable: &resTable{ - Table: tview.NewTable(), - app: app, - actions: make(keyActions), - baseTitle: title, - }, - sortCol: sortColumn{0, 0, true}, - cmdBuff: newCmdBuff('/'), + resTable: newResTable(app, title), + cmdBuff: newCmdBuff('/'), + sortCol: sortColumn{0, 0, true}, } - v.SetFixed(1, 0) - v.SetBorder(true) - v.SetBackgroundColor(config.AsColor(app.styles.Table().BgColor)) - v.SetBorderColor(config.AsColor(app.styles.Table().FgColor)) - v.SetBorderFocusColor(config.AsColor(app.styles.Frame().Border.FocusColor)) - v.SetBorderAttributes(tcell.AttrBold) - v.SetBorderPadding(0, 0, 1, 1) - v.SetSelectable(true, false) - v.SetSelectedStyle( - tcell.ColorBlack, - config.AsColor(app.styles.Table().CursorColor), - tcell.AttrBold, - ) v.cmdBuff.addListener(app.cmd()) v.cmdBuff.reset() @@ -71,6 +39,10 @@ func newTableView(app *appView, title string) *tableView { return &v } +func (v *tableView) setFilterFn(fn func(string)) { + v.filterFn = fn +} + func (v *tableView) bindKeys() { v.actions = keyActions{ tcell.KeyCtrlS: newKeyAction("Save", v.saveCmd, true), @@ -86,15 +58,6 @@ func (v *tableView) bindKeys() { } } -func (v *tableView) setFilterFn(fn func(string)) { - v.filterFn = fn -} - -func (v *tableView) clearSelection() { - v.Select(0, 0) - v.ScrollToBeginning() -} - func (v *tableView) keyboard(evt *tcell.EventKey) *tcell.EventKey { key := evt.Key() if key == tcell.KeyRune { @@ -119,22 +82,6 @@ func (v *tableView) keyboard(evt *tcell.EventKey) *tcell.EventKey { return evt } -func (v *tableView) selectFirstRow() { - if v.GetRowCount() > 0 { - v.Select(1, 0) - } -} - -func (v *tableView) saveCmd(evt *tcell.EventKey) *tcell.EventKey { - if path, err := saveTable(v.app.config.K9s.CurrentCluster, v.baseTitle, v.data); err != nil { - v.app.flash().err(err) - } else { - v.app.flash().infof("File %s saved successfully!", path) - } - - return nil -} - func (v *tableView) filterCmd(evt *tcell.EventKey) *tcell.EventKey { if v.cmdBuff.isActive() { v.cmdBuff.setActive(false) @@ -171,14 +118,6 @@ func (v *tableView) resetCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } -func (v *tableView) nameColIndex() int { - col := 0 - if v.currentNS == resource.AllNamespaces { - col++ - } - return col -} - func (v *tableView) sortColCmd(col int) func(evt *tcell.EventKey) *tcell.EventKey { return func(evt *tcell.EventKey) *tcell.EventKey { if col == -1 { @@ -214,35 +153,11 @@ func (v *tableView) activateCmd(evt *tcell.EventKey) *tcell.EventKey { return nil } -func (v *tableView) setDeleted() { - r, _ := v.GetSelection() - cols := v.GetColumnCount() - for x := 0; x < cols; x++ { - v.GetCell(r, x).SetAttributes(tcell.AttrDim) - } -} - // SetColorer sets up table row color management. func (v *tableView) setColorer(f colorerFn) { v.colorerFn = f } -// SetActions sets up keyboard action listener. -func (v *tableView) setActions(aa keyActions) { - for k, a := range aa { - v.actions[k] = a - } -} - -// Hints options -func (v *tableView) hints() hints { - if v.actions != nil { - return v.actions.toHints() - } - - return nil -} - func (v *tableView) refresh() { v.update(v.data) } @@ -365,26 +280,6 @@ func (v *tableView) addHeaderCell(numerical bool, col int, name string) { v.SetCell(0, col, c) } -func (v *tableView) formatCell(numerical bool, header, field string, padding int) (string, int) { - if header == "AGE" { - dur, err := time.ParseDuration(field) - if err == nil { - field = duration.HumanDuration(dur) - } - } - - if numerical || cpuRX.MatchString(header) || memRX.MatchString(header) { - return field, tview.AlignRight - } - - align := tview.AlignLeft - if isASCII(field) { - return pad(field, padding), align - } - - return field, align -} - func (v *tableView) resetTitle() { var title string