-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
flash.go
111 lines (96 loc) · 1.98 KB
/
flash.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
package ui
import (
"context"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/tview"
"github.com/gdamore/tcell/v2"
"github.com/rs/zerolog/log"
)
const (
emoHappy = "😎"
emoDoh = "😗"
emoRed = "😡"
)
// Flash represents a flash message indicator.
type Flash struct {
*tview.TextView
app *App
testMode bool
}
// NewFlash returns a new flash view.
func NewFlash(app *App) *Flash {
f := Flash{
app: app,
TextView: tview.NewTextView(),
}
f.SetTextColor(tcell.ColorAqua)
f.SetDynamicColors(true)
f.SetTextAlign(tview.AlignCenter)
f.SetBorderPadding(0, 0, 1, 1)
f.app.Styles.AddListener(&f)
return &f
}
// SetTestMode for testing ONLY!
func (f *Flash) SetTestMode(b bool) {
f.testMode = b
}
// StylesChanged notifies listener the skin changed.
func (f *Flash) StylesChanged(s *config.Styles) {
f.SetBackgroundColor(s.BgColor())
f.SetTextColor(s.FgColor())
}
// Watch watches for flash changes.
func (f *Flash) Watch(ctx context.Context, c model.FlashChan) {
defer log.Debug().Msgf("Flash Watch Canceled!")
for {
select {
case <-ctx.Done():
return
case msg := <-c:
f.SetMessage(msg)
}
}
}
// SetMessage sets flash message and level.
func (f *Flash) SetMessage(m model.LevelMessage) {
fn := func() {
if m.Text == "" {
f.Clear()
return
}
f.SetTextColor(flashColor(m.Level))
f.SetText(f.flashEmoji(m.Level) + " " + m.Text)
}
if f.testMode {
fn()
} else {
f.app.QueueUpdateDraw(fn)
}
}
func (f *Flash) flashEmoji(l model.FlashLevel) string {
if f.app.Config.K9s.NoIcons {
return ""
}
// nolint:exhaustive
switch l {
case model.FlashWarn:
return emoDoh
case model.FlashErr:
return emoRed
default:
return emoHappy
}
}
// Helpers...
func flashColor(l model.FlashLevel) tcell.Color {
// nolint:exhaustive
switch l {
case model.FlashWarn:
return tcell.ColorOrange
case model.FlashErr:
return tcell.ColorOrangeRed
default:
return tcell.ColorNavajoWhite
}
}