github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/log_view.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 view 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "time" 24 25 "github.com/gdamore/tcell/v2" 26 "github.com/rivo/tview" 27 28 "github.com/oam-dev/kubevela/references/cli/top/component" 29 "github.com/oam-dev/kubevela/references/cli/top/model" 30 ) 31 32 // LogView is the log view, this view display log of pod 33 type LogView struct { 34 *tview.TextView 35 app *App 36 actions model.KeyActions 37 ctx context.Context 38 writer io.Writer 39 cancelFunc func() 40 } 41 42 var ( 43 logViewInstance = new(LogView) 44 ) 45 46 // NewLogView return a new log view 47 func NewLogView(ctx context.Context, app *App) model.View { 48 logViewInstance.ctx = ctx 49 if logViewInstance.TextView == nil { 50 logViewInstance.TextView = tview.NewTextView() 51 logViewInstance.app = app 52 logViewInstance.actions = make(model.KeyActions) 53 logViewInstance.writer = tview.ANSIWriter(logViewInstance.TextView) 54 } 55 return logViewInstance 56 } 57 58 // Init the log view 59 func (v *LogView) Init() { 60 title := fmt.Sprintf("[ %s ]", v.Name()) 61 v.SetDynamicColors(true) 62 v.SetBorder(true) 63 v.SetBorderAttributes(tcell.AttrItalic) 64 v.SetTitleColor(v.app.config.Theme.Table.Title.Color()) 65 v.SetTitle(title) 66 v.SetBorderPadding(1, 1, 2, 2) 67 v.bindKeys() 68 v.SetInputCapture(v.keyboard) 69 } 70 71 // Start the log view 72 func (v *LogView) Start() { 73 var ctx context.Context 74 ctx, v.cancelFunc = context.WithCancel(context.Background()) 75 76 cluster := v.ctx.Value(&model.CtxKeyCluster).(string) 77 pod := v.ctx.Value(&model.CtxKeyPod).(string) 78 namespace := v.ctx.Value(&model.CtxKeyNamespace).(string) 79 80 logC, err := model.PrintLogOfPod(ctx, v.app.config.RestConfig, cluster, namespace, pod, "") 81 if err != nil { 82 return 83 } 84 go func() { 85 for { 86 select { 87 case <-ctx.Done(): 88 return 89 case line := <-logC: 90 v.app.QueueUpdateDraw(func() { 91 _, _ = v.writer.Write([]byte(line)) 92 }) 93 default: 94 time.Sleep(time.Second) 95 } 96 } 97 }() 98 } 99 100 // Stop the log view 101 func (v *LogView) Stop() { 102 v.cancelFunc() 103 v.Clear() 104 } 105 106 // Name return the name of log view 107 func (v *LogView) Name() string { 108 return "Log" 109 } 110 111 // Hint return the menu hints of log view 112 func (v *LogView) Hint() []model.MenuHint { 113 return v.actions.Hint() 114 } 115 116 func (v *LogView) keyboard(event *tcell.EventKey) *tcell.EventKey { 117 key := event.Key() 118 if key == tcell.KeyUp || key == tcell.KeyDown { 119 return event 120 } 121 if a, ok := v.actions[component.StandardizeKey(event)]; ok { 122 return a.Action(event) 123 } 124 return event 125 } 126 127 func (v *LogView) bindKeys() { 128 v.actions.Delete([]tcell.Key{tcell.KeyEnter}) 129 v.actions.Add(model.KeyActions{ 130 component.KeyQ: model.KeyAction{Description: "Back", Action: v.app.Back, Visible: true, Shared: true}, 131 component.KeyHelp: model.KeyAction{Description: "Help", Action: v.app.helpView, Visible: true, Shared: true}, 132 }) 133 }