-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
crumbs.go
71 lines (60 loc) · 1.61 KB
/
crumbs.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
package ui
import (
"fmt"
"strings"
"github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/model"
"github.com/derailed/tview"
)
// Crumbs represents user breadcrumbs.
type Crumbs struct {
*tview.TextView
styles *config.Styles
stack *model.Stack
}
// NewCrumbs returns a new breadcrumb view.
func NewCrumbs(styles *config.Styles) *Crumbs {
c := Crumbs{
stack: model.NewStack(),
styles: styles,
TextView: tview.NewTextView(),
}
c.SetBackgroundColor(styles.BgColor())
c.SetTextAlign(tview.AlignLeft)
c.SetBorderPadding(0, 0, 1, 1)
c.SetDynamicColors(true)
styles.AddListener(&c)
return &c
}
// StylesChanged notifies skin changed.
func (c *Crumbs) StylesChanged(s *config.Styles) {
c.styles = s
c.SetBackgroundColor(s.BgColor())
c.refresh(c.stack.Flatten())
}
// StackPushed indicates a new item was added.
func (c *Crumbs) StackPushed(comp model.Component) {
c.stack.Push(comp)
c.refresh(c.stack.Flatten())
}
// StackPopped indicates an item was deleted
func (c *Crumbs) StackPopped(_, _ model.Component) {
c.stack.Pop()
c.refresh(c.stack.Flatten())
}
// StackTop indicates the top of the stack
func (c *Crumbs) StackTop(top model.Component) {}
// Refresh updates view with new crumbs.
func (c *Crumbs) refresh(crumbs []string) {
c.Clear()
last, bgColor := len(crumbs)-1, c.styles.Frame().Crumb.BgColor
for i, crumb := range crumbs {
if i == last {
bgColor = c.styles.Frame().Crumb.ActiveColor
}
fmt.Fprintf(c, "[%s:%s:b] <%s> [-:%s:-] ",
c.styles.Frame().Crumb.FgColor,
bgColor, strings.Replace(strings.ToLower(crumb), " ", "", -1),
c.styles.Body().BgColor)
}
}