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