mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add ctop header, disabled by default
This commit is contained in:
parent
f311aad105
commit
bebdfd844f
@ -4,6 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var GlobalConfig = NewDefaultConfig()
|
||||||
var configChan = make(chan ConfigMsg)
|
var configChan = make(chan ConfigMsg)
|
||||||
|
|
||||||
type Config map[string]string
|
type Config map[string]string
|
||||||
@ -23,8 +24,9 @@ func NewDefaultConfig() Config {
|
|||||||
docker = "unix:///var/run/docker.sock"
|
docker = "unix:///var/run/docker.sock"
|
||||||
}
|
}
|
||||||
config := Config{
|
config := Config{
|
||||||
"dockerHost": docker,
|
"dockerHost": docker,
|
||||||
"sortField": "id",
|
"sortField": "id",
|
||||||
|
"enableHeader": "0",
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for m := range configChan {
|
for m := range configChan {
|
||||||
|
@ -9,16 +9,13 @@ var filters = map[string][]string{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewContainerMap() *ContainerMap {
|
func NewContainerMap() *ContainerMap {
|
||||||
config := NewDefaultConfig()
|
|
||||||
|
|
||||||
// init docker client
|
// init docker client
|
||||||
client, err := docker.NewClient(config["dockerHost"])
|
client, err := docker.NewClient(GlobalConfig["dockerHost"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cm := &ContainerMap{
|
cm := &ContainerMap{
|
||||||
config: config,
|
|
||||||
client: client,
|
client: client,
|
||||||
containers: make(map[string]*Container),
|
containers: make(map[string]*Container),
|
||||||
}
|
}
|
||||||
@ -27,7 +24,6 @@ func NewContainerMap() *ContainerMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ContainerMap struct {
|
type ContainerMap struct {
|
||||||
config Config
|
|
||||||
client *docker.Client
|
client *docker.Client
|
||||||
containers map[string]*Container
|
containers map[string]*Container
|
||||||
}
|
}
|
||||||
@ -66,6 +62,6 @@ func (cm *ContainerMap) All() []*Container {
|
|||||||
for _, c := range cm.containers {
|
for _, c := range cm.containers {
|
||||||
containers = append(containers, c)
|
containers = append(containers, c)
|
||||||
}
|
}
|
||||||
SortContainers(cm.config["sortField"], containers)
|
SortContainers(GlobalConfig["sortField"], containers)
|
||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
|
11
grid.go
11
grid.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/widgets"
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ type Grid struct {
|
|||||||
cursorID string // id of currently selected container
|
cursorID string // id of currently selected container
|
||||||
containers []*Container
|
containers []*Container
|
||||||
containerMap *ContainerMap
|
containerMap *ContainerMap
|
||||||
|
header *widgets.CTopHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGrid() *Grid {
|
func NewGrid() *Grid {
|
||||||
@ -19,6 +21,7 @@ func NewGrid() *Grid {
|
|||||||
cursorID: containers[0].id,
|
cursorID: containers[0].id,
|
||||||
containers: containers,
|
containers: containers,
|
||||||
containerMap: containerMap,
|
containerMap: containerMap,
|
||||||
|
header: widgets.NewCTopHeader(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +70,11 @@ func (g *Grid) redrawRows() {
|
|||||||
ui.Body.Rows = []*ui.Row{}
|
ui.Body.Rows = []*ui.Row{}
|
||||||
|
|
||||||
// build layout
|
// 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 {
|
for _, c := range g.containers {
|
||||||
ui.Body.AddRows(c.widgets.Row())
|
ui.Body.AddRows(c.widgets.Row())
|
||||||
}
|
}
|
||||||
@ -76,7 +83,7 @@ func (g *Grid) redrawRows() {
|
|||||||
ui.Render(ui.Body)
|
ui.Render(ui.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func header() *ui.Row {
|
func fieldHeader() *ui.Row {
|
||||||
return ui.NewRow(
|
return ui.NewRow(
|
||||||
ui.NewCol(2, 0, headerPar("NAME")),
|
ui.NewCol(2, 0, headerPar("NAME")),
|
||||||
ui.NewCol(2, 0, headerPar("CID")),
|
ui.NewCol(2, 0, headerPar("CID")),
|
||||||
|
47
widgets/header.go
Normal file
47
widgets/header.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user