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 (
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
5
grid.go
5
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
|
||||
|
3
main.go
3
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 {
|
||||
|
7
menus.go
7
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/<enter>", 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/<enter>", func(ui.Event) {
|
||||
updateConfig("sortField", m.Items[m.CursorPos])
|
||||
config.Update("sortField", m.Items[m.CursorPos])
|
||||
ui.StopLoop()
|
||||
})
|
||||
ui.Loop()
|
||||
|
6
sort.go
6
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])
|
||||
|
Loading…
Reference in New Issue
Block a user