mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
55
modules/cmdrunner/widget.go
Normal file
55
modules/cmdrunner/widget.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package cmdrunner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
|
||||
args []string
|
||||
cmd string
|
||||
result string
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget(app, "CmdRunner", "cmdrunner", false),
|
||||
|
||||
args: wtf.ToStrs(wtf.Config.UList("wtf.mods.cmdrunner.args")),
|
||||
cmd: wtf.Config.UString("wtf.mods.cmdrunner.cmd"),
|
||||
}
|
||||
|
||||
widget.View.SetWrap(true)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.execute()
|
||||
|
||||
title := tview.TranslateANSI(wtf.Config.UString("wtf.mods.cmdrunner.title", widget.String()))
|
||||
widget.View.SetTitle(title)
|
||||
|
||||
widget.View.SetText(widget.result)
|
||||
}
|
||||
|
||||
func (widget *Widget) String() string {
|
||||
args := strings.Join(widget.args, " ")
|
||||
|
||||
if args != "" {
|
||||
return fmt.Sprintf(" %s %s ", widget.cmd, args)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(" %s ", widget.cmd)
|
||||
}
|
||||
|
||||
func (widget *Widget) execute() {
|
||||
cmd := exec.Command(widget.cmd, widget.args...)
|
||||
widget.result = tview.TranslateANSI(wtf.ExecuteCommand(cmd))
|
||||
}
|
||||
Reference in New Issue
Block a user