Files
kubevela/references/cli/top/component/menu.go
Siege Lion 2e18eaa3b2 Feat: add filtering features to the Application view and K8S object view of vela top (#4612)
* Feat: when `vela top` launch, can specify the namespace of the presentation application

Signed-off-by: HanMengnan <1448189829@qq.com>

* Feat: add filtering function to the k8s object view of the `vela top` command, which can be filtered by cluster and cluster namespace

Signed-off-by: HanMengnan <1448189829@qq.com>

Signed-off-by: HanMengnan <1448189829@qq.com>
2022-08-15 10:36:12 +08:00

113 lines
2.9 KiB
Go

/*
Copyright 2022 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package component
import (
"fmt"
"math"
"strconv"
"github.com/rivo/tview"
"github.com/oam-dev/kubevela/references/cli/top/config"
"github.com/oam-dev/kubevela/references/cli/top/model"
)
// Menu is menu component which display key actions of app's main view
type Menu struct {
*tview.Table
}
// NewMenu return a new menu instance
func NewMenu() *Menu {
m := &Menu{
Table: tview.NewTable(),
}
return m
}
// StackPop change itself when accept "pop" notify from app's main view
func (m *Menu) StackPop(old, new model.Component) {
if new == nil {
m.UpdateMenu([]model.MenuHint{})
} else {
m.UpdateMenu(new.Hint())
}
}
// StackPush change itself when accept "push" notify from app's main view
func (m *Menu) StackPush(component model.Component) {
m.UpdateMenu(component.Hint())
}
// UpdateMenu update menu component
func (m *Menu) UpdateMenu(hints []model.MenuHint) {
m.Clear()
// convert one-dimensional hints to two-dimensional hints
tableCellStrHint := make([][]model.MenuHint, config.MenuRowNum)
columCount := int(math.Ceil(float64(len(hints)) / config.MenuRowNum))
for row := 0; row < config.MenuRowNum; row++ {
tableCellStrHint[row] = make([]model.MenuHint, columCount)
}
// convert two-dimensional hints to two-dimensional string
tableCellStr := m.buildMenuTable(hints, tableCellStrHint)
for row := 0; row < len(tableCellStr); row++ {
for col := 0; col < len(tableCellStr[row]); col++ {
c := tview.NewTableCell(tableCellStr[row][col])
if len(tableCellStr[row][col]) == 0 {
c = tview.NewTableCell("")
}
m.SetCell(row, col, c)
}
}
}
func (m *Menu) buildMenuTable(hints []model.MenuHint, table [][]model.MenuHint) [][]string {
var row, col int
for _, h := range hints {
table[row][col] = h
row++
if row == config.MenuRowNum {
row, col = 0, col+1
}
}
out := make([][]string, len(table))
for r := range out {
out[r] = make([]string, len(table[r]))
}
for r := range table {
for c := range table[r] {
out[r][c] = formatPlainMenu(table[r][c])
}
}
return out
}
func formatPlainMenu(h model.MenuHint) string {
menuFmt := " [blue:-:b]%-" + strconv.Itoa(10) + "s [:-:b]%s "
return fmt.Sprintf(menuFmt, menuFormat(h), h.Description)
}
func menuFormat(hint model.MenuHint) string {
if len(hint.Key) == 0 && len(hint.Description) == 0 {
return ""
}
return fmt.Sprintf("<%s>", hint.Key)
}