mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add ContainerWidgets interface, config channel
This commit is contained in:
parent
6d63d09c83
commit
f311aad105
26
config.go
26
config.go
@ -4,20 +4,32 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
var configChan = make(chan ConfigMsg)
|
||||||
dockerHost string
|
|
||||||
sortField string
|
type Config map[string]string
|
||||||
|
|
||||||
|
type ConfigMsg struct {
|
||||||
|
key string
|
||||||
|
val string
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultConfig = NewDefaultConfig()
|
func updateConfig(k, v string) {
|
||||||
|
configChan <- ConfigMsg{k, v}
|
||||||
|
}
|
||||||
|
|
||||||
func NewDefaultConfig() Config {
|
func NewDefaultConfig() Config {
|
||||||
docker := os.Getenv("DOCKER_HOST")
|
docker := os.Getenv("DOCKER_HOST")
|
||||||
if docker == "" {
|
if docker == "" {
|
||||||
docker = "unix:///var/run/docker.sock"
|
docker = "unix:///var/run/docker.sock"
|
||||||
}
|
}
|
||||||
return Config{
|
config := Config{
|
||||||
dockerHost: docker,
|
"dockerHost": docker,
|
||||||
sortField: "id",
|
"sortField": "id",
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
for m := range configChan {
|
||||||
|
config[m.key] = m.val
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return config
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ type Container struct {
|
|||||||
name string
|
name string
|
||||||
done chan bool
|
done chan bool
|
||||||
stats chan *docker.Stats
|
stats chan *docker.Stats
|
||||||
widgets *widgets.Compact
|
widgets widgets.ContainerWidgets
|
||||||
reader *StatReader
|
reader *StatReader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,10 +9,10 @@ var filters = map[string][]string{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewContainerMap() *ContainerMap {
|
func NewContainerMap() *ContainerMap {
|
||||||
config := DefaultConfig
|
config := NewDefaultConfig()
|
||||||
|
|
||||||
// init docker client
|
// init docker client
|
||||||
client, err := docker.NewClient(config.dockerHost)
|
client, err := docker.NewClient(config["dockerHost"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -66,6 +66,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(cm.config["sortField"], containers)
|
||||||
return containers
|
return containers
|
||||||
}
|
}
|
||||||
|
6
grid.go
6
grid.go
@ -54,11 +54,9 @@ func (g *Grid) cursorDown() {
|
|||||||
func (g *Grid) redrawCursor() {
|
func (g *Grid) redrawCursor() {
|
||||||
for _, c := range g.containers {
|
for _, c := range g.containers {
|
||||||
if c.id == g.cursorID {
|
if c.id == g.cursorID {
|
||||||
c.widgets.Name.TextFgColor = ui.ColorDefault
|
c.widgets.Highlight()
|
||||||
c.widgets.Name.TextBgColor = ui.ColorWhite
|
|
||||||
} else {
|
} else {
|
||||||
c.widgets.Name.TextFgColor = ui.ColorWhite
|
c.widgets.UnHighlight()
|
||||||
c.widgets.Name.TextBgColor = ui.ColorDefault
|
|
||||||
}
|
}
|
||||||
ui.Render(ui.Body)
|
ui.Render(ui.Body)
|
||||||
}
|
}
|
||||||
|
2
menus.go
2
menus.go
@ -32,7 +32,7 @@ func SortMenu(g *Grid) {
|
|||||||
ui.Render(m)
|
ui.Render(m)
|
||||||
m.NavigationHandlers()
|
m.NavigationHandlers()
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
g.containerMap.config.sortField = m.Items[m.CursorPos]
|
updateConfig("sortField", m.Items[m.CursorPos])
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Loop()
|
ui.Loop()
|
||||||
|
@ -7,6 +7,15 @@ import (
|
|||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ContainerWidgets interface {
|
||||||
|
Row() *ui.Row
|
||||||
|
Highlight()
|
||||||
|
UnHighlight()
|
||||||
|
SetCPU(int)
|
||||||
|
SetNet(int64, int64)
|
||||||
|
SetMem(int64, int64, int)
|
||||||
|
}
|
||||||
|
|
||||||
type Compact struct {
|
type Compact struct {
|
||||||
Cid *ui.Par
|
Cid *ui.Par
|
||||||
Net *ui.Par
|
Net *ui.Par
|
||||||
@ -35,6 +44,16 @@ func (w *Compact) Row() *ui.Row {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Compact) Highlight() {
|
||||||
|
w.Name.TextFgColor = ui.ColorDefault
|
||||||
|
w.Name.TextBgColor = ui.ColorWhite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Compact) UnHighlight() {
|
||||||
|
w.Name.TextFgColor = ui.ColorWhite
|
||||||
|
w.Name.TextBgColor = ui.ColorDefault
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Compact) SetCPU(val int) {
|
func (w *Compact) SetCPU(val int) {
|
||||||
w.Cpu.BarColor = colorScale(val)
|
w.Cpu.BarColor = colorScale(val)
|
||||||
w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
w.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
||||||
|
Loading…
Reference in New Issue
Block a user