Sha256: 07663ddf56576719d40a34d79295c3883c344fd459fb08b0a3a899cec538eb56

Contents?: true

Size: 983 Bytes

Versions: 21

Compression:

Stored size: 983 Bytes

Contents

require 'native'

NodeRepl = Native(`OpalNode.node_require('repl')`)

def NodeRepl.start(options = {})
  Native::Object.new(`#{@native}.start(#{options.to_n})`)
end

line = 1
prompt_interrupted = false

prompt = ->(context) {
  tip = prompt_interrupted ? '*' : '>'
  "irb(#{context}):#{line.to_s.rjust(3, '0')}#{tip} "
}

$repl = NodeRepl.start(
  prompt: prompt.call(self),
  useGlobal: true,
  ignoreUndefined: true,
  eval: ->(cmd, context, filename, callback) {
    line += 1
    cmd = cmd[1...-1].chomp
    if cmd.empty?
      prompt_interrupted = true
      $repl.prompt = prompt.call(self)
      callback.call('')
      next
    end
    prompt_interrupted = false
    $repl.prompt = prompt.call(self)
    begin
      result = `OpalNode.run(cmd, filename)`
      result = nil if `#{result} == nil`
      callback.call('=> ' + result.inspect)
    rescue => e
      callback.call(e.backtrace.join("\n"))
    end
  },
)

# Add a newline before exiting
$repl.on :exit, -> { puts }

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
opal-1.4.1 stdlib/nodejs/irb.rb
opal-1.4.0 stdlib/nodejs/irb.rb
opal-1.4.0.alpha1 stdlib/nodejs/irb.rb
opal-1.3.2 stdlib/nodejs/irb.rb
opal-1.3.1 stdlib/nodejs/irb.rb
opal-1.3.0 stdlib/nodejs/irb.rb
opal-1.3.0.rc1 stdlib/nodejs/irb.rb
opal-1.3.0.alpha1 stdlib/nodejs/irb.rb
opal-1.2.0 stdlib/nodejs/irb.rb
opal-1.2.0.beta1 stdlib/nodejs/irb.rb
opal-1.1.1 stdlib/nodejs/irb.rb
opal-1.1.1.rc1 stdlib/nodejs/irb.rb
opal-1.1.0 stdlib/nodejs/irb.rb
opal-1.1.0.rc1 stdlib/nodejs/irb.rb
opal-1.0.5 stdlib/nodejs/irb.rb
opal-1.0.4 stdlib/nodejs/irb.rb
opal-1.0.3 stdlib/nodejs/irb.rb
opal-1.0.2 stdlib/nodejs/irb.rb
opal-1.0.1 stdlib/nodejs/irb.rb
opal-1.0.0 stdlib/nodejs/irb.rb