From da27ad60a9a50e2e47b68dac4c072c1149a9fa33 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 12 Apr 2018 15:15:11 -0700 Subject: [PATCH] Add Ctrl-R to manually force a widget refresh --- wtf.go | 72 ++++++++++++++++++++++++++++++---------------- wtf/text_viewer.go | 3 ++ 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/wtf.go b/wtf.go index aa5f76ef..5ac1f23b 100644 --- a/wtf.go +++ b/wtf.go @@ -1,9 +1,11 @@ package main import ( + //"fmt" "os" "time" + "github.com/gdamore/tcell" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/bamboohr" "github.com/senorprogrammer/wtf/gcal" @@ -18,21 +20,6 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) -func refresher(stat *status.Widget, app *tview.Application) { - tick := time.NewTicker(time.Duration(Config.UInt("wtf.refreshInterval", 1)) * time.Second) - quit := make(chan struct{}) - - for { - select { - case <-tick.C: - app.Draw() - case <-quit: - tick.Stop() - return - } - } -} - func addToApp(grid *tview.Grid, widget wtf.TextViewer) { if widget.Disabled() { return @@ -50,9 +37,37 @@ func addToApp(grid *tview.Grid, widget wtf.TextViewer) { ) } +func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { + // Ctrl-R: force-refreshes every widget + if event.Key() == tcell.KeyCtrlR { + for _, module := range Modules { + go module.Refresh() + } + } + + return event +} + +func refresher(stat *status.Widget, app *tview.Application) { + tick := time.NewTicker(time.Duration(Config.UInt("wtf.refreshInterval", 1)) * time.Second) + quit := make(chan struct{}) + + for { + select { + case <-tick.C: + app.Draw() + case <-quit: + tick.Stop() + return + } + } +} + var result = wtf.CreateConfigDir() var Config = wtf.LoadConfigFile() +var Modules []wtf.TextViewer + /* -------------------- Main -------------------- */ func main() { @@ -105,18 +120,25 @@ func main() { weather := weather.NewWidget() go wtf.Schedule(weather) - addToApp(grid, bamboo) - addToApp(grid, cal) - addToApp(grid, git) - addToApp(grid, github) - addToApp(grid, newrelic) - addToApp(grid, weather) - addToApp(grid, sec) - addToApp(grid, opsgenie) - addToApp(grid, jira) - addToApp(grid, stat) + Modules = []wtf.TextViewer{ + bamboo, + cal, + git, + github, + jira, + newrelic, + opsgenie, + sec, + stat, + weather, + } + + for _, module := range Modules { + addToApp(grid, module) + } app := tview.NewApplication() + app.SetInputCapture(keyboardIntercept) // Loop in a routine to redraw the screen go refresher(stat, app) diff --git a/wtf/text_viewer.go b/wtf/text_viewer.go index 3c477d8d..f8fe1bc8 100644 --- a/wtf/text_viewer.go +++ b/wtf/text_viewer.go @@ -6,7 +6,10 @@ import ( type TextViewer interface { Enabler + + Refresh() TextView() *tview.TextView + Top() int Left() int Width() int