feat(commands/greenhouse): support reading from pipes

pager
sammyette 2023-03-25 18:12:51 -04:00
parent 7531bbfd14
commit d196799abf
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 39 additions and 0 deletions

View File

@ -10,6 +10,11 @@ commander.register('greenhouse', function(args, sinks)
local gh = Greenhouse(sinks.out)
local done = false
if sinks['in'].pipe then
local page = Page(sinks['in']:readAll())
gh:addPage(page)
end
for _, name in ipairs(args) do
local f <close> = io.open(name, 'r')
if not f then

34
sink.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strings"
"hilbish/util"
@ -31,6 +32,7 @@ func setupSinkType(rtm *rt.Runtime) {
sinkFuncs := map[string]util.LuaExport{
"flush": {luaSinkFlush, 1, false},
"read": {luaSinkRead, 1, false},
"readAll": {luaSinkReadAll, 1, false},
"autoFlush": {luaSinkAutoFlush, 2, false},
"write": {luaSinkWrite, 2, false},
"writeln": {luaSinkWriteln, 2, false},
@ -65,6 +67,38 @@ func setupSinkType(rtm *rt.Runtime) {
l.SetRegistry(sinkMetaKey, rt.TableValue(sinkMeta))
}
// #member
// read() -> string
// --- @returns string
// Reads input from the sink.
func luaSinkReadAll(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
s, err := sinkArg(c, 0)
if err != nil {
return nil, err
}
lines := []string{}
for {
line, err := s.reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
lines = append(lines, line)
}
return c.PushingNext1(t.Runtime, rt.StringValue(strings.Join(lines, ""))), nil
}
// #member
// read() -> string
// --- @returns string