From 8012207dc1305e942d467b990aa8fe554cc206d1 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Fri, 29 Mar 2019 15:40:07 -0500 Subject: [PATCH] Use compact element printing (#195) * Use compact element printing * Remove old comments --- src/io.jl | 16 ++++++++++++---- test/io.jl | 6 +++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/io.jl b/src/io.jl index bece818be..e91781853 100644 --- a/src/io.jl +++ b/src/io.jl @@ -92,9 +92,7 @@ function Base.show(io::IO, tz::VariableTimeZone) end function Base.show(io::IO, t::Transition) - # Note: Using combo of `:typeinfo` and `:limit` as a way of detecting when a vector of - # transitions is being printed in the REPL. - if get(io, :compact, false) || get(io, :typeinfo, Union{}) == Transition && get(io, :limit, false) + if get(io, :compact, false) || get(io, :compact_el, false) print(io, t) else # Fallback to calling the default show instead of reimplementing it. @@ -103,7 +101,7 @@ function Base.show(io::IO, t::Transition) end function Base.show(io::IO, zdt::ZonedDateTime) - if get(io, :compact, false) + if get(io, :compact, false) || get(io, :compact_el, false) print(io, zdt) else values = [ @@ -127,3 +125,13 @@ end Base.show(io::IO, ::MIME"text/plain", t::Transition) = print(io, t) Base.show(io::IO, ::MIME"text/plain", tz::TimeZone) = print(IOContext(io, :compact => false), tz) Base.show(io::IO, ::MIME"text/plain", zdt::ZonedDateTime) = print(io, zdt) + +# Use compact printing on certain element types +for T in (:Transition, :ZonedDateTime) + @eval function Base.show(io::IO, m::MIME"text/plain", X::AbstractArray{$T}) + # Specify a custom IO context which we can use to perform compact printing of + # elements. + io = IOContext(io, :compact_el => true) + invoke(show, Tuple{IO, MIME"text/plain", AbstractArray}, io, m, X) + end +end diff --git a/test/io.jl b/test/io.jl index 12325770d..7ce908e07 100644 --- a/test/io.jl +++ b/test/io.jl @@ -145,7 +145,7 @@ zdt = ZonedDateTime(dt, warsaw) @testset "REPL vector" begin expected_full = string( - TimeZones.Transition, + Transition, "[", join(map(t -> sprint(show, t, context=:compact => false), transitions), ", "), "]", @@ -154,13 +154,13 @@ zdt = ZonedDateTime(dt, warsaw) # Note: The output here is different from the interactive REPL but is representative # of the output. expected_repl = string( - TimeZones.Transition, + Transition, "[", join(map(t -> sprint(show, t, context=:compact => true), transitions), ", "), "]", ) @test sprint(show, transitions, context=:compact => false) == expected_full - @test sprint(show, transitions; context=:limit => true) == expected_repl + @test sprint(show, transitions; context=:compact_el => true) == expected_repl end end