2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-04-20 20:43:23 +00:00

fix: change directory of sh interp

This commit is contained in:
sammyette 2025-04-03 00:12:53 -04:00
parent 589ef8a6d3
commit 2a9330c692
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
5 changed files with 48 additions and 7 deletions

View File

@ -40,6 +40,11 @@ This function has no parameters.
A Snail is a shell script interpreter instance. A Snail is a shell script interpreter instance.
### Methods ### Methods
#### run(command, streams) #### dir(path)
Runs a shell command. Works the same as `hilbish.run`. 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.

View File

@ -2,10 +2,15 @@
local snail = {} 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. --- Creates a new Snail instance.
function snail.new() end 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 function snail:run(command, streams) end
return snail return snail

View File

@ -32,6 +32,7 @@ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
snailMethods := rt.NewTable() snailMethods := rt.NewTable()
snailFuncs := map[string]util.LuaExport{ snailFuncs := map[string]util.LuaExport{
"run": {snailrun, 3, false}, "run": {snailrun, 3, false},
"dir": {snaildir, 2, false},
} }
util.SetExports(rtm, snailMethods, snailFuncs) util.SetExports(rtm, snailMethods, snailFuncs)
@ -63,7 +64,9 @@ func snailnew(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// #member // #member
// run(command, streams) // 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) { func snailrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.CheckNArgs(2); err != nil { if err := c.CheckNArgs(2); err != nil {
return nil, err return nil, err
@ -92,7 +95,7 @@ func snailrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
} }
case rt.NilType: // noop case rt.NilType: // noop
default: 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 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 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 { func handleStream(v rt.Value, strms *util.Streams, errStream, inStream bool) error {
if v == rt.NilValue { if v == rt.NilValue {
return nil return nil

View File

@ -79,6 +79,7 @@ function dirs.setOld(d)
end end
bait.catch('hilbish.cd', function(path, oldPath) bait.catch('hilbish.cd', function(path, oldPath)
print(path, oldPath)
dirs.setOld(oldPath) dirs.setOld(oldPath)
dirs.push(path) dirs.push(path)
end) end)

View File

@ -1,9 +1,11 @@
-- @module hilbish -- @module hilbish
local hilbish = require 'hilbish' local bait = require 'bait'
local snail = require 'snail' local snail = require 'snail'
hilbish.snail = snail.new() hilbish.snail = snail.new()
bait.catch('hilbish.cd', function(path)
hilbish.snail:dir(path)
end)
--- Runs `cmd` in Hilbish's shell script interpreter. --- Runs `cmd` in Hilbish's shell script interpreter.
--- The `streams` parameter specifies the output and input streams the command should use. --- The `streams` parameter specifies the output and input streams the command should use.
--- For example, to write command output to a sink. --- For example, to write command output to a sink.