Simulating a TTY?

Q

Quintus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi there,

I'm trying to write something like a terminal emulator for use inside a
wxRuby GUI as a debugging console in the current context or just as a
possibility to issue some fast commands rather than clicking around. I
have to problems with this:

1. I wanted to use IRB to just allow the user to enter Ruby code in the
current project context, but IRB seems to only read from STDIN. I didn't
find a possibility to direct IRB somewhere else. So, is there one or can
I give up on this?

2. The second problem arises due to the first. I thought I may reassign
STDIN to a StringIO I write into what the text control I present to the
user gets inputted, but it seems that a #gets called on this StringIO
does not wait for the user to enter input, but just returns nil
immediately. So far I found out that this is because #tty? returns false
on the StringIO, but overriding the method to return true didn't work
for me. So, how do I make #gets and all the other inputting methods wait
until something is written to the StringIO, i.e. behave as if the
StringIO was a TTY?

A major part of this problems is that the code has to be
platform-independent, because the GUI is going to be run on Windows and
Linux systems (and maybe Macs as well)...

Thanks in advance,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkx1O8UACgkQDYShvwAbcNnQPwCaA5AHY4lozQsrSKm2Gj/zq34d
2rIAn3Z9oaM1ByxaT2q5uLW2c5Y7Fxbk
=GP0F
-----END PGP SIGNATURE-----
 
R

Roger Pack

I'm trying to write something like a terminal emulator for use inside a
wxRuby GUI as a debugging console in the current context or just as a
possibility to issue some fast commands rather than clicking around. I
have to problems with this:

1. I wanted to use IRB to just allow the user to enter Ruby code in the
current project context, but IRB seems to only read from STDIN. I didn't
find a possibility to direct IRB somewhere else. So, is there one or can
I give up on this?

A few related projects that might help:

live console [1] monkey patches irb to allow it to receive from a socket
(probably not quite what you wanted...)
as does fxri
[1] http://github.com/pete/live-console/blob/master/lib/live_console.rb

redcar also has a "REPL" which "is like" IRB (without ever using irb
though).

GL.
-r
 
Q

Quintus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 25.08.2010 18:09, schrieb Roger Pack:
I'm trying to write something like a terminal emulator for use inside a
wxRuby GUI as a debugging console in the current context or just as a
possibility to issue some fast commands rather than clicking around. I
have to problems with this:

1. I wanted to use IRB to just allow the user to enter Ruby code in the
current project context, but IRB seems to only read from STDIN. I didn't
find a possibility to direct IRB somewhere else. So, is there one or can
I give up on this?

A few related projects that might help:

live console [1] monkey patches irb to allow it to receive from a socket
(probably not quite what you wanted...)
as does fxri
[1] http://github.com/pete/live-console/blob/master/lib/live_console.rb

redcar also has a "REPL" which "is like" IRB (without ever using irb
though).

GL.
-r

Thanks, fxri pointed me to the right direction. I already solved problem
2 by using IO.pipe which I didn't know before and was positively
surprised that it works on Windows, and hopefully I can conquer IRB now.
I will also have a look at REPL.

However, it's quite annoying that methods like IO#nread or IO#ready?
aren't implemented on Windows (event with requiring "io/wait" #ready?
always returns false and #nread gives a NoMethodError). I wish that
project should only work on Linux...

Thanks,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkx2kFIACgkQDYShvwAbcNmB/QCeL0D/saIEyV62q2onWNahVWOi
woAAn1LARMAooVQaLZ3s0TYGN2UnUlEf
=tkUl
-----END PGP SIGNATURE-----
 
E

Ed Howland

I'd look at guirb [1] as well. Even though it is written in Tk (and
Fx?) they had to solve the same problem. Haven't tried it on Windows
myself.

BTW, is your code public or open source?

Cheers,
Ed

Ed Howland
http://greenprogrammer.wordpress.com
http://twitter.com/ed_howland



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 25.08.2010 18:09, schrieb Roger Pack:
I'm trying to write something like a terminal emulator for use inside a
wxRuby GUI as a debugging console in the current context or just as a
possibility to issue some fast commands rather than clicking around. I
have to problems with this:

1. I wanted to use IRB to just allow the user to enter Ruby code in the
current project context, but IRB seems to only read from STDIN. I didn't
find a possibility to direct IRB somewhere else. So, is there one or can
I give up on this?

A few related projects that might help:

live console [1] monkey patches irb to allow it to receive from a socket
(probably not quite what you wanted...)
as does fxri
[1] http://github.com/pete/live-console/blob/master/lib/live_console.rb

redcar also has a "REPL" which "is like" IRB (without ever using irb
though).

GL.
-r

Thanks, fxri pointed me to the right direction. I already solved problem
2 by using IO.pipe which I didn't know before and was positively
surprised that it works on Windows, and hopefully I can conquer IRB now.
I will also have a look at REPL.

However, it's quite annoying that methods like IO#nread or IO#ready?
aren't implemented on Windows (event with requiring "io/wait" #ready?
always returns false and #nread gives a NoMethodError). I wish that
project should only work on Linux...

Thanks,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkx2kFIACgkQDYShvwAbcNmB/QCeL0D/saIEyV62q2onWNahVWOi
woAAn1LARMAooVQaLZ3s0TYGN2UnUlEf
=tkUl
-----END PGP SIGNATURE-----
 
Q

Quintus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 28.08.2010 17:13, schrieb Ed Howland:
I'd look at guirb [1] as well. Even though it is written in Tk (and
Fx?) they had to solve the same problem. Haven't tried it on Windows
myself.

BTW, is your code public or open source?

Cheers,
Ed

Ed Howland
http://greenprogrammer.wordpress.com
http://twitter.com/ed_howland

guirb looks good. I will definetely look at it.
BTW, is your code public or open source?

It is OpenSource, no worries. I'm working together with some others on
this project: http://githib.com/Quintus/OpenRubyRMK . Sadly the others
have not yet found out how to use git... but that comes over time I think.
My terminal emulator code isn't loaded up yet, it's highly incomplete.
Here's the current status: http://www.pastie.org/1122829
So far, I haven't messed with IRB yet, I'm struggling with the fact that
wxRuby has some problems with multithreading. If you run the code, it's
likely that you notice the terminal is responding quite slowly...

Vale,
Quintus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkx5WfkACgkQDYShvwAbcNkBwwCdGj2dKhbGfuaYPaZ8uyQg9D+D
GdwAnAlakN6Vilx7J9UABVm831eyJJwI
=14Jt
-----END PGP SIGNATURE-----
 
J

James Edward Gray II

1. I wanted to use IRB to just allow the user to enter Ruby code in = the
current project context, but IRB seems to only read from STDIN. I = didn't
find a possibility to direct IRB somewhere else. So, is there one or = can
I give up on this?

On Unix, you can use a Psuedo-Terminal (PTY) to manage an IRb process:

$ ruby pty_irb.rb=20Cakey-Please:~/Desktop $ cat pty_irb.rb=20
require "pty"
require "expect"

PTY.spawn("irb -f --simple-prompt") do |reader, writer, pid|
input =3D ""
while output =3D reader.expect(/^>>\s+/)
if output and output.first
output.first.sub!(/\A#{Regexp.escape(input.to_s.rstrip)}\r?\n/, "")
end
print output
$stdout.flush
input =3D gets
writer << input
end
end
A major part of this problems is that the code has to be
platform-independent, because the GUI is going to be run on Windows = and
Linux systems (and maybe Macs as well)...

I don't think the PTY library works on Windows, unfortunately. Perhaps =
there's a different but similar strategy you can use there.

James Edward Gray II=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top