diff --git a/config.go b/config/main.go similarity index 74% rename from config.go rename to config/main.go index 67488a5..6352198 100644 --- a/config.go +++ b/config/main.go @@ -1,10 +1,10 @@ -package main +package config import ( "os" ) -var GlobalConfig = NewDefaultConfig() +var Global = NewDefaultConfig() var configChan = make(chan ConfigMsg) type Config map[string]string @@ -14,17 +14,17 @@ type ConfigMsg struct { val string } -func updateConfig(k, v string) { - log.Noticef("config update: %s = %s", k, v) +func Update(k, v string) { + //log.Noticef("config update: %s = %s", k, v) configChan <- ConfigMsg{k, v} } // Toggle a boolean option -func (c Config) toggle(k string) { - if c[k] == "0" { - c[k] = "1" +func Toggle(k string) { + if Global[k] == "0" { + Global[k] = "1" } else { - c[k] = "0" + Global[k] = "0" } } diff --git a/containermap.go b/containermap.go index a291e5d..ac59422 100644 --- a/containermap.go +++ b/containermap.go @@ -7,13 +7,14 @@ import ( "strings" "github.com/bcicen/ctop/collector" + "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/widgets" "github.com/fsouza/go-dockerclient" ) func NewContainerMap() *ContainerMap { // init docker client - client, err := docker.NewClient(GlobalConfig["dockerHost"]) + client, err := docker.NewClient(config.Global["dockerHost"]) if err != nil { panic(err) } @@ -99,7 +100,7 @@ func (cm *ContainerMap) Del(ids ...string) { func (cm *ContainerMap) All() []*Container { var containers Containers - filter := GlobalConfig["filterStr"] + filter := config.Global["filterStr"] re := regexp.MustCompile(fmt.Sprintf(".*%s", filter)) for _, c := range cm.containers { diff --git a/grid.go b/grid.go index 292dae8..d43de21 100644 --- a/grid.go +++ b/grid.go @@ -3,6 +3,7 @@ package main import ( "fmt" + "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/widgets" ui "github.com/gizak/termui" ) @@ -74,7 +75,7 @@ func (g *Grid) redrawRows() { ui.Clear() // build layout - if GlobalConfig["enableHeader"] == "1" { + if config.Global["enableHeader"] == "1" { g.header.SetCount(len(g.containers)) ui.Body.AddRows(g.header.Row()) } @@ -170,7 +171,7 @@ func Display(g *Grid) bool { ui.StopLoop() }) ui.Handle("/sys/kbd/r", func(e ui.Event) { - GlobalConfig.toggle("sortReversed") + config.Toggle("sortReversed") }) ui.Handle("/sys/kbd/s", func(ui.Event) { menu = SortMenu diff --git a/main.go b/main.go index d46df6b..9062869 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,12 @@ package main import ( + "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/logging" ui "github.com/gizak/termui" ) -var log = logging.New(GlobalConfig["loggingEnabled"]) +var log = logging.New(config.Global["loggingEnabled"]) func main() { if err := ui.Init(); err != nil { diff --git a/menus.go b/menus.go index d97bb83..75d6784 100644 --- a/menus.go +++ b/menus.go @@ -1,6 +1,7 @@ package main import ( + "github.com/bcicen/ctop/config" "github.com/bcicen/ctop/widgets" ui "github.com/gizak/termui" ) @@ -32,7 +33,7 @@ func FilterMenu() { ui.Render(i) i.InputHandlers() ui.Handle("/sys/kbd/", func(ui.Event) { - updateConfig("filterStr", i.Data) + config.Update("filterStr", i.Data) ui.StopLoop() }) ui.Loop() @@ -46,7 +47,7 @@ func SortMenu() { m.BorderFg = ui.ColorCyan // set cursor position to current sort field - current := GlobalConfig["sortField"] + current := config.Global["sortField"] for n, field := range m.Items { if field == current { m.CursorPos = n @@ -56,7 +57,7 @@ func SortMenu() { ui.Render(m) m.NavigationHandlers() ui.Handle("/sys/kbd/", func(ui.Event) { - updateConfig("sortField", m.Items[m.CursorPos]) + config.Update("sortField", m.Items[m.CursorPos]) ui.StopLoop() }) ui.Loop() diff --git a/sort.go b/sort.go index d4b09d4..e0c6f73 100644 --- a/sort.go +++ b/sort.go @@ -2,6 +2,8 @@ package main import ( "sort" + + "github.com/bcicen/ctop/config" ) 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) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a Containers) Less(i, j int) bool { - f := Sorters[GlobalConfig["sortField"]] - if GlobalConfig["sortReversed"] == "1" { + f := Sorters[config.Global["sortField"]] + if config.Global["sortReversed"] == "1" { return f(a[j], a[i]) } return f(a[i], a[j])