Sha256: cedf89bdd87661964cfae9fec0dc73814fd529d3fef76ff5ba4fd4958768e8a8

Contents?: true

Size: 957 Bytes

Versions: 1

Compression:

Stored size: 957 Bytes

Contents

rb_file = ARGV[1]

# Check if source file provided is good
if !rb_file
  puts "Provide a Ruby file to run"
  exit 1
elsif !File.exist? rb_file
  puts "Can't find file: #{rb_file}"
  exit 1
end

# Add libraries
require 'open3'
require 'readline'
require 'io/wait'

line = 1  # the current line number, to be incremented

# Open a new process for the  Ruby file
stdin, stdout, stderr, wait_thr = Open3.popen3("ruby -r 'ruby2d/cli/enable_console' #{rb_file}")

loop do

  # Read the next command
  cmd = Readline.readline("ruby2d:#{line}> ", true)

  # Quit if command is 'exit'
  if cmd == 'exit'
    Process.kill 'INT', wait_thr.pid
    wait_thr.value
    exit
  end

  # Skip if command is an empty string
  unless cmd.empty?

    # Send command to the Ruby file
    stdin.puts cmd

    # Read and print output from the Ruby file
    puts stdout.gets
    while stdout.ready? do
      puts stdout.gets
    end
  end

  # Advance to next line
  line += 1
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby2d-0.8.0 lib/ruby2d/cli/console.rb