mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
@@ -18,6 +18,7 @@ type Settings struct {
|
||||
args []string `help:"The arguments to the command, with each item as an element in an array. Example: for curl -I cisco.com, the arguments array would be ['-I', 'cisco.com']."`
|
||||
cmd string `help:"The terminal command to be run, withouth the arguments. Ie: ping, whoami, curl."`
|
||||
tail bool `help:"Automatically scroll to the end of the command output."`
|
||||
pty bool `help:"Run the command in a pseudo-terminal. Some apps will behave differently if they feel in a terminal. For example, some apps will produce colorized output in a terminal, and non-colorized output otherwise. Default false" optional:"true"`
|
||||
maxLines int `help:"Maximum number of lines kept in the buffer."`
|
||||
|
||||
// The dimensions of the module
|
||||
@@ -32,6 +33,7 @@ func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig
|
||||
|
||||
args: utils.ToStrs(moduleConfig.UList("args")),
|
||||
cmd: moduleConfig.UString("cmd"),
|
||||
pty: moduleConfig.UBool("pty", false),
|
||||
tail: moduleConfig.UBool("tail", false),
|
||||
maxLines: moduleConfig.UInt("maxLines", 256),
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@ package cmdrunner
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/creack/pty"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
@@ -121,23 +123,45 @@ func runCommandLoop(widget *Widget) {
|
||||
<-widget.runChan
|
||||
widget.resetBuffer()
|
||||
cmd := exec.Command(widget.settings.cmd, widget.settings.args...)
|
||||
cmd.Stdout = widget
|
||||
cmd.Env = widget.environment()
|
||||
err := cmd.Run()
|
||||
|
||||
// The command has exited, print any error messages
|
||||
var err error
|
||||
if widget.settings.pty {
|
||||
err = runCommandPty(widget, cmd)
|
||||
} else {
|
||||
err = runCommand(widget, cmd)
|
||||
}
|
||||
if err != nil {
|
||||
widget.m.Lock()
|
||||
_, writeErr := widget.buffer.WriteString(err.Error())
|
||||
if writeErr != nil {
|
||||
return
|
||||
}
|
||||
widget.m.Unlock()
|
||||
widget.handleError(err)
|
||||
}
|
||||
widget.redrawChan <- true
|
||||
}
|
||||
}
|
||||
|
||||
func runCommand(widget *Widget, cmd *exec.Cmd) error {
|
||||
cmd.Stdout = widget
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func runCommandPty(widget *Widget, cmd *exec.Cmd) error {
|
||||
f, err := pty.Start(cmd)
|
||||
// The command has exited, print any error messages
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(widget.buffer, f)
|
||||
return err
|
||||
}
|
||||
|
||||
func (widget *Widget) handleError(err error) {
|
||||
widget.m.Lock()
|
||||
defer widget.m.Unlock()
|
||||
_, writeErr := widget.buffer.WriteString(err.Error())
|
||||
if writeErr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func redrawLoop(widget *Widget) {
|
||||
for {
|
||||
widget.Redraw(widget.content)
|
||||
|
||||
Reference in New Issue
Block a user