Open drognisep opened 3 years ago
@ZenLiuCN Of course I can override if need be. Shouldn't it be consistent in the base implementation? What is the benefit of not having both reference the same streams in the same way?
I think linking both of them to Go's os.*
streams would make this use-case simpler for people and make the change to redirect much more consistent with gopher expectations without having force people to dig into the internals and override global functions. Is there something I'm missing?
@ZenLiuCN Of course I can override if need be. Shouldn't it be consistent in the base implementation? What is the benefit of not having both reference the same streams in the same way?
I think linking both of them to Go's
os.*
streams would make this use-case simpler for people and make the change to redirect much more consistent with gopher expectations without having force people to dig into the internals and override global functions. Is there something I'm missing?
so why not just try to make a pull request?
Maybe I misunderstood. Is this not being maintained anymore?
I do not know. just been use this lib current days. ^_^
Ah, okay. I don't have the time to prop up a lib with no maintainer. Thanks for the tip though @ZenLiuCN. :D
I have a very similar issue: I'm running Lua scripts as part of a web server and want to capture the output to send it to a webpage (using SSE, WebSockets, or anything like that). The 'obvious' choice would be simply to redirect Lua's stdout to some Go stream and push that out to the HTTP stream (probably it may be even simpler than the code I'm writing; I'm adding some error checking, etc.). Sadly, GopherLua seems to simply send everything from print()
to stdout, with no way to grab it. Is that so? I'm still looking through all the posted issues here to see if there is a way using the current codebase...
Oh, sure, I could simply override print()
, but, in that case, I'd have to deal with all the syntactic sugar from Lua's own print()
. I might more easily create my own pseudoprint()
which just sends output... uh, somewhere where I can grab it (note that I'm using luar to make things much simpler, so I might not be able to delve as deep as I wish).
Update: On another issue here, I've found something that would work in my case: https://github.com/xyproto/algernon/blob/34d3806bfa890e7f6296cc400e8fd51951cdc926/basic.go#L67
Im maintaining a fork of this project since a few years, where I added the feature to optionally redirect stdin, stdout and stderr. All calls (even internal usages of stdout/stderr) are using those instead.
The streams can be overwritten in lua.Options, where I added the following fields:
// Options is a configuration that is used to create a new LState.
type Options struct {
// <other fields>
// Stdin stream. Defaults to os.Stdin
Stdin io.ReadCloser
// Stdout stream. Defaults to os.Stdout
Stdout io.WriteCloser
// Stderr stream. Defaults to os.Stderr
Stderr io.WriteCloser
}
I'm using this fork in production for a few years now without any issues. If there's any interest, I'd be glad to provide a PR.
Speaking strictly as an end-user of gopher-lua
, I'd love to have your PR! Hopefully, @yuin is able to review it and add it to the main branch...
Please answer the following before submitting your issue:
print
andio.write
to print to the redirected stream.print
prints to os.Stdout (usingfmt.Print
), butio.write
does not. I would expect these functions to be consistent in how they select their output stream.I'm trying to wrap gopher-lua into a graphical script runner with Fyne along with an output panel to show the result of the script. I've observed that Lua-side
print
writes toos.Stdout
no problem because it usesfmt.Print
, butio.write
instead writes to the application's original stdout stream regardless of the value ofos.Stdout
. To support this functionality it would be nice if bothprint
andio.write
(and other I/O functions) used Go-sideos.Stdout
,os.Stderr
,os.Stdin
streams so they can be easily redirected to the graphical component. The alternative is generating a new executable and running it in a new process to redirect streams every time the user changes the script in the UI, which would be a huge pain.