summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMario Visic <[email protected]>2019-01-11 16:44:10 +1100
committerTom Black <[email protected]>2019-01-10 21:44:10 -0800
commit6a58faf836542089eb104f985aee04196207c648 (patch)
tree9dffd45bae0fa0122ba6e8abeb4175b6b7d94c3b
parent9994b770686428ebbde636081156abb5a50dc6c2 (diff)
downloadruby2d-6a58faf836542089eb104f985aee04196207c648.tar.gz
ruby2d-6a58faf836542089eb104f985aee04196207c648.zip
Rescue console broken pipe (#143)
-rw-r--r--lib/ruby2d/cli/console.rb38
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