Skip to content

Commit 7ec2d96

Browse files
Merge branch 'FixRunBackgroundShell' into dev
2 parents 74f9b76 + 4658fa2 commit 7ec2d96

File tree

4 files changed

+73
-38
lines changed

4 files changed

+73
-38
lines changed

cmd/micro/initlua.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,8 @@ 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-
wrapperFunc := func(output string, args []interface{}) {
109-
errVal, ok := args[0].(error)
110-
if ok {
111-
cb(output, errVal)
112-
} else {
113-
cb(output, nil)
114-
}
115-
}
116-
var passArgs []interface{}
117-
passArgs = append(passArgs, runErr)
118-
jobFunc := shell.JobFunction{wrapperFunc, output, passArgs}
119-
shell.Jobs <- jobFunc
120-
}
121-
}()
122-
}))
104+
ulua.L.SetField(pkg, "ExecBackgroundCommand", luar.New(ulua.L, shell.ExecBackgroundCommand))
105+
ulua.L.SetField(pkg, "RunBackgroundCommand", luar.New(ulua.L, shell.RunBackgroundCommand))
123106
ulua.L.SetField(pkg, "RunInteractiveShell", luar.New(ulua.L, shell.RunInteractiveShell))
124107
ulua.L.SetField(pkg, "JobStart", luar.New(ulua.L, shell.JobStart))
125108
ulua.L.SetField(pkg, "JobSpawn", luar.New(ulua.L, shell.JobSpawn))

internal/action/command.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -857,20 +857,18 @@ func (h *BufPane) UnbindCmd(args []string) {
857857

858858
// RunCmd runs a shell command in the background
859859
func (h *BufPane) RunCmd(args []string) {
860-
runCmd := shellquote.Join(args...)
861-
runf, err := shell.RunBackgroundShell(runCmd)
862-
if err != nil {
863-
InfoBar.Error(err)
864-
} else {
865-
go func() {
866-
output, runErr := runf()
867-
if runErr != nil {
868-
output = fmt.Sprint(runCmd, " exited with error: ", err)
869-
}
870-
InfoBar.Message(output)
871-
screen.Redraw()
872-
}()
860+
if len(args) == 0 {
861+
InfoBar.Error("No arguments")
862+
return
873863
}
864+
865+
shell.ExecBackgroundCommand(func(output string, runErr error) {
866+
if runErr != nil {
867+
output = fmt.Sprint(args[0], " exited with error: ", runErr)
868+
}
869+
InfoBar.Message(output)
870+
screen.Redraw()
871+
}, args[0], args[1:]...)
874872
}
875873

876874
// QuitCmd closes the main view

internal/shell/shell.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func RunCommand(input string) (string, error) {
4444
return ExecCommand(inputCmd, args[1:]...)
4545
}
4646

47+
// **Deprecated, don't use**
4748
// RunBackgroundShell runs a shell command in the background
4849
// It returns a function which will run the command and returns a string
4950
// message result
@@ -60,6 +61,47 @@ func RunBackgroundShell(input string) (func() (string, error), error) {
6061
}, nil
6162
}
6263

64+
// ExecBackgroundCommand executes a command in the background by accepting
65+
// a optional callback function for the command output or error if any,
66+
// the command name and a list of arguments.
67+
func ExecBackgroundCommand(cb func(string, error), name string, args ...string) {
68+
go func() {
69+
output, runErr := ExecCommand(name, args[0:]...)
70+
if cb != nil {
71+
wrapperFunc := func(output string, args []interface{}) {
72+
errVal, ok := args[0].(error)
73+
if ok {
74+
cb(output, errVal)
75+
} else {
76+
cb(output, nil)
77+
}
78+
}
79+
80+
var passArgs []interface{}
81+
passArgs = append(passArgs, runErr)
82+
jobFunc := JobFunction{wrapperFunc, output, passArgs}
83+
Jobs <- jobFunc
84+
}
85+
}()
86+
}
87+
88+
// RunBackgroundCommand runs a shell command in the background by accepting
89+
// an input for running the command and an optional callback function for
90+
// the command output or error if any.
91+
// It returns an error if it fails to split the input command
92+
func RunBackgroundCommand(input string, cb func(string, error)) error {
93+
args, err := shellquote.Split(input)
94+
if err != nil {
95+
return err
96+
}
97+
if len(args) == 0 {
98+
return errors.New("No arguments")
99+
}
100+
101+
ExecBackgroundCommand(cb, args[0], args[1:]...)
102+
return nil
103+
}
104+
63105
// RunInteractiveShell runs a shellcommand interactively
64106
func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error) {
65107
args, err := shellquote.Split(input)

runtime/help/plugins.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,20 @@ 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), error)`: returns a
253-
function that will run the given shell command and return its output.
252+
- **Deprecated** `RunBackgroundShell(input string) (func() string, error)`:
253+
returns a function that will run the given shell command and return
254+
its output.
255+
256+
- `ExecBackgroundCommand(cb func(string, error), name string, args ...string)`:
257+
executes a command in the background by accepting a optional callback
258+
function for the command output or error if any, the command name and
259+
a list of arguments.
260+
261+
- `RunBackgroundCommand(input string, cb func(string, error)) error`:
262+
runs a shell command in the background by accepting an input for
263+
running the command and an optional callback function for the
264+
command output or error if any.
265+
It returns an error if it fails to split the input command
254266

255267
- `LaunchBackgroundShellFunc(f func() (string, error), cb func(string, error))`: Runs
256268
the returned function from `RunBackgroundShell` in the background. The output and
@@ -267,10 +279,10 @@ The packages and their contents are listed below (in Go type signatures):
267279
onExit func(string, []interface{}), userargs ...interface{})
268280
*exec.Cmd`:
269281
Starts a background job by running the shell on the given command
270-
(using `sh -c`). Three callbacks can be provided which will be called
271-
when the command generates stdout, stderr, or exits. The userargs will
272-
be passed to the callbacks, along with the output as the first
273-
argument of the callback. Returns the started command.
282+
(using `sh -c` or `cmd /v:on /c`). Three callbacks can be provided which
283+
will be called when the command generates stdout, stderr, or exits.
284+
The userargs will be passed to the callbacks, along with the output
285+
as the first argument of the callback. Returns the started command.
274286

275287
- `JobSpawn(cmd string, cmdArgs []string, onStdout, onStderr,
276288
onExit func(string, []interface{}), userargs ...interface{})

0 commit comments

Comments
 (0)