mirror of https://github.com/Hilbis/Hilbish
Compare commits
2 Commits
54f89db578
...
29c3c6e55a
Author | SHA1 | Date |
---|---|---|
sammyette | 29c3c6e55a | |
sammyette | 9e45929da7 |
26
sink.go
26
sink.go
|
@ -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
|
||||
}
|
||||
|
@ -27,16 +29,34 @@ func setupSinkType(rtm *rt.Runtime) {
|
|||
|
||||
sinkMethods := rt.NewTable()
|
||||
sinkFuncs := map[string]util.LuaExport{
|
||||
"read": {luaSinkRead, 0, false},
|
||||
"flush": {luaSinkFlush, 1, false},
|
||||
"read": {luaSinkRead, 1, false},
|
||||
"write": {luaSinkWrite, 2, false},
|
||||
"writeln": {luaSinkWriteln, 2, false},
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -137,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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue