Skip to content

Commit 7aed46d

Browse files
Adding lua function to use result of updated RunBackgroundShell
1 parent fa31745 commit 7aed46d

4 files changed

Lines changed: 23 additions & 14 deletions

File tree

cmd/micro/initlua.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ func luaImportMicroShell() *lua.LTable {
101101
ulua.L.SetField(pkg, "ExecCommand", luar.New(ulua.L, shell.ExecCommand))
102102
ulua.L.SetField(pkg, "RunCommand", luar.New(ulua.L, shell.RunCommand))
103103
ulua.L.SetField(pkg, "RunBackgroundShell", luar.New(ulua.L, shell.RunBackgroundShell))
104+
ulua.L.SetField(pkg, "LaunchBackgroundShellFunc", luar.New(ulua.L, func(f func() (string, error), cb func(string, error)) {
105+
go func() {
106+
output, runErr := f()
107+
if cb != nil {
108+
cb(output, runErr)
109+
}
110+
}()
111+
}))
104112
ulua.L.SetField(pkg, "RunInteractiveShell", luar.New(ulua.L, shell.RunInteractiveShell))
105113
ulua.L.SetField(pkg, "JobStart", luar.New(ulua.L, shell.JobStart))
106114
ulua.L.SetField(pkg, "JobSpawn", luar.New(ulua.L, shell.JobSpawn))

internal/action/command.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,17 @@ func (h *BufPane) UnbindCmd(args []string) {
819819

820820
// RunCmd runs a shell command in the background
821821
func (h *BufPane) RunCmd(args []string) {
822-
runf, err := shell.RunBackgroundShell(shellquote.Join(args...))
822+
runCmd := shellquote.Join(args...)
823+
runf, err := shell.RunBackgroundShell(runCmd)
823824
if err != nil {
824825
InfoBar.Error(err)
825826
} else {
826827
go func() {
827-
InfoBar.Message(runf())
828+
output, runErr := runf()
829+
if runErr != nil {
830+
output = fmt.Sprint(runCmd, " exited with error: ", err)
831+
}
832+
InfoBar.Message(output)
828833
screen.Redraw()
829834
}()
830835
}

internal/shell/shell.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package shell
33
import (
44
"bytes"
55
"errors"
6-
"fmt"
76
"io"
87
"os"
98
"os/exec"
@@ -48,23 +47,16 @@ func RunCommand(input string) (string, error) {
4847
// RunBackgroundShell runs a shell command in the background
4948
// It returns a function which will run the command and returns a string
5049
// message result
51-
func RunBackgroundShell(input string) (func() string, error) {
50+
func RunBackgroundShell(input string) (func() (string, error), error) {
5251
args, err := shellquote.Split(input)
5352
if err != nil {
5453
return nil, err
5554
}
5655
if len(args) == 0 {
5756
return nil, errors.New("No arguments")
5857
}
59-
inputCmd := args[0]
60-
return func() string {
61-
output, err := RunCommand(input)
62-
63-
str := output
64-
if err != nil {
65-
str = fmt.Sprint(inputCmd, " exited with error: ", err, ": ", output)
66-
}
67-
return str
58+
return func() (string, error) {
59+
return RunCommand(input)
6860
}, nil
6961
}
7062

runtime/help/plugins.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,13 @@ The packages and their contents are listed below (in Go type signatures):
249249
two arguments in the `ExecCommand` argument list (quoting arguments
250250
will preserve spaces).
251251

252-
- `RunBackgroundShell(input string) (func() string, error)`: returns a
252+
- `RunBackgroundShell(input string) (func() (string, error), error)`: returns a
253253
function that will run the given shell command and return its output.
254254

255+
- `LaunchBackgroundShellFunc(f func() (string, error), cb func(string, error))`: Runs
256+
the returned function from `RunBackgroundShell` in the background. The output and
257+
erorr of such function will be passed to the callback.
258+
255259
- `RunInteractiveShell(input string, wait bool, getOutput bool)
256260
(string, error)`:
257261
temporarily closes micro and runs the given command in the terminal.

0 commit comments

Comments
 (0)