From a62b91089323681efed09c580e914d53851460af Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 17 May 2018 17:15:03 -0700 Subject: [PATCH] Closes #43. Add CommandRunner module to the app. CommandRunner allows you to define a terminal command and arguments, run it on a schedule, and view the output. Examples: ping -3 cisco.com --- bamboohr/widget.go | 6 ++---- clocks/widget.go | 2 +- cmdrunner/widget.go | 40 +++++++++++++++++++++++++++++++++++++++- gcal/widget.go | 4 ++-- git/widget.go | 14 ++++++-------- github/widget.go | 4 +--- jira/widget.go | 4 +--- newrelic/widget.go | 5 +---- opsgenie/widget.go | 5 +---- security/widget.go | 5 ++--- status/widget.go | 5 ++--- system/widget.go | 3 +-- textfile/widget.go | 4 +--- todo/widget.go | 3 +-- weather/widget.go | 5 ++--- wtf.go | 11 +++++++---- wtf/text_widget.go | 4 ++++ 17 files changed, 74 insertions(+), 50 deletions(-) diff --git a/bamboohr/widget.go b/bamboohr/widget.go index 776c5129..a8997013 100644 --- a/bamboohr/widget.go +++ b/bamboohr/widget.go @@ -2,7 +2,6 @@ package bamboohr import ( "fmt" - "time" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" @@ -37,12 +36,11 @@ func (widget *Widget) Refresh() { wtf.Now().Format(wtf.DateFormat), ) + widget.UpdateRefreshedAt() widget.View.SetTitle(fmt.Sprintf(" 👽 Away (%d) ", len(todayItems))) - widget.View.Clear() - fmt.Fprintf(widget.View, "%s", widget.contentFrom(todayItems)) - widget.RefreshedAt = time.Now() + fmt.Fprintf(widget.View, "%s", widget.contentFrom(todayItems)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/clocks/widget.go b/clocks/widget.go index e53e76ec..e5214fb6 100644 --- a/clocks/widget.go +++ b/clocks/widget.go @@ -33,9 +33,9 @@ func (widget *Widget) Refresh() { return } + widget.UpdateRefreshedAt() widget.View.Clear() widget.display(widget.clockColl.Sorted()) - widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ diff --git a/cmdrunner/widget.go b/cmdrunner/widget.go index bd147e8c..2c2b81a8 100644 --- a/cmdrunner/widget.go +++ b/cmdrunner/widget.go @@ -2,6 +2,11 @@ package cmdrunner import ( "fmt" + "os/exec" + "strings" + + "github.com/olebedev/config" + "github.com/senorprogrammer/wtf/wtf" ) // Config is a pointer to the global config object @@ -10,9 +15,42 @@ var Config *config.Config type Widget struct { wtf.TextWidget - cmd string + args []string + cmd string + result string } func NewWidget() *Widget { + widget := Widget{ + TextWidget: wtf.NewTextWidget(" 🏃 Runner ", "cmdrunner", true), + + args: wtf.ToStrs(Config.UList("wtf.mods.cmdrunner.args")), + cmd: Config.UString("wtf.mods.cmdrunner.cmd"), + } + + return &widget +} + +func (widget *Widget) Refresh() { + if widget.Disabled() { + return + } + + widget.UpdateRefreshedAt() + widget.execute() + widget.View.Clear() + widget.View.SetTitle(fmt.Sprintf(" %s ", widget)) + + fmt.Fprintf(widget.View, "%s", widget.result) +} + +func (widget *Widget) String() string { + args := strings.Join(widget.args, " ") + return fmt.Sprintf("%s %s", widget.cmd, args) +} + +func (widget *Widget) execute() { + cmd := exec.Command(widget.cmd, widget.args...) + widget.result = wtf.ExecuteCommand(cmd) } diff --git a/gcal/widget.go b/gcal/widget.go index d2b1fd11..112be1ca 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -35,10 +35,10 @@ func (widget *Widget) Refresh() { events, _ := Fetch() + widget.UpdateRefreshedAt() widget.View.Clear() - fmt.Fprintf(widget.View, "%s", widget.contentFrom(events)) - widget.RefreshedAt = time.Now() + fmt.Fprintf(widget.View, "%s", widget.contentFrom(events)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/git/widget.go b/git/widget.go index 03842394..aa822106 100644 --- a/git/widget.go +++ b/git/widget.go @@ -1,8 +1,6 @@ package git import ( - "time" - "github.com/gdamore/tcell" "github.com/olebedev/config" "github.com/rivo/tview" @@ -26,7 +24,7 @@ const helpText = ` type Widget struct { wtf.TextWidget - app *tview.Application + app *tview.Application Data []*GitRepo Idx int pages *tview.Pages @@ -36,9 +34,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(" Git ", "git", true), - app: app, - Idx: 0, - pages: pages, + app: app, + Idx: 0, + pages: pages, } widget.View.SetInputCapture(widget.keyboardIntercept) @@ -54,10 +52,10 @@ func (widget *Widget) Refresh() { } repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories")) - widget.Data = widget.gitRepos(repoPaths) + widget.UpdateRefreshedAt() + widget.Data = widget.gitRepos(repoPaths) widget.display() - widget.RefreshedAt = time.Now() } func (widget *Widget) Next() { diff --git a/github/widget.go b/github/widget.go index 195859e9..ed430099 100644 --- a/github/widget.go +++ b/github/widget.go @@ -1,8 +1,6 @@ package github import ( - "time" - "github.com/gdamore/tcell" "github.com/olebedev/config" "github.com/rivo/tview" @@ -56,8 +54,8 @@ func (widget *Widget) Refresh() { widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories")) + widget.UpdateRefreshedAt() widget.display() - widget.RefreshedAt = time.Now() } func (widget *Widget) Next() { diff --git a/jira/widget.go b/jira/widget.go index 19102d6e..4c69e2da 100644 --- a/jira/widget.go +++ b/jira/widget.go @@ -2,7 +2,6 @@ package jira import ( "fmt" - "time" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" @@ -32,6 +31,7 @@ func (widget *Widget) Refresh() { searchResult, err := IssuesFor(Config.UString("wtf.mods.jira.username")) + widget.UpdateRefreshedAt() widget.View.Clear() if err != nil { @@ -49,8 +49,6 @@ func (widget *Widget) Refresh() { ) fmt.Fprintf(widget.View, "%s", widget.contentFrom(searchResult)) } - - widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ diff --git a/newrelic/widget.go b/newrelic/widget.go index bf5836c8..70466174 100644 --- a/newrelic/widget.go +++ b/newrelic/widget.go @@ -2,7 +2,6 @@ package newrelic import ( "fmt" - "time" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" @@ -39,8 +38,8 @@ func (widget *Widget) Refresh() { appName = app.Name } + widget.UpdateRefreshedAt() widget.View.SetTitle(fmt.Sprintf(" New Relic: [green]%s[white] ", appName)) - widget.View.Clear() if depErr != nil { @@ -50,8 +49,6 @@ func (widget *Widget) Refresh() { widget.View.SetWrap(false) fmt.Fprintf(widget.View, "%s", widget.contentFrom(deploys)) } - - widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ diff --git a/opsgenie/widget.go b/opsgenie/widget.go index 8cba1dd2..3a75ce37 100644 --- a/opsgenie/widget.go +++ b/opsgenie/widget.go @@ -3,7 +3,6 @@ package opsgenie import ( "fmt" "strings" - "time" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" @@ -33,8 +32,8 @@ func (widget *Widget) Refresh() { data, err := Fetch() + widget.UpdateRefreshedAt() widget.View.SetTitle(" ⏰ On Call ") - widget.View.Clear() if err != nil { @@ -44,8 +43,6 @@ func (widget *Widget) Refresh() { widget.View.SetWrap(false) fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) } - - widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ diff --git a/security/widget.go b/security/widget.go index e4a919cf..c240557e 100644 --- a/security/widget.go +++ b/security/widget.go @@ -2,7 +2,6 @@ package security import ( "fmt" - "time" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" @@ -33,10 +32,10 @@ func (widget *Widget) Refresh() { data := NewSecurityData() data.Fetch() + widget.UpdateRefreshedAt() widget.View.Clear() - fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) - widget.RefreshedAt = time.Now() + fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) } /* -------------------- Unexported Functions -------------------- */ diff --git a/status/widget.go b/status/widget.go index 19b09ec8..a4f2b4d0 100644 --- a/status/widget.go +++ b/status/widget.go @@ -2,7 +2,6 @@ package status import ( "fmt" - "time" "github.com/olebedev/config" "github.com/senorprogrammer/wtf/wtf" @@ -33,14 +32,14 @@ func (widget *Widget) Refresh() { return } + widget.UpdateRefreshedAt() widget.View.Clear() + fmt.Fprintf( widget.View, "\n%s", widget.animation(), ) - - widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ diff --git a/system/widget.go b/system/widget.go index 76833ebe..5cfc8e60 100644 --- a/system/widget.go +++ b/system/widget.go @@ -37,6 +37,7 @@ func (widget *Widget) Refresh() { return } + widget.UpdateRefreshedAt() widget.View.Clear() fmt.Fprintf( @@ -51,8 +52,6 @@ func (widget *Widget) Refresh() { "Build", widget.systemInfo.BuildVersion, ) - - widget.RefreshedAt = time.Now() } func (widget *Widget) prettyDate() string { diff --git a/textfile/widget.go b/textfile/widget.go index 4ecfc0e7..23023945 100644 --- a/textfile/widget.go +++ b/textfile/widget.go @@ -3,7 +3,6 @@ package textfile import ( "fmt" "io/ioutil" - "time" "github.com/gdamore/tcell" "github.com/olebedev/config" @@ -53,9 +52,8 @@ func (widget *Widget) Refresh() { return } + widget.UpdateRefreshedAt() widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.filePath)) - widget.RefreshedAt = time.Now() - widget.View.Clear() filePath, _ := wtf.ExpandHomeDir(widget.filePath) diff --git a/todo/widget.go b/todo/widget.go index 835276ad..3e769c57 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -3,7 +3,6 @@ package todo import ( "fmt" "io/ioutil" - "time" "github.com/gdamore/tcell" "github.com/olebedev/config" @@ -66,9 +65,9 @@ func (widget *Widget) Refresh() { return } + widget.UpdateRefreshedAt() widget.load() widget.display() - widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ diff --git a/weather/widget.go b/weather/widget.go index c0e9bff4..4d85ba12 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -2,7 +2,6 @@ package weather import ( "os" - "time" owm "github.com/briandowns/openweathermap" "github.com/gdamore/tcell" @@ -81,8 +80,8 @@ func (widget *Widget) Refresh() { widget.Data = widget.Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes()))) + widget.UpdateRefreshedAt() widget.display() - widget.RefreshedAt = time.Now() } // Next displays data for the next city data in the list. If the current city is the last @@ -219,7 +218,7 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { case "/": widget.showHelp() return nil - case "h": + case "h": widget.Prev() return nil case "l": diff --git a/wtf.go b/wtf.go index 00ef3e6a..8f3157b8 100644 --- a/wtf.go +++ b/wtf.go @@ -10,6 +10,7 @@ import ( "github.com/rivo/tview" "github.com/senorprogrammer/wtf/bamboohr" "github.com/senorprogrammer/wtf/clocks" + "github.com/senorprogrammer/wtf/cmdrunner" "github.com/senorprogrammer/wtf/gcal" "github.com/senorprogrammer/wtf/git" "github.com/senorprogrammer/wtf/github" @@ -63,7 +64,7 @@ func buildGrid(modules []wtf.Wtfable) *tview.Grid { func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyCtrlR: - refreshAllModules() + refreshAllWidgets() case tcell.KeyTab: FocusTracker.Next() case tcell.KeyBacktab: @@ -96,9 +97,9 @@ func redrawApp(app *tview.Application) { } } -func refreshAllModules() { - for _, module := range Widgets { - go module.Refresh() +func refreshAllWidgets() { + for _, widget := range Widgets { + go widget.Refresh() } } @@ -151,6 +152,7 @@ func main() { bamboohr.Config = Config clocks.Config = Config + cmdrunner.Config = Config gcal.Config = Config git.Config = Config github.Config = Config @@ -168,6 +170,7 @@ func main() { Widgets = []wtf.Wtfable{ bamboohr.NewWidget(), clocks.NewWidget(), + cmdrunner.NewWidget(), gcal.NewWidget(), git.NewWidget(app, pages), github.NewWidget(app, pages), diff --git a/wtf/text_widget.go b/wtf/text_widget.go index bf74dba1..7ee3200d 100644 --- a/wtf/text_widget.go +++ b/wtf/text_widget.go @@ -85,3 +85,7 @@ func (widget *TextWidget) addView() { widget.View = view } + +func (widget *TextWidget) UpdateRefreshedAt() { + widget.RefreshedAt = time.Now() +}