-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
logger.go
172 lines (138 loc) 路 3.9 KB
/
logger.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package view
import (
"context"
"github.com/atotto/clipboard"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/k9s/internal/ui"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
)
// Logger represents a generic log viewer.
type Logger struct {
*tview.TextView
actions ui.KeyActions
app *App
title, subject string
cmdBuff *model.FishBuff
}
// NewLogger returns a logger viewer.
func NewLogger(app *App) *Logger {
return &Logger{
TextView: tview.NewTextView(),
app: app,
actions: make(ui.KeyActions),
cmdBuff: model.NewFishBuff('/', model.FilterBuffer),
}
}
// Init initializes the viewer.
func (l *Logger) Init(_ context.Context) error {
if l.title != "" {
l.SetBorder(true)
}
l.SetScrollable(true).SetWrap(true).SetRegions(true)
l.SetDynamicColors(true)
l.SetHighlightColor(tcell.ColorOrange)
l.SetTitleColor(tcell.ColorAqua)
l.SetInputCapture(l.keyboard)
l.SetBorderPadding(0, 0, 1, 1)
l.app.Styles.AddListener(l)
l.StylesChanged(l.app.Styles)
l.app.Prompt().SetModel(l.cmdBuff)
l.cmdBuff.AddListener(l)
l.bindKeys()
l.SetInputCapture(l.keyboard)
return nil
}
// BufferChanged indicates the buffer was changel.
func (l *Logger) BufferChanged(s string) {}
// BufferCompleted indicates input was acceptel.
func (l *Logger) BufferCompleted(s string) {
}
// BufferActive indicates the buff activity changel.
func (l *Logger) BufferActive(state bool, k model.BufferKind) {
l.app.BufferActive(state, k)
}
func (l *Logger) bindKeys() {
l.actions.Set(ui.KeyActions{
tcell.KeyEscape: ui.NewKeyAction("Back", l.resetCmd, false),
tcell.KeyCtrlS: ui.NewKeyAction("Save", l.saveCmd, false),
ui.KeyC: ui.NewKeyAction("Copy", l.cpCmd, true),
ui.KeySlash: ui.NewSharedKeyAction("Filter Mode", l.activateCmd, false),
tcell.KeyDelete: ui.NewSharedKeyAction("Erase", l.eraseCmd, false),
})
}
func (l *Logger) keyboard(evt *tcell.EventKey) *tcell.EventKey {
if a, ok := l.actions[ui.AsKey(evt)]; ok {
return a.Action(evt)
}
return evt
}
// StylesChanged notifies the skin changel.
func (l *Logger) StylesChanged(s *config.Styles) {
l.SetBackgroundColor(l.app.Styles.BgColor())
l.SetTextColor(l.app.Styles.FgColor())
l.SetBorderFocusColor(l.app.Styles.Frame().Border.FocusColor.Color())
}
// SetSubject updates the subject.
func (l *Logger) SetSubject(s string) {
l.subject = s
}
// Actions returns menu actions
func (l *Logger) Actions() ui.KeyActions {
return l.actions
}
// Name returns the component name.
func (l *Logger) Name() string { return l.title }
// Start starts the view updater.
func (l *Logger) Start() {}
// Stop terminates the updater.
func (l *Logger) Stop() {
l.app.Styles.RemoveListener(l)
}
// Hints returns menu hints.
func (l *Logger) Hints() model.MenuHints {
return l.actions.Hints()
}
// ExtraHints returns additional hints.
func (l *Logger) ExtraHints() map[string]string {
return nil
}
func (l *Logger) activateCmd(evt *tcell.EventKey) *tcell.EventKey {
if l.app.InCmdMode() {
return evt
}
l.app.ResetPrompt(l.cmdBuff)
return nil
}
func (l *Logger) eraseCmd(evt *tcell.EventKey) *tcell.EventKey {
if !l.cmdBuff.IsActive() {
return nil
}
l.cmdBuff.Delete()
return nil
}
func (l *Logger) resetCmd(evt *tcell.EventKey) *tcell.EventKey {
if !l.cmdBuff.InCmdMode() {
l.cmdBuff.Reset()
return l.app.PrevCmd(evt)
}
l.cmdBuff.SetActive(false)
l.cmdBuff.Reset()
return nil
}
func (l *Logger) saveCmd(evt *tcell.EventKey) *tcell.EventKey {
if path, err := saveYAML(l.app.Config.K9s.CurrentCluster, l.title, l.GetText(true)); err != nil {
l.app.Flash().Err(err)
} else {
l.app.Flash().Infof("Log %s saved successfully!", path)
}
return nil
}
func (l *Logger) cpCmd(evt *tcell.EventKey) *tcell.EventKey {
l.app.Flash().Info("Content copied to clipboard...")
if err := clipboard.WriteAll(l.GetText(true)); err != nil {
l.app.Flash().Err(err)
}
return nil
}