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

67 lines
1.3 KiB
Go

package cmdrunner
import (
"fmt"
"os/exec"
"strings"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
app *tview.Application
args []string
cmd string
settings *Settings
}
// NewWidget creates a new instance of the widget
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, settings.common, false),
app: app,
args: settings.args,
cmd: settings.cmd,
settings: settings,
}
widget.View.SetWrap(true)
return &widget
}
// Refresh executes the command and updates the view with the results
func (widget *Widget) Refresh() {
result := widget.execute()
ansiTitle := tview.TranslateANSI(widget.String())
ansiResult := tview.TranslateANSI(result)
widget.app.QueueUpdateDraw(func() {
widget.View.SetTitle(ansiTitle)
widget.View.SetText(ansiResult)
})
}
// String returns the string representation of the widget
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)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) execute() string {
cmd := exec.Command(widget.cmd, widget.args...)
return wtf.ExecuteCommand(cmd)
}