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:
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?
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/
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:
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?
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/
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/
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:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.