Skip to content

Commit

Permalink
Merge pull request #44 from aerth/fork1
Browse files Browse the repository at this point in the history
stdout and stderr buffers for command execution
  • Loading branch information
zyedidia committed Apr 19, 2016
2 parents c767b3d + 838a932 commit 9603baa
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions cmd/micro/command.go
Expand Up @@ -14,15 +14,27 @@ import (
func HandleShellCommand(input string, view *View) {
inputCmd := strings.Split(input, " ")[0]
args := strings.Split(input, " ")[1:]

// Execute Command
if inputCmd == "exit" {
messenger.Message("Ctrl+Q to exit")
return
}
if inputCmd == "help" {
DisplayHelp()
return
}
cmd := exec.Command(inputCmd, args...)
outputBytes := &bytes.Buffer{}
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout // send output to buffer
cmd.Stderr = &stderr // send error to a different buffer
// Execute Command
err := cmd.Run()
if err != nil {
messenger.Message(err.Error() + "... " + stderr.String())
return
}

cmd.Stdout = outputBytes // send output to buffer
cmd.Start()
cmd.Wait() // wait for command to finish
outstring := outputBytes.String()
outstring := stdout.String()
totalLines := strings.Split(outstring, "\n")

if len(totalLines) < 3 {
Expand All @@ -31,7 +43,6 @@ func HandleShellCommand(input string, view *View) {
}

if outstring != "" {
// Display nonblank output
DisplayBlock(outstring)
}
}
Expand Down

0 comments on commit 9603baa

Please sign in to comment.