mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package cmdrunner
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/rivo/tview"
|
|
"github.com/wtfutil/wtf/utils"
|
|
"github.com/wtfutil/wtf/view"
|
|
)
|
|
|
|
type Widget struct {
|
|
view.TextWidget
|
|
|
|
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: view.NewTextWidget(app, settings.common, false),
|
|
|
|
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.CommonSettings().Title)
|
|
if ansiTitle == defaultTitle {
|
|
ansiTitle = tview.TranslateANSI(widget.String())
|
|
}
|
|
ansiResult := tview.TranslateANSI(result)
|
|
|
|
widget.Redraw(ansiTitle, ansiResult, false)
|
|
}
|
|
|
|
// 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 utils.ExecuteCommand(cmd)
|
|
}
|