feat(fs): add some functions and properties (closes #168)

website
TorchedSammy 2022-06-20 16:47:16 -04:00
parent 3e807f5235
commit 453ba9f8ad
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 68 additions and 0 deletions

View File

@ -48,6 +48,13 @@ includes git commit, branch, and (new!!) release name.
foreground and backgrounds a job respectively. foreground and backgrounds a job respectively.
- Friendlier functions to the `hilbish.runner` interface, which also allow - Friendlier functions to the `hilbish.runner` interface, which also allow
having and using multiple runners. having and using multiple runners.
- A few new functions to the `fs` module:
- `fs.basename(path)` gets the basename of path
- `fs.dir(path)` gets the directory part of path
- `fs.glob(pattern)` globs files and directories based on patterns
- .. and 2 properties
- `fs.pathSep` is the separator for filesystem paths and directories
- `fs.pathListSep` is the separator for $PATH env entries
### Changed ### Changed
- **Breaking Change:** Upgraded to Lua 5.4. - **Breaking Change:** Upgraded to Lua 5.4.

View File

@ -24,9 +24,14 @@ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
"stat": util.LuaExport{fstat, 1, false}, "stat": util.LuaExport{fstat, 1, false},
"readdir": util.LuaExport{freaddir, 1, false}, "readdir": util.LuaExport{freaddir, 1, false},
"abs": util.LuaExport{fabs, 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},
} }
mod := rt.NewTable() mod := rt.NewTable()
util.SetExports(rtm, mod, exports) util.SetExports(rtm, mod, exports)
mod.Set(rt.StringValue("pathSep"), rt.StringValue(string(os.PathSeparator)))
mod.Set(rt.StringValue("pathListSep"), rt.StringValue(string(os.PathListSeparator)))
util.Document(mod, `The fs module provides easy and simple access to util.Document(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
@ -155,3 +160,59 @@ func fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.PushingNext1(t.Runtime, rt.StringValue(abspath)), nil return c.PushingNext1(t.Runtime, rt.StringValue(abspath)), nil
} }
// basename(path)
// Gives the basename of `path`. For the rules,
// see Go's filepath.Base
func fbasename(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
path, err := c.StringArg(0)
if err != nil {
return nil, err
}
return c.PushingNext(t.Runtime, rt.StringValue(filepath.Base(path))), nil
}
// dir(path)
// Returns the directory part of `path`. For the rules, see Go's
// filepath.Dir
func fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
path, err := c.StringArg(0)
if err != nil {
return nil, err
}
return c.PushingNext(t.Runtime, rt.StringValue(filepath.Dir(path))), nil
}
// glob(pattern)
// Glob all files and directories that match the pattern.
// For the rules, see Go's filepath.Glob
func fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
pattern, err := c.StringArg(0)
if err != nil {
return nil, err
}
matches, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
luaMatches := rt.NewTable()
for i, match := range matches {
luaMatches.Set(rt.IntValue(int64(i + 1)), rt.StringValue(match))
}
return c.PushingNext(t.Runtime, rt.TableValue(luaMatches)), nil
}