forked from bcicen/ctop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
88 lines (75 loc) · 1.54 KB
/
header.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
package widgets
import (
"fmt"
"time"
ui "github.com/gizak/termui"
)
type CTopHeader struct {
Time *ui.Par
Count *ui.Par
Filter *ui.Par
bg *ui.Par
}
func NewCTopHeader() *CTopHeader {
return &CTopHeader{
Time: headerPar(2, ""),
Count: headerPar(24, "-"),
Filter: headerPar(40, ""),
bg: headerBg(),
}
}
func (c *CTopHeader) Buffer() ui.Buffer {
buf := ui.NewBuffer()
c.Time.Text = timeStr()
buf.Merge(c.bg.Buffer())
buf.Merge(c.Time.Buffer())
buf.Merge(c.Count.Buffer())
buf.Merge(c.Filter.Buffer())
return buf
}
func (c *CTopHeader) Align() {
c.bg.SetWidth(ui.TermWidth() - 1)
}
func (c *CTopHeader) Height() int {
return c.bg.Height
}
func headerBgBordered() *ui.Par {
bg := ui.NewPar("")
bg.X = 1
bg.Height = 3
bg.Bg = ui.ThemeAttr("header.bg")
return bg
}
func headerBg() *ui.Par {
bg := ui.NewPar("")
bg.X = 1
bg.Height = 1
bg.Border = false
bg.Bg = ui.ThemeAttr("header.bg")
return bg
}
func (c *CTopHeader) SetCount(val int) {
c.Count.Text = fmt.Sprintf("%d containers", val)
}
func (c *CTopHeader) SetFilter(val string) {
if val == "" {
c.Filter.Text = ""
} else {
c.Filter.Text = fmt.Sprintf("filter: %s", val)
}
}
func timeStr() string {
ts := time.Now().Local().Format("15:04:05 MST")
return fmt.Sprintf("ctop - %s", ts)
}
func headerPar(x int, s string) *ui.Par {
p := ui.NewPar(fmt.Sprintf(" %s", s))
p.X = x
p.Border = false
p.Height = 1
p.Width = 20
p.Bg = ui.ThemeAttr("header.bg")
p.TextFgColor = ui.ThemeAttr("header.fg")
p.TextBgColor = ui.ThemeAttr("header.bg")
return p
}