]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/command.go
stdout and stderr buffers for command execution
[micro.git] / cmd / micro / command.go
index 2c6f1ecfd1c059dde85ba4b181502f8f8fe12186..eb610e887f33c0a7964b35a6e9fa1ed7c04e2504 100644 (file)
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "bytes"
        "os"
        "os/exec"
        "regexp"
@@ -9,21 +10,39 @@ import (
        "github.com/gdamore/tcell"
 )
 
+// HandleShellCommand runs the shell command and outputs to DisplayBlock
 func HandleShellCommand(input string, view *View) {
        inputCmd := strings.Split(input, " ")[0]
        args := strings.Split(input, " ")[1:]
-
+       if inputCmd == "exit" {
+               messenger.Message("Ctrl+Q to exit")
+               return
+       }
+       if inputCmd == "help" {
+               DisplayHelp()
+               return
+       }
+       cmd := exec.Command(inputCmd, args...)
+       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
-       cmdout := exec.Command(inputCmd, args...)
-       output, _ := cmdout.CombinedOutput()
-       outstring := string(output)
+       err := cmd.Run()
+       if err != nil {
+               messenger.Message(err.Error() + "... " + stderr.String())
+               return
+       }
+
+       outstring := stdout.String()
        totalLines := strings.Split(outstring, "\n")
-       if len(totalLines) == 2 {
+
+       if len(totalLines) < 3 {
                messenger.Message(outstring)
                return
        }
+
        if outstring != "" {
-               // Display nonblank output
                DisplayBlock(outstring)
        }
 }
@@ -58,6 +77,16 @@ func DisplayBlock(text string) {
                        _, height = e.Size()
                case *tcell.EventKey:
                        switch e.Key() {
+                       case tcell.KeyPgUp:
+                               if topline > height {
+                                       topline = topline - height
+                               } else {
+                                       topline = 0
+                               }
+                       case tcell.KeyPgDn:
+                               if topline < len(totalLines)-height {
+                                       topline = topline + height
+                               }
                        case tcell.KeyUp:
                                if topline > 0 {
                                        topline--
@@ -68,6 +97,8 @@ func DisplayBlock(text string) {
                                }
                        case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC:
                                return
+                       default:
+                               return
                        }
                }
        }
@@ -97,7 +128,7 @@ func HandleCommand(input string, view *View) {
        case "set":
                SetOption(view, args)
        case "quit":
-               if view.CanClose("Quit anyway? ") {
+               if view.CanClose("Quit anyway? (yes, no, save) ") {
                        screen.Fini()
                        os.Exit(0)
                }