how do i open a terminal using ruby?

P

Philip Rutkowski

hi-

i want to open up a terminal from ruby if it was not executed in one.
so,

if not STDIN.isatty
#Open up a terminal
end

do i have to use IO.popen? i would rather not because, i'm using linux
but this ruby script will also be executed on windows. how do i account
for when to use "gnome-terminal" and when to use "cmd"? then, how do i
write to that terminal window?

is there something easier?

thanks!!!
 
T

Tim Pease

hi-

i want to open up a terminal from ruby if it was not executed in one.
so,

if not STDIN.isatty
#Open up a terminal
end

do i have to use IO.popen? i would rather not because, i'm using linux
but this ruby script will also be executed on windows. how do i
account
for when to use "gnome-terminal" and when to use "cmd"? then, how do i
write to that terminal window?

I have no idea if this will work, but it's worth a shot ...


Kernel.exec '/bin/bash', '-c', "ruby #{__FILE__} #{ARGV.join(' ')}"


Beware the infinite recursion!

Blessings,
TwP
 
A

ara.t.howard

hi-

i want to open up a terminal from ruby if it was not executed in one.
so,

if not STDIN.isatty
#Open up a terminal
end

do i have to use IO.popen? i would rather not because, i'm using linux
but this ruby script will also be executed on windows. how do i
account
for when to use "gnome-terminal" and when to use "cmd"? then, how do i
write to that terminal window?

is there something easier?
thanks!!!


why?

a @ http://codeforpeople.com/
 
S

Sebastian Hungerecker

Tim said:
Kernel.exec '/bin/bash', '-c', "ruby #{__FILE__} #{ARGV.join(' ')}"

That won't open a terminal windows. It will simply execute bash which will
execute ruby, but if you weren't in a window before, you're still not.
 
S

Sebastian Hungerecker

Philip said:
if not STDIN.isatty
=C2=A0 #Open up a terminal
end
[...]
is there something easier?

If I'm not mistaken, on Windows ruby scripts automatically open in a comman=
d=20
window. And most filemanagers on unix ask you whether to open a terminal wh=
en=20
clicking on an executable file. So I don't think there's any need to do wha=
t=20
you're trying to do.

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
J

James Dinkel

Sebastian said:
Philip said:
if not STDIN.isatty
  #Open up a terminal
end
[...]
is there something easier?

If I'm not mistaken, on Windows ruby scripts automatically open in a
command
window. And most filemanagers on unix ask you whether to open a terminal
when
clicking on an executable file. So I don't think there's any need to do
what
you're trying to do.

HTH,
Sebastian

I have the same need. I have a script that runs in a terminal right
now, but I eventually want to make it gui (probably with Shoes) and I'll
need an option to click a button and open a command prompt (then run a
command in that command prompt and leave the prompt open for the user to
use).

I haven't even begun converting to the gui part yet though, so I haven't
put much thought into how I'm going to accomplish this. Oh yeah, and
this will need to be cross-platform, too, but it may just be a matter of
writing a slightly different program for each platform.

James
 
L

Logan Barnett

I have the same need. I have a script that runs in a terminal right
now, but I eventually want to make it gui (probably with Shoes) and
I'll
need an option to click a button and open a command prompt (then run a
command in that command prompt and leave the prompt open for the
user to
use).

I haven't even begun converting to the gui part yet though, so I
haven't
put much thought into how I'm going to accomplish this. Oh yeah, and
this will need to be cross-platform, too, but it may just be a
matter of
writing a slightly different program for each platform.

James

Check out Monkeybars (http://monkeybars.rubyforge.org/) for GUI stuff.
Combine that with Rawr and you should have a slick cross-platform GUI
along with native .app/.exe files and/or a web start link. Monkeybars
sits on JRuby, so you might be able to find some Java libraries to
help you out along with your gems. A quick search for Java terminal
stuff yielded JLine (http://jline.sourceforge.net/). Of course, that's
if the gem you had in mind isn't adequate for what you're doing.
 
D

David Masover

That won't open a terminal windows. It will simply execute bash which will
execute ruby, but if you weren't in a window before, you're still not.

I don't think there's any standard way to open another terminal _window_,
given that some platforms allow you to install many different kinds of
terminals in the first place, and there simply is no way to do this
consistently and cross-platform.

On Windows, you could definitely call 'cmd'. On OS X, you're probably safe
doing something with Terminal.app. But on Linux, what do you do?
Gnome-terminal? Konsole? xterm? These are probably the most common to be
installed, yet none are guaranteed...

You could always implement your own terminal, in your own GUI -- but then
you'll irritate people who have a favorite terminal, and would rather work
from there. So this only really makes sense if you're just going to go
whole-hog and use the GUI for everything.

I think about all you can do here is detect that you're not running in a
terminal, and if you're not, exit with an error. Most platforms make it easy
enough to run a script in a terminal -- it's the default on Windows; OS X has
a "script menu" that opens them in Terminal; and on typical Linux desktop
environments, application launchers have a simple checkbox for "run in
terminal", and double-clicking on a Ruby script should run it in a terminal
anyway.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top