github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/menu.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package component 18 19 import ( 20 "fmt" 21 "math" 22 23 "github.com/rivo/tview" 24 25 "github.com/oam-dev/kubevela/references/cli/top/config" 26 "github.com/oam-dev/kubevela/references/cli/top/model" 27 ) 28 29 // Menu is menu component which display key actions of app's main view 30 type Menu struct { 31 *tview.Table 32 style *config.ThemeConfig 33 } 34 35 // NewMenu return a new menu instance 36 func NewMenu(config *config.ThemeConfig) *Menu { 37 m := &Menu{ 38 Table: tview.NewTable(), 39 style: config, 40 } 41 return m 42 } 43 44 // StackPop change itself when accept "pop" notify from app's main view 45 func (m *Menu) StackPop(_, new model.View) { 46 if new == nil { 47 m.UpdateMenu([]model.MenuHint{}) 48 } else { 49 m.UpdateMenu(new.Hint()) 50 } 51 } 52 53 // StackPush change itself when accept "push" notify from app's main view 54 func (m *Menu) StackPush(_, new model.View) { 55 m.UpdateMenu(new.Hint()) 56 } 57 58 // UpdateMenu update menu component 59 func (m *Menu) UpdateMenu(hints []model.MenuHint) { 60 m.Clear() 61 // convert one-dimensional hints to two-dimensional hints 62 tableCellStrHint := make([][]model.MenuHint, config.MenuRowNum) 63 columCount := int(math.Ceil(float64(len(hints)) / config.MenuRowNum)) 64 for row := 0; row < config.MenuRowNum; row++ { 65 tableCellStrHint[row] = make([]model.MenuHint, columCount) 66 } 67 68 // convert two-dimensional hints to two-dimensional string 69 tableCellStr := m.buildMenuTable(hints, tableCellStrHint) 70 for row := 0; row < len(tableCellStr); row++ { 71 for col := 0; col < len(tableCellStr[row]); col++ { 72 c := tview.NewTableCell(tableCellStr[row][col]) 73 if len(tableCellStr[row][col]) == 0 { 74 c = tview.NewTableCell("") 75 } 76 m.SetCell(row, col, c) 77 } 78 } 79 } 80 81 func (m *Menu) buildMenuTable(hints []model.MenuHint, table [][]model.MenuHint) [][]string { 82 var row, col int 83 for _, h := range hints { 84 table[row][col] = h 85 row++ 86 if row == config.MenuRowNum { 87 row, col = 0, col+1 88 } 89 } 90 out := make([][]string, len(table)) 91 for r := range out { 92 out[r] = make([]string, len(table[r])) 93 } 94 for r := range table { 95 for c := range table[r] { 96 out[r][c] = m.formatPlainMenu(table[r][c]) 97 } 98 } 99 return out 100 } 101 102 func (m *Menu) formatPlainMenu(h model.MenuHint) string { 103 return fmt.Sprintf("[%s:-:b]%-8s [%s:-:b]%s", m.style.Menu.Key, menuFormat(h), m.style.Menu.Description, h.Description) 104 } 105 106 func menuFormat(hint model.MenuHint) string { 107 if len(hint.Key) == 0 && len(hint.Description) == 0 { 108 return "" 109 } 110 return fmt.Sprintf("<%s>", hint.Key) 111 }