Redirect STDOUT

Hi all

I'm writting a program to detect the cursor position with the vt100 command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

yong wrote:

Hi all

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

I suggest looking at the code for the win2console library, which gives
an example of something similar.

···

--
Posted via http://www.ruby-forum.com/\.

David Roberts wrote:

yong wrote:

Hi all

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

I suggest looking at the code for the win2console library, which gives
an example of something similar.

Oh bother! That should be "win32console".

DJ

···

--
Posted via http://www.ruby-forum.com/\.

Thank you very much.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?

Thanks.

David Roberts <smartgpx@gmail.com> writes:

···

yong wrote:

Hi all

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

Thanks

I suggest looking at the code for the win2console library, which gives
an example of something similar.

--
Posted via http://www.ruby-forum.com/\.

--
My Personal Website:

http://www.twinbee.info

本人驻守於

cn.comp.lang.perl
cn.comp.lang.c
cn.comp.www
cn.music
cn.music.classical

use backticks:

   string = ` your_cmd.exe `

substitution works like double quotes

   cmd = "your_cmd.exe"
   string = ` #{ cmd } `

-a

···

On Sun, 15 Oct 2006, yong wrote:

Thank you very much.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?

Thanks.

--
my religion is very simple. my religion is kindness. -- the dalai lama

yong wrote:

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How
can I redirect it to a string?

see "ri StringIO"

DJ

···

--
Posted via http://www.ruby-forum.com/\.

You don't.

You should avoid touching STDOUT and STDERR as much as possible.

Instead assign a StringIO to $stdout.

http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout

···

On Oct 14, 2006, at 8:50 PM, yong wrote:

David Roberts <smartgpx@gmail.com> writes:

yong wrote:

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

I suggest looking at the code for the win2console library, which gives
an example of something similar.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But How can I redirect it to a string?

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

#!/usr/bin/ruby

require 'stringio'

mystring=""
sstring=StringIO.open(mystring,"w+")

#STDOUT.reopen(sstring) #can't work
stdoutbackup=$stdout
$stdout=sstring

print "BUFFERED TEXT\r\n"

$stdout=stdoutbackup
print mystring
<<<

It works.

Thank you very much. :slight_smile:

Eric Hodel <drbrain@segment7.net> writes:

···

On Oct 14, 2006, at 8:50 PM, yong wrote:

David Roberts <smartgpx@gmail.com> writes:

yong wrote:

I'm writting a program to detect the cursor position with the vt100
command "\x1b[6n".

So I need to redirect STDOUT to a string.

How can I do this?

I suggest looking at the code for the win2console library, which
gives
an example of something similar.

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But
How can I redirect it to a string?

You don't.

You should avoid touching STDOUT and STDERR as much as possible.

Instead assign a StringIO to $stdout.

http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

--
My Personal Website:

http://www.twinbee.info

本人驻守於

cn.comp.lang.perl
cn.comp.lang.c
cn.comp.www
cn.music
cn.music.classical

Eric Hodel <drbrain@segment7.net> writes:

Now I can use STDOUT.reopen to redirect STDOUT output to a file.But
How can I redirect it to a string?

You don't.

You should avoid touching STDOUT and STDERR as much as possible.

Instead assign a StringIO to $stdout.

http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout

require 'stringio'

mystring=""
sstring=StringIO.open(mystring,"w+")

sstring = StringIO.new will suffice.

#STDOUT.reopen(sstring) #can't work
stdoutbackup=$stdout
$stdout=sstring

print "BUFFERED TEXT\r\n"

$stdout=stdoutbackup

I tend to wrap this up in a method that yields. See util_capture in ZenTest's test/zentest_assertions.rb

···

On Oct 15, 2006, at 3:30 AM, yong wrote:

On Oct 14, 2006, at 8:50 PM, yong wrote:

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com