From d196799abf5da57f397456193559c757e4af51ff Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 25 Mar 2023 18:12:51 -0400 Subject: [PATCH] feat(commands/greenhouse): support reading from pipes --- nature/commands/greenhouse.lua | 5 +++++ sink.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/nature/commands/greenhouse.lua b/nature/commands/greenhouse.lua index 2baf3cc..351b120 100644 --- a/nature/commands/greenhouse.lua +++ b/nature/commands/greenhouse.lua @@ -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 = io.open(name, 'r') if not f then diff --git a/sink.go b/sink.go index 2ecc19d..62e9bec 100644 --- a/sink.go +++ b/sink.go @@ -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