Skip to content

Commit

Permalink
Use compact element printing (#195)
Browse files Browse the repository at this point in the history
* Use compact element printing

* Remove old comments
  • Loading branch information
omus authored Mar 29, 2019
1 parent c57e8e3 commit 8012207
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = [
Expand All @@ -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
6 changes: 3 additions & 3 deletions test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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), ", "),
"]",
Expand All @@ -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

0 comments on commit 8012207

Please sign in to comment.