From 2a9330c692e636f18f02a8d907ffdb04a2bd8c62 Mon Sep 17 00:00:00 2001 From: sammyette Date: Thu, 3 Apr 2025 00:12:53 -0400 Subject: [PATCH] fix: change directory of sh interp --- docs/api/snail.md | 9 +++++++-- emmyLuaDocs/snail.lua | 7 ++++++- golibs/snail/lua.go | 32 ++++++++++++++++++++++++++++++-- nature/dirs.lua | 1 + nature/hilbish.lua | 6 ++++-- 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/docs/api/snail.md b/docs/api/snail.md index e5dcb6d..f183306 100644 --- a/docs/api/snail.md +++ b/docs/api/snail.md @@ -40,6 +40,11 @@ This function has no parameters. A Snail is a shell script interpreter instance. ### Methods -#### run(command, streams) -Runs a shell command. Works the same as `hilbish.run`. +#### dir(path) +Changes the directory of the snail instance. +The interpreter keeps its set directory even when the Hilbish process changes +directory, so this should be called on the `hilbish.cd` hook. + +#### run(command, streams) +Runs a shell command. Works the same as `hilbish.run`, but only accepts a table of streams. diff --git a/emmyLuaDocs/snail.lua b/emmyLuaDocs/snail.lua index 331a1ff..94c84df 100644 --- a/emmyLuaDocs/snail.lua +++ b/emmyLuaDocs/snail.lua @@ -2,10 +2,15 @@ local snail = {} +--- Changes the directory of the snail instance. +--- The interpreter keeps its set directory even when the Hilbish process changes +--- directory, so this should be called on the `hilbish.cd` hook. +function snail:dir(path) end + --- Creates a new Snail instance. function snail.new() end ---- Runs a shell command. Works the same as `hilbish.run`. +--- Runs a shell command. Works the same as `hilbish.run`, but only accepts a table of streams. function snail:run(command, streams) end return snail diff --git a/golibs/snail/lua.go b/golibs/snail/lua.go index 2c8a20c..5850f37 100644 --- a/golibs/snail/lua.go +++ b/golibs/snail/lua.go @@ -32,6 +32,7 @@ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { snailMethods := rt.NewTable() snailFuncs := map[string]util.LuaExport{ "run": {snailrun, 3, false}, + "dir": {snaildir, 2, false}, } util.SetExports(rtm, snailMethods, snailFuncs) @@ -63,7 +64,9 @@ func snailnew(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // #member // run(command, streams) -// Runs a shell command. Works the same as `hilbish.run`. +// Runs a shell command. Works the same as `hilbish.run`, but only accepts a table of streams. +// #param command string +// #param streams table func snailrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.CheckNArgs(2); err != nil { return nil, err @@ -92,7 +95,7 @@ func snailrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { } case rt.NilType: // noop default: - return nil, errors.New("expected 3rd arg to either be a table or a boolean") + return nil, errors.New("expected 3rd arg to be a table") } var newline bool @@ -133,6 +136,31 @@ func snailrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.PushingNext1(t.Runtime, rt.TableValue(runnerRet)), nil } +// #member +// dir(path) +// Changes the directory of the snail instance. +// The interpreter keeps its set directory even when the Hilbish process changes +// directory, so this should be called on the `hilbish.cd` hook. +// #param path string Has to be an absolute path. +func snaildir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.CheckNArgs(2); err != nil { + return nil, err + } + + s, err := snailArg(c, 0) + if err != nil { + return nil, err + } + + dir, err := c.StringArg(1) + if err != nil { + return nil, err + } + + interp.Dir(dir)(s.runner) + return c.Next(), nil +} + func handleStream(v rt.Value, strms *util.Streams, errStream, inStream bool) error { if v == rt.NilValue { return nil diff --git a/nature/dirs.lua b/nature/dirs.lua index 0576f8a..abb28df 100644 --- a/nature/dirs.lua +++ b/nature/dirs.lua @@ -79,6 +79,7 @@ function dirs.setOld(d) end bait.catch('hilbish.cd', function(path, oldPath) + print(path, oldPath) dirs.setOld(oldPath) dirs.push(path) end) diff --git a/nature/hilbish.lua b/nature/hilbish.lua index 77d5511..d6a869c 100644 --- a/nature/hilbish.lua +++ b/nature/hilbish.lua @@ -1,9 +1,11 @@ -- @module hilbish -local hilbish = require 'hilbish' +local bait = require 'bait' local snail = require 'snail' hilbish.snail = snail.new() - +bait.catch('hilbish.cd', function(path) + hilbish.snail:dir(path) +end) --- Runs `cmd` in Hilbish's shell script interpreter. --- The `streams` parameter specifies the output and input streams the command should use. --- For example, to write command output to a sink.