1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/irb/inspector.rb
Stan Lo 44c1316293 [ruby/irb] Centralize coloring control (https://github.com/ruby/irb/pull/374)
* Use colorable: argument as the only coloring control

* Centalize color controling logic at Color.colorable?

There are 2 requirements for coloring output:

1. It's supported on the platform
2. The user wants it: `IRB.conf[:USE_COLORIZE] == true`

Right now we check 1 and 2 separately whenever we colorize things.
But it's error-prone because while 1 is the default of `colorable`
parameter, 2 always need to manually checked. When 2 is overlooked, it
causes issues like https://github.com/ruby/irb/pull/362

And there's 0 case where we may want to colorize even when the user
disables it. So I think we should merge 2 into `Color.colorable?` so it
can be automatically picked up.

* Add tests for all inspect modes

* Simplify inspectors' coloring logic

* Replace use_colorize? with Color.colorable?

* Remove Context#use_colorize cause it's redundant

https://github.com/ruby/irb/commit/1c53023ac4
2022-06-28 22:30:42 +09:00

128 lines
3.7 KiB
Ruby

# frozen_string_literal: false
#
# irb/inspector.rb - inspect methods
# $Release Version: 0.9.6$
# $Revision: 1.19 $
# $Date: 2002/06/11 07:51:31 $
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
#
#
module IRB # :nodoc:
# Convenience method to create a new Inspector, using the given +inspect+
# proc, and optional +init+ proc and passes them to Inspector.new
#
# irb(main):001:0> ins = IRB::Inspector(proc{ |v| "omg! #{v}" })
# irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
# irb(main):001:0> "what?" #=> omg! what?
#
def IRB::Inspector(inspect, init = nil)
Inspector.new(inspect, init)
end
# An irb inspector
#
# In order to create your own custom inspector there are two things you
# should be aware of:
#
# Inspector uses #inspect_value, or +inspect_proc+, for output of return values.
#
# This also allows for an optional #init+, or +init_proc+, which is called
# when the inspector is activated.
#
# Knowing this, you can create a rudimentary inspector as follows:
#
# irb(main):001:0> ins = IRB::Inspector.new(proc{ |v| "omg! #{v}" })
# irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
# irb(main):001:0> "what?" #=> omg! what?
#
class Inspector
# Default inspectors available to irb, this includes:
#
# +:pp+:: Using Kernel#pretty_inspect
# +:yaml+:: Using YAML.dump
# +:marshal+:: Using Marshal.dump
INSPECTORS = {}
# Determines the inspector to use where +inspector+ is one of the keys passed
# during inspector definition.
def self.keys_with_inspector(inspector)
INSPECTORS.select{|k,v| v == inspector}.collect{|k, v| k}
end
# Example
#
# Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
# Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
# Inspector.def_inspector(key, inspector)
# Inspector.def_inspector([key1,...], inspector)
def self.def_inspector(key, arg=nil, &block)
if block_given?
inspector = IRB::Inspector(block, arg)
else
inspector = arg
end
case key
when Array
for k in key
def_inspector(k, inspector)
end
when Symbol
INSPECTORS[key] = inspector
INSPECTORS[key.to_s] = inspector
when String
INSPECTORS[key] = inspector
INSPECTORS[key.intern] = inspector
else
INSPECTORS[key] = inspector
end
end
# Creates a new inspector object, using the given +inspect_proc+ when
# output return values in irb.
def initialize(inspect_proc, init_proc = nil)
@init = init_proc
@inspect = inspect_proc
end
# Proc to call when the inspector is activated, good for requiring
# dependent libraries.
def init
@init.call if @init
end
# Proc to call when the input is evaluated and output in irb.
def inspect_value(v)
@inspect.call(v)
rescue
puts "(Object doesn't support #inspect)"
''
end
end
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
Inspector.def_inspector([:p, :inspect]){|v|
Color.colorize_code(v.inspect, colorable: Color.colorable? && Color.inspect_colorable?(v))
}
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require_relative "color_printer"}){|v|
IRB::ColorPrinter.pp(v, '').chomp
}
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
begin
YAML.dump(v)
rescue
puts "(can't dump yaml. use inspect)"
v.inspect
end
}
Inspector.def_inspector([:marshal, :Marshal, :MARSHAL, Marshal]){|v|
Marshal.dump(v)
}
end