mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor global config into subpackage
This commit is contained in:
parent
d5d00b730b
commit
85cd6864ba
@ -1,10 +1,10 @@
|
|||||||
package main
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var GlobalConfig = NewDefaultConfig()
|
var Global = NewDefaultConfig()
|
||||||
var configChan = make(chan ConfigMsg)
|
var configChan = make(chan ConfigMsg)
|
||||||
|
|
||||||
type Config map[string]string
|
type Config map[string]string
|
||||||
@ -14,17 +14,17 @@ type ConfigMsg struct {
|
|||||||
val string
|
val string
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateConfig(k, v string) {
|
func Update(k, v string) {
|
||||||
log.Noticef("config update: %s = %s", k, v)
|
//log.Noticef("config update: %s = %s", k, v)
|
||||||
configChan <- ConfigMsg{k, v}
|
configChan <- ConfigMsg{k, v}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle a boolean option
|
// Toggle a boolean option
|
||||||
func (c Config) toggle(k string) {
|
func Toggle(k string) {
|
||||||
if c[k] == "0" {
|
if Global[k] == "0" {
|
||||||
c[k] = "1"
|
Global[k] = "1"
|
||||||
} else {
|
} else {
|
||||||
c[k] = "0"
|
Global[k] = "0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,13 +7,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bcicen/ctop/collector"
|
"github.com/bcicen/ctop/collector"
|
||||||
|
"github.com/bcicen/ctop/config"
|
||||||
"github.com/bcicen/ctop/widgets"
|
"github.com/bcicen/ctop/widgets"
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewContainerMap() *ContainerMap {
|
func NewContainerMap() *ContainerMap {
|
||||||
// init docker client
|
// init docker client
|
||||||
client, err := docker.NewClient(GlobalConfig["dockerHost"])
|
client, err := docker.NewClient(config.Global["dockerHost"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -99,7 +100,7 @@ func (cm *ContainerMap) Del(ids ...string) {
|
|||||||
func (cm *ContainerMap) All() []*Container {
|
func (cm *ContainerMap) All() []*Container {
|
||||||
var containers Containers
|
var containers Containers
|
||||||
|
|
||||||
filter := GlobalConfig["filterStr"]
|
filter := config.Global["filterStr"]
|
||||||
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
|
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
|
||||||
|
|
||||||
for _, c := range cm.containers {
|
for _, c := range cm.containers {
|
||||||
|
5
grid.go
5
grid.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/config"
|
||||||
"github.com/bcicen/ctop/widgets"
|
"github.com/bcicen/ctop/widgets"
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
@ -74,7 +75,7 @@ func (g *Grid) redrawRows() {
|
|||||||
ui.Clear()
|
ui.Clear()
|
||||||
|
|
||||||
// build layout
|
// build layout
|
||||||
if GlobalConfig["enableHeader"] == "1" {
|
if config.Global["enableHeader"] == "1" {
|
||||||
g.header.SetCount(len(g.containers))
|
g.header.SetCount(len(g.containers))
|
||||||
ui.Body.AddRows(g.header.Row())
|
ui.Body.AddRows(g.header.Row())
|
||||||
}
|
}
|
||||||
@ -170,7 +171,7 @@ func Display(g *Grid) bool {
|
|||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Handle("/sys/kbd/r", func(e ui.Event) {
|
ui.Handle("/sys/kbd/r", func(e ui.Event) {
|
||||||
GlobalConfig.toggle("sortReversed")
|
config.Toggle("sortReversed")
|
||||||
})
|
})
|
||||||
ui.Handle("/sys/kbd/s", func(ui.Event) {
|
ui.Handle("/sys/kbd/s", func(ui.Event) {
|
||||||
menu = SortMenu
|
menu = SortMenu
|
||||||
|
3
main.go
3
main.go
@ -1,11 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/bcicen/ctop/config"
|
||||||
"github.com/bcicen/ctop/logging"
|
"github.com/bcicen/ctop/logging"
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logging.New(GlobalConfig["loggingEnabled"])
|
var log = logging.New(config.Global["loggingEnabled"])
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := ui.Init(); err != nil {
|
if err := ui.Init(); err != nil {
|
||||||
|
7
menus.go
7
menus.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/bcicen/ctop/config"
|
||||||
"github.com/bcicen/ctop/widgets"
|
"github.com/bcicen/ctop/widgets"
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
@ -32,7 +33,7 @@ func FilterMenu() {
|
|||||||
ui.Render(i)
|
ui.Render(i)
|
||||||
i.InputHandlers()
|
i.InputHandlers()
|
||||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||||
updateConfig("filterStr", i.Data)
|
config.Update("filterStr", i.Data)
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Loop()
|
ui.Loop()
|
||||||
@ -46,7 +47,7 @@ func SortMenu() {
|
|||||||
m.BorderFg = ui.ColorCyan
|
m.BorderFg = ui.ColorCyan
|
||||||
|
|
||||||
// set cursor position to current sort field
|
// set cursor position to current sort field
|
||||||
current := GlobalConfig["sortField"]
|
current := config.Global["sortField"]
|
||||||
for n, field := range m.Items {
|
for n, field := range m.Items {
|
||||||
if field == current {
|
if field == current {
|
||||||
m.CursorPos = n
|
m.CursorPos = n
|
||||||
@ -56,7 +57,7 @@ func SortMenu() {
|
|||||||
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) {
|
||||||
updateConfig("sortField", m.Items[m.CursorPos])
|
config.Update("sortField", m.Items[m.CursorPos])
|
||||||
ui.StopLoop()
|
ui.StopLoop()
|
||||||
})
|
})
|
||||||
ui.Loop()
|
ui.Loop()
|
||||||
|
6
sort.go
6
sort.go
@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sortMethod func(c1, c2 *Container) bool
|
type sortMethod func(c1, c2 *Container) bool
|
||||||
@ -29,8 +31,8 @@ type Containers []*Container
|
|||||||
func (a Containers) Len() int { return len(a) }
|
func (a Containers) Len() int { return len(a) }
|
||||||
func (a Containers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
func (a Containers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
func (a Containers) Less(i, j int) bool {
|
func (a Containers) Less(i, j int) bool {
|
||||||
f := Sorters[GlobalConfig["sortField"]]
|
f := Sorters[config.Global["sortField"]]
|
||||||
if GlobalConfig["sortReversed"] == "1" {
|
if config.Global["sortReversed"] == "1" {
|
||||||
return f(a[j], a[i])
|
return f(a[j], a[i])
|
||||||
}
|
}
|
||||||
return f(a[i], a[j])
|
return f(a[i], a[j])
|
||||||
|
Loading…
Reference in New Issue
Block a user