From 6a58faf836542089eb104f985aee04196207c648 Mon Sep 17 00:00:00 2001 From: Mario Visic Date: Fri, 11 Jan 2019 16:44:10 +1100 Subject: Rescue console broken pipe (#143) --- lib/ruby2d/cli/console.rb | 38 +++++++++++++++++++++++++++----------- 1 file 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 -- cgit v1.2.3