Terminal plugin callback support

This commit is contained in:
Zachary Yedidia
2019-08-26 14:47:27 -04:00
parent 199d65017f
commit b68461cf72
10 changed files with 115 additions and 11 deletions

View File

@@ -106,8 +106,14 @@ func JobSend(cmd *exec.Cmd, data string) {
// to the lua function
func luaFunctionJob(fn string) func(string, ...interface{}) {
luaFn := strings.Split(fn, ".")
if len(luaFn) <= 1 {
return nil
}
plName, plFn := luaFn[0], luaFn[1]
pl := config.FindPlugin(plName)
if pl == nil {
return nil
}
return func(output string, args ...interface{}) {
var luaArgs []lua.LValue
luaArgs = append(luaArgs, luar.New(ulua.L, output))

View File

@@ -6,13 +6,19 @@ import (
"os"
"os/exec"
"strconv"
"strings"
lua "github.com/yuin/gopher-lua"
"github.com/zyedidia/micro/internal/buffer"
"github.com/zyedidia/micro/internal/config"
ulua "github.com/zyedidia/micro/internal/lua"
"github.com/zyedidia/micro/internal/screen"
"github.com/zyedidia/terminal"
luar "layeh.com/gopher-luar"
)
type TermType int
type CallbackFunc func(string)
const (
TTClose = iota // Should be closed
@@ -20,6 +26,12 @@ const (
TTDone // Finished running a command
)
var CloseTerms chan bool
func init() {
CloseTerms = make(chan bool)
}
// A Terminal holds information for the terminal emulator
type Terminal struct {
State terminal.State
@@ -30,7 +42,7 @@ type Terminal struct {
wait bool
getOutput bool
output *bytes.Buffer
callback string
callback CallbackFunc
}
// HasSelection returns whether this terminal has a valid selection
@@ -64,7 +76,7 @@ func (t *Terminal) GetSelection(width int) string {
}
// Start begins a new command in this terminal with a given view
func (t *Terminal) Start(execCmd []string, getOutput bool, wait bool) error {
func (t *Terminal) Start(execCmd []string, getOutput bool, wait bool, callback string, userargs []interface{}) error {
if len(execCmd) <= 0 {
return nil
}
@@ -84,6 +96,25 @@ func (t *Terminal) Start(execCmd []string, getOutput bool, wait bool) error {
t.title = execCmd[0] + ":" + strconv.Itoa(cmd.Process.Pid)
t.wait = wait
luaFn := strings.Split(callback, ".")
if len(luaFn) >= 2 {
plName, plFn := luaFn[0], luaFn[1]
pl := config.FindPlugin(plName)
if pl != nil {
t.callback = func(out string) {
var luaArgs []lua.LValue
luaArgs = append(luaArgs, luar.New(ulua.L, out))
for _, v := range userargs {
luaArgs = append(luaArgs, luar.New(ulua.L, v))
}
_, err := pl.Call(plFn, luaArgs...)
if err != nil {
screen.TermMessage(err)
}
}
}
}
go func() {
for {
err := Term.Parse()
@@ -108,6 +139,7 @@ func (t *Terminal) Stop() {
t.Status = TTDone
} else {
t.Close()
CloseTerms <- true
}
}
@@ -117,11 +149,9 @@ func (t *Terminal) Close() {
t.Status = TTClose
// call the lua function that the user has given as a callback
if t.getOutput {
// TODO: plugin callback on Term emulator
// _, err := Call(t.callback, t.output.String())
// if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
// TermMessage(err)
// }
if t.callback != nil {
t.callback(t.output.String())
}
}
}