Sha256: edbb7ce9444481db732e8a735072af37e6fe35b4a4f0e15086498956c2079bc8

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'rib'

# from https://github.com/janlelis/ripl-multi_line
module Rib::Multiline
  include Rib::Plugin
  Shell.use(self)

  # test those:
  # ruby -e '"'
  # ruby -e '{'
  # ruby -e '['
  # ruby -e '('
  # ruby -e '/'
  # ruby -e 'class C'
  # ruby -e 'def f'
  # ruby -e 'begin'
  ERROR_REGEXP = Regexp.new(
    [ # string or regexp
      "unterminated \\w+ meets end of file",
      # mri and rubinius
      "syntax error, unexpected \\$end",
      # rubinius
      "expecting '.+'( or '.+')*",
      # jruby
      "syntax error, unexpected end-of-file",
    ].join('|'))

  def prompt
    return super if Multiline.disabled?
    if multiline_buffer.empty?
      super
    else
      "#{' '*(config[:prompt].size-2)}| "
    end
  end

  def loop_once
    return super if Multiline.disabled?
    catch(:multiline_cont) do
      super
      multiline_buffer.clear
    end
  end

  def loop_eval(input)
    return super if Multiline.disabled?
    multiline_buffer << input
    super(multiline_buffer.join("\n"))
  end

  def print_eval_error(e)
    return super if Multiline.disabled?
    if e.is_a?(SyntaxError) && e.message =~ ERROR_REGEXP
      throw :multiline_cont
    else
      super
    end
  end

  def handle_interrupt
    return super if Multiline.disabled?
    if multiline_buffer.empty?
      super
    else
      line = multiline_buffer.pop
      print "[removed this line: #{line}]"
      super
      throw :multiline_cont
    end
  end

  private
  def multiline_buffer
    @multiline_buffer ||= []
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rib-0.1.0 lib/rib/more/multiline.rb