mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
update config with get, gettoggle methods
This commit is contained in:
parent
d5d91398f5
commit
d56af1d932
@ -9,10 +9,34 @@ import (
|
||||
var (
|
||||
Global = NewDefaultConfig()
|
||||
log = logging.Init()
|
||||
configChan = make(chan ConfigMsg)
|
||||
)
|
||||
|
||||
type Config map[string]string
|
||||
type Config struct {
|
||||
params map[string]string
|
||||
toggles map[string]bool
|
||||
updates chan ConfigMsg
|
||||
}
|
||||
|
||||
// Return param value
|
||||
func Get(k string) string {
|
||||
if _, ok := Global.params[k]; ok == true {
|
||||
return Global.params[k]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Return toggle value
|
||||
func GetToggle(k string) bool {
|
||||
if _, ok := Global.toggles[k]; ok == true {
|
||||
return Global.toggles[k]
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Toggle a boolean option
|
||||
func Toggle(k string) {
|
||||
Global.toggles[k] = Global.toggles[k] != true
|
||||
}
|
||||
|
||||
type ConfigMsg struct {
|
||||
key string
|
||||
@ -21,16 +45,7 @@ type ConfigMsg struct {
|
||||
|
||||
func Update(k, v string) {
|
||||
log.Noticef("config update: %s = %s", k, v)
|
||||
configChan <- ConfigMsg{k, v}
|
||||
}
|
||||
|
||||
// Toggle a boolean option
|
||||
func Toggle(k string) {
|
||||
if Global[k] == "0" {
|
||||
Global[k] = "1"
|
||||
} else {
|
||||
Global[k] = "0"
|
||||
}
|
||||
Global.updates <- ConfigMsg{k, v}
|
||||
}
|
||||
|
||||
func NewDefaultConfig() Config {
|
||||
@ -38,17 +53,23 @@ func NewDefaultConfig() Config {
|
||||
if docker == "" {
|
||||
docker = "unix:///var/run/docker.sock"
|
||||
}
|
||||
config := Config{
|
||||
|
||||
params := map[string]string{
|
||||
"dockerHost": docker,
|
||||
"filterStr": "",
|
||||
"sortField": "id",
|
||||
"sortReverse": "0",
|
||||
"enableHeader": "0",
|
||||
"loggingEnabled": "1",
|
||||
}
|
||||
|
||||
toggles := map[string]bool{
|
||||
"sortReverse": false,
|
||||
"enableHeader": false,
|
||||
"loggingEnabled": true,
|
||||
}
|
||||
|
||||
config := Config{params, toggles, make(chan ConfigMsg)}
|
||||
go func() {
|
||||
for m := range configChan {
|
||||
config[m.key] = m.val
|
||||
for m := range config.updates {
|
||||
config.params[m.key] = m.val
|
||||
}
|
||||
}()
|
||||
return config
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
|
||||
func NewContainerMap() *ContainerMap {
|
||||
// init docker client
|
||||
client, err := docker.NewClient(config.Global["dockerHost"])
|
||||
client, err := docker.NewClient(config.Get("dockerHost"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -100,7 +100,7 @@ func (cm *ContainerMap) Del(ids ...string) {
|
||||
func (cm *ContainerMap) All() []*Container {
|
||||
var containers Containers
|
||||
|
||||
filter := config.Global["filterStr"]
|
||||
filter := config.Get("filterStr")
|
||||
re := regexp.MustCompile(fmt.Sprintf(".*%s", filter))
|
||||
|
||||
for _, c := range cm.containers {
|
||||
|
2
grid.go
2
grid.go
@ -75,7 +75,7 @@ func (g *Grid) redrawRows() {
|
||||
ui.Clear()
|
||||
|
||||
// build layout
|
||||
if config.Global["enableHeader"] == "1" {
|
||||
if config.GetToggle("enableHeader") {
|
||||
g.header.SetCount(len(g.containers))
|
||||
ui.Body.AddRows(g.header.Row())
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -10,7 +10,7 @@ var log *logging.CTopLogger
|
||||
|
||||
func main() {
|
||||
log = logging.Init()
|
||||
if config.Global["loggingEnabled"] == "1" {
|
||||
if config.GetToggle("loggingEnabled") {
|
||||
log.StartServer()
|
||||
}
|
||||
if err := ui.Init(); err != nil {
|
||||
|
2
menus.go
2
menus.go
@ -47,7 +47,7 @@ func SortMenu() {
|
||||
m.BorderFg = ui.ColorCyan
|
||||
|
||||
// set cursor position to current sort field
|
||||
current := config.Global["sortField"]
|
||||
current := config.Get("sortField")
|
||||
for n, field := range m.Items {
|
||||
if field == current {
|
||||
m.CursorPos = n
|
||||
|
4
sort.go
4
sort.go
@ -31,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[config.Global["sortField"]]
|
||||
if config.Global["sortReversed"] == "1" {
|
||||
f := Sorters[config.Get("sortField")]
|
||||
if config.GetToggle("sortReversed") {
|
||||
return f(a[j], a[i])
|
||||
}
|
||||
return f(a[i], a[j])
|
||||
|
Loading…
Reference in New Issue
Block a user