Simulating a TTY?

-----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-----

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] live-console/lib/live_console.rb at master · pete/live-console · GitHub

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

GL.
-r

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

Am 25.08.2010 18:09, schrieb Roger P.:

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-----

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://twitter.com/ed_howland

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

Am 28.08.2010 17:13, schrieb 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-----

On Aug 25, 2010, at 10:50 AM, Quintus wrote:

  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

j = “james”
=> “james”

j.capitalize
=> “James”

exit
Cakey-Please:~/Desktop $ cat pty_irb.rb
require “pty”
require “expect”

PTY.spawn(“irb -f --simple-prompt”) do |reader, writer, pid|
input = “”
while output = 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 = 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 G. II