librelist archives

« back to archive

green shoes interactive command

green shoes interactive command

From:
leandro
Date:
2014-10-21 @ 16:19
hi!

i'm trying to execute an interactive command into a green_shoes window
users can write the command in an edit_line and i want to display the
result of the command in an edit_box. when the user is requested to enter
input i pretend to show an ask()

the code i am trying is:

    o = open("|#{cmd}")
    @box.text = o.read()
    cmd2 = ask(o.read())

    o.puts (cmd2)
    @box.text = o.read()

this won't work at all, besides it works somehow in a simple console ruby
script (without green_shoes)

i've also tried backticks, popen3, system, etc...
this way the command is executed, but i can't get the output to an "ask
window" to enter the user response to the command 'cause the pipe must be
closed *before* showing the output


somebody can help me?


PS: i have a working version of one-way commands window (not interactive)
in https://github.com/novia713/rhosgobel

-Leandro

Re: [shoes] green shoes interactive command

From:
peter marien
Date:
2014-10-21 @ 19:14
Hey leandro.
I don't understand exactly what or rather how you try to do that but
perhaps you find inspiration in this script.
It comes from my collection but I failed to note the author, sorry for that.

require 'green_shoes'

class ShoesIO < StringIO
  def initialize app, stdout, *args
    @app, @stdout = app, stdout
    super *args
  end

  def gets
    res = @app.ask @stdout.string.split("\n").last
    res.to_s + "\n"
  end
end

Shoes.app do
  stack do
    @console = edit_box
    @button = button "Ask Name"
  end

  $stdout = StringIO.new
  $stdin = ShoesIO.new self, $stdout, ''

  e = every do |i|
    @console.text = $stdout.string
  end

  @button.click do
    puts "What is your name?"
    ans = gets.chomp
    ans = ans == '' ? 'no answer' : ('Name: ' + ans)
    puts ans
  end
end

I hope it serves you, succes !

Peter


2014-10-21 18:19 GMT+02:00 leandro <leandro@leandro.org>:

> hi!
>
> i'm trying to execute an interactive command into a green_shoes window
> users can write the command in an edit_line and i want to display the
> result of the command in an edit_box. when the user is requested to enter
> input i pretend to show an ask()
>
> the code i am trying is:
>
>     o = open("|#{cmd}")
>     @box.text = o.read()
>     cmd2 = ask(o.read())
>
>     o.puts (cmd2)
>     @box.text = o.read()
>
> this won't work at all, besides it works somehow in a simple console ruby
> script (without green_shoes)
>
> i've also tried backticks, popen3, system, etc...
> this way the command is executed, but i can't get the output to an "ask
> window" to enter the user response to the command 'cause the pipe must be
> closed *before* showing the output
>
>
> somebody can help me?
>
>
> PS: i have a working version of one-way commands window (not interactive)
> in https://github.com/novia713/rhosgobel
>
> -Leandro
>

Re: [shoes] green shoes interactive command

From:
Bruce Perens
Date:
2014-10-21 @ 19:19
Open3#popen3 will do what you wish. You can close the pipe for file 
descriptor zero (the input) while leaving 1 (output) and 2 (error 
output) open.

But don't even think of using the shell to run your command because that 
will give people the power to run whatever shell command they wish with 
things like "your expected command;nasty command". Run the executable 
directly using its full path (I guess it's a drupal thing). And get a 
good systems programmer to review this for security issues, because it 
is bound to be a security nightmare.

     Thanks

     Bruce

On 10/21/2014 09:19 AM, leandro wrote:
> hi!
>
> i'm trying to execute an interactive command into a green_shoes window 
> users can write the command in an edit_line and i want to display the 
> result of the command in an edit_box. when the user is requested to 
> enter input i pretend to show an ask()
>
> the code i am trying is:
>
>     o = open("|#{cmd}")
>     @box.text = o.read()
>     cmd2 = ask(o.read())
>
>     o.puts (cmd2)
>     @box.text = o.read()
>
> this won't work at all, besides it works somehow in a simple console 
> ruby script (without green_shoes)
>
> i've also tried backticks, popen3, system, etc...
> this way the command is executed, but i can't get the output to an 
> "ask window" to enter the user response to the command 'cause the pipe 
> must be closed *before* showing the output
>
>
> somebody can help me?
>
>
> PS: i have a working version of one-way commands window (not 
> interactive) in https://github.com/novia713/rhosgobel
>
> -Leandro