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:
parent
589ef8a6d3
commit
2a9330c692
@ -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.
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user