-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
cow.go
148 lines (123 loc) · 3.11 KB
/
cow.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
package view
import (
"context"
"fmt"
"strings"
"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"
)
// Cow represents a bomb viewer.
type Cow struct {
*tview.TextView
actions ui.KeyActions
app *App
says string
}
// NewCow returns a have a cow viewer.
func NewCow(app *App, says string) *Cow {
return &Cow{
TextView: tview.NewTextView(),
app: app,
actions: make(ui.KeyActions),
says: says,
}
}
// Init initializes the viewer.
func (c *Cow) Init(_ context.Context) error {
c.SetBorder(true)
c.SetScrollable(true).SetWrap(true).SetRegions(true)
c.SetDynamicColors(true)
c.SetHighlightColor(tcell.ColorOrange)
c.SetTitleColor(tcell.ColorAqua)
c.SetInputCapture(c.keyboard)
c.SetBorderPadding(0, 0, 1, 1)
c.updateTitle()
c.SetTextAlign(tview.AlignCenter)
c.app.Styles.AddListener(c)
c.StylesChanged(c.app.Styles)
c.bindKeys()
c.SetInputCapture(c.keyboard)
c.talk()
return nil
}
// InCmdMode checks if prompt is active.
func (*Cow) InCmdMode() bool {
return false
}
func (c *Cow) talk() {
says := c.says
if len(says) == 0 {
says = "Nothing to report here. Please move along..."
}
x, _, w, _ := c.GetRect()
c.SetText(cowTalk(says, (x+w)/2))
}
func cowTalk(says string, w int) string {
msg := fmt.Sprintf("[red::]< [::b]Ruroh? %s[::-] >", says)
buff := make([]string, 0, len(cow)+3)
buff = append(buff, "[red::] "+strings.Repeat("─", len(says)+8))
buff = append(buff, msg)
buff = append(buff, " "+strings.Repeat("─", len(says)+8))
rCount := w/2 - 8
if rCount < 0 {
rCount = w / 2
}
spacer := strings.Repeat(" ", rCount)
for _, s := range cow {
buff = append(buff, "[red::b]"+spacer+s)
}
return strings.Join(buff, "\n")
}
func (c *Cow) bindKeys() {
c.actions.Set(ui.KeyActions{
tcell.KeyEscape: ui.NewKeyAction("Back", c.resetCmd, false),
})
}
func (c *Cow) keyboard(evt *tcell.EventKey) *tcell.EventKey {
if a, ok := c.actions[ui.AsKey(evt)]; ok {
return a.Action(evt)
}
return evt
}
// StylesChanged notifies the skin changes.
func (c *Cow) StylesChanged(s *config.Styles) {
c.SetBackgroundColor(c.app.Styles.BgColor())
c.SetTextColor(c.app.Styles.FgColor())
c.SetBorderFocusColor(c.app.Styles.Frame().Border.FocusColor.Color())
}
func (c *Cow) resetCmd(evt *tcell.EventKey) *tcell.EventKey {
return c.app.PrevCmd(evt)
}
// Actions returns menu actions.
func (c *Cow) Actions() ui.KeyActions {
return c.actions
}
// Name returns the component name.
func (c *Cow) Name() string { return "cow" }
// Start starts the view updater.
func (c *Cow) Start() {}
// Stop terminates the updater.
func (c *Cow) Stop() {
c.app.Styles.RemoveListener(c)
}
// Hints returns menu hints.
func (c *Cow) Hints() model.MenuHints {
return c.actions.Hints()
}
// ExtraHints returns additional hints.
func (c *Cow) ExtraHints() map[string]string {
return nil
}
func (c *Cow) updateTitle() {
c.SetTitle(" Error ")
}
var cow = []string{
`\ ^__^ `,
` \ (oo)\_______ `,
` (__)\ )\/\`,
` ||----w | `,
` || || `,
}