add names column to multiview

This commit is contained in:
Bradley Cicenas 2016-12-27 02:24:02 +00:00
parent 055257528a
commit 407e8fca0e
4 changed files with 45 additions and 59 deletions

View File

@ -2,24 +2,28 @@ package main
import (
"fmt"
"strings"
"github.com/fsouza/go-dockerclient"
)
type Container struct {
id string
name string
done chan bool
stats chan *docker.Stats
widgets *Widgets
reader *StatReader
}
func NewContainer(cid string) *Container {
func NewContainer(cid string, names []string) *Container {
name := strings.Join(names, ",")
return &Container{
id: cid,
name: name,
done: make(chan bool),
stats: make(chan *docker.Stats),
widgets: NewWidgets(cid),
widgets: NewWidgets(cid, name),
reader: &StatReader{},
}
}

57
grid.go
View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"sort"
ui "github.com/gizak/termui"
@ -11,8 +12,8 @@ type Grid struct {
containers map[string]*Container
}
func (g *Grid) AddContainer(id string) {
g.containers[id] = NewContainer(id)
func (g *Grid) AddContainer(id string, names []string) {
g.containers[id] = NewContainer(id, names)
}
// Return number of containers/rows
@ -48,52 +49,30 @@ func (g *Grid) Cursor() {
func (g *Grid) Rows() (rows []*ui.Row) {
for _, cid := range g.CIDs() {
c := g.containers[cid]
rows = append(rows, ui.NewRow(
ui.NewCol(1, 0, c.widgets.cid),
ui.NewCol(2, 0, c.widgets.cpu),
ui.NewCol(2, 0, c.widgets.memory),
ui.NewCol(2, 0, c.widgets.net),
))
rows = append(rows, c.widgets.MakeRow())
}
return rows
}
func header() *ui.Row {
// cid
c1 := ui.NewPar(" CID")
c1.Border = false
c1.Height = 2
c1.Width = 20
c1.TextFgColor = ui.ColorWhite
// cpu
c2 := ui.NewPar(" CPU")
c2.Border = false
c2.Height = 2
c2.Width = 10
c2.TextFgColor = ui.ColorWhite
// mem
c3 := ui.NewPar(" MEM")
c3.Border = false
c3.Height = 2
c3.Width = 10
c3.TextFgColor = ui.ColorWhite
// net
c4 := ui.NewPar(" NET RX/TX")
c4.Border = false
c4.Height = 2
c4.Width = 10
c4.TextFgColor = ui.ColorWhite
return ui.NewRow(
ui.NewCol(1, 0, c1),
ui.NewCol(2, 0, c2),
ui.NewCol(2, 0, c3),
ui.NewCol(2, 0, c4),
ui.NewCol(1, 0, headerPar("CID")),
ui.NewCol(2, 0, headerPar("CPU")),
ui.NewCol(2, 0, headerPar("MEM")),
ui.NewCol(2, 0, headerPar("NET RX/TX")),
ui.NewCol(2, 0, headerPar("NAMES")),
)
}
func headerPar(s string) *ui.Par {
p := ui.NewPar(fmt.Sprintf(" %s", s))
p.Border = false
p.Height = 2
p.Width = 20
p.TextFgColor = ui.ColorWhite
return p
}
func Display(g *Grid) {
if err := ui.Init(); err != nil {
panic(err)

20
main.go
View File

@ -6,7 +6,7 @@ import (
"github.com/fsouza/go-dockerclient"
)
func runningCIDs(client *docker.Client) (running []string) {
func getContainers(client *docker.Client) []docker.APIContainers {
filters := make(map[string][]string)
filters["status"] = []string{"running"}
opts := docker.ListContainersOptions{
@ -16,15 +16,10 @@ func runningCIDs(client *docker.Client) (running []string) {
if err != nil {
panic(err)
}
for _, c := range containers {
running = append(running, c.ID[:12])
}
return running
return containers
}
func main() {
var containers []string
dockerhost := os.Getenv("DOCKER_HOST")
if dockerhost == "" {
dockerhost = "unix:///var/run/docker.sock"
@ -35,16 +30,9 @@ func main() {
panic(err)
}
// Default to all running containers
if len(os.Args) < 2 {
containers = runningCIDs(client)
} else {
containers = os.Args[1:]
}
g := &Grid{0, make(map[string]*Container)}
for _, c := range containers {
g.AddContainer(c)
for _, c := range getContainers(client) {
g.AddContainer(c.ID[:12], c.Names)
}
for _, c := range g.containers {

View File

@ -15,6 +15,16 @@ type Widgets struct {
memory *ui.Gauge
}
func (w *Widgets) MakeRow() *ui.Row {
return ui.NewRow(
ui.NewCol(1, 0, w.cid),
ui.NewCol(2, 0, w.cpu),
ui.NewCol(2, 0, w.memory),
ui.NewCol(2, 0, w.net),
ui.NewCol(2, 0, w.names),
)
}
func (w *Widgets) SetCPU(val int) {
w.cpu.BarColor = colorScale(val)
w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
@ -36,13 +46,18 @@ func (w *Widgets) SetMem(val int64, limit int64) {
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
}
func NewWidgets(id string) *Widgets {
func NewWidgets(id string, names string) *Widgets {
cid := ui.NewPar(id)
cid.Border = false
cid.Height = 1
cid.Width = 20
cid.TextFgColor = ui.ColorWhite
return &Widgets{cid, mkGauge(), mkGauge(), mkGauge()}
name := ui.NewPar(names)
name.Border = false
name.Height = 1
name.Width = 20
name.TextFgColor = ui.ColorWhite
return &Widgets{cid, name, mkGauge(), mkGauge(), mkGauge()}
}
func mkGauge() *ui.Gauge {