Guillaume Marcais
9/16/2006 12:06:00 AM
Here is my use case, on UNIX. I wrote a ncurses program which can get
its data from stdin (similar to what happen to less when run as 'cat
file | less'). The problem is that stdin is not connected to your
terminal any more and ncurses goes wild interpreting the data from a
file as key strokes.
The trick seems, as guessed with lsof on less, to dup stdin and dup2
stdout to stdin. Then ncurses is happy with stdin connected to a
terminal, and the data can be read from some higher file descriptor
(most probably 4).
How to do the same thing in Ruby? There is no IO#dup or IO#dup2. Only
IO#reopen. But I need to already have an object for the destination IO,
which I don't have when I do the first "dup".
Right now, I solved the problem by doing the following:
r, w = IO.pipe
w.close
r.reopen($stdin)
$stdin.reopen($stdout)
# start ncurses, read data from r
This works. But it seems a little weird to create a pipe to destroy it
right away and for the only purpose of creating an IO object.
Any suggestions of a nicer way to do this? Or is this a valid use case
for supporting IO#dup, at least on the UNIX like platform?
Thanks,
Guillaume.