Skip to content

Commit da5d405

Browse files
Adding lua function to use result of updated RunBackgroundShell
Using Jobs channel to call lua callback
1 parent 45342bb commit da5d405

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-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: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -873,15 +873,17 @@ func (h *BufPane) UnbindCmd(args []string) {
873873

874874
// RunCmd runs a shell command in the background
875875
func (h *BufPane) RunCmd(args []string) {
876-
runf, err := shell.RunBackgroundShell(shellquote.Join(args...))
877-
if err != nil {
878-
InfoBar.Error(err)
879-
} else {
880-
go func() {
881-
InfoBar.Message(runf())
882-
screen.Redraw()
883-
}()
876+
if len(args) == 0 {
877+
InfoBar.Error("No arguments")
878+
return
884879
}
880+
881+
shell.ExecBackgroundCommand(func(output string, runErr error) {
882+
if runErr != nil {
883+
output = fmt.Sprint(args[0], " exited with error: ", runErr)
884+
}
885+
InfoBar.Message(output)
886+
}, args[0], args[1:]...)
885887
}
886888

887889
// QuitCmd closes the main view

internal/shell/shell.go

Lines changed: 38 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,43 @@ func RunBackgroundShell(input string) (func() string, error) {
6869
}, nil
6970
}
7071

72+
// ExecBackgroundCommand is similar to ExecCommand except it runs in the
73+
// background and accept an optional callback function for the command output
74+
// or error if any.
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 []any) {
80+
errVal, _ := args[0].(error)
81+
cb(output, errVal)
82+
}
83+
84+
var passArgs []any
85+
passArgs = append(passArgs, runErr)
86+
jobFunc := JobFunction{wrapperFunc, output, passArgs}
87+
Jobs <- jobFunc
88+
}
89+
}()
90+
}
91+
92+
// RunBackgroundCommand is similar to RunCommand except it runs in the
93+
// background and accept an optional callback function for the command output
94+
// or error if any.
95+
// It returns an error immediately if it fails to split the input command
96+
func RunBackgroundCommand(input string, cb func(string, error)) error {
97+
args, err := shellquote.Split(input)
98+
if err != nil {
99+
return err
100+
}
101+
if len(args) == 0 {
102+
return errors.New("No arguments")
103+
}
104+
105+
ExecBackgroundCommand(cb, args[0], args[1:]...)
106+
return nil
107+
}
108+
71109
// RunInteractiveShell runs a shellcommand interactively
72110
func RunInteractiveShell(input string, wait bool, getOutput bool) (string, error) {
73111
args, err := shellquote.Split(input)

runtime/help/plugins.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,18 @@ The packages and their contents are listed below (in Go type signatures):
253253
two arguments in the `ExecCommand` argument list (quoting arguments
254254
will preserve spaces).
255255

256-
- `RunBackgroundShell(input string) (func() string, error)`: returns a
257-
function that will run the given shell command and return its output.
256+
- **Deprecated** `RunBackgroundShell(input string) (func() string, error)`:
257+
returns a function that will run the given shell command and return
258+
its output.
259+
260+
- `ExecBackgroundCommand(cb func(string, error), name string, args ...string)`:
261+
similar to ExecCommand except it runs in the background and accept
262+
an optional callback function for the command output or error if any.
263+
264+
- `RunBackgroundCommand(input string, cb func(string, error)) error`:
265+
similar to RunCommand except it runs in the background and accept
266+
an optional callback function for the command output or error if any.
267+
It returns an error immediately if it fails to split the input command
258268

259269
- `RunInteractiveShell(input string, wait bool, getOutput bool)
260270
(string, error)`:

0 commit comments

Comments
 (0)