add a new shell type displaying the output

This commit is contained in:
Gokcehan committed 2018-03-26 21:22:18 +03:00
1 parent 5a88ca59a3
commit 32d729eac1
7 files changed
+66 -28

No files matched your search

+42 -13
View File
@@ -1,7 +1,9 @@
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
@@ -168,18 +170,19 @@ func waitKey() error {
// This function is used to run a command in shell. Following modes are used:
//
// Prefix Wait Async Stdin/Stdout/Stderr UI action
// $ No No Yes Pause and then resume
// ! Yes No Yes Pause and then resume
// & No Yes No Do nothing
//
// Waiting async commands are not used for now.
func (app *app) runShell(s string, args []string, wait bool, async bool) {
// Prefix Wait Async Stdin Stdout Stderr UI action
// $ No No Yes Yes Yes Pause and then resume
// % No No No Yes Yes Display output in statline
// ! Yes No Yes Yes Yes Pause and then resume
// & No Yes No No No Do nothing
func (app *app) runShell(s string, args []string, prefix string) {
app.exportVars()
cmd := shellCommand(s, args)
if !async {
var out io.Reader
switch prefix {
case "$", "!":
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -187,25 +190,51 @@ func (app *app) runShell(s string, args []string, wait bool, async bool) {
app.ui.pause()
defer app.ui.resume()
defer app.nav.renew(app.ui.wins[0].h)
case "%":
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("reading stdout: %s", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Printf("reading stderr: %s", err)
}
out = io.MultiReader(stdout, stderr)
defer app.nav.renew(app.ui.wins[0].h)
}
var err error
if async {
err = cmd.Start()
} else {
switch prefix {
case "$", "!":
err = cmd.Run()
case "%", "&":
err = cmd.Start()
}
if err != nil {
app.ui.printf("running shell: %s", err)
}
if wait {
switch prefix {
case "!":
if err := waitKey(); err != nil {
app.ui.printf("waiting shell: %s", err)
app.ui.printf("waiting key: %s", err)
}
}
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
switch prefix {
case "%":
app.ui.cmdPrefix = ""
scanner := bufio.NewScanner(out)
for scanner.Scan() {
app.ui.msg = scanner.Text()
app.ui.draw(app.nav)
}
if err := cmd.Wait(); err != nil {
log.Printf("running shell: %s", err)
}
}
}
+1
View File
@@ -36,6 +36,7 @@ var (
"reload",
"read",
"read-shell",
"read-shell-pipe",
"read-shell-wait",
"read-shell-async",
"search",
+2
View File
@@ -34,6 +34,7 @@ The following commands are provided by lf with default keybindings:
reload (default '<c-r>')
read (default ':')
read-shell (default '$')
read-shell-pipe (default '%')
read-shell-wait (default '!')
read-shell-async (default '&')
search (default '/')
@@ -143,6 +144,7 @@ The following command prefixes are used by lf:
: read (default) builtin/custom command
$ read-shell shell command
% read-shell-pipe shell command displaying the output
! read-shell-wait shell command waiting for key press
& read-shell-async asynchronous shell command
/ search search file in current directory
+2
View File
@@ -38,6 +38,7 @@ The following commands are provided by lf with default keybindings:
reload (default '<c-r>')
read (default ':')
read-shell (default '$')
read-shell-pipe (default '%')
read-shell-wait (default '!')
read-shell-async (default '&')
search (default '/')
@@ -150,6 +151,7 @@ The following command prefixes are used by lf:
: read (default) builtin/custom command
$ read-shell shell command
% read-shell-pipe shell command displaying the output
! read-shell-wait shell command waiting for key press
& read-shell-async asynchronous shell command
/ search search file in current directory
+16 -12
View File
@@ -346,6 +346,8 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.cmdPrefix = ":"
case "read-shell":
app.ui.cmdPrefix = "$"
case "read-shell-pipe":
app.ui.cmdPrefix = "%"
case "read-shell-wait":
app.ui.cmdPrefix = "!"
case "read-shell-async":
@@ -434,13 +436,18 @@ func (e *callExpr) eval(app *app, args []string) {
}
case "$":
log.Printf("shell: %s", s)
app.runShell(s, nil, false, false)
app.runShell(s, nil, app.ui.cmdPrefix)
case "%":
log.Printf("shell-pipe: %s", s)
app.runShell(s, nil, app.ui.cmdPrefix)
app.cmdHist = append(app.cmdHist, cmdItem{"%", s})
return
case "!":
log.Printf("shell-wait: %s", s)
app.runShell(s, nil, true, false)
app.runShell(s, nil, app.ui.cmdPrefix)
case "&":
log.Printf("shell-async: %s", s)
app.runShell(s, nil, false, true)
app.runShell(s, nil, app.ui.cmdPrefix)
case "/":
log.Printf("search: %s", s)
app.nav.search = s
@@ -548,19 +555,16 @@ func (e *execExpr) eval(app *app, args []string) {
switch e.prefix {
case "$":
log.Printf("shell: %s -- %s", e, args)
app.runShell(e.value, args, false, false)
app.runShell(e.value, args, e.prefix)
case "%":
log.Printf("shell-pipe: %s -- %s", e, args)
app.runShell(e.value, args, e.prefix)
case "!":
log.Printf("shell-wait: %s -- %s", e, args)
app.runShell(e.value, args, true, false)
app.runShell(e.value, args, e.prefix)
case "&":
log.Printf("shell-async: %s -- %s", e, args)
app.runShell(e.value, args, false, true)
case "/":
log.Printf("search: %s -- %s", e, args)
// TODO: implement
case "?":
log.Printf("search-back: %s -- %s", e, args)
// TODO: implement
app.runShell(e.value, args, e.prefix)
default:
log.Printf("evaluating unknown execution prefix: %q", e.prefix)
}
+1
View File
@@ -76,6 +76,7 @@ func init() {
gOpts.keys["<c-r>"] = &callExpr{"reload", nil}
gOpts.keys[":"] = &callExpr{"read", nil}
gOpts.keys["$"] = &callExpr{"read-shell", nil}
gOpts.keys["%"] = &callExpr{"read-shell-pipe", nil}
gOpts.keys["!"] = &callExpr{"read-shell-wait", nil}
gOpts.keys["&"] = &callExpr{"read-shell-async", nil}
gOpts.keys["/"] = &callExpr{"search", nil}
+2 -3
View File
@@ -15,7 +15,7 @@ const (
// no explicit keyword type
tokenIdent // e.g. set, ratios, 1:2:3
tokenColon // :
tokenPrefix // $, !, &, / or ?
tokenPrefix // $, %, !, &
tokenLBraces // {{
tokenRBraces // }}
tokenCommand // in between a prefix to \n or between {{ and }}
@@ -90,8 +90,7 @@ func isDigit(b byte) bool {
}
func isPrefix(b byte) bool {
// TODO: how to differentiate slash in path vs search?
return b == '$' || b == '!' || b == '&' // || b == '/' || b == '?'
return b == '$' || b == '%' || b == '!' || b == '&'
}
func (s *scanner) scan() bool {