Compare commits

..

No commits in common. "0a5a2e727e713a726644d369c72feee79000f786" and "5a3b28142c832838bb26e5ff43595da600b28e62" have entirely different histories.

11 changed files with 157 additions and 167 deletions

View File

@ -73,7 +73,6 @@ func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
"list": util.LuaExport{a.luaList, 0, false},
"del": util.LuaExport{a.luaDelete, 1, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, hshaliasesLua)

101
api.go
View File

@ -31,14 +31,10 @@ var exports = map[string]util.LuaExport{
"cwd": util.LuaExport{hlcwd, 0, false},
/*
"exec": hlexec,
*/
"runnerMode": util.LuaExport{hlrunnerMode, 1, false},
/*
"runnerMode": hlrunnerMode,
"goro": hlgoro,
*/
"highlighter": util.LuaExport{hlhighlighter, 1, false},
"hinter": util.LuaExport{hlhinter, 1, false},
/*
"highlighter": hlhighlighter,
"hinter": hlhinter,
"multiprompt": hlmlprompt,
"prependPath": hlprependPath,
*/
@ -65,7 +61,7 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) {
util.SetExports(rtm, mod, exports)
hshMod = mod
host, _ := os.Hostname()
// host, _ := os.Hostname()
username := curuser.Username
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!
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(rtm, mod, "host", rt.StringValue(host), "Host name of the machine")
/*
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
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, "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, "vimMode", rt.NilValue, "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.Document(rtm, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
/*util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
util.SetField(l, hshMod, "exitCode", lua.LNumber(0), "Exit code of last exected command")
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
*/
// hilbish.userDir table
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.")
L.SetField(mod, "completion", hshcomp)
*/
// hilbish.runner table
runnerModule := runnerModeLoader(rtm)
//util.Document(L, runnerModule, "Runner/exec interface for Hilbish.")
mod.Set(rt.StringValue("runner"), rt.TableValue(runnerModule))
runnerModule := runnerModeLoader(L)
util.Document(L, runnerModule, "Runner/exec interface for Hilbish.")
L.SetField(mod, "runner", runnerModule)
*/
// hilbish.jobs table
jobs = newJobHandler()
jobModule := jobs.loader(rtm)
// util.Document(L, jobModule, "(Background) job interface.")
mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule))
/*
jobModule := jobs.loader(L)
util.Document(L, jobModule, "(Background) job interface.")
L.SetField(mod, "jobs", jobModule)
*/
return rt.TableValue(mod), nil
}
@ -542,6 +545,7 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
/*
// runnerMode(mode)
// 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.
@ -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
// will call it to execute user input instead.
// --- @param mode string|function
func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
mode := c.Arg(0)
func hlrunnerMode(L *lua.LState) int {
mode := L.CheckAny(1)
switch mode.Type() {
case rt.StringType:
switch mode.AsString() {
case lua.LTString:
switch mode.String() {
// no fallthrough doesnt work so eh
case "hybrid", "hybridRev", "lua", "sh": runnerMode = mode
default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
case "hybrid": fallthrough
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
default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
case lua.LTFunction: runnerMode = mode
default: L.RaiseError("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received %v", mode)
}
return c.Next(), nil
return 0
}
// 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
// which will be used for the hint.
// --- @param cb function
func hlhinter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
hinterCb, err := c.ClosureArg(0)
if err != nil {
return nil, err
}
func hlhinter(L *lua.LState) int {
hinterCb := L.CheckFunction(1)
hinter = hinterCb
return c.Next(), err
return 0
}
// 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
// be used to display in the line.
// --- @param cb function
func hlhighlighter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
highlighterCb, err := c.ClosureArg(0)
if err != nil {
return nil, err
}
func hlhighlighter(L *lua.LState) int {
highlighterCb := L.CheckFunction(1)
highlighter = highlighterCb
return c.Next(), err
return 0
}
*/

28
exec.go
View File

@ -24,15 +24,15 @@ import (
)
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) {
running = true
cmdString := aliases.Resolve(input)
hooks.Em.Emit("command.preexec", input, cmdString)
if runnerMode.Type() == rt.StringType {
switch runnerMode.AsString() {
// if runnerMode.Type() == lua.LTString {
switch /*runnerMode.String()*/ "hybrid" {
case "hybrid":
_, err := handleLua(cmdString)
if err == nil {
@ -68,29 +68,35 @@ func runInput(input string, priv bool) {
}
cmdFinish(exitCode, cmdString, priv)
}
} else {
// } else {
// 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 {
fmt.Fprintln(os.Stderr, err)
cmdFinish(124, cmdString, priv)
return
}
luaexitcode := term.Get(0) // first return value (makes sense right i love stacks)
runErr := term.Get(1)
luaexitcode := l.Get(-2) // first return value (makes sense right i love stacks)
runErr := l.Get(-1)
l.Pop(2)
var exitCode uint8
if code, ok := luaexitcode.TryInt(); ok {
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
exitCode = uint8(code)
}
if runErr != rt.NilValue {
if runErr != lua.LNil {
fmt.Fprintln(os.Stderr, runErr)
}
cmdFinish(exitCode, cmdString, priv)
}
*/
// }
}
func handleLua(cmdString string) (uint8, error) {

View File

@ -24,14 +24,14 @@ func New() Bait {
Em: emitter,
}
b.Loader = packagelib.Loader{
Load: b.loaderFunc,
Load: b.LoaderFunc,
Name: "bait",
}
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{
"catch": util.LuaExport{b.bcatch, 2, false},
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},

View File

@ -18,14 +18,14 @@ func New() Commander {
Events: emission.NewEmitter(),
}
c.Loader = packagelib.Loader{
Load: c.loaderFunc,
Load: c.LoaderFunc,
Name: "commander",
}
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{
"register": util.LuaExport{c.cregister, 2, false},
"deregister": util.LuaExport{c.cderegister, 1, false},

View File

@ -12,11 +12,11 @@ import (
)
var Loader = packagelib.Loader{
Load: loaderFunc,
Load: LoaderFunc,
Name: "fs",
}
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
func LoaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
exports := map[string]util.LuaExport{
"cd": util.LuaExport{fcd, 1, false},
"mkdir": util.LuaExport{fmkdir, 2, false},

81
job.go
View File

@ -3,10 +3,6 @@ package main
import (
"sync"
"os"
"hilbish/util"
rt "github.com/arnodel/golua/runtime"
)
var jobs *jobHandler
@ -23,7 +19,7 @@ type job struct {
func (j *job) start(pid int) {
j.pid = pid
j.running = true
hooks.Em.Emit("job.start", j.lua())
// hooks.Em.Emit("job.start", j.lua())
}
func (j *job) stop() {
@ -33,36 +29,39 @@ func (j *job) stop() {
func (j *job) finish() {
j.running = false
hooks.Em.Emit("job.done", j.lua())
// hooks.Em.Emit("job.done", j.lua())
}
func (j *job) setHandle(handle *os.Process) {
j.proc = handle
}
func (j *job) lua() rt.Value {
jobFuncs := map[string]util.LuaExport{
"stop": {j.luaStop, 0, false},
/*
func (j *job) lua() *lua.LTable {
// returns lua table for job
// because userdata is gross
jobFuncs := map[string]lua.LGFunction{
"stop": j.luaStop,
}
luaJob := rt.NewTable()
util.SetExports(l, luaJob, jobFuncs)
luaJob := l.SetFuncs(l.NewTable(), jobFuncs)
luaJob.Set(rt.StringValue("cmd"), rt.StringValue(j.cmd))
luaJob.Set(rt.StringValue("running"), rt.BoolValue(j.running))
luaJob.Set(rt.StringValue("id"), rt.IntValue(int64(j.id)))
luaJob.Set(rt.StringValue("pid"), rt.IntValue(int64(j.pid)))
luaJob.Set(rt.StringValue("exitCode"), rt.IntValue(int64(j.exitCode)))
l.SetField(luaJob, "cmd", lua.LString(j.cmd))
l.SetField(luaJob, "running", lua.LBool(j.running))
l.SetField(luaJob, "id", lua.LNumber(j.id))
l.SetField(luaJob, "pid", lua.LNumber(j.pid))
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 {
j.stop()
}
return c.Next(), nil
return 0
}
*/
type jobHandler struct {
jobs map[int]*job
@ -97,46 +96,42 @@ func (j *jobHandler) getLatest() *job {
return j.jobs[j.latestID]
}
func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
jobFuncs := map[string]util.LuaExport{
"all": {j.luaAllJobs, 0, false},
"get": {j.luaGetJob, 1, false},
/*
func (j *jobHandler) loader(L *lua.LState) *lua.LTable {
jobFuncs := map[string]lua.LGFunction{
"all": j.luaAllJobs,
"get": j.luaGetJob,
}
luaJob := rt.NewTable()
util.SetExports(rtm, luaJob, jobFuncs)
luaJob := l.SetFuncs(l.NewTable(), jobFuncs)
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()
defer j.mu.RUnlock()
if err := c.Check1Arg(); err != nil {
return nil, err
jobID := L.CheckInt(1)
job := j.jobs[jobID]
if job != nil {
return 0
}
jobID, err := c.IntArg(0)
if err != nil {
return nil, err
L.Push(job.lua())
return 1
}
job := j.jobs[int(jobID)]
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()
defer j.mu.RUnlock()
jobTbl := rt.NewTable()
jobTbl := L.NewTable()
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
View File

@ -13,8 +13,10 @@ type lineReader struct {
rl *readline.Instance
}
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 {
rl := readline.NewInstance()
@ -45,43 +47,53 @@ func newLineReader(prompt string, noHist bool) *lineReader {
}
hooks.Em.Emit("hilbish.vimAction", actionStr, args)
}
/*
rl.HintText = func(line []rune, pos int) []rune {
if hinter == nil {
if hinter == lua.LNil {
return []rune{}
}
retVal, err := rt.Call1(l.MainThread(), rt.FunctionValue(highlighter),
rt.StringValue(string(line)), rt.IntValue(int64(pos)))
err := l.CallByParam(lua.P{
Fn: hinter,
NRet: 1,
Protect: true,
}, lua.LString(string(line)), lua.LNumber(pos))
if err != nil {
fmt.Println(err)
return []rune{}
}
retVal := l.Get(-1)
hintText := ""
if luaStr, ok := retVal.TryString(); ok {
hintText = luaStr
if luaStr, ok := retVal.(lua.LString); retVal != lua.LNil && ok {
hintText = luaStr.String()
}
return []rune(hintText)
}
rl.SyntaxHighlighter = func(line []rune) string {
if highlighter == nil {
if highlighter == lua.LNil {
return string(line)
}
retVal, err := rt.Call1(l.MainThread(), rt.FunctionValue(highlighter),
rt.StringValue(string(line)))
err := l.CallByParam(lua.P{
Fn: highlighter,
NRet: 1,
Protect: true,
}, lua.LString(string(line)))
if err != nil {
fmt.Println(err)
return string(line)
}
retVal := l.Get(-1)
highlighted := ""
if luaStr, ok := retVal.TryString(); ok {
highlighted = luaStr
if luaStr, ok := retVal.(lua.LString); retVal != lua.LNil && ok {
highlighted = luaStr.String()
}
return highlighted
}
*/
rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
ctx := string(line)
var completions []string

View File

@ -1,56 +1,47 @@
package main
/*
import (
"hilbish/util"
rt "github.com/arnodel/golua/runtime"
)
func runnerModeLoader(rtm *rt.Runtime) *rt.Table {
exports := map[string]util.LuaExport{
"sh": {shRunner, 1, false},
"lua": {luaRunner, 1, false},
"setMode": {hlrunnerMode, 1, false},
func runnerModeLoader(L *lua.LState) *lua.LTable {
exports := map[string]lua.LGFunction{
"sh": shRunner,
"lua": luaRunner,
"setMode": hlrunnerMode,
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
mod := L.SetFuncs(L.NewTable(), exports)
L.SetField(mod, "mode", runnerMode)
return mod
}
func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
}
func shRunner(L *lua.LState) int {
cmd := L.CheckString(1)
exitCode, err := handleSh(cmd)
var luaErr rt.Value = rt.NilValue
var luaErr lua.LValue = lua.LNil
if err != nil {
luaErr = rt.StringValue(err.Error())
luaErr = lua.LString(err.Error())
}
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
}
func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
L.Push(lua.LNumber(exitCode))
L.Push(luaErr)
return 2
}
func luaRunner(L *lua.LState) int {
cmd := L.CheckString(1)
exitCode, err := handleLua(cmd)
var luaErr rt.Value = rt.NilValue
var luaErr lua.LValue = lua.LNil
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
}
*/

View File

@ -4,14 +4,12 @@ import (
rt "github.com/arnodel/golua/runtime"
)
// LuaExport represents a Go function which can be exported to Lua.
type LuaExport struct {
Function rt.GoFunctionFunc
ArgNum int
Variadic bool
}
// SetExports puts the Lua function exports in the table.
func SetExports(rtm *rt.Runtime, tbl *rt.Table, exports map[string]LuaExport) {
for name, export := range exports {
rtm.SetEnvGoFunc(tbl, name, export.Function, export.ArgNum, export.Variadic)

View File

@ -39,7 +39,6 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d
module.Set(rt.StringValue(field), value)
}
// DoString runs the code string in the Lua runtime.
func DoString(rtm *rt.Runtime, code string) error {
chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv()))
if chunk != nil {
@ -49,7 +48,6 @@ func DoString(rtm *rt.Runtime, code string) error {
return err
}
// DoFile runs the contents of the file in the Lua runtime.
func DoFile(rtm *rt.Runtime, filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
@ -59,8 +57,6 @@ func DoFile(rtm *rt.Runtime, filename string) error {
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) {
if err := c.CheckNArgs(2); err != nil {
return "", nil, err