add ctop header, disabled by default

This commit is contained in:
Bradley Cicenas 2017-01-06 13:49:22 +00:00
parent f311aad105
commit bebdfd844f
4 changed files with 62 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import (
"os"
)
var GlobalConfig = NewDefaultConfig()
var configChan = make(chan ConfigMsg)
type Config map[string]string
@ -23,8 +24,9 @@ func NewDefaultConfig() Config {
docker = "unix:///var/run/docker.sock"
}
config := Config{
"dockerHost": docker,
"sortField": "id",
"dockerHost": docker,
"sortField": "id",
"enableHeader": "0",
}
go func() {
for m := range configChan {

View File

@ -9,16 +9,13 @@ var filters = map[string][]string{
}
func NewContainerMap() *ContainerMap {
config := NewDefaultConfig()
// init docker client
client, err := docker.NewClient(config["dockerHost"])
client, err := docker.NewClient(GlobalConfig["dockerHost"])
if err != nil {
panic(err)
}
cm := &ContainerMap{
config: config,
client: client,
containers: make(map[string]*Container),
}
@ -27,7 +24,6 @@ func NewContainerMap() *ContainerMap {
}
type ContainerMap struct {
config Config
client *docker.Client
containers map[string]*Container
}
@ -66,6 +62,6 @@ func (cm *ContainerMap) All() []*Container {
for _, c := range cm.containers {
containers = append(containers, c)
}
SortContainers(cm.config["sortField"], containers)
SortContainers(GlobalConfig["sortField"], containers)
return containers
}

11
grid.go
View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/bcicen/ctop/widgets"
ui "github.com/gizak/termui"
)
@ -10,6 +11,7 @@ type Grid struct {
cursorID string // id of currently selected container
containers []*Container
containerMap *ContainerMap
header *widgets.CTopHeader
}
func NewGrid() *Grid {
@ -19,6 +21,7 @@ func NewGrid() *Grid {
cursorID: containers[0].id,
containers: containers,
containerMap: containerMap,
header: widgets.NewCTopHeader(),
}
}
@ -67,7 +70,11 @@ func (g *Grid) redrawRows() {
ui.Body.Rows = []*ui.Row{}
// build layout
ui.Body.AddRows(header())
if GlobalConfig["enableHeader"] == "1" {
g.header.SetCount(len(g.containers))
ui.Body.AddRows(g.header.Row())
}
ui.Body.AddRows(fieldHeader())
for _, c := range g.containers {
ui.Body.AddRows(c.widgets.Row())
}
@ -76,7 +83,7 @@ func (g *Grid) redrawRows() {
ui.Render(ui.Body)
}
func header() *ui.Row {
func fieldHeader() *ui.Row {
return ui.NewRow(
ui.NewCol(2, 0, headerPar("NAME")),
ui.NewCol(2, 0, headerPar("CID")),

47
widgets/header.go Normal file
View File

@ -0,0 +1,47 @@
package widgets
import (
"fmt"
"time"
ui "github.com/gizak/termui"
)
type CTopHeader struct {
Time *ui.Par
Count *ui.Par
}
func NewCTopHeader() *CTopHeader {
return &CTopHeader{
Time: headerPar(timeStr()),
Count: headerPar("-"),
}
}
func (c *CTopHeader) Row() *ui.Row {
c.Time.Text = timeStr()
return ui.NewRow(
ui.NewCol(2, 0, c.Time),
ui.NewCol(2, 0, c.Count),
)
}
func (c *CTopHeader) SetCount(val int) {
c.Count.Text = fmt.Sprintf("%d containers", val)
}
func timeStr() string {
return time.Now().Local().Format("15:04:05 MST")
}
func headerPar(s string) *ui.Par {
p := ui.NewPar(fmt.Sprintf(" %s", s))
p.Border = false
p.Height = 1
p.Width = 20
p.TextFgColor = ui.ColorDefault
p.TextBgColor = ui.ColorWhite
p.Bg = ui.ColorWhite
return p
}