diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index 1c0589e..9e03325 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -19,38 +19,25 @@ import ( rt "github.com/arnodel/golua/runtime" "github.com/arnodel/golua/lib/packagelib" "github.com/arnodel/golua/lib/iolib" - "mvdan.cc/sh/v3/interp" ) -type fs struct{ - runner *interp.Runner - Loader packagelib.Loader +var Loader = packagelib.Loader{ + Load: loaderFunc, + Name: "fs", } -func New(runner *interp.Runner) *fs { - f := &fs{ - runner: runner, - } - f.Loader = packagelib.Loader{ - Load: f.loaderFunc, - Name: "fs", - } - - return f -} - -func (f *fs) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { +func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { exports := map[string]util.LuaExport{ - "cd": util.LuaExport{f.fcd, 1, false}, - "mkdir": util.LuaExport{f.fmkdir, 2, false}, - "stat": util.LuaExport{f.fstat, 1, false}, - "readdir": util.LuaExport{f.freaddir, 1, false}, - "abs": util.LuaExport{f.fabs, 1, false}, - "basename": util.LuaExport{f.fbasename, 1, false}, - "dir": util.LuaExport{f.fdir, 1, false}, - "glob": util.LuaExport{f.fglob, 1, false}, - "join": util.LuaExport{f.fjoin, 0, true}, - "pipe": util.LuaExport{f.fpipe, 0, false}, + "cd": util.LuaExport{fcd, 1, false}, + "mkdir": util.LuaExport{fmkdir, 2, false}, + "stat": util.LuaExport{fstat, 1, false}, + "readdir": util.LuaExport{freaddir, 1, false}, + "abs": util.LuaExport{fabs, 1, false}, + "basename": util.LuaExport{fbasename, 1, false}, + "dir": util.LuaExport{fdir, 1, false}, + "glob": util.LuaExport{fglob, 1, false}, + "join": util.LuaExport{fjoin, 0, true}, + "pipe": util.LuaExport{fpipe, 0, false}, } mod := rt.NewTable() util.SetExports(rtm, mod, exports) @@ -65,7 +52,7 @@ func (f *fs) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { // This can be used to resolve short paths like `..` to `/home/user`. // #param path string // #returns string -func (f *fs) fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { path, err := c.StringArg(0) if err != nil { return nil, err @@ -85,7 +72,7 @@ func (f *fs) fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // `.` will be returned. // #param path string Path to get the base name of. // #returns string -func (f *fs) fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -100,7 +87,7 @@ func (f *fs) fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // cd(dir) // Changes Hilbish's directory to `dir`. // #param dir string Path to change directory to. -func (f *fs) fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -123,7 +110,7 @@ func (f *fs) fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // `~/Documents/doc.txt` then this function will return `~/Documents`. // #param path string Path to get the directory for. // #returns string -func (f *fs) fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -154,7 +141,7 @@ print(matches) -- -> {'init.lua', 'code.lua'} #example */ -func (f *fs) fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -188,7 +175,7 @@ print(fs.join(hilbish.userDir.config, 'hilbish')) -- -> '/home/user/.config/hilbish' on Linux #example */ -func (f *fs) fjoin(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fjoin(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { strs := make([]string, len(c.Etc())) for i, v := range c.Etc() { if v.Type() != rt.StringType { @@ -215,7 +202,7 @@ func (f *fs) fjoin(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { fs.mkdir('./foo/bar', true) #example */ -func (f *fs) fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.CheckNArgs(2); err != nil { return nil, err } @@ -246,7 +233,7 @@ func (f *fs) fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // The type returned is a Lua file, same as returned from `io` functions. // #returns File // #returns File -func (f *fs) fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { rf, wf, err := os.Pipe() if err != nil { return nil, err @@ -261,7 +248,7 @@ func (f *fs) fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // Returns a list of all files and directories in the provided path. // #param dir string // #returns table -func (f *fs) freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -309,7 +296,7 @@ Would print the following: ]]-- #example */ -func (f *fs) fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } diff --git a/lua.go b/lua.go index 88fedf8..94b7910 100644 --- a/lua.go +++ b/lua.go @@ -30,8 +30,7 @@ func luaInit() { util.DoString(l, "hilbish = require 'hilbish'") // Add fs and terminal module module to Lua - f := fs.New(runner) - lib.LoadLibs(l, f.Loader) + lib.LoadLibs(l, fs.Loader) lib.LoadLibs(l, terminal.Loader) cmds = commander.New(l)