diff --git a/app.go b/app.go index b6619c5e..b7324700 100644 --- a/app.go +++ b/app.go @@ -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) + } + } } diff --git a/comp.go b/comp.go index dd41774e..cb50622a 100644 --- a/comp.go +++ b/comp.go @@ -36,6 +36,7 @@ var ( "reload", "read", "read-shell", + "read-shell-pipe", "read-shell-wait", "read-shell-async", "search", diff --git a/doc.go b/doc.go index 26a95466..0e0bcf9b 100644 --- a/doc.go +++ b/doc.go @@ -34,6 +34,7 @@ The following commands are provided by lf with default keybindings: reload (default '') 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 diff --git a/docstring.go b/docstring.go index 45bcf4f7..0dc5712e 100644 --- a/docstring.go +++ b/docstring.go @@ -38,6 +38,7 @@ The following commands are provided by lf with default keybindings: reload (default '') 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 diff --git a/eval.go b/eval.go index 1f82a6a4..4b38e9f9 100644 --- a/eval.go +++ b/eval.go @@ -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) } diff --git a/opts.go b/opts.go index 4131ce99..e8655943 100644 --- a/opts.go +++ b/opts.go @@ -76,6 +76,7 @@ func init() { gOpts.keys[""] = &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} diff --git a/scan.go b/scan.go index 0d6a85c6..b9c57267 100644 --- a/scan.go +++ b/scan.go @@ -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 {