Have HandleShellCommand return the stdout

HandleShellCommand will now return the stdout as a string and
it also takes an additional flag indicating whether it should
wait before closing the shell and returning to the editor.
This commit is contained in:
Zachary Yedidia 2016-09-06 19:27:57 -04:00
parent 94465ef1ae
commit dce56a2b85
5 changed files with 27 additions and 14 deletions

View file

@ -1273,7 +1273,7 @@ func (v *View) ShellMode(usePlugin bool) bool {
input, canceled := messenger.Prompt("$ ", "Shell", NoCompletion)
if !canceled {
// The true here is for openTerm to make the command interactive
HandleShellCommand(input, true)
HandleShellCommand(input, true, true)
if usePlugin {
return PostActionCall("ShellMode", v)
}

View file

@ -2,6 +2,7 @@ package main
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
@ -225,7 +226,7 @@ func Bind(args []string) {
// Run runs a shell command in the background
func Run(args []string) {
// Run a shell command in the background (openTerm is false)
HandleShellCommand(strings.Join(args, " "), false)
HandleShellCommand(strings.Join(args, " "), false, true)
}
// Quit closes the main view
@ -362,7 +363,7 @@ func RunShellCommand(input string) (string, error) {
// HandleShellCommand runs the shell command
// The openTerm argument specifies whether a terminal should be opened (for viewing output
// or interacting with stdin)
func HandleShellCommand(input string, openTerm bool) {
func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
inputCmd := strings.Split(input, " ")[0]
if !openTerm {
// Simply run the command in the background and notify the user when it's done
@ -391,9 +392,10 @@ func HandleShellCommand(input string, openTerm bool) {
args := strings.Split(input, " ")[1:]
// Set up everything for the command
var outputBuf bytes.Buffer
cmd := exec.Command(inputCmd, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stdout = io.MultiWriter(os.Stdout, &outputBuf)
cmd.Stderr = os.Stderr
// This is a trap for Ctrl-C so that it doesn't kill micro
@ -406,16 +408,25 @@ func HandleShellCommand(input string, openTerm bool) {
}
}()
// Start the command
cmd.Start()
cmd.Wait()
err := cmd.Wait()
// This is just so we don't return right away and let the user press enter to return
TermMessage("")
output := outputBuf.String()
if err != nil {
output = err.Error()
}
if waitToFinish {
// This is just so we don't return right away and let the user press enter to return
TermMessage("")
}
// Start the screen back up
InitScreen()
return output
}
return ""
}
// HandleCommand handles input from the user

File diff suppressed because one or more lines are too long

View file

@ -73,8 +73,10 @@ as Go's GOOS variable, so `darwin`, `windows`, `linux`, `freebsd`...)
* `HandleCommand(cmd string)`: runs the given command
* `HandleShellCommand(shellCmd string, interactive bool)`: runs the given shell
command
* `HandleShellCommand(shellCmd string, interactive bool, waitToClose bool)`: runs the given shell
command. The `interactive` bool specifies whether the command should run in the background. The
`waitToClose` bool only applies if `interactive` is true and means that it should wait before
returning to the editor.
* `JobStart(cmd string, onStdout, onStderr, onExit string, userargs ...string)`:
Starts running the given shell command in the background. `onStdout` `onStderr` and `onExit`

View file

@ -80,7 +80,7 @@ You can do that by putting the following in `init.lua`:
function gorun()
local buf = CurView().Buf -- The current buffer
if buf:FileType() == "go" then
HandleShellCommand("go run " .. buf.Path, true) -- true means don't run it in the background
HandleShellCommand("go run " .. buf.Path, true, true) -- the first true means don't run it in the background
end
end