mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "0a5a2e727e713a726644d369c72feee79000f786" and "5a3b28142c832838bb26e5ff43595da600b28e62" have entirely different histories.
0a5a2e727e
...
5a3b28142c
|
@ -66,14 +66,13 @@ func (a *aliasHandler) Resolve(cmdstr string) string {
|
||||||
|
|
||||||
// lua section
|
// lua section
|
||||||
|
|
||||||
func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
|
func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
|
||||||
// create a lua module with our functions
|
// create a lua module with our functions
|
||||||
hshaliasesLua := map[string]util.LuaExport{
|
hshaliasesLua := map[string]util.LuaExport{
|
||||||
"add": util.LuaExport{a.luaAdd, 2, false},
|
"add": util.LuaExport{a.luaAdd, 2, false},
|
||||||
"list": util.LuaExport{a.luaList, 0, false},
|
"list": util.LuaExport{a.luaList, 0, false},
|
||||||
"del": util.LuaExport{a.luaDelete, 1, false},
|
"del": util.LuaExport{a.luaDelete, 1, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
mod := rt.NewTable()
|
mod := rt.NewTable()
|
||||||
util.SetExports(rtm, mod, hshaliasesLua)
|
util.SetExports(rtm, mod, hshaliasesLua)
|
||||||
|
|
||||||
|
|
101
api.go
101
api.go
|
@ -31,14 +31,10 @@ var exports = map[string]util.LuaExport{
|
||||||
"cwd": util.LuaExport{hlcwd, 0, false},
|
"cwd": util.LuaExport{hlcwd, 0, false},
|
||||||
/*
|
/*
|
||||||
"exec": hlexec,
|
"exec": hlexec,
|
||||||
*/
|
"runnerMode": hlrunnerMode,
|
||||||
"runnerMode": util.LuaExport{hlrunnerMode, 1, false},
|
|
||||||
/*
|
|
||||||
"goro": hlgoro,
|
"goro": hlgoro,
|
||||||
*/
|
"highlighter": hlhighlighter,
|
||||||
"highlighter": util.LuaExport{hlhighlighter, 1, false},
|
"hinter": hlhinter,
|
||||||
"hinter": util.LuaExport{hlhinter, 1, false},
|
|
||||||
/*
|
|
||||||
"multiprompt": hlmlprompt,
|
"multiprompt": hlmlprompt,
|
||||||
"prependPath": hlprependPath,
|
"prependPath": hlprependPath,
|
||||||
*/
|
*/
|
||||||
|
@ -65,7 +61,7 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) {
|
||||||
util.SetExports(rtm, mod, exports)
|
util.SetExports(rtm, mod, exports)
|
||||||
hshMod = mod
|
hshMod = mod
|
||||||
|
|
||||||
host, _ := os.Hostname()
|
// host, _ := os.Hostname()
|
||||||
username := curuser.Username
|
username := curuser.Username
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
|
@ -76,17 +72,22 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) {
|
||||||
The nice lil shell for {blue}Lua{reset} fanatics!
|
The nice lil shell for {blue}Lua{reset} fanatics!
|
||||||
Check out the {blue}{bold}guide{reset} command to get started.
|
Check out the {blue}{bold}guide{reset} command to get started.
|
||||||
`
|
`
|
||||||
util.SetField(rtm, mod, "ver", rt.StringValue(version), "Hilbish version")
|
/*
|
||||||
util.SetField(rtm, mod, "user", rt.StringValue(username), "Username of user")
|
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
|
||||||
util.SetField(rtm, mod, "host", rt.StringValue(host), "Host name of the machine")
|
util.SetField(L, mod, "user", lua.LString(username), "Username of user")
|
||||||
|
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
|
||||||
|
*/
|
||||||
util.SetField(rtm, mod, "home", rt.StringValue(curuser.HomeDir), "Home directory of the user")
|
util.SetField(rtm, mod, "home", rt.StringValue(curuser.HomeDir), "Home directory of the user")
|
||||||
util.SetField(rtm, mod, "dataDir", rt.StringValue(dataDir), "Directory for Hilbish's data files")
|
util.SetField(rtm, mod, "dataDir", rt.StringValue(dataDir), "Directory for Hilbish's data files")
|
||||||
util.SetField(rtm, mod, "interactive", rt.BoolValue(interactive), "If this is an interactive shell")
|
/*
|
||||||
util.SetField(rtm, mod, "login", rt.BoolValue(login), "Whether this is a login shell")
|
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
|
||||||
|
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
|
||||||
|
*/
|
||||||
util.SetField(rtm, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.")
|
util.SetField(rtm, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.")
|
||||||
util.SetField(rtm, mod, "vimMode", rt.NilValue, "Current Vim mode of Hilbish (nil if not in Vim mode)")
|
/*util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
|
||||||
util.SetField(rtm, hshMod, "exitCode", rt.IntValue(0), "Exit code of last exected command")
|
util.SetField(l, hshMod, "exitCode", lua.LNumber(0), "Exit code of last exected command")
|
||||||
//util.Document(rtm, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
|
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
|
||||||
|
*/
|
||||||
|
|
||||||
// hilbish.userDir table
|
// hilbish.userDir table
|
||||||
hshuser := rt.NewTable()
|
hshuser := rt.NewTable()
|
||||||
|
@ -128,17 +129,19 @@ Check out the {blue}{bold}guide{reset} command to get started.
|
||||||
util.Document(L, hshcomp, "Completions interface for Hilbish.")
|
util.Document(L, hshcomp, "Completions interface for Hilbish.")
|
||||||
L.SetField(mod, "completion", hshcomp)
|
L.SetField(mod, "completion", hshcomp)
|
||||||
|
|
||||||
*/
|
|
||||||
// hilbish.runner table
|
// hilbish.runner table
|
||||||
runnerModule := runnerModeLoader(rtm)
|
runnerModule := runnerModeLoader(L)
|
||||||
//util.Document(L, runnerModule, "Runner/exec interface for Hilbish.")
|
util.Document(L, runnerModule, "Runner/exec interface for Hilbish.")
|
||||||
mod.Set(rt.StringValue("runner"), rt.TableValue(runnerModule))
|
L.SetField(mod, "runner", runnerModule)
|
||||||
|
*/
|
||||||
|
|
||||||
// hilbish.jobs table
|
// hilbish.jobs table
|
||||||
jobs = newJobHandler()
|
jobs = newJobHandler()
|
||||||
jobModule := jobs.loader(rtm)
|
/*
|
||||||
// util.Document(L, jobModule, "(Background) job interface.")
|
jobModule := jobs.loader(L)
|
||||||
mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule))
|
util.Document(L, jobModule, "(Background) job interface.")
|
||||||
|
L.SetField(mod, "jobs", jobModule)
|
||||||
|
*/
|
||||||
|
|
||||||
return rt.TableValue(mod), nil
|
return rt.TableValue(mod), nil
|
||||||
}
|
}
|
||||||
|
@ -542,6 +545,7 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
return c.Next(), nil
|
return c.Next(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// runnerMode(mode)
|
// runnerMode(mode)
|
||||||
// Sets the execution/runner mode for interactive Hilbish. This determines whether
|
// Sets the execution/runner mode for interactive Hilbish. This determines whether
|
||||||
// Hilbish wll try to run input as Lua and/or sh or only do one of either.
|
// Hilbish wll try to run input as Lua and/or sh or only do one of either.
|
||||||
|
@ -549,24 +553,24 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
// sh, and lua. It also accepts a function, to which if it is passed one
|
// sh, and lua. It also accepts a function, to which if it is passed one
|
||||||
// will call it to execute user input instead.
|
// will call it to execute user input instead.
|
||||||
// --- @param mode string|function
|
// --- @param mode string|function
|
||||||
func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func hlrunnerMode(L *lua.LState) int {
|
||||||
if err := c.Check1Arg(); err != nil {
|
mode := L.CheckAny(1)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
mode := c.Arg(0)
|
|
||||||
|
|
||||||
switch mode.Type() {
|
switch mode.Type() {
|
||||||
case rt.StringType:
|
case lua.LTString:
|
||||||
switch mode.AsString() {
|
switch mode.String() {
|
||||||
// no fallthrough doesnt work so eh
|
// no fallthrough doesnt work so eh
|
||||||
case "hybrid", "hybridRev", "lua", "sh": runnerMode = mode
|
case "hybrid": fallthrough
|
||||||
default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
|
case "hybridRev": fallthrough
|
||||||
|
case "lua": fallthrough
|
||||||
|
case "sh":
|
||||||
|
runnerMode = mode
|
||||||
|
default: L.RaiseError("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received %v", mode)
|
||||||
}
|
}
|
||||||
case rt.FunctionType: runnerMode = mode
|
case lua.LTFunction: runnerMode = mode
|
||||||
default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
|
default: L.RaiseError("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received %v", mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Next(), nil
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// hinter(cb)
|
// hinter(cb)
|
||||||
|
@ -575,17 +579,11 @@ func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
// the current line and the position. It is expected to return a string
|
// the current line and the position. It is expected to return a string
|
||||||
// which will be used for the hint.
|
// which will be used for the hint.
|
||||||
// --- @param cb function
|
// --- @param cb function
|
||||||
func hlhinter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func hlhinter(L *lua.LState) int {
|
||||||
if err := c.Check1Arg(); err != nil {
|
hinterCb := L.CheckFunction(1)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
hinterCb, err := c.ClosureArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
hinter = hinterCb
|
hinter = hinterCb
|
||||||
|
|
||||||
return c.Next(), err
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// highlighter(cb)
|
// highlighter(cb)
|
||||||
|
@ -594,15 +592,10 @@ func hlhinter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
// is passed the current line as typed and is expected to return a line that will
|
// is passed the current line as typed and is expected to return a line that will
|
||||||
// be used to display in the line.
|
// be used to display in the line.
|
||||||
// --- @param cb function
|
// --- @param cb function
|
||||||
func hlhighlighter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func hlhighlighter(L *lua.LState) int {
|
||||||
if err := c.Check1Arg(); err != nil {
|
highlighterCb := L.CheckFunction(1)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
highlighterCb, err := c.ClosureArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
highlighter = highlighterCb
|
highlighter = highlighterCb
|
||||||
|
|
||||||
return c.Next(), err
|
return 0
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
28
exec.go
28
exec.go
|
@ -24,15 +24,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var errNotExec = errors.New("not executable")
|
var errNotExec = errors.New("not executable")
|
||||||
var runnerMode rt.Value = rt.StringValue("hybrid")
|
//var runnerMode lua.LValue = lua.LString("hybrid")
|
||||||
|
|
||||||
func runInput(input string, priv bool) {
|
func runInput(input string, priv bool) {
|
||||||
running = true
|
running = true
|
||||||
cmdString := aliases.Resolve(input)
|
cmdString := aliases.Resolve(input)
|
||||||
hooks.Em.Emit("command.preexec", input, cmdString)
|
hooks.Em.Emit("command.preexec", input, cmdString)
|
||||||
|
|
||||||
if runnerMode.Type() == rt.StringType {
|
// if runnerMode.Type() == lua.LTString {
|
||||||
switch runnerMode.AsString() {
|
switch /*runnerMode.String()*/ "hybrid" {
|
||||||
case "hybrid":
|
case "hybrid":
|
||||||
_, err := handleLua(cmdString)
|
_, err := handleLua(cmdString)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -68,29 +68,35 @@ func runInput(input string, priv bool) {
|
||||||
}
|
}
|
||||||
cmdFinish(exitCode, cmdString, priv)
|
cmdFinish(exitCode, cmdString, priv)
|
||||||
}
|
}
|
||||||
} else {
|
// } else {
|
||||||
// can only be a string or function so
|
// can only be a string or function so
|
||||||
term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 2, false)
|
/*
|
||||||
err := rt.Call(l.MainThread(), runnerMode, []rt.Value{rt.StringValue(cmdString)}, term)
|
err := l.CallByParam(lua.P{
|
||||||
|
Fn: runnerMode,
|
||||||
|
NRet: 2,
|
||||||
|
Protect: true,
|
||||||
|
}, lua.LString(cmdString))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
cmdFinish(124, cmdString, priv)
|
cmdFinish(124, cmdString, priv)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
luaexitcode := term.Get(0) // first return value (makes sense right i love stacks)
|
luaexitcode := l.Get(-2) // first return value (makes sense right i love stacks)
|
||||||
runErr := term.Get(1)
|
runErr := l.Get(-1)
|
||||||
|
l.Pop(2)
|
||||||
|
|
||||||
var exitCode uint8
|
var exitCode uint8
|
||||||
if code, ok := luaexitcode.TryInt(); ok {
|
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
|
||||||
exitCode = uint8(code)
|
exitCode = uint8(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if runErr != rt.NilValue {
|
if runErr != lua.LNil {
|
||||||
fmt.Fprintln(os.Stderr, runErr)
|
fmt.Fprintln(os.Stderr, runErr)
|
||||||
}
|
}
|
||||||
cmdFinish(exitCode, cmdString, priv)
|
cmdFinish(exitCode, cmdString, priv)
|
||||||
}
|
*/
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLua(cmdString string) (uint8, error) {
|
func handleLua(cmdString string) (uint8, error) {
|
||||||
|
|
|
@ -24,14 +24,14 @@ func New() Bait {
|
||||||
Em: emitter,
|
Em: emitter,
|
||||||
}
|
}
|
||||||
b.Loader = packagelib.Loader{
|
b.Loader = packagelib.Loader{
|
||||||
Load: b.loaderFunc,
|
Load: b.LoaderFunc,
|
||||||
Name: "bait",
|
Name: "bait",
|
||||||
}
|
}
|
||||||
|
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bait) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
func (b *Bait) LoaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
||||||
exports := map[string]util.LuaExport{
|
exports := map[string]util.LuaExport{
|
||||||
"catch": util.LuaExport{b.bcatch, 2, false},
|
"catch": util.LuaExport{b.bcatch, 2, false},
|
||||||
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
|
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
|
||||||
|
|
|
@ -18,14 +18,14 @@ func New() Commander {
|
||||||
Events: emission.NewEmitter(),
|
Events: emission.NewEmitter(),
|
||||||
}
|
}
|
||||||
c.Loader = packagelib.Loader{
|
c.Loader = packagelib.Loader{
|
||||||
Load: c.loaderFunc,
|
Load: c.LoaderFunc,
|
||||||
Name: "commander",
|
Name: "commander",
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
func (c *Commander) LoaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
||||||
exports := map[string]util.LuaExport{
|
exports := map[string]util.LuaExport{
|
||||||
"register": util.LuaExport{c.cregister, 2, false},
|
"register": util.LuaExport{c.cregister, 2, false},
|
||||||
"deregister": util.LuaExport{c.cderegister, 1, false},
|
"deregister": util.LuaExport{c.cderegister, 1, false},
|
||||||
|
|
|
@ -12,11 +12,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var Loader = packagelib.Loader{
|
var Loader = packagelib.Loader{
|
||||||
Load: loaderFunc,
|
Load: LoaderFunc,
|
||||||
Name: "fs",
|
Name: "fs",
|
||||||
}
|
}
|
||||||
|
|
||||||
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
func LoaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
||||||
exports := map[string]util.LuaExport{
|
exports := map[string]util.LuaExport{
|
||||||
"cd": util.LuaExport{fcd, 1, false},
|
"cd": util.LuaExport{fcd, 1, false},
|
||||||
"mkdir": util.LuaExport{fmkdir, 2, false},
|
"mkdir": util.LuaExport{fmkdir, 2, false},
|
||||||
|
|
79
job.go
79
job.go
|
@ -3,10 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"hilbish/util"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var jobs *jobHandler
|
var jobs *jobHandler
|
||||||
|
@ -23,7 +19,7 @@ type job struct {
|
||||||
func (j *job) start(pid int) {
|
func (j *job) start(pid int) {
|
||||||
j.pid = pid
|
j.pid = pid
|
||||||
j.running = true
|
j.running = true
|
||||||
hooks.Em.Emit("job.start", j.lua())
|
// hooks.Em.Emit("job.start", j.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) stop() {
|
func (j *job) stop() {
|
||||||
|
@ -33,36 +29,39 @@ func (j *job) stop() {
|
||||||
|
|
||||||
func (j *job) finish() {
|
func (j *job) finish() {
|
||||||
j.running = false
|
j.running = false
|
||||||
hooks.Em.Emit("job.done", j.lua())
|
// hooks.Em.Emit("job.done", j.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) setHandle(handle *os.Process) {
|
func (j *job) setHandle(handle *os.Process) {
|
||||||
j.proc = handle
|
j.proc = handle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) lua() rt.Value {
|
/*
|
||||||
jobFuncs := map[string]util.LuaExport{
|
func (j *job) lua() *lua.LTable {
|
||||||
"stop": {j.luaStop, 0, false},
|
// returns lua table for job
|
||||||
|
// because userdata is gross
|
||||||
|
jobFuncs := map[string]lua.LGFunction{
|
||||||
|
"stop": j.luaStop,
|
||||||
}
|
}
|
||||||
luaJob := rt.NewTable()
|
luaJob := l.SetFuncs(l.NewTable(), jobFuncs)
|
||||||
util.SetExports(l, luaJob, jobFuncs)
|
|
||||||
|
|
||||||
luaJob.Set(rt.StringValue("cmd"), rt.StringValue(j.cmd))
|
l.SetField(luaJob, "cmd", lua.LString(j.cmd))
|
||||||
luaJob.Set(rt.StringValue("running"), rt.BoolValue(j.running))
|
l.SetField(luaJob, "running", lua.LBool(j.running))
|
||||||
luaJob.Set(rt.StringValue("id"), rt.IntValue(int64(j.id)))
|
l.SetField(luaJob, "id", lua.LNumber(j.id))
|
||||||
luaJob.Set(rt.StringValue("pid"), rt.IntValue(int64(j.pid)))
|
l.SetField(luaJob, "pid", lua.LNumber(j.pid))
|
||||||
luaJob.Set(rt.StringValue("exitCode"), rt.IntValue(int64(j.exitCode)))
|
l.SetField(luaJob, "exitCode", lua.LNumber(j.exitCode))
|
||||||
|
|
||||||
return rt.TableValue(luaJob)
|
return luaJob
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *job) luaStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (j *job) luaStop(L *lua.LState) int {
|
||||||
if j.running {
|
if j.running {
|
||||||
j.stop()
|
j.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Next(), nil
|
return 0
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
type jobHandler struct {
|
type jobHandler struct {
|
||||||
jobs map[int]*job
|
jobs map[int]*job
|
||||||
|
@ -97,46 +96,42 @@ func (j *jobHandler) getLatest() *job {
|
||||||
return j.jobs[j.latestID]
|
return j.jobs[j.latestID]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
|
/*
|
||||||
jobFuncs := map[string]util.LuaExport{
|
func (j *jobHandler) loader(L *lua.LState) *lua.LTable {
|
||||||
"all": {j.luaAllJobs, 0, false},
|
jobFuncs := map[string]lua.LGFunction{
|
||||||
"get": {j.luaGetJob, 1, false},
|
"all": j.luaAllJobs,
|
||||||
|
"get": j.luaGetJob,
|
||||||
}
|
}
|
||||||
|
|
||||||
luaJob := rt.NewTable()
|
luaJob := l.SetFuncs(l.NewTable(), jobFuncs)
|
||||||
util.SetExports(rtm, luaJob, jobFuncs)
|
|
||||||
|
|
||||||
return luaJob
|
return luaJob
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (j *jobHandler) luaGetJob(L *lua.LState) int {
|
||||||
j.mu.RLock()
|
j.mu.RLock()
|
||||||
defer j.mu.RUnlock()
|
defer j.mu.RUnlock()
|
||||||
|
|
||||||
if err := c.Check1Arg(); err != nil {
|
jobID := L.CheckInt(1)
|
||||||
return nil, err
|
job := j.jobs[jobID]
|
||||||
}
|
if job != nil {
|
||||||
jobID, err := c.IntArg(0)
|
return 0
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
L.Push(job.lua())
|
||||||
|
|
||||||
job := j.jobs[int(jobID)]
|
return 1
|
||||||
if job == nil {
|
|
||||||
return c.Next(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, job.lua()), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (j *jobHandler) luaAllJobs(L *lua.LState) int {
|
||||||
j.mu.RLock()
|
j.mu.RLock()
|
||||||
defer j.mu.RUnlock()
|
defer j.mu.RUnlock()
|
||||||
|
|
||||||
jobTbl := rt.NewTable()
|
jobTbl := L.NewTable()
|
||||||
for id, job := range j.jobs {
|
for id, job := range j.jobs {
|
||||||
jobTbl.Set(rt.IntValue(int64(id)), job.lua())
|
jobTbl.Insert(id, job.lua())
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, rt.TableValue(jobTbl)), nil
|
L.Push(jobTbl)
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
36
rl.go
36
rl.go
|
@ -13,8 +13,10 @@ type lineReader struct {
|
||||||
rl *readline.Instance
|
rl *readline.Instance
|
||||||
}
|
}
|
||||||
var fileHist *fileHistory
|
var fileHist *fileHistory
|
||||||
var hinter *rt.Closure
|
/*
|
||||||
var highlighter *rt.Closure
|
var hinter lua.LValue = lua.LNil
|
||||||
|
var highlighter lua.LValue = lua.LNil
|
||||||
|
*/
|
||||||
|
|
||||||
func newLineReader(prompt string, noHist bool) *lineReader {
|
func newLineReader(prompt string, noHist bool) *lineReader {
|
||||||
rl := readline.NewInstance()
|
rl := readline.NewInstance()
|
||||||
|
@ -45,43 +47,53 @@ func newLineReader(prompt string, noHist bool) *lineReader {
|
||||||
}
|
}
|
||||||
hooks.Em.Emit("hilbish.vimAction", actionStr, args)
|
hooks.Em.Emit("hilbish.vimAction", actionStr, args)
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
rl.HintText = func(line []rune, pos int) []rune {
|
rl.HintText = func(line []rune, pos int) []rune {
|
||||||
if hinter == nil {
|
if hinter == lua.LNil {
|
||||||
return []rune{}
|
return []rune{}
|
||||||
}
|
}
|
||||||
|
|
||||||
retVal, err := rt.Call1(l.MainThread(), rt.FunctionValue(highlighter),
|
err := l.CallByParam(lua.P{
|
||||||
rt.StringValue(string(line)), rt.IntValue(int64(pos)))
|
Fn: hinter,
|
||||||
|
NRet: 1,
|
||||||
|
Protect: true,
|
||||||
|
}, lua.LString(string(line)), lua.LNumber(pos))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return []rune{}
|
return []rune{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
retVal := l.Get(-1)
|
||||||
hintText := ""
|
hintText := ""
|
||||||
if luaStr, ok := retVal.TryString(); ok {
|
if luaStr, ok := retVal.(lua.LString); retVal != lua.LNil && ok {
|
||||||
hintText = luaStr
|
hintText = luaStr.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
return []rune(hintText)
|
return []rune(hintText)
|
||||||
}
|
}
|
||||||
rl.SyntaxHighlighter = func(line []rune) string {
|
rl.SyntaxHighlighter = func(line []rune) string {
|
||||||
if highlighter == nil {
|
if highlighter == lua.LNil {
|
||||||
return string(line)
|
return string(line)
|
||||||
}
|
}
|
||||||
retVal, err := rt.Call1(l.MainThread(), rt.FunctionValue(highlighter),
|
err := l.CallByParam(lua.P{
|
||||||
rt.StringValue(string(line)))
|
Fn: highlighter,
|
||||||
|
NRet: 1,
|
||||||
|
Protect: true,
|
||||||
|
}, lua.LString(string(line)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return string(line)
|
return string(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
retVal := l.Get(-1)
|
||||||
highlighted := ""
|
highlighted := ""
|
||||||
if luaStr, ok := retVal.TryString(); ok {
|
if luaStr, ok := retVal.(lua.LString); retVal != lua.LNil && ok {
|
||||||
highlighted = luaStr
|
highlighted = luaStr.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
return highlighted
|
return highlighted
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
|
rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
|
||||||
ctx := string(line)
|
ctx := string(line)
|
||||||
var completions []string
|
var completions []string
|
||||||
|
|
|
@ -1,56 +1,47 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
/*
|
||||||
import (
|
import (
|
||||||
"hilbish/util"
|
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func runnerModeLoader(rtm *rt.Runtime) *rt.Table {
|
func runnerModeLoader(L *lua.LState) *lua.LTable {
|
||||||
exports := map[string]util.LuaExport{
|
exports := map[string]lua.LGFunction{
|
||||||
"sh": {shRunner, 1, false},
|
"sh": shRunner,
|
||||||
"lua": {luaRunner, 1, false},
|
"lua": luaRunner,
|
||||||
"setMode": {hlrunnerMode, 1, false},
|
"setMode": hlrunnerMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
mod := rt.NewTable()
|
mod := L.SetFuncs(L.NewTable(), exports)
|
||||||
util.SetExports(rtm, mod, exports)
|
L.SetField(mod, "mode", runnerMode)
|
||||||
|
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
|
|
||||||
func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func shRunner(L *lua.LState) int {
|
||||||
if err := c.Check1Arg(); err != nil {
|
cmd := L.CheckString(1)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cmd, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
exitCode, err := handleSh(cmd)
|
exitCode, err := handleSh(cmd)
|
||||||
var luaErr rt.Value = rt.NilValue
|
var luaErr lua.LValue = lua.LNil
|
||||||
if err != nil {
|
if err != nil {
|
||||||
luaErr = rt.StringValue(err.Error())
|
luaErr = lua.LString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
L.Push(lua.LNumber(exitCode))
|
||||||
|
L.Push(luaErr)
|
||||||
|
|
||||||
|
return 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func luaRunner(L *lua.LState) int {
|
||||||
if err := c.Check1Arg(); err != nil {
|
cmd := L.CheckString(1)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cmd, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
exitCode, err := handleLua(cmd)
|
exitCode, err := handleLua(cmd)
|
||||||
var luaErr rt.Value = rt.NilValue
|
var luaErr lua.LValue = lua.LNil
|
||||||
if err != nil {
|
if err != nil {
|
||||||
luaErr = rt.StringValue(err.Error())
|
luaErr = lua.LString(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
L.Push(lua.LNumber(exitCode))
|
||||||
|
L.Push(luaErr)
|
||||||
|
|
||||||
|
return 2
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -4,14 +4,12 @@ import (
|
||||||
rt "github.com/arnodel/golua/runtime"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LuaExport represents a Go function which can be exported to Lua.
|
|
||||||
type LuaExport struct {
|
type LuaExport struct {
|
||||||
Function rt.GoFunctionFunc
|
Function rt.GoFunctionFunc
|
||||||
ArgNum int
|
ArgNum int
|
||||||
Variadic bool
|
Variadic bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetExports puts the Lua function exports in the table.
|
|
||||||
func SetExports(rtm *rt.Runtime, tbl *rt.Table, exports map[string]LuaExport) {
|
func SetExports(rtm *rt.Runtime, tbl *rt.Table, exports map[string]LuaExport) {
|
||||||
for name, export := range exports {
|
for name, export := range exports {
|
||||||
rtm.SetEnvGoFunc(tbl, name, export.Function, export.ArgNum, export.Variadic)
|
rtm.SetEnvGoFunc(tbl, name, export.Function, export.ArgNum, export.Variadic)
|
||||||
|
|
|
@ -39,7 +39,6 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d
|
||||||
module.Set(rt.StringValue(field), value)
|
module.Set(rt.StringValue(field), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoString runs the code string in the Lua runtime.
|
|
||||||
func DoString(rtm *rt.Runtime, code string) error {
|
func DoString(rtm *rt.Runtime, code string) error {
|
||||||
chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv()))
|
chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv()))
|
||||||
if chunk != nil {
|
if chunk != nil {
|
||||||
|
@ -49,7 +48,6 @@ func DoString(rtm *rt.Runtime, code string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoFile runs the contents of the file in the Lua runtime.
|
|
||||||
func DoFile(rtm *rt.Runtime, filename string) error {
|
func DoFile(rtm *rt.Runtime, filename string) error {
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,8 +57,6 @@ func DoFile(rtm *rt.Runtime, filename string) error {
|
||||||
return DoString(rtm, string(data))
|
return DoString(rtm, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleStrCallback handles function parameters for Go functions which take
|
|
||||||
// a string and a closure.
|
|
||||||
func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
|
func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
|
||||||
if err := c.CheckNArgs(2); err != nil {
|
if err := c.CheckNArgs(2); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
|
|
Loading…
Reference in New Issue