github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/table.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  	"github.com/gdamore/tcell/v2"
    21  	"github.com/rivo/tview"
    22  
    23  	"github.com/oam-dev/kubevela/references/cli/top/config"
    24  	"github.com/oam-dev/kubevela/references/cli/top/model"
    25  )
    26  
    27  // Table is a base table component which can be reused by other component
    28  type Table struct {
    29  	*tview.Table
    30  	style   *config.ThemeConfig
    31  	actions model.KeyActions
    32  }
    33  
    34  // NewTable return a new table component
    35  func NewTable(style *config.ThemeConfig) *Table {
    36  	return &Table{
    37  		Table:   tview.NewTable(),
    38  		style:   style,
    39  		actions: make(model.KeyActions),
    40  	}
    41  }
    42  
    43  // Init table component
    44  func (t *Table) Init() {
    45  	t.SetBorderAttributes(tcell.AttrItalic)
    46  	t.SetBorder(true)
    47  	t.SetBorderColor(t.style.Border.Table.Color())
    48  	t.SetBorderPadding(1, 1, 1, 1)
    49  	t.SetInputCapture(t.keyboard)
    50  }
    51  
    52  // Name return table's name
    53  func (t *Table) Name() string {
    54  	return "table"
    55  }
    56  
    57  // Start table component
    58  func (t *Table) Start() {
    59  }
    60  
    61  // Stop table component
    62  func (t *Table) Stop() {
    63  	t.Clear()
    64  }
    65  
    66  // Hint return key action menu hints of the component
    67  func (t *Table) Hint() []model.MenuHint {
    68  	return t.actions.Hint()
    69  }
    70  
    71  // Actions return actions
    72  func (t *Table) Actions() model.KeyActions {
    73  	return t.actions
    74  }
    75  
    76  func (t *Table) keyboard(event *tcell.EventKey) *tcell.EventKey {
    77  	key := event.Key()
    78  	if key == tcell.KeyUp || key == tcell.KeyDown {
    79  		return event
    80  	}
    81  	if a, ok := t.Actions()[StandardizeKey(event)]; ok {
    82  		return a.Action(event)
    83  	}
    84  	return event
    85  }