mirror of https://github.com/Hilbis/Hilbish
feat: add sink for commanders to write output/read input (#232)
to write output, you would usually just use the print builtin since commanders are just lua custom commands but this does not consider the fact of pipes or other shell operators being used to redirect or whatever. this adds readable/writable "sinks" which is a type for input or output and is currently only used for commanders but can be used for other hilbish things in the futuremultiline
parent
088e326bd1
commit
2f6ab5fd92
@ -1,15 +1,15 @@
|
||||
local commander = require 'commander'
|
||||
|
||||
commander.register('bg', function()
|
||||
commander.register('bg', function(_, sinks)
|
||||
local job = hilbish.jobs.last()
|
||||
if not job then
|
||||
print 'bg: no last job'
|
||||
sinks.out:writeln 'bg: no last job'
|
||||
return 1
|
||||
end
|
||||
|
||||
local err = job.background()
|
||||
if err then
|
||||
print('bg: ' .. err)
|
||||
sinks.out:writeln('bg: ' .. err)
|
||||
return 2
|
||||
end
|
||||
end)
|
||||
|
@ -1,15 +1,15 @@
|
||||
local commander = require 'commander'
|
||||
|
||||
commander.register('fg', function()
|
||||
commander.register('fg', function(_, sinks)
|
||||
local job = hilbish.jobs.last()
|
||||
if not job then
|
||||
print 'fg: no last job'
|
||||
sinks.out:writeln 'fg: no last job'
|
||||
return 1
|
||||
end
|
||||
|
||||
local err = job.foreground() -- waits for job; blocks
|
||||
if err then
|
||||
print('fg: ' .. err)
|
||||
sinks.out:writeln('fg: ' .. err)
|
||||
return 2
|
||||
end
|
||||
end)
|
||||
|
@ -0,0 +1,121 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"hilbish/util"
|
||||
|
||||
rt "github.com/arnodel/golua/runtime"
|
||||
)
|
||||
|
||||
var sinkMetaKey = rt.StringValue("hshsink")
|
||||
|
||||
// a sink is a structure that has input and/or output
|
||||
// it is like a lua file when used in popen, but specific to hilbish
|
||||
type sink struct{
|
||||
writer io.Writer
|
||||
reader io.Reader
|
||||
ud *rt.UserData
|
||||
}
|
||||
|
||||
func setupSinkType(rtm *rt.Runtime) {
|
||||
sinkMeta := rt.NewTable()
|
||||
|
||||
sinkMethods := rt.NewTable()
|
||||
sinkFuncs := map[string]util.LuaExport{
|
||||
"write": {luaSinkWrite, 2, false},
|
||||
"writeln": {luaSinkWriteln, 2, false},
|
||||
}
|
||||
util.SetExports(l, sinkMethods, sinkFuncs)
|
||||
|
||||
sinkIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
arg := c.Arg(1)
|
||||
val := sinkMethods.Get(arg)
|
||||
|
||||
return c.PushingNext1(t.Runtime, val), nil
|
||||
}
|
||||
|
||||
sinkMeta.Set(rt.StringValue("__index"), rt.FunctionValue(rt.NewGoFunction(sinkIndex, "__index", 2, false)))
|
||||
l.SetRegistry(sinkMetaKey, rt.TableValue(sinkMeta))
|
||||
}
|
||||
|
||||
func luaSinkWrite(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s, err := sinkArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.writer.Write([]byte(data))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func luaSinkWriteln(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s, err := sinkArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.writer.Write([]byte(data + "\n"))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func newSinkInput(r io.Reader) *sink {
|
||||
s := &sink{
|
||||
reader: r,
|
||||
}
|
||||
s.ud = sinkUserData(s)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func newSinkOutput(w io.Writer) *sink {
|
||||
s := &sink{
|
||||
writer: w,
|
||||
}
|
||||
s.ud = sinkUserData(s)
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func sinkArg(c *rt.GoCont, arg int) (*sink, error) {
|
||||
s, ok := valueToSink(c.Arg(arg))
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("#%d must be a sink", arg + 1)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func valueToSink(val rt.Value) (*sink, bool) {
|
||||
u, ok := val.TryUserData()
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
s, ok := u.Value().(*sink)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
func sinkUserData(s *sink) *rt.UserData {
|
||||
sinkMeta := l.Registry(sinkMetaKey)
|
||||
return rt.NewUserData(s, sinkMeta.AsTable())
|
||||
}
|
Loading…
Reference in new issue