shell command output is held in buffer until completion

This commit is contained in:
aerth 2016-04-19 13:40:05 +00:00
parent f3e9271cae
commit 54f00cb937
No known key found for this signature in database
GPG key ID: 63B5467D7A697B1D

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"os"
"os/exec"
"regexp"
@ -9,19 +10,26 @@ 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:]
// Execute Command
cmdout := exec.Command(inputCmd, args...)
output, _ := cmdout.CombinedOutput()
outstring := string(output)
cmd := exec.Command(inputCmd, args...)
outputBytes := &bytes.Buffer{}
cmd.Stdout = outputBytes // send output to buffer
cmd.Start()
cmd.Wait() // wait for command to finish
outstring := outputBytes.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 +66,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 +86,8 @@ func DisplayBlock(text string) {
}
case tcell.KeyCtrlQ, tcell.KeyCtrlW, tcell.KeyEscape, tcell.KeyCtrlC:
return
default:
return
}
}
}