diff options
| author | Mario Visic <[email protected]> | 2019-01-11 16:44:10 +1100 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2019-01-10 21:44:10 -0800 |
| commit | 6a58faf836542089eb104f985aee04196207c648 (patch) | |
| tree | 9dffd45bae0fa0122ba6e8abeb4175b6b7d94c3b /lib | |
| parent | 9994b770686428ebbde636081156abb5a50dc6c2 (diff) | |
| download | ruby2d-6a58faf836542089eb104f985aee04196207c648.tar.gz ruby2d-6a58faf836542089eb104f985aee04196207c648.zip | |
Rescue console broken pipe (#143)
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d/cli/console.rb | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/lib/ruby2d/cli/console.rb b/lib/ruby2d/cli/console.rb index b30570d..684cab5 100644 --- a/lib/ruby2d/cli/console.rb +++ b/lib/ruby2d/cli/console.rb @@ -1,3 +1,6 @@ +# Interactive Ruby 2D console + +# Save the Ruby file from the command-line arguments rb_file = ARGV[1] # Check if source file provided is good @@ -14,11 +17,12 @@ require 'open3' require 'readline' require 'io/wait' -line = 1 # the current line number, to be incremented +line = 1 # the current line number -# Open a new process for the Ruby file +# Open a new process for the Ruby file stdin, stdout, stderr, wait_thr = Open3.popen3("ruby -r 'ruby2d/cli/enable_console' #{rb_file}") +# Request input and send commands loop do # Read the next command @@ -31,19 +35,31 @@ loop do exit end - # Skip if command is an empty string - unless cmd.empty? + # Try sending commands + begin + + # Skip if command is an empty string + unless cmd.empty? - # Send command to the Ruby file - stdin.puts cmd + # Send command to the Ruby file + stdin.puts cmd - # Read and print output from the Ruby file - puts stdout.gets - while stdout.ready? do + # Read and print output from the Ruby file puts stdout.gets + while stdout.ready? do + puts stdout.gets + end + end + + # Increment the line number + line += 1 + + # Rescue exception if can't send commands to the Ruby 2D window + rescue Errno::EPIPE + puts "Can't connect to the window (was it closed?)", + "For help, see: ruby2d.com/learn/console" + exit 1 end - # Advance to next line - line += 1 end |
