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,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)
}