mirror of https://github.com/Hilbis/Hilbish
Compare commits
9 Commits
26add67287
...
dce02e5310
Author | SHA1 | Date |
---|---|---|
sammyette | dce02e5310 | |
TorchedSammy | c0b08a340a | |
TorchedSammy | 506f90de06 | |
TorchedSammy | 7909aeb4b3 | |
TorchedSammy | a7722fa331 | |
TorchedSammy | b887ad4fa9 | |
TorchedSammy | e466085d24 | |
TorchedSammy | d2d423a4ef | |
TorchedSammy | eff942433d |
47
api.go
47
api.go
|
@ -22,24 +22,32 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var exports = map[string]util.LuaExport{
|
var exports = map[string]util.LuaExport{
|
||||||
/*"alias": hlalias,
|
/*
|
||||||
|
"alias": hlalias,
|
||||||
"appendPath": hlappendPath,
|
"appendPath": hlappendPath,
|
||||||
"complete": hlcomplete,
|
"complete": hlcomplete,
|
||||||
"cwd": hlcwd,
|
*/
|
||||||
|
"cwd": util.LuaExport{hlcwd, 0, false},
|
||||||
|
/*
|
||||||
"exec": hlexec,
|
"exec": hlexec,
|
||||||
"runnerMode": hlrunnerMode,
|
"runnerMode": hlrunnerMode,
|
||||||
"goro": hlgoro,
|
"goro": hlgoro,
|
||||||
"highlighter": hlhighlighter,
|
"highlighter": hlhighlighter,
|
||||||
"hinter": hlhinter,
|
"hinter": hlhinter,
|
||||||
"multiprompt": hlmlprompt,
|
"multiprompt": hlmlprompt,
|
||||||
"prependPath": hlprependPath,*/
|
"prependPath": hlprependPath,
|
||||||
|
*/
|
||||||
"prompt": util.LuaExport{hlprompt, 1, false},
|
"prompt": util.LuaExport{hlprompt, 1, false},
|
||||||
/*"inputMode": hlinputMode,
|
/*
|
||||||
|
"inputMode": hlinputMode,
|
||||||
"interval": hlinterval,
|
"interval": hlinterval,
|
||||||
"read": hlread,
|
*/
|
||||||
|
"read": util.LuaExport{hlprompt, 1, false},
|
||||||
|
/*
|
||||||
"run": hlrun,
|
"run": hlrun,
|
||||||
"timeout": hltimeout,
|
"timeout": hltimeout,
|
||||||
"which": hlwhich,*/
|
"which": hlwhich,
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
var greeting string
|
var greeting string
|
||||||
|
@ -69,8 +77,8 @@ Check out the {blue}{bold}guide{reset} command to get started.
|
||||||
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
|
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, "user", lua.LString(username), "Username of user")
|
||||||
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
|
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
|
||||||
util.SetField(L, mod, "home", lua.LString(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(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
|
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
|
||||||
|
@ -218,37 +226,40 @@ func hlrun(L *lua.LState) int {
|
||||||
L.Push(lua.LNumber(exitcode))
|
L.Push(lua.LNumber(exitcode))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// cwd()
|
// cwd()
|
||||||
// Returns the current directory of the shell
|
// Returns the current directory of the shell
|
||||||
func hlcwd(L *lua.LState) int {
|
func hlcwd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
cwd, _ := os.Getwd()
|
cwd, _ := os.Getwd()
|
||||||
|
|
||||||
L.Push(lua.LString(cwd))
|
return c.PushingNext1(t.Runtime, rt.StringValue(cwd)), nil
|
||||||
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read(prompt) -> input?
|
// read(prompt) -> input?
|
||||||
// Read input from the user, using Hilbish's line editor/input reader.
|
// Read input from the user, using Hilbish's line editor/input reader.
|
||||||
// This is a separate instance from the one Hilbish actually uses.
|
// This is a separate instance from the one Hilbish actually uses.
|
||||||
// Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
|
// Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
|
||||||
// --- @param prompt string
|
// --- @param prompt string
|
||||||
func hlread(L *lua.LState) int {
|
func hlread(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
luaprompt := L.CheckString(1)
|
if err := c.Check1Arg(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
luaprompt, err := c.StringArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
lualr := newLineReader("", true)
|
lualr := newLineReader("", true)
|
||||||
lualr.SetPrompt(luaprompt)
|
lualr.SetPrompt(luaprompt)
|
||||||
|
|
||||||
input, err := lualr.Read()
|
input, err := lualr.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
L.Push(lua.LNil)
|
return c.Next(), nil
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
L.Push(lua.LString(input))
|
return c.PushingNext1(t.Runtime, rt.StringValue(input)), nil
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
prompt(str)
|
prompt(str)
|
||||||
|
|
26
exec.go
26
exec.go
|
@ -183,37 +183,27 @@ func execCommand(cmd string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If command is defined in Lua then run it
|
// If command is defined in Lua then run it
|
||||||
/*
|
luacmdArgs := rt.NewTable()
|
||||||
luacmdArgs := l.NewTable()
|
for i, str := range args[1:] {
|
||||||
for _, str := range args[1:] {
|
luacmdArgs.Set(rt.IntValue(int64(i + 1)), rt.StringValue(str))
|
||||||
luacmdArgs.Append(lua.LString(str))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if commands[args[0]] != nil {
|
if commands[args[0]] != nil {
|
||||||
err := l.CallByParam(lua.P{
|
luaexitcode, err := rt.Call1(l.MainThread(), rt.FunctionValue(commands[args[0]]), rt.TableValue(luacmdArgs))
|
||||||
Fn: commands[args[0]],
|
|
||||||
NRet: 1,
|
|
||||||
Protect: true,
|
|
||||||
}, luacmdArgs)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr,
|
fmt.Fprintln(os.Stderr,
|
||||||
"Error in command:\n\n" + err.Error())
|
"Error in command:\n\n" + err.Error())
|
||||||
return interp.NewExitStatus(1)
|
return interp.NewExitStatus(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
luaexitcode := l.Get(-1)
|
|
||||||
var exitcode uint8
|
var exitcode uint8
|
||||||
|
|
||||||
l.Pop(1)
|
if code, ok := luaexitcode.TryInt(); ok {
|
||||||
|
|
||||||
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
|
|
||||||
exitcode = uint8(code)
|
exitcode = uint8(code)
|
||||||
}
|
} // TODO: deregister commander if return isnt number
|
||||||
|
|
||||||
return interp.NewExitStatus(exitcode)
|
return interp.NewExitStatus(exitcode)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
err := lookpath(args[0])
|
err := lookpath(args[0])
|
||||||
if err == errNotExec {
|
if err == errNotExec {
|
||||||
|
@ -447,6 +437,8 @@ func cmdFinish(code uint8, cmdstr string, private bool) {
|
||||||
handleHistory(cmdstr)
|
handleHistory(cmdstr)
|
||||||
}
|
}
|
||||||
// util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
|
// util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
|
||||||
// using AsValue on an interface which is an int results in it being unknown .... ????
|
// using AsValue (to convert to lua type) on an interface which is an int
|
||||||
|
// results in it being unknown in lua .... ????
|
||||||
|
// so we allow the hook handler to take lua runtime Values
|
||||||
hooks.Em.Emit("command.exit", rt.IntValue(int64(code)), cmdstr)
|
hooks.Em.Emit("command.exit", rt.IntValue(int64(code)), cmdstr)
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -20,3 +20,5 @@ replace mvdan.cc/sh/v3 => github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.2022030614040
|
||||||
replace github.com/maxlandon/readline => ./readline
|
replace github.com/maxlandon/readline => ./readline
|
||||||
|
|
||||||
replace layeh.com/gopher-luar => github.com/layeh/gopher-luar v1.0.10
|
replace layeh.com/gopher-luar => github.com/layeh/gopher-luar v1.0.10
|
||||||
|
|
||||||
|
replace github.com/arnodel/golua => github.com/Rosettea/golua v0.0.0-20220329005655-c29b74de7d20
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
||||||
|
github.com/Rosettea/golua v0.0.0-20220329005655-c29b74de7d20 h1:i4XwNhWKlHP9+oHfpNandGsjj9XQG+uah+SdAjLknbc=
|
||||||
|
github.com/Rosettea/golua v0.0.0-20220329005655-c29b74de7d20/go.mod h1:9jzpYPiU2is0HVGCiuIOBSXdergHUW44IEjmuN1UrIE=
|
||||||
github.com/Rosettea/readline-1 v0.0.0-20220302012429-9ce5d23760f7 h1:LoY+kBKqMQqBcilRpVvifBTVve84asa3btpx3D/+IvM=
|
github.com/Rosettea/readline-1 v0.0.0-20220302012429-9ce5d23760f7 h1:LoY+kBKqMQqBcilRpVvifBTVve84asa3btpx3D/+IvM=
|
||||||
github.com/Rosettea/readline-1 v0.0.0-20220302012429-9ce5d23760f7/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
github.com/Rosettea/readline-1 v0.0.0-20220302012429-9ce5d23760f7/go.mod h1:QiUAvbhg8PzCA4hlafCUl0bKD/0VmcocM4AjqtszAJs=
|
||||||
github.com/Rosettea/readline-1 v0.0.0-20220305004552-071c22768119 h1:rGsc30WTD5hk+oiXrAKsAIwZn5qBeTAdr29y3HhJh9E=
|
github.com/Rosettea/readline-1 v0.0.0-20220305004552-071c22768119 h1:rGsc30WTD5hk+oiXrAKsAIwZn5qBeTAdr29y3HhJh9E=
|
||||||
|
|
|
@ -4,9 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
"github.com/chuckpreslar/emission"
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
"github.com/arnodel/golua/lib/packagelib"
|
"github.com/arnodel/golua/lib/packagelib"
|
||||||
|
"github.com/chuckpreslar/emission"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bait struct{
|
type Bait struct{
|
||||||
|
@ -54,22 +54,6 @@ failed, etc. To find all available hooks, see doc hooks.`)
|
||||||
return rt.TableValue(mod), nil
|
return rt.TableValue(mod), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleArgs(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
|
|
||||||
if err := c.CheckNArgs(2); err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
name, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
catcher, err := c.ClosureArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return name, catcher, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) {
|
func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) {
|
||||||
funcVal := rt.FunctionValue(catcher)
|
funcVal := rt.FunctionValue(catcher)
|
||||||
var luaArgs []rt.Value
|
var luaArgs []rt.Value
|
||||||
|
@ -117,7 +101,7 @@ func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
// --- @param name string
|
// --- @param name string
|
||||||
// --- @param cb function
|
// --- @param cb function
|
||||||
func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
name, catcher, err := handleArgs(t, c)
|
name, catcher, err := util.HandleStrCallback(t, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -134,7 +118,7 @@ func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
// --- @param name string
|
// --- @param name string
|
||||||
// --- @param cb function
|
// --- @param cb function
|
||||||
func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (b *Bait) bcatchOnce(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
name, catcher, err := handleArgs(t, c)
|
name, catcher, err := util.HandleStrCallback(t, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,52 +3,68 @@ package commander
|
||||||
import (
|
import (
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
|
rt "github.com/arnodel/golua/runtime"
|
||||||
|
"github.com/arnodel/golua/lib/packagelib"
|
||||||
"github.com/chuckpreslar/emission"
|
"github.com/chuckpreslar/emission"
|
||||||
"github.com/yuin/gopher-lua"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Commander struct{
|
type Commander struct{
|
||||||
Events *emission.Emitter
|
Events *emission.Emitter
|
||||||
|
Loader packagelib.Loader
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() Commander {
|
func New() Commander {
|
||||||
return Commander{
|
c := Commander{
|
||||||
Events: emission.NewEmitter(),
|
Events: emission.NewEmitter(),
|
||||||
}
|
}
|
||||||
|
c.Loader = packagelib.Loader{
|
||||||
|
Load: c.LoaderFunc,
|
||||||
|
Name: "commander",
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) Loader(L *lua.LState) int {
|
func (c *Commander) LoaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
||||||
exports := map[string]lua.LGFunction{
|
exports := map[string]util.LuaExport{
|
||||||
"register": c.cregister,
|
"register": util.LuaExport{c.cregister, 2, false},
|
||||||
"deregister": c.cderegister,
|
"deregister": util.LuaExport{c.cderegister, 1, false},
|
||||||
}
|
}
|
||||||
mod := L.SetFuncs(L.NewTable(), exports)
|
mod := rt.NewTable()
|
||||||
util.Document(L, mod, "Commander is Hilbish's custom command library, a way to write commands in Lua.")
|
util.SetExports(rtm, mod, exports)
|
||||||
L.Push(mod)
|
// util.Document(L, mod, "Commander is Hilbish's custom command library, a way to write commands in Lua.")
|
||||||
|
|
||||||
return 1
|
return rt.TableValue(mod), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// register(name, cb)
|
// register(name, cb)
|
||||||
// Register a command with `name` that runs `cb` when ran
|
// Register a command with `name` that runs `cb` when ran
|
||||||
// --- @param name string
|
// --- @param name string
|
||||||
// --- @param cb function
|
// --- @param cb function
|
||||||
func (c *Commander) cregister(L *lua.LState) int {
|
func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
||||||
cmdName := L.CheckString(1)
|
cmdName, cmd, err := util.HandleStrCallback(t, ct)
|
||||||
cmd := L.CheckFunction(2)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
c.Events.Emit("commandRegister", cmdName, cmd)
|
c.Events.Emit("commandRegister", cmdName, cmd)
|
||||||
|
|
||||||
return 0
|
return ct.Next(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// deregister(name)
|
// deregister(name)
|
||||||
// Deregisters any command registered with `name`
|
// Deregisters any command registered with `name`
|
||||||
// --- @param name string
|
// --- @param name string
|
||||||
func (c *Commander) cderegister(L *lua.LState) int {
|
func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
|
||||||
cmdName := L.CheckString(1)
|
if err := ct.Check1Arg(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
cmdName, err := ct.StringArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
c.Events.Emit("commandDeregister", cmdName)
|
c.Events.Emit("commandDeregister", cmdName)
|
||||||
|
|
||||||
return 0
|
return ct.Next(), err
|
||||||
}
|
}
|
||||||
|
|
121
golibs/fs/fs.go
121
golibs/fs/fs.go
|
@ -8,51 +8,71 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
"github.com/yuin/gopher-lua"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
|
"github.com/arnodel/golua/lib/packagelib"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Loader(L *lua.LState) int {
|
var Loader = packagelib.Loader{
|
||||||
mod := L.SetFuncs(L.NewTable(), exports)
|
Load: LoaderFunc,
|
||||||
|
Name: "fs",
|
||||||
|
}
|
||||||
|
|
||||||
|
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},
|
||||||
|
"stat": util.LuaExport{fstat, 1, false},
|
||||||
|
"readdir": util.LuaExport{freaddir, 1, false},
|
||||||
|
}
|
||||||
|
mod := rt.NewTable()
|
||||||
|
util.SetExports(rtm, mod, exports)
|
||||||
|
|
||||||
|
/*
|
||||||
util.Document(L, mod, `The fs module provides easy and simple access to
|
util.Document(L, mod, `The fs module provides easy and simple access to
|
||||||
filesystem functions and other things, and acts an
|
filesystem functions and other things, and acts an
|
||||||
addition to the Lua standard library's I/O and fs functions.`)
|
addition to the Lua standard library's I/O and fs functions.`)
|
||||||
|
*/
|
||||||
|
|
||||||
L.Push(mod)
|
return rt.TableValue(mod), nil
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
var exports = map[string]lua.LGFunction{
|
|
||||||
"cd": fcd,
|
|
||||||
"mkdir": fmkdir,
|
|
||||||
"stat": fstat,
|
|
||||||
"readdir": freaddir,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cd(dir)
|
// cd(dir)
|
||||||
// Changes directory to `dir`
|
// Changes directory to `dir`
|
||||||
// --- @param dir string
|
// --- @param dir string
|
||||||
func fcd(L *lua.LState) int {
|
func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
path := L.CheckString(1)
|
if err := c.Check1Arg(); err != nil {
|
||||||
|
return nil, err
|
||||||
err := os.Chdir(strings.TrimSpace(path))
|
}
|
||||||
|
path, err := c.StringArg(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := err.(*os.PathError).Err.Error()
|
return nil, err
|
||||||
L.RaiseError(e + ": " + path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
err = os.Chdir(strings.TrimSpace(path))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Next(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// mkdir(name, recursive)
|
// mkdir(name, recursive)
|
||||||
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
|
||||||
// --- @param name string
|
// --- @param name string
|
||||||
// --- @param recursive boolean
|
// --- @param recursive boolean
|
||||||
func fmkdir(L *lua.LState) int {
|
func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
dirname := L.CheckString(1)
|
if err := c.CheckNArgs(2); err != nil {
|
||||||
recursive := L.ToBool(2)
|
return nil, err
|
||||||
|
}
|
||||||
|
dirname, err := c.StringArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
recursive, err := c.BoolArg(1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
path := strings.TrimSpace(dirname)
|
path := strings.TrimSpace(dirname)
|
||||||
var err error
|
|
||||||
|
|
||||||
if recursive {
|
if recursive {
|
||||||
err = os.MkdirAll(path, 0744)
|
err = os.MkdirAll(path, 0744)
|
||||||
|
@ -60,51 +80,58 @@ func fmkdir(L *lua.LState) int {
|
||||||
err = os.Mkdir(path, 0744)
|
err = os.Mkdir(path, 0744)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
L.RaiseError(err.Error() + ": " + path)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return c.Next(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// stat(path)
|
// stat(path)
|
||||||
// Returns info about `path`
|
// Returns info about `path`
|
||||||
// --- @param path string
|
// --- @param path string
|
||||||
func fstat(L *lua.LState) int {
|
func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
path := L.CheckString(1)
|
if err := c.Check1Arg(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
path, err := c.StringArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
pathinfo, err := os.Stat(path)
|
pathinfo, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
L.RaiseError(err.Error() + ": " + path)
|
return nil, err
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
statTbl := L.NewTable()
|
statTbl := rt.NewTable()
|
||||||
L.SetField(statTbl, "name", lua.LString(pathinfo.Name()))
|
statTbl.Set(rt.StringValue("name"), rt.StringValue(pathinfo.Name()))
|
||||||
L.SetField(statTbl, "size", lua.LNumber(pathinfo.Size()))
|
statTbl.Set(rt.StringValue("size"), rt.IntValue(pathinfo.Size()))
|
||||||
L.SetField(statTbl, "mode", lua.LString("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
|
statTbl.Set(rt.StringValue("mode"), rt.StringValue("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
|
||||||
L.SetField(statTbl, "isDir", lua.LBool(pathinfo.IsDir()))
|
statTbl.Set(rt.StringValue("isDir"), rt.BoolValue(pathinfo.IsDir()))
|
||||||
L.Push(statTbl)
|
|
||||||
|
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// readdir(dir)
|
// readdir(dir)
|
||||||
// Returns a table of files in `dir`
|
// Returns a table of files in `dir`
|
||||||
// --- @param dir string
|
// --- @param dir string
|
||||||
// --- @return table
|
// --- @return table
|
||||||
func freaddir(L *lua.LState) int {
|
func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
dir := L.CheckString(1)
|
if err := c.Check1Arg(); err != nil {
|
||||||
names := L.NewTable()
|
return nil, err
|
||||||
|
}
|
||||||
|
dir, err := c.StringArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
names := rt.NewTable()
|
||||||
|
|
||||||
dirEntries, err := os.ReadDir(dir)
|
dirEntries, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
L.RaiseError(err.Error() + ": " + dir)
|
return nil, err
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
for _, entry := range dirEntries {
|
for i, entry := range dirEntries {
|
||||||
names.Append(lua.LString(entry.Name()))
|
names.Set(rt.IntValue(int64(i + 1)), rt.StringValue(entry.Name()))
|
||||||
}
|
}
|
||||||
|
|
||||||
L.Push(names)
|
return c.PushingNext1(t.Runtime, rt.TableValue(names)), nil
|
||||||
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
32
lua.go
32
lua.go
|
@ -6,9 +6,9 @@ import (
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
/*
|
|
||||||
"hilbish/golibs/commander"
|
"hilbish/golibs/commander"
|
||||||
"hilbish/golibs/fs"
|
"hilbish/golibs/fs"
|
||||||
|
/*
|
||||||
"hilbish/golibs/terminal"
|
"hilbish/golibs/terminal"
|
||||||
*/
|
*/
|
||||||
rt "github.com/arnodel/golua/runtime"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
|
@ -23,24 +23,23 @@ func luaInit() {
|
||||||
|
|
||||||
lib.LoadLibs(l, hilbishLoader)
|
lib.LoadLibs(l, hilbishLoader)
|
||||||
// yes this is stupid, i know
|
// yes this is stupid, i know
|
||||||
chunk, _ := l.CompileAndLoadLuaChunk("", []byte("hilbish = require 'hilbish'"), rt.TableValue(l.GlobalEnv()))
|
util.DoString(l, "hilbish = require 'hilbish'")
|
||||||
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
||||||
fmt.Println("hsh load", err)
|
|
||||||
|
|
||||||
// Add fs and terminal module module to Lua
|
// Add fs and terminal module module to Lua
|
||||||
/* l.PreloadModule("fs", fs.Loader)
|
lib.LoadLibs(l, fs.Loader)
|
||||||
|
/*
|
||||||
l.PreloadModule("terminal", terminal.Loader)
|
l.PreloadModule("terminal", terminal.Loader)
|
||||||
|
*/
|
||||||
|
|
||||||
cmds := commander.New()
|
cmds := commander.New()
|
||||||
// When a command from Lua is added, register it for use
|
// When a command from Lua is added, register it for use
|
||||||
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
|
cmds.Events.On("commandRegister", func(cmdName string, cmd *rt.Closure) {
|
||||||
commands[cmdName] = cmd
|
commands[cmdName] = cmd
|
||||||
})
|
})
|
||||||
cmds.Events.On("commandDeregister", func(cmdName string) {
|
cmds.Events.On("commandDeregister", func(cmdName string) {
|
||||||
delete(commands, cmdName)
|
delete(commands, cmdName)
|
||||||
})
|
})
|
||||||
l.PreloadModule("commander", cmds.Loader)
|
lib.LoadLibs(l, cmds.Loader)
|
||||||
*/
|
|
||||||
|
|
||||||
hooks = bait.New()
|
hooks = bait.New()
|
||||||
lib.LoadLibs(l, hooks.Loader)
|
lib.LoadLibs(l, hooks.Loader)
|
||||||
|
@ -53,20 +52,19 @@ func luaInit() {
|
||||||
})
|
})
|
||||||
|
|
||||||
// Add more paths that Lua can require from
|
// Add more paths that Lua can require from
|
||||||
chunk, _ = l.CompileAndLoadLuaChunk("", []byte("package.path = package.path .. " + requirePaths), rt.TableValue(l.GlobalEnv()))
|
err := util.DoString(l, "package.path = package.path .. " + requirePaths)
|
||||||
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
||||||
fmt.Println("package path", err)
|
|
||||||
|
|
||||||
data, err := os.ReadFile("prelude/init.lua")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
data, err = os.ReadFile(preloadPath)
|
fmt.Fprintln(os.Stderr, "Could not add preload paths! Libraries will be missing. This shouldn't happen.")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.DoFile(l, "prelude/init.lua")
|
||||||
|
if err != nil {
|
||||||
|
err = util.DoFile(l, preloadPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
|
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
chunk, _ = l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
|
fmt.Println(err)
|
||||||
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
||||||
fmt.Println("prelude", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runConfig(confpath string) {
|
func runConfig(confpath string) {
|
||||||
|
|
2
main.go
2
main.go
|
@ -23,7 +23,7 @@ var (
|
||||||
l *rt.Runtime
|
l *rt.Runtime
|
||||||
lr *lineReader
|
lr *lineReader
|
||||||
|
|
||||||
commands = map[string]*lua.LFunction{}
|
commands = map[string]*rt.Closure{}
|
||||||
luaCompletions = map[string]*lua.LFunction{}
|
luaCompletions = map[string]*lua.LFunction{}
|
||||||
|
|
||||||
confDir string
|
confDir string
|
||||||
|
|
|
@ -8,7 +8,7 @@ local _ = require 'succulent' -- Function additions
|
||||||
local oldDir = hilbish.cwd()
|
local oldDir = hilbish.cwd()
|
||||||
|
|
||||||
local shlvl = tonumber(os.getenv 'SHLVL')
|
local shlvl = tonumber(os.getenv 'SHLVL')
|
||||||
if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 0) end
|
if shlvl ~= nil then os.setenv('SHLVL', tostring(shlvl + 1)) else os.setenv('SHLVL', '0') end
|
||||||
|
|
||||||
-- Builtins
|
-- Builtins
|
||||||
local recentDirs = {}
|
local recentDirs = {}
|
||||||
|
|
16
util/util.go
16
util/util.go
|
@ -55,3 +55,19 @@ func DoFile(rtm *rt.Runtime, filename string) error {
|
||||||
|
|
||||||
return DoString(rtm, string(data))
|
return DoString(rtm, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) {
|
||||||
|
if err := c.CheckNArgs(2); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
name, err := c.StringArg(0)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
cb, err := c.ClosureArg(1)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return name, cb, err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue