2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2000-05-12 13:07:57 +04:00
|
|
|
#
|
|
|
|
# xmp.rb - irb version of gotoken xmp
|
2002-07-09 15:17:17 +04:00
|
|
|
# $Release Version: 0.9$
|
2000-05-12 13:07:57 +04:00
|
|
|
# $Revision$
|
|
|
|
# by Keiju ISHITSUKA(Nippon Rational Inc.)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2000-05-12 13:07:57 +04:00
|
|
|
#
|
|
|
|
|
2002-07-09 15:17:17 +04:00
|
|
|
require "irb"
|
2018-11-02 20:52:33 +03:00
|
|
|
require_relative "frame"
|
2000-05-12 13:07:57 +04:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# An example printer for irb.
|
|
|
|
#
|
|
|
|
# It's much like the standard library PrettyPrint, that shows the value of each
|
|
|
|
# expression as it runs.
|
|
|
|
#
|
|
|
|
# In order to use this library, you must first require it:
|
|
|
|
#
|
|
|
|
# require 'irb/xmp'
|
|
|
|
#
|
|
|
|
# Now, you can take advantage of the Object#xmp convenience method.
|
|
|
|
#
|
|
|
|
# xmp <<END
|
|
|
|
# foo = "bar"
|
|
|
|
# baz = 42
|
|
|
|
# END
|
|
|
|
# #=> foo = "bar"
|
|
|
|
# #==>"bar"
|
|
|
|
# #=> baz = 42
|
|
|
|
# #==>42
|
|
|
|
#
|
|
|
|
# You can also create an XMP object, with an optional binding to print
|
|
|
|
# expressions in the given binding:
|
|
|
|
#
|
|
|
|
# ctx = binding
|
|
|
|
# x = XMP.new ctx
|
|
|
|
# x.puts
|
|
|
|
# #=> today = "a good day"
|
|
|
|
# #==>"a good day"
|
|
|
|
# ctx.eval 'today # is what?'
|
|
|
|
# #=> "a good day"
|
2000-05-12 13:07:57 +04:00
|
|
|
class XMP
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Creates a new XMP object.
|
|
|
|
#
|
|
|
|
# The top-level binding or, optional +bind+ parameter will be used when
|
|
|
|
# creating the workspace. See WorkSpace.new for more information.
|
|
|
|
#
|
|
|
|
# This uses the +:XMP+ prompt mode, see IRB@Customizing+the+IRB+Prompt for
|
|
|
|
# full detail.
|
2000-05-12 13:07:57 +04:00
|
|
|
def initialize(bind = nil)
|
2002-07-09 15:17:17 +04:00
|
|
|
IRB.init_config(nil)
|
2000-05-12 13:07:57 +04:00
|
|
|
|
2002-07-09 15:17:17 +04:00
|
|
|
IRB.conf[:PROMPT_MODE] = :XMP
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
bind = IRB::Frame.top(1) unless bind
|
2002-07-09 15:17:17 +04:00
|
|
|
ws = IRB::WorkSpace.new(bind)
|
2000-05-12 13:07:57 +04:00
|
|
|
@io = StringInputMethod.new
|
2002-07-09 15:17:17 +04:00
|
|
|
@irb = IRB::Irb.new(ws, @io)
|
2000-05-12 13:07:57 +04:00
|
|
|
@irb.context.ignore_sigint = false
|
|
|
|
|
|
|
|
IRB.conf[:MAIN_CONTEXT] = @irb.context
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Evaluates the given +exps+, for example:
|
|
|
|
#
|
|
|
|
# require 'irb/xmp'
|
|
|
|
# x = XMP.new
|
|
|
|
#
|
|
|
|
# x.puts '{:a => 1, :b => 2, :c => 3}'
|
|
|
|
# #=> {:a => 1, :b => 2, :c => 3}
|
|
|
|
# # ==>{:a=>1, :b=>2, :c=>3}
|
|
|
|
# x.puts 'foo = "bar"'
|
|
|
|
# # => foo = "bar"
|
|
|
|
# # ==>"bar"
|
2000-05-12 13:07:57 +04:00
|
|
|
def puts(exps)
|
|
|
|
@io.puts exps
|
|
|
|
|
|
|
|
if @irb.context.ignore_sigint
|
|
|
|
begin
|
2014-08-09 05:36:49 +04:00
|
|
|
trap_proc_b = trap("SIGINT"){@irb.signal_handle}
|
|
|
|
catch(:IRB_EXIT) do
|
|
|
|
@irb.eval_input
|
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
ensure
|
2014-08-09 05:36:49 +04:00
|
|
|
trap("SIGINT", trap_proc_b)
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
catch(:IRB_EXIT) do
|
2014-08-09 05:36:49 +04:00
|
|
|
@irb.eval_input
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# A custom InputMethod class used by XMP for evaluating string io.
|
2000-05-12 13:07:57 +04:00
|
|
|
class StringInputMethod < IRB::InputMethod
|
2012-12-21 09:45:50 +04:00
|
|
|
# Creates a new StringInputMethod object
|
2000-05-12 13:07:57 +04:00
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
@exps = []
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Whether there are any expressions left in this printer.
|
2000-05-12 13:07:57 +04:00
|
|
|
def eof?
|
|
|
|
@exps.empty?
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Reads the next expression from this printer.
|
|
|
|
#
|
|
|
|
# See IO#gets for more information.
|
2000-05-12 13:07:57 +04:00
|
|
|
def gets
|
|
|
|
while l = @exps.shift
|
2014-08-09 05:36:49 +04:00
|
|
|
next if /^\s+$/ =~ l
|
|
|
|
l.concat "\n"
|
|
|
|
print @prompt, l
|
|
|
|
break
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|
|
|
|
l
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Concatenates all expressions in this printer, separated by newlines.
|
|
|
|
#
|
|
|
|
# An Encoding::CompatibilityError is raised of the given +exps+'s encoding
|
|
|
|
# doesn't match the previous expression evaluated.
|
2000-05-12 13:07:57 +04:00
|
|
|
def puts(exps)
|
2009-01-17 11:59:54 +03:00
|
|
|
if @encoding and exps.encoding != @encoding
|
2014-08-09 05:36:49 +04:00
|
|
|
enc = Encoding.compatible?(@exps.join("\n"), exps)
|
|
|
|
if enc.nil?
|
|
|
|
raise Encoding::CompatibilityError, "Encoding in which the passed expression is encoded is not compatible to the preceding's one"
|
|
|
|
else
|
|
|
|
@encoding = enc
|
|
|
|
end
|
2009-01-17 11:59:54 +03:00
|
|
|
else
|
2014-08-09 05:36:49 +04:00
|
|
|
@encoding = exps.encoding
|
2009-01-17 11:59:54 +03:00
|
|
|
end
|
2000-05-12 13:07:57 +04:00
|
|
|
@exps.concat exps.split(/\n/)
|
|
|
|
end
|
2009-01-17 11:59:54 +03:00
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# Returns the encoding of last expression printed by #puts.
|
2009-01-17 11:59:54 +03:00
|
|
|
attr_reader :encoding
|
2000-05-12 13:07:57 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 09:45:50 +04:00
|
|
|
# A convenience method that's only available when the you require the IRB::XMP standard library.
|
|
|
|
#
|
|
|
|
# Creates a new XMP object, using the given expressions as the +exps+
|
|
|
|
# parameter, and optional binding as +bind+ or uses the top-level binding. Then
|
|
|
|
# evaluates the given expressions using the +:XMP+ prompt mode.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# require 'irb/xmp'
|
|
|
|
# ctx = binding
|
|
|
|
# xmp 'foo = "bar"', ctx
|
|
|
|
# #=> foo = "bar"
|
|
|
|
# #==>"bar"
|
|
|
|
# ctx.eval 'foo'
|
|
|
|
# #=> "bar"
|
|
|
|
#
|
|
|
|
# See XMP.new for more information.
|
2000-05-12 13:07:57 +04:00
|
|
|
def xmp(exps, bind = nil)
|
|
|
|
bind = IRB::Frame.top(1) unless bind
|
|
|
|
xmp = XMP.new(bind)
|
|
|
|
xmp.puts exps
|
|
|
|
xmp
|
|
|
|
end
|