1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

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
This commit is contained in:
Chris Cummer 2018-05-17 17:15:03 -07:00
parent c3f14025ba
commit a62b910893
17 changed files with 74 additions and 50 deletions

View File

@ -2,7 +2,6 @@ package bamboohr
import ( import (
"fmt" "fmt"
"time"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
@ -37,12 +36,11 @@ func (widget *Widget) Refresh() {
wtf.Now().Format(wtf.DateFormat), wtf.Now().Format(wtf.DateFormat),
) )
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf(" 👽 Away (%d) ", len(todayItems))) widget.View.SetTitle(fmt.Sprintf(" 👽 Away (%d) ", len(todayItems)))
widget.View.Clear() 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 -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -33,9 +33,9 @@ func (widget *Widget) Refresh() {
return return
} }
widget.UpdateRefreshedAt()
widget.View.Clear() widget.View.Clear()
widget.display(widget.clockColl.Sorted()) widget.display(widget.clockColl.Sorted())
widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -2,6 +2,11 @@ package cmdrunner
import ( import (
"fmt" "fmt"
"os/exec"
"strings"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
) )
// Config is a pointer to the global config object // Config is a pointer to the global config object
@ -10,9 +15,42 @@ var Config *config.Config
type Widget struct { type Widget struct {
wtf.TextWidget wtf.TextWidget
args []string
cmd string cmd string
result string
} }
func NewWidget() *Widget { 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)
} }

View File

@ -35,10 +35,10 @@ func (widget *Widget) Refresh() {
events, _ := Fetch() events, _ := Fetch()
widget.UpdateRefreshedAt()
widget.View.Clear() 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 -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -1,8 +1,6 @@
package git package git
import ( import (
"time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -54,10 +52,10 @@ func (widget *Widget) Refresh() {
} }
repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories")) repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories"))
widget.Data = widget.gitRepos(repoPaths)
widget.UpdateRefreshedAt()
widget.Data = widget.gitRepos(repoPaths)
widget.display() widget.display()
widget.RefreshedAt = time.Now()
} }
func (widget *Widget) Next() { func (widget *Widget) Next() {

View File

@ -1,8 +1,6 @@
package github package github
import ( import (
"time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -56,8 +54,8 @@ func (widget *Widget) Refresh() {
widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories")) widget.Data = widget.buildRepoCollection(Config.UMap("wtf.mods.github.repositories"))
widget.UpdateRefreshedAt()
widget.display() widget.display()
widget.RefreshedAt = time.Now()
} }
func (widget *Widget) Next() { func (widget *Widget) Next() {

View File

@ -2,7 +2,6 @@ package jira
import ( import (
"fmt" "fmt"
"time"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
@ -32,6 +31,7 @@ func (widget *Widget) Refresh() {
searchResult, err := IssuesFor(Config.UString("wtf.mods.jira.username")) searchResult, err := IssuesFor(Config.UString("wtf.mods.jira.username"))
widget.UpdateRefreshedAt()
widget.View.Clear() widget.View.Clear()
if err != nil { if err != nil {
@ -49,8 +49,6 @@ func (widget *Widget) Refresh() {
) )
fmt.Fprintf(widget.View, "%s", widget.contentFrom(searchResult)) fmt.Fprintf(widget.View, "%s", widget.contentFrom(searchResult))
} }
widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package newrelic
import ( import (
"fmt" "fmt"
"time"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
@ -39,8 +38,8 @@ func (widget *Widget) Refresh() {
appName = app.Name appName = app.Name
} }
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf(" New Relic: [green]%s[white] ", appName)) widget.View.SetTitle(fmt.Sprintf(" New Relic: [green]%s[white] ", appName))
widget.View.Clear() widget.View.Clear()
if depErr != nil { if depErr != nil {
@ -50,8 +49,6 @@ func (widget *Widget) Refresh() {
widget.View.SetWrap(false) widget.View.SetWrap(false)
fmt.Fprintf(widget.View, "%s", widget.contentFrom(deploys)) fmt.Fprintf(widget.View, "%s", widget.contentFrom(deploys))
} }
widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -3,7 +3,6 @@ package opsgenie
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
@ -33,8 +32,8 @@ func (widget *Widget) Refresh() {
data, err := Fetch() data, err := Fetch()
widget.UpdateRefreshedAt()
widget.View.SetTitle(" ⏰ On Call ") widget.View.SetTitle(" ⏰ On Call ")
widget.View.Clear() widget.View.Clear()
if err != nil { if err != nil {
@ -44,8 +43,6 @@ func (widget *Widget) Refresh() {
widget.View.SetWrap(false) widget.View.SetWrap(false)
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data)) fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
} }
widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package security
import ( import (
"fmt" "fmt"
"time"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
@ -33,10 +32,10 @@ func (widget *Widget) Refresh() {
data := NewSecurityData() data := NewSecurityData()
data.Fetch() data.Fetch()
widget.UpdateRefreshedAt()
widget.View.Clear() 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 -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package status
import ( import (
"fmt" "fmt"
"time"
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
@ -33,14 +32,14 @@ func (widget *Widget) Refresh() {
return return
} }
widget.UpdateRefreshedAt()
widget.View.Clear() widget.View.Clear()
fmt.Fprintf( fmt.Fprintf(
widget.View, widget.View,
"\n%s", "\n%s",
widget.animation(), widget.animation(),
) )
widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -37,6 +37,7 @@ func (widget *Widget) Refresh() {
return return
} }
widget.UpdateRefreshedAt()
widget.View.Clear() widget.View.Clear()
fmt.Fprintf( fmt.Fprintf(
@ -51,8 +52,6 @@ func (widget *Widget) Refresh() {
"Build", "Build",
widget.systemInfo.BuildVersion, widget.systemInfo.BuildVersion,
) )
widget.RefreshedAt = time.Now()
} }
func (widget *Widget) prettyDate() string { func (widget *Widget) prettyDate() string {

View File

@ -3,7 +3,6 @@ package textfile
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
@ -53,9 +52,8 @@ func (widget *Widget) Refresh() {
return return
} }
widget.UpdateRefreshedAt()
widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.filePath)) widget.View.SetTitle(fmt.Sprintf(" 📄 %s ", widget.filePath))
widget.RefreshedAt = time.Now()
widget.View.Clear() widget.View.Clear()
filePath, _ := wtf.ExpandHomeDir(widget.filePath) filePath, _ := wtf.ExpandHomeDir(widget.filePath)

View File

@ -3,7 +3,6 @@ package todo
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
@ -66,9 +65,9 @@ func (widget *Widget) Refresh() {
return return
} }
widget.UpdateRefreshedAt()
widget.load() widget.load()
widget.display() widget.display()
widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */

View File

@ -2,7 +2,6 @@ package weather
import ( import (
"os" "os"
"time"
owm "github.com/briandowns/openweathermap" owm "github.com/briandowns/openweathermap"
"github.com/gdamore/tcell" "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.Data = widget.Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
widget.UpdateRefreshedAt()
widget.display() widget.display()
widget.RefreshedAt = time.Now()
} }
// Next displays data for the next city data in the list. If the current city is the last // Next displays data for the next city data in the list. If the current city is the last

11
wtf.go
View File

@ -10,6 +10,7 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/senorprogrammer/wtf/bamboohr" "github.com/senorprogrammer/wtf/bamboohr"
"github.com/senorprogrammer/wtf/clocks" "github.com/senorprogrammer/wtf/clocks"
"github.com/senorprogrammer/wtf/cmdrunner"
"github.com/senorprogrammer/wtf/gcal" "github.com/senorprogrammer/wtf/gcal"
"github.com/senorprogrammer/wtf/git" "github.com/senorprogrammer/wtf/git"
"github.com/senorprogrammer/wtf/github" "github.com/senorprogrammer/wtf/github"
@ -63,7 +64,7 @@ func buildGrid(modules []wtf.Wtfable) *tview.Grid {
func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() { switch event.Key() {
case tcell.KeyCtrlR: case tcell.KeyCtrlR:
refreshAllModules() refreshAllWidgets()
case tcell.KeyTab: case tcell.KeyTab:
FocusTracker.Next() FocusTracker.Next()
case tcell.KeyBacktab: case tcell.KeyBacktab:
@ -96,9 +97,9 @@ func redrawApp(app *tview.Application) {
} }
} }
func refreshAllModules() { func refreshAllWidgets() {
for _, module := range Widgets { for _, widget := range Widgets {
go module.Refresh() go widget.Refresh()
} }
} }
@ -151,6 +152,7 @@ func main() {
bamboohr.Config = Config bamboohr.Config = Config
clocks.Config = Config clocks.Config = Config
cmdrunner.Config = Config
gcal.Config = Config gcal.Config = Config
git.Config = Config git.Config = Config
github.Config = Config github.Config = Config
@ -168,6 +170,7 @@ func main() {
Widgets = []wtf.Wtfable{ Widgets = []wtf.Wtfable{
bamboohr.NewWidget(), bamboohr.NewWidget(),
clocks.NewWidget(), clocks.NewWidget(),
cmdrunner.NewWidget(),
gcal.NewWidget(), gcal.NewWidget(),
git.NewWidget(app, pages), git.NewWidget(app, pages),
github.NewWidget(app, pages), github.NewWidget(app, pages),

View File

@ -85,3 +85,7 @@ func (widget *TextWidget) addView() {
widget.View = view widget.View = view
} }
func (widget *TextWidget) UpdateRefreshedAt() {
widget.RefreshedAt = time.Now()
}