mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Colorize IRB's inspect result
Closes: https://github.com/ruby/ruby/pull/2150
This commit is contained in:
parent
94af6cd383
commit
b55201dd09
3 changed files with 39 additions and 2 deletions
|
@ -41,6 +41,19 @@ module IRB # :nodoc:
|
||||||
$stdout.tty? && ENV.key?('TERM')
|
$stdout.tty? && ENV.key?('TERM')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def inspect_colorable?(obj)
|
||||||
|
case obj
|
||||||
|
when Hash
|
||||||
|
obj.all? { |k, v| inspect_colorable?(k) && inspect_colorable?(v) }
|
||||||
|
when Array
|
||||||
|
obj.all? { |o| inspect_colorable?(o) }
|
||||||
|
when String, Symbol, Integer, Float, FalseClass, TrueClass, NilClass
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def clear
|
def clear
|
||||||
return '' unless colorable?
|
return '' unless colorable?
|
||||||
"\e[#{CLEAR}m"
|
"\e[#{CLEAR}m"
|
||||||
|
|
|
@ -106,12 +106,22 @@ module IRB # :nodoc:
|
||||||
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
|
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
|
||||||
Inspector.def_inspector([true, :p, :inspect]){|v|
|
Inspector.def_inspector([true, :p, :inspect]){|v|
|
||||||
begin
|
begin
|
||||||
v.inspect
|
result = v.inspect
|
||||||
|
if Color.inspect_colorable?(v)
|
||||||
|
result = Color.colorize_code(result)
|
||||||
|
end
|
||||||
|
result
|
||||||
rescue NoMethodError
|
rescue NoMethodError
|
||||||
puts "(Object doesn't support #inspect)"
|
puts "(Object doesn't support #inspect)"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v| v.pretty_inspect.chomp}
|
Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v|
|
||||||
|
result = v.pretty_inspect.chomp
|
||||||
|
if Color.inspect_colorable?(v)
|
||||||
|
result = Color.colorize_code(result)
|
||||||
|
end
|
||||||
|
result
|
||||||
|
}
|
||||||
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
|
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
|
||||||
begin
|
begin
|
||||||
YAML.dump(v)
|
YAML.dump(v)
|
||||||
|
|
|
@ -26,5 +26,19 @@ module TestIRB
|
||||||
assert_equal(result, IRB::Color.colorize_code(code))
|
assert_equal(result, IRB::Color.colorize_code(code))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_inspect_colorable
|
||||||
|
{
|
||||||
|
1 => true,
|
||||||
|
2.3 => true,
|
||||||
|
['foo', :bar] => true,
|
||||||
|
{ a: 4 } => true,
|
||||||
|
Object.new => false,
|
||||||
|
Struct.new(:a) => false,
|
||||||
|
Struct.new(:a).new(1) => false,
|
||||||
|
}.each do |object, result|
|
||||||
|
assert_equal(result, IRB::Color.inspect_colorable?(object))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue