package main
import (
+ "bytes"
"os"
"os/exec"
"regexp"
"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)
}
}
_, 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--
}
case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC:
return
+ default:
+ return
}
}
}
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)
}