Skip to content

Commit 2c3265b

Browse files
Merge branch 'FixRunBackgroundShell' into dev
2 parents 32bd5ce + 58535a1 commit 2c3265b

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
> - [Adding auto complete support for multi cursors #3442](https://github.com/zyedidia/micro/pull/3442)
1616
> - [Fixing comment plugin not using user settings when overriding default setting #3424](https://github.com/zyedidia/micro/pull/3424)
1717
> - [Add the ability lock settings.json and bindings.json for plugins #3618](https://github.com/zyedidia/micro/pull/3618)
18-
> - [Adding error screen for lua functions running in status line #3691 ](https://github.com/zyedidia/micro/pull/3691)
18+
> - [Adding error screen for lua functions running in status line #3691](https://github.com/zyedidia/micro/pull/3691)
19+
> - [Adding lua function to use result of RunBackgroundShell #3692](https://github.com/zyedidia/micro/pull/3692)
1920
>
2021
> To see the diff between this and upstream master, click [here](https://github.com/zyedidia/micro/compare/master...Neko-Box-Coder:micro-dev:dev)
2122

cmd/micro/initlua.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ 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+
}))
104123
ulua.L.SetField(pkg, "RunInteractiveShell", luar.New(ulua.L, shell.RunInteractiveShell))
105124
ulua.L.SetField(pkg, "JobStart", luar.New(ulua.L, shell.JobStart))
106125
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
@@ -857,12 +857,17 @@ func (h *BufPane) UnbindCmd(args []string) {
857857

858858
// RunCmd runs a shell command in the background
859859
func (h *BufPane) RunCmd(args []string) {
860-
runf, err := shell.RunBackgroundShell(shellquote.Join(args...))
860+
runCmd := shellquote.Join(args...)
861+
runf, err := shell.RunBackgroundShell(runCmd)
861862
if err != nil {
862863
InfoBar.Error(err)
863864
} else {
864865
go func() {
865-
InfoBar.Message(runf())
866+
output, runErr := runf()
867+
if runErr != nil {
868+
output = fmt.Sprint(runCmd, " exited with error: ", err)
869+
}
870+
InfoBar.Message(output)
866871
screen.Redraw()
867872
}()
868873
}

internal/shell/job.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"io"
66
"os/exec"
7+
"runtime"
78
)
89

910
var Jobs chan JobFunction
@@ -54,7 +55,11 @@ func (f *CallbackFile) Write(data []byte) (int, error) {
5455
// JobStart starts a shell command in the background with the given callbacks
5556
// It returns an *exec.Cmd as the job id
5657
func JobStart(cmd string, onStdout, onStderr, onExit func(string, []interface{}), userargs ...interface{}) *Job {
57-
return JobSpawn("sh", []string{"-c", cmd}, onStdout, onStderr, onExit, userargs...)
58+
if runtime.GOOS == "windows" {
59+
return JobSpawn("cmd", []string{"/v:on", "/c", cmd}, onStdout, onStderr, onExit, userargs...)
60+
} else {
61+
return JobSpawn("sh", []string{"-c", cmd}, onStdout, onStderr, onExit, userargs...)
62+
}
5863
}
5964

6065
// JobSpawn starts a process with args in the background with the given callbacks

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)