feat: add method autoFlush to toggle/change automatic flushing

sink-enhance
sammyette 2023-02-19 15:52:56 -04:00
parent 29c3c6e55a
commit ac7acebb2f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 28 additions and 0 deletions

28
sink.go
View File

@ -31,6 +31,7 @@ func setupSinkType(rtm *rt.Runtime) {
sinkFuncs := map[string]util.LuaExport{ sinkFuncs := map[string]util.LuaExport{
"flush": {luaSinkFlush, 1, false}, "flush": {luaSinkFlush, 1, false},
"read": {luaSinkRead, 1, false}, "read": {luaSinkRead, 1, false},
"autoFlush": {luaSinkAutoFlush, 2, false},
"write": {luaSinkWrite, 2, false}, "write": {luaSinkWrite, 2, false},
"writeln": {luaSinkWriteln, 2, false}, "writeln": {luaSinkWriteln, 2, false},
} }
@ -151,6 +152,32 @@ func luaSinkFlush(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil return c.Next(), nil
} }
// #member
// autoFlush(auto)
// Sets/toggles the option of automatically flushing output.
// A call with no argument will toggle the value.
// --- @param auto boolean|nil
func luaSinkAutoFlush(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
s, err := sinkArg(c, 0)
if err != nil {
return nil, err
}
v := c.Arg(1)
if v.Type() != rt.BoolType && v.Type() != rt.NilType {
return nil, fmt.Errorf("#1 must be a boolean")
}
value := !s.autoFlush
if v.Type() == rt.BoolType {
value = v.AsBool()
}
s.autoFlush = value
return c.Next(), nil
}
func newSinkInput(r io.Reader) *sink { func newSinkInput(r io.Reader) *sink {
s := &sink{ s := &sink{
reader: bufio.NewReader(r), reader: bufio.NewReader(r),
@ -167,6 +194,7 @@ func newSinkInput(r io.Reader) *sink {
func newSinkOutput(w io.Writer) *sink { func newSinkOutput(w io.Writer) *sink {
s := &sink{ s := &sink{
writer: bufio.NewWriter(w), writer: bufio.NewWriter(w),
autoFlush: true,
} }
s.ud = sinkUserData(s) s.ud = sinkUserData(s)