Skip to content

Commit 96f2735

Browse files
Adding lua function to use result of updated RunBackgroundShell
Using Jobs channel to call lua callback
1 parent fa31745 commit 96f2735

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

cmd/micro/initlua.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +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, "ExecBackgroundCommand", luar.New(ulua.L, shell.ExecBackgroundCommand))
105+
ulua.L.SetField(pkg, "RunBackgroundCommand", luar.New(ulua.L, shell.RunBackgroundCommand))
104106
ulua.L.SetField(pkg, "RunInteractiveShell", luar.New(ulua.L, shell.RunInteractiveShell))
105107
ulua.L.SetField(pkg, "JobStart", luar.New(ulua.L, shell.JobStart))
106108
ulua.L.SetField(pkg, "JobSpawn", luar.New(ulua.L, shell.JobSpawn))

internal/action/command.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -819,15 +819,18 @@ 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...))
823-
if err != nil {
824-
InfoBar.Error(err)
825-
} else {
826-
go func() {
827-
InfoBar.Message(runf())
828-
screen.Redraw()
829-
}()
822+
if len(args) == 0 {
823+
InfoBar.Error("No arguments")
824+
return
830825
}
826+
827+
shell.ExecBackgroundCommand(func(output string, runErr error) {
828+
if runErr != nil {
829+
output = fmt.Sprint(args[0], " exited with error: ", runErr)
830+
}
831+
InfoBar.Message(output)
832+
screen.Redraw()
833+
}, args[0], args[1:]...)
831834
}
832835

833836
// QuitCmd closes the main view

internal/shell/shell.go

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

48+
// **Deprecated, don't use**
4849
// RunBackgroundShell runs a shell command in the background
4950
// It returns a function which will run the command and returns a string
5051
// message result
@@ -68,6 +69,47 @@ func RunBackgroundShell(input string) (func() string, error) {
6869
}, nil
6970
}
7071

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

runtime/help/plugins.md

Lines changed: 14 additions & 2 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)`: 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
- `RunInteractiveShell(input string, wait bool, getOutput bool)
256268
(string, error)`:

0 commit comments

Comments
 (0)