mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Let pty be optional
This commit is contained in:
parent
34154620f6
commit
e23cec2897
@ -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']."`
|
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."`
|
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."`
|
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."`
|
maxLines int `help:"Maximum number of lines kept in the buffer."`
|
||||||
|
|
||||||
// The dimensions of the module
|
// The dimensions of the module
|
||||||
@ -32,6 +33,7 @@ func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig
|
|||||||
|
|
||||||
args: utils.ToStrs(moduleConfig.UList("args")),
|
args: utils.ToStrs(moduleConfig.UList("args")),
|
||||||
cmd: moduleConfig.UString("cmd"),
|
cmd: moduleConfig.UString("cmd"),
|
||||||
|
pty: moduleConfig.UBool("pty", false),
|
||||||
tail: moduleConfig.UBool("tail", false),
|
tail: moduleConfig.UBool("tail", false),
|
||||||
maxLines: moduleConfig.UInt("maxLines", 256),
|
maxLines: moduleConfig.UInt("maxLines", 256),
|
||||||
}
|
}
|
||||||
|
@ -124,13 +124,12 @@ func runCommandLoop(widget *Widget) {
|
|||||||
widget.resetBuffer()
|
widget.resetBuffer()
|
||||||
cmd := exec.Command(widget.settings.cmd, widget.settings.args...)
|
cmd := exec.Command(widget.settings.cmd, widget.settings.args...)
|
||||||
cmd.Env = widget.environment()
|
cmd.Env = widget.environment()
|
||||||
f, err := pty.Start(cmd)
|
var err error
|
||||||
// The command has exited, print any error messages
|
if widget.settings.pty {
|
||||||
if err != nil {
|
err = runCommandPty(widget, cmd)
|
||||||
widget.handleError(err)
|
} else {
|
||||||
|
err = runCommand(widget, cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.Copy(widget.buffer, f)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
widget.handleError(err)
|
widget.handleError(err)
|
||||||
}
|
}
|
||||||
@ -138,6 +137,22 @@ func runCommandLoop(widget *Widget) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
func (widget *Widget) handleError(err error) {
|
||||||
widget.m.Lock()
|
widget.m.Lock()
|
||||||
_, writeErr := widget.buffer.WriteString(err.Error())
|
_, writeErr := widget.buffer.WriteString(err.Error())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user