-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
log_indicator.go
158 lines (131 loc) · 4.16 KB
/
log_indicator.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package view
import (
"fmt"
"sync/atomic"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/tview"
)
const spacer = " "
// LogIndicator represents a log view indicator.
type LogIndicator struct {
*tview.TextView
styles *config.Styles
scrollStatus int32
indicator []byte
fullScreen bool
textWrap bool
showTime bool
allContainers bool
shouldDisplayAllContainers bool
}
// NewLogIndicator returns a new indicator.
func NewLogIndicator(cfg *config.Config, styles *config.Styles, allContainers bool) *LogIndicator {
l := LogIndicator{
styles: styles,
TextView: tview.NewTextView(),
indicator: make([]byte, 0, 100),
scrollStatus: 1,
fullScreen: cfg.K9s.UI.DefaultsToFullScreen,
textWrap: cfg.K9s.Logger.TextWrap,
showTime: cfg.K9s.Logger.ShowTime,
shouldDisplayAllContainers: allContainers,
}
l.StylesChanged(styles)
styles.AddListener(&l)
l.SetTextAlign(tview.AlignCenter)
l.SetDynamicColors(true)
return &l
}
// StylesChanged notifies listener the skin changed.
func (l *LogIndicator) StylesChanged(styles *config.Styles) {
l.SetBackgroundColor(styles.K9s.Views.Log.Indicator.BgColor.Color())
l.SetTextColor(styles.K9s.Views.Log.Indicator.FgColor.Color())
l.Refresh()
}
// AutoScroll reports the current scrolling status.
func (l *LogIndicator) AutoScroll() bool {
return atomic.LoadInt32(&l.scrollStatus) == 1
}
// Timestamp reports the current timestamp mode.
func (l *LogIndicator) Timestamp() bool {
return l.showTime
}
// TextWrap reports the current wrap mode.
func (l *LogIndicator) TextWrap() bool {
return l.textWrap
}
// FullScreen reports the current screen mode.
func (l *LogIndicator) FullScreen() bool {
return l.fullScreen
}
// ToggleTimestamp toggles the current timestamp mode.
func (l *LogIndicator) ToggleTimestamp() {
l.showTime = !l.showTime
}
// ToggleFullScreen toggles the screen mode.
func (l *LogIndicator) ToggleFullScreen() {
l.fullScreen = !l.fullScreen
l.Refresh()
}
// ToggleTextWrap toggles the wrap mode.
func (l *LogIndicator) ToggleTextWrap() {
l.textWrap = !l.textWrap
l.Refresh()
}
// ToggleAutoScroll toggles the scroll mode.
func (l *LogIndicator) ToggleAutoScroll() {
var val int32 = 1
if l.AutoScroll() {
val = 0
}
atomic.StoreInt32(&l.scrollStatus, val)
l.Refresh()
}
// ToggleAllContainers toggles the all-containers mode.
func (l *LogIndicator) ToggleAllContainers() {
l.allContainers = !l.allContainers
l.Refresh()
}
func (l *LogIndicator) reset() {
l.Clear()
l.indicator = l.indicator[:0]
}
// Refresh updates the view.
func (l *LogIndicator) Refresh() {
l.reset()
var (
toggleFmt = "[::b]%s:["
toggleOnFmt = toggleFmt + string(l.styles.K9s.Views.Log.Indicator.ToggleOnColor) + "::b]On[-::] %s"
toggleOffFmt = toggleFmt + string(l.styles.K9s.Views.Log.Indicator.ToggleOffColor) + "::d]Off[-::]%s"
)
if l.shouldDisplayAllContainers {
if l.allContainers {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOnFmt, "AllContainers", spacer)...)
} else {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOffFmt, "AllContainers", spacer)...)
}
}
if l.AutoScroll() {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOnFmt, "Autoscroll", spacer)...)
} else {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOffFmt, "Autoscroll", spacer)...)
}
if l.FullScreen() {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOnFmt, "FullScreen", spacer)...)
} else {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOffFmt, "FullScreen", spacer)...)
}
if l.Timestamp() {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOnFmt, "Timestamps", spacer)...)
} else {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOffFmt, "Timestamps", spacer)...)
}
if l.TextWrap() {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOnFmt, "Wrap", "")...)
} else {
l.indicator = append(l.indicator, fmt.Sprintf(toggleOffFmt, "Wrap", "")...)
}
_, _ = l.Write(l.indicator)
}