From 1f19b8fd41aed3b24dda074bd10fab3c81085df2 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:03:54 +0100 Subject: [PATCH] Fix printing of `StepRangeLen` with complex elements (#513) --- src/display.jl | 12 +++++++++++- test/runtests.jl | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/display.jl b/src/display.jl index 5dfc27f2..274f555e 100644 --- a/src/display.jl +++ b/src/display.jl @@ -131,7 +131,7 @@ function show(io::IO, mime::MIME"text/plain", x::Quantity) end end -function show(io::IO, r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity +function show(io::IO, r::StepRange{T}) where T<:Quantity a,s,b = first(r), step(r), last(r) U = unit(a) print(io, '(') @@ -145,6 +145,16 @@ function show(io::IO, r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity show(io, U) end +function show(io::IO, r::StepRangeLen{T}) where T<:Quantity + a,s,b = first(r), step(r), last(r) + U = unit(a) + print(io, '(') + show(io, StepRangeLen(ustrip(U, a), ustrip(U, s), length(r))) + print(io, ')') + has_unit_spacing(U) && print(io,' ') + show(io, U) +end + function show(io::IO, x::typeof(NoDims)) print(io, "NoDims") end diff --git a/test/runtests.jl b/test/runtests.jl index 405167fa..f68ebd12 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1451,6 +1451,8 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") @test repr((1:10)*u"kg/m^3") == "(1:10) kg m^-3" @test repr((1.0:0.1:10.0)*u"kg/m^3") == "(1.0:0.1:10.0) kg m^-3" @test repr((1:10)*°) == "(1:10)°" + @test repr(range(1.0+2.0im, length=5)*u"m") == "(1.0 + 2.0im:1.0 + 0.0im:5.0 + 2.0im) m" + @test repr(range(1+2im, step=1+1im, length=5)*u"m") == "(1 + 2im:1 + 1im:5 + 6im) m" end withenv("UNITFUL_FANCY_EXPONENTS" => true) do @test repr(1.0 * u"m * s * kg^(-1//2)") == "1.0 m s kg⁻¹ᐟ²"