From 6b5db0cd092acf97e396f3d2484fd7f944fec92c Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Thu, 12 Apr 2018 19:43:17 -0700 Subject: [PATCH] Async widget initialization --- main.go | 44 ++++++++++++++++++++++++++++++++++---------- widgets/proc.go | 7 ++----- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index a51ba96..781cabf 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ var ( helpVisible = false // proc widget takes longer to load, wait to render until it loads data - procLoaded = make(chan bool, 1) + widgetsLoaded = make(chan bool, 1) // used to render the proc widget whenever a key is pressed for it keyPressed = make(chan bool, 1) // used to render cpu and mem when zoom has changed @@ -32,6 +32,7 @@ var ( colorscheme = colorschemes.Default minimal = false + widgetCount = 6 interval = time.Second zoom = 7 zoomInterval = 3 @@ -71,6 +72,9 @@ Colorschemes: } minimal, _ = args["--minimal"].(bool) + if minimal { + widgetCount = 3 + } rateStr, _ := args["--rate"].(string) rate, _ := strconv.ParseFloat(rateStr, 64) @@ -193,13 +197,35 @@ func main() { // need to do this before initializing widgets so that they can inherit the colors termuiColors() - cpu = w.NewCPU(interval, zoom) - mem = w.NewMem(interval, zoom) - proc = w.NewProc(procLoaded, keyPressed) + go func() { + cpu = w.NewCPU(interval, zoom) + widgetsLoaded <- true + }() + go func() { + mem = w.NewMem(interval, zoom) + widgetsLoaded <- true + }() + go func() { + proc = w.NewProc(keyPressed) + widgetsLoaded <- true + }() if !minimal { - net = w.NewNet() - disk = w.NewDisk() - temp = w.NewTemp() + go func() { + net = w.NewNet() + widgetsLoaded <- true + }() + go func() { + disk = w.NewDisk() + widgetsLoaded <- true + }() + go func() { + temp = w.NewTemp() + widgetsLoaded <- true + }() + } + + for i := 0; i < widgetCount; i++ { + <-widgetsLoaded } widgetColors() @@ -213,7 +239,7 @@ func main() { setupGrid() - // load help widget after init termui/termbox so that it has access to terminal size + // load help widget after init termui so that it has access to terminal size help = w.NewHelpMenu() ui.On("", func(e ui.Event) { @@ -241,8 +267,6 @@ func main() { } } else { select { - case <-procLoaded: - ui.Render(proc) case <-helpToggled: ui.Clear() ui.Render(help) diff --git a/widgets/proc.go b/widgets/proc.go index 0421d54..cd48219 100644 --- a/widgets/proc.go +++ b/widgets/proc.go @@ -35,7 +35,7 @@ type Proc struct { KeyPressed chan bool } -func NewProc(loaded, keyPressed chan bool) *Proc { +func NewProc(keyPressed chan bool) *Proc { cpuCount, _ := psCPU.Counts(false) self := &Proc{ Table: ui.NewTable(), @@ -56,10 +56,7 @@ func NewProc(loaded, keyPressed chan bool) *Proc { self.keyBinds() - go func() { - self.update() - loaded <- true - }() + self.update() ticker := time.NewTicker(self.interval) go func() {