pipe stdin for 'shell-pipe' commands

This commit is contained in:
Gokcehan 2018-04-03 22:22:58 +03:00
parent 68232c06e3
commit c63c8f63ac
3 changed files with 51 additions and 18 deletions

47
app.go
View File

@ -19,6 +19,7 @@ type app struct {
ui *ui ui *ui
nav *nav nav *nav
quitChan chan bool quitChan chan bool
cmdIn io.WriteCloser
cmdHist []cmdItem cmdHist []cmdItem
cmdHistInd int cmdHistInd int
} }
@ -191,16 +192,17 @@ func (app *app) runShell(s string, args []string, prefix string) {
defer app.ui.resume() defer app.ui.resume()
defer app.nav.renew(app.ui.wins[0].h) defer app.nav.renew(app.ui.wins[0].h)
case "%": case "%":
stdin, err := cmd.StdinPipe()
if err != nil {
log.Printf("writing stdin: %s", err)
}
app.cmdIn = stdin
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
log.Printf("reading stdout: %s", err) log.Printf("reading stdout: %s", err)
} }
stderr, err := cmd.StderrPipe() out = stdout
if err != nil { cmd.Stderr = cmd.Stdout
log.Printf("reading stderr: %s", err)
}
out = io.MultiReader(stdout, stderr)
defer app.nav.renew(app.ui.wins[0].h)
} }
var err error var err error
@ -227,14 +229,31 @@ func (app *app) runShell(s string, args []string, prefix string) {
switch prefix { switch prefix {
case "%": case "%":
app.ui.cmdPrefix = "" go func() {
scanner := bufio.NewScanner(out) app.ui.msg = ""
for scanner.Scan() { app.ui.cmdPrefix = ">"
app.ui.msg = scanner.Text()
reader := bufio.NewReader(out)
var buf []byte
for {
b, err := reader.ReadByte()
if err == io.EOF {
break
}
buf = append(buf, b)
app.ui.msg = string(buf)
app.ui.draw(app.nav)
if b == '\n' {
buf = nil
}
}
if err := cmd.Wait(); err != nil {
log.Printf("running shell: %s", err)
}
app.nav.renew(app.ui.wins[0].h)
app.ui.cmdPrefix = ""
app.ui.draw(app.nav) app.ui.draw(app.nav)
} }()
if err := cmd.Wait(); err != nil {
log.Printf("running shell: %s", err)
}
} }
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"io"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -410,6 +411,9 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(e.args[0])...) app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(e.args[0])...)
} }
case "cmd-escape": case "cmd-escape":
if app.ui.cmdPrefix == ">" {
return
}
app.ui.menuBuf = nil app.ui.menuBuf = nil
app.ui.cmdAccLeft = nil app.ui.cmdAccLeft = nil
app.ui.cmdAccRight = nil app.ui.cmdAccRight = nil
@ -453,6 +457,9 @@ func (e *callExpr) eval(app *app, args []string) {
app.runShell(s, nil, app.ui.cmdPrefix) app.runShell(s, nil, app.ui.cmdPrefix)
app.cmdHist = append(app.cmdHist, cmdItem{"%", s}) app.cmdHist = append(app.cmdHist, cmdItem{"%", s})
return return
case ">":
io.WriteString(app.cmdIn, s+"\n")
return
case "!": case "!":
log.Printf("shell-wait: %s", s) log.Printf("shell-wait: %s", s)
app.runShell(s, nil, app.ui.cmdPrefix) app.runShell(s, nil, app.ui.cmdPrefix)

15
ui.go
View File

@ -611,14 +611,21 @@ func (ui *ui) draw(nav *nav) {
ui.wins[woff+i].printDir(nav.dirs[doff+i], nav.marks, nav.saves) ui.wins[woff+i].printDir(nav.dirs[doff+i], nav.marks, nav.saves)
} }
if ui.cmdPrefix != "" { switch ui.cmdPrefix {
case "":
ui.drawStatLine(nav)
termbox.HideCursor()
case ">":
ui.msgWin.printLine(0, 0, fg, bg, ui.cmdPrefix)
ui.msgWin.print(len(ui.cmdPrefix), 0, fg, bg, ui.msg)
ui.msgWin.print(len(ui.cmdPrefix)+len(ui.msg), 0, fg, bg, string(ui.cmdAccLeft))
ui.msgWin.print(len(ui.cmdPrefix)+len(ui.msg)+runeSliceWidth(ui.cmdAccLeft), 0, fg, bg, string(ui.cmdAccRight))
termbox.SetCursor(ui.msgWin.x+len(ui.cmdPrefix)+len(ui.msg)+runeSliceWidth(ui.cmdAccLeft), ui.msgWin.y)
default:
ui.msgWin.printLine(0, 0, fg, bg, ui.cmdPrefix) ui.msgWin.printLine(0, 0, fg, bg, ui.cmdPrefix)
ui.msgWin.print(len(ui.cmdPrefix), 0, fg, bg, string(ui.cmdAccLeft)) ui.msgWin.print(len(ui.cmdPrefix), 0, fg, bg, string(ui.cmdAccLeft))
ui.msgWin.print(len(ui.cmdPrefix)+runeSliceWidth(ui.cmdAccLeft), 0, fg, bg, string(ui.cmdAccRight)) ui.msgWin.print(len(ui.cmdPrefix)+runeSliceWidth(ui.cmdAccLeft), 0, fg, bg, string(ui.cmdAccRight))
termbox.SetCursor(ui.msgWin.x+len(ui.cmdPrefix)+runeSliceWidth(ui.cmdAccLeft), ui.msgWin.y) termbox.SetCursor(ui.msgWin.x+len(ui.cmdPrefix)+runeSliceWidth(ui.cmdAccLeft), ui.msgWin.y)
} else {
ui.drawStatLine(nav)
termbox.HideCursor()
} }
if gOpts.preview { if gOpts.preview {