mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
IRB colorize: take into account recursive arrays and hashes (#2555)
[Bug #16250]
This commit is contained in:
parent
ddd42d8ebd
commit
96617ad1d5
2 changed files with 20 additions and 4 deletions
|
@ -70,16 +70,30 @@ module IRB # :nodoc:
|
||||||
$stdout.tty? && supported? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
|
$stdout.tty? && supported? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect_colorable?(obj)
|
def inspect_colorable?(obj, seen = {})
|
||||||
case obj
|
case obj
|
||||||
when String, Symbol, Regexp, Integer, Float, FalseClass, TrueClass, NilClass
|
when String, Symbol, Regexp, Integer, Float, FalseClass, TrueClass, NilClass
|
||||||
true
|
true
|
||||||
when Hash
|
when Hash
|
||||||
obj.all? { |k, v| inspect_colorable?(k) && inspect_colorable?(v) }
|
if seen.has_key?(obj.object_id)
|
||||||
|
false
|
||||||
|
else
|
||||||
|
seen[obj.object_id] = true
|
||||||
|
colorable = obj.all? { |k, v| inspect_colorable?(k, seen) && inspect_colorable?(v, seen) }
|
||||||
|
seen.delete(obj.object_id)
|
||||||
|
colorable
|
||||||
|
end
|
||||||
when Array
|
when Array
|
||||||
obj.all? { |o| inspect_colorable?(o) }
|
if seen.has_key?(obj.object_id)
|
||||||
|
false
|
||||||
|
else
|
||||||
|
seen[obj.object_id] = true
|
||||||
|
colorable = obj.all? { |o| inspect_colorable?(o, seen) }
|
||||||
|
seen.delete(obj.object_id)
|
||||||
|
colorable
|
||||||
|
end
|
||||||
when Range
|
when Range
|
||||||
inspect_colorable?(obj.begin) && inspect_colorable?(obj.end)
|
inspect_colorable?(obj.begin, seen) && inspect_colorable?(obj.end, seen)
|
||||||
when Module
|
when Module
|
||||||
!obj.name.nil?
|
!obj.name.nil?
|
||||||
else
|
else
|
||||||
|
|
|
@ -125,6 +125,8 @@ module TestIRB
|
||||||
1 => true,
|
1 => true,
|
||||||
2.3 => true,
|
2.3 => true,
|
||||||
['foo', :bar] => true,
|
['foo', :bar] => true,
|
||||||
|
(a = []; a << a; a) => false,
|
||||||
|
(h = {}; h[h] = h; h) => false,
|
||||||
{ a: 4 } => true,
|
{ a: 4 } => true,
|
||||||
/reg/ => true,
|
/reg/ => true,
|
||||||
(1..3) => true,
|
(1..3) => true,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue