feat: add pipe property to sinks to see if stdin sink is a pipe

sink-enhance
sammyette 2023-02-19 13:37:50 -04:00
parent 9e45929da7
commit 29c3c6e55a
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 23 additions and 0 deletions

23
sink.go
View File

@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"os"
"hilbish/util"
@ -18,6 +19,7 @@ var sinkMetaKey = rt.StringValue("hshsink")
type sink struct{
writer *bufio.Writer
reader *bufio.Reader
file *os.File
ud *rt.UserData
autoFlush bool
}
@ -35,9 +37,26 @@ func setupSinkType(rtm *rt.Runtime) {
util.SetExports(l, sinkMethods, sinkFuncs)
sinkIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
s, _ := sinkArg(c, 0)
arg := c.Arg(1)
val := sinkMethods.Get(arg)
if val != rt.NilValue {
return c.PushingNext1(t.Runtime, val), nil
}
keyStr, _ := arg.TryString()
switch keyStr {
case "pipe":
val = rt.BoolValue(false)
if s.file != nil {
fileInfo, _ := s.file.Stat();
val = rt.BoolValue(fileInfo.Mode() & os.ModeCharDevice == 0)
}
}
return c.PushingNext1(t.Runtime, val), nil
}
@ -138,6 +157,10 @@ func newSinkInput(r io.Reader) *sink {
}
s.ud = sinkUserData(s)
if f, ok := r.(*os.File); ok {
s.file = f
}
return s
}