From 838a932dd96a709c37fbee7c0117ad4b29356d28 Mon Sep 17 00:00:00 2001 From: aerth Date: Tue, 19 Apr 2016 19:49:43 +0000 Subject: [PATCH] stdout and stderr buffers for command execution --- cmd/micro/command.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index 39bc2f8fe..eb610e887 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -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 { @@ -31,7 +43,6 @@ func HandleShellCommand(input string, view *View) { } if outstring != "" { - // Display nonblank output DisplayBlock(outstring) } }