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
@ -7,12 +7,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Global = NewDefaultConfig()
|
Global = NewDefaultConfig()
|
||||||
log = logging.Init()
|
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 {
|
type ConfigMsg struct {
|
||||||
key string
|
key string
|
||||||
@ -21,16 +45,7 @@ type ConfigMsg struct {
|
|||||||
|
|
||||||
func Update(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}
|
Global.updates <- ConfigMsg{k, v}
|
||||||
}
|
|
||||||
|
|
||||||
// Toggle a boolean option
|
|
||||||
func Toggle(k string) {
|
|
||||||
if Global[k] == "0" {
|
|
||||||
Global[k] = "1"
|
|
||||||
} else {
|
|
||||||
Global[k] = "0"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultConfig() Config {
|
func NewDefaultConfig() Config {
|
||||||
@ -38,17 +53,23 @@ func NewDefaultConfig() Config {
|
|||||||
if docker == "" {
|
if docker == "" {
|
||||||
docker = "unix:///var/run/docker.sock"
|
docker = "unix:///var/run/docker.sock"
|
||||||
}
|
}
|
||||||
config := Config{
|
|
||||||
"dockerHost": docker,
|
params := map[string]string{
|
||||||
"filterStr": "",
|
"dockerHost": docker,
|
||||||
"sortField": "id",
|
"filterStr": "",
|
||||||
"sortReverse": "0",
|
"sortField": "id",
|
||||||
"enableHeader": "0",
|
|
||||||
"loggingEnabled": "1",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toggles := map[string]bool{
|
||||||
|
"sortReverse": false,
|
||||||
|
"enableHeader": false,
|
||||||
|
"loggingEnabled": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
config := Config{params, toggles, make(chan ConfigMsg)}
|
||||||
go func() {
|
go func() {
|
||||||
for m := range configChan {
|
for m := range config.updates {
|
||||||
config[m.key] = m.val
|
config.params[m.key] = m.val
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return config
|
return config
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
func NewContainerMap() *ContainerMap {
|
func NewContainerMap() *ContainerMap {
|
||||||
// init docker client
|
// init docker client
|
||||||
client, err := docker.NewClient(config.Global["dockerHost"])
|
client, err := docker.NewClient(config.Get("dockerHost"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -100,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 := config.Global["filterStr"]
|
filter := config.Get("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 {
|
||||||
|
2
grid.go
2
grid.go
@ -75,7 +75,7 @@ func (g *Grid) redrawRows() {
|
|||||||
ui.Clear()
|
ui.Clear()
|
||||||
|
|
||||||
// build layout
|
// build layout
|
||||||
if config.Global["enableHeader"] == "1" {
|
if config.GetToggle("enableHeader") {
|
||||||
g.header.SetCount(len(g.containers))
|
g.header.SetCount(len(g.containers))
|
||||||
ui.Body.AddRows(g.header.Row())
|
ui.Body.AddRows(g.header.Row())
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@ -10,7 +10,7 @@ var log *logging.CTopLogger
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log = logging.Init()
|
log = logging.Init()
|
||||||
if config.Global["loggingEnabled"] == "1" {
|
if config.GetToggle("loggingEnabled") {
|
||||||
log.StartServer()
|
log.StartServer()
|
||||||
}
|
}
|
||||||
if err := ui.Init(); err != nil {
|
if err := ui.Init(); err != nil {
|
||||||
|
2
menus.go
2
menus.go
@ -47,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 := config.Global["sortField"]
|
current := config.Get("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
|
||||||
|
4
sort.go
4
sort.go
@ -31,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[config.Global["sortField"]]
|
f := Sorters[config.Get("sortField")]
|
||||||
if config.Global["sortReversed"] == "1" {
|
if config.GetToggle("sortReversed") {
|
||||||
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