Skip to content

Commit 86bf322

Browse files
authored
Merge 9caac72 into 492c475
2 parents 492c475 + 9caac72 commit 86bf322

File tree

5 files changed

+55
-46
lines changed

5 files changed

+55
-46
lines changed

src/display.jl

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ function prefix(x::Unit)
4444
end
4545
end
4646

47-
function show(io::IO, x::Unit{N,D}) where {N,D}
48-
show(io, FreeUnits{(x,), D, nothing}())
49-
end
50-
5147
abstract type BracketStyle end
5248

5349
struct NoBrackets <: BracketStyle end
@@ -89,7 +85,7 @@ brackets are not printed.
8985
"""
9086
function showval(io::IO, x::Number, brackets::Bool=true)
9187
brackets && print_opening_bracket(io, x)
92-
show(io, x)
88+
show(io, MIME"text/plain"(), x)
9389
brackets && print_closing_bracket(io, x)
9490
end
9591

@@ -107,20 +103,10 @@ has_unit_spacing(u) = true
107103
has_unit_spacing(u::Units{(Unit{:Degree, NoDims}(0, 1//1),), NoDims}) = false
108104

109105
"""
110-
show(io::IO, x::Quantity)
106+
show(io::IO, mime::MIME"text/plain", x::Quantity)
111107
Show a unitful quantity by calling [`showval`](@ref) on the numeric value, appending a
112108
space, and then calling `show` on a units object `U()`.
113109
"""
114-
function show(io::IO, x::Quantity)
115-
if isunitless(unit(x))
116-
showval(io, x.val, false)
117-
else
118-
showval(io, x.val, true)
119-
has_unit_spacing(unit(x)) && print(io, ' ')
120-
show(io, unit(x))
121-
end
122-
end
123-
124110
function show(io::IO, mime::MIME"text/plain", x::Quantity)
125111
if isunitless(unit(x))
126112
showval(io, mime, x.val, false)
@@ -131,30 +117,30 @@ function show(io::IO, mime::MIME"text/plain", x::Quantity)
131117
end
132118
end
133119

134-
function show(io::IO, r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity
120+
function show(io::IO, mime::MIME"text/plain", r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity
135121
a,s,b = first(r), step(r), last(r)
136122
U = unit(a)
137123
print(io, '(')
138124
if ustrip(U, s) == 1
139-
show(io, ustrip(U, a):ustrip(U, b))
125+
show(io, mime, ustrip(U, a):ustrip(U, b))
140126
else
141-
show(io, ustrip(U, a):ustrip(U, s):ustrip(U, b))
127+
show(io, mime, ustrip(U, a):ustrip(U, s):ustrip(U, b))
142128
end
143129
print(io, ')')
144130
has_unit_spacing(U) && print(io,' ')
145-
show(io, U)
131+
show(io, mime, U)
146132
end
147133

148-
function show(io::IO, x::typeof(NoDims))
134+
function show(io::IO, ::MIME"text/plain", x::typeof(NoDims))
149135
print(io, "NoDims")
150136
end
151137

152138
"""
153-
show(io::IO, x::Unitlike)
139+
show(io::IO, ::MIME"text/plain", x::Unitlike)
154140
Call [`Unitful.showrep`](@ref) on each object in the tuple that is the type
155141
variable of a [`Unitful.Units`](@ref) or [`Unitful.Dimensions`](@ref) object.
156142
"""
157-
function show(io::IO, x::Unitlike)
143+
function show(io::IO, ::MIME"text/plain", x::Unitlike)
158144
showoperators = get(io, :showoperators, false)
159145
first = ""
160146
sep = showoperators ? "*" : " "
@@ -241,3 +227,12 @@ superscript(i::Integer) = map(repr(i)) do c
241227
c == '0' ? '\u2070' :
242228
error("unexpected character")
243229
end
230+
231+
Base.print(io::IO, x::AbstractQuantity) = show(io, "text/plain", x)
232+
Base.print(io::IO, x::Dimension) = show(io, "text/plain", x)
233+
Base.print(io::IO, x::Unit) = show(io, "text/plain", x)
234+
Base.print(io::IO, x::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity = show(io, "text/plain", x)
235+
Base.print(io::IO, x::Unitlike) = show(io, "text/plain", x)
236+
Base.print(io::IO, x::MixedUnits) = show(io, "text/plain", x)
237+
Base.print(io::IO, x::LogScaled) = show(io, "text/plain", x)
238+
Base.print(io::IO, x::IsRootPowerRatio) = show(io, "text/plain", x)

src/logarithm.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ tolog(L,x) = (1+isrootpower(L)) * prefactor(L()) * (logfn(L()))(x)
8484
fromlog(L,S,x) = unwrap(S) * expfn(L())( x / ((1+isrootpower(S))*prefactor(L())) )
8585
fromlog(L,x) = expfn(L())( x / ((1+isrootpower(L))*prefactor(L())) )
8686

87-
function Base.show(io::IO, x::MixedUnits{T,U}) where {T,U}
87+
function Base.show(io::IO, mime::MIME"text/plain", x::MixedUnits{T,U}) where {T,U}
8888
print(io, abbr(x))
8989
if x.units != NoUnits
9090
print(io, " ")
91-
show(io, x.units)
91+
show(io, mime, x.units)
9292
end
9393
end
9494

@@ -312,11 +312,11 @@ function Base.promote_rule(::Type{G}, ::Type{N}) where {L,S,T1, G<:Gain{L,S,T1},
312312
end
313313
Base.promote_rule(A::Type{G}, B::Type{L}) where {G<:Gain, L2, L<:Level{L2}} = LogScaled{L2}
314314

315-
function Base.show(io::IO, x::Gain)
315+
function Base.show(io::IO, mime::MIME"text/plain", x::Gain)
316316
print(io, x.val, " ", abbr(x))
317317
nothing
318318
end
319-
function Base.show(io::IO, x::Level)
319+
function Base.show(io::IO, mime::MIME"text/plain", x::Level)
320320
print(io, ustrip(x), " ", abbr(x))
321321
nothing
322322
end

src/types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ struct IsRootPowerRatio{S,T}
273273
val::T
274274
end
275275
IsRootPowerRatio{S}(x) where {S} = IsRootPowerRatio{S, typeof(x)}(x)
276-
Base.show(io::IO, x::IsRootPowerRatio{S}) where {S} =
276+
Base.show(io::IO, ::MIME"text/plain", x::IsRootPowerRatio{S}) where {S} =
277277
print(io, ifelse(S, "root-power ratio", "power ratio"), " with reference ", x.val)
278278
const PowerRatio{T} = IsRootPowerRatio{false,T}
279279
const RootPowerRatio{T} = IsRootPowerRatio{true,T}

src/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct DimensionError <: Exception
236236
end
237237

238238
Base.showerror(io::IO, e::DimensionError) =
239-
print(io, "DimensionError: $(e.x) and $(e.y) are not dimensionally compatible.");
239+
print(io, "DimensionError: ", e.x, " and ", e.y, " are not dimensionally compatible.");
240240

241241
"""
242242
struct AffineError <: Exception
@@ -246,4 +246,4 @@ struct AffineError <: Exception
246246
x
247247
end
248248

249-
Base.showerror(io::IO, e::AffineError) = print(io, "AffineError: $(e.x)")
249+
Base.showerror(io::IO, e::AffineError) = print(io, "AffineError: ", e.x)

test/runtests.jl

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,21 +1291,21 @@ end
12911291
withenv("UNITFUL_FANCY_EXPONENTS" => false) do
12921292
@static if VERSION v"1.6.0-DEV.770"
12931293
@test string(typeof(1.0m/s)) ==
1294-
"Quantity{Float64, 𝐋 𝐓^-1, FreeUnits{(m, s^-1), 𝐋 𝐓^-1, nothing}}"
1294+
"Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}"
12951295
@test string(typeof(m/s)) ==
1296-
"FreeUnits{(m, s^-1), 𝐋 𝐓^-1, nothing}"
1296+
"FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}"
12971297
else
12981298
@test string(typeof(1.0m/s)) ==
1299-
"Quantity{Float64,𝐋 𝐓^-1,FreeUnits{(m, s^-1),𝐋 𝐓^-1,nothing}}"
1299+
"Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}"
13001300
@test string(typeof(m/s)) ==
1301-
"FreeUnits{(m, s^-1),𝐋 𝐓^-1,nothing}"
1301+
"FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}"
13021302
end
13031303
@test string(dimension(1u"m/s")) == "𝐋 𝐓^-1"
13041304
@test string(NoDims) == "NoDims"
13051305
end
13061306
@testset ":fancy_exponent IOContext property" begin
1307-
@test sprint(io -> show(IOContext(io, :fancy_exponent => true), u"m/s")) == "m s⁻¹"
1308-
@test sprint(io -> show(IOContext(io, :fancy_exponent => false), u"m/s")) == "m s^-1"
1307+
@test sprint(print, u"m/s", context = :fancy_exponent => true) == "m s⁻¹"
1308+
@test sprint(print, u"m/s", context = :fancy_exponent => false) == "m s^-1"
13091309
end
13101310
end
13111311

@@ -1315,31 +1315,45 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0")
13151315

13161316
@testset "Show quantities" begin
13171317
withenv("UNITFUL_FANCY_EXPONENTS" => false) do
1318-
@test repr(1.0 * u"m * s * kg^-1") == "1.0 m s kg^-1"
1318+
@test repr(1.0 * u"m * s * kg^-1") == "Quantity{Float64, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1.0)"
13191319
@test repr("text/plain", 1.0 * u"m * s * kg^-1") == "1.0 m s kg^-1"
1320-
@test repr(Foo() * u"m * s * kg^-1") == "1 m s kg^-1"
1320+
@test repr(Foo() * u"m * s * kg^-1") == "Quantity{Foo, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), FreeUnits{(Unitful.Unit{:Gram, Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}()}(3, -1//1), Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, 1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Mass}(-1//1), Unitful.Dimension{:Time}(1//1))}(), nothing}}(1)"
13211321
@test repr("text/plain", Foo() * u"m * s * kg^-1") == "42.0 m s kg^-1"
13221322

13231323
# Complex quantities
1324-
@test repr((1+2im) * u"m/s") == "(1 + 2im) m s^-1"
1324+
@test repr((1+2im) * u"m/s") == "Quantity{Complex{Int64}, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), FreeUnits{(Unitful.Unit{:Meter, Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}()}(0, 1//1), Unitful.Unit{:Second, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}()}(0, -1//1)), Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1), Unitful.Dimension{:Time}(-1//1))}(), nothing}}(1 + 2im)"
13251325
@test repr("text/plain", (1+2im) * u"m/s") == "(1 + 2im) m s^-1"
13261326

13271327
# Angular degree printing #253
1328-
@test sprint(show, 1.0°) == "1.0°"
1328+
@test sprint(show, 1.0°) == "Quantity{Float64, Unitful.Dimensions{()}(), FreeUnits{(Unitful.Unit{:Degree, Unitful.Dimensions{()}()}(0, 1//1),), Unitful.Dimensions{()}(), nothing}}(1.0)"
13291329
@test repr("text/plain", 1.0°) == "1.0°"
13301330

13311331
# Concise printing of ranges
1332-
@test repr((1:10)*u"kg/m^3") == "(1:10) kg m^-3"
1333-
@test repr((1.0:0.1:10.0)*u"kg/m^3") == "(1.0:0.1:10.0) kg m^-3"
1334-
@test repr((1:10)*°) == "(1:10)°"
1332+
@test repr("text/plain", (1:10)*u"kg/m^3") == "(1:10) kg m^-3"
1333+
@test repr("text/plain", (1.0:0.1:10.0)*u"kg/m^3") == "(1.0:0.1:10.0) kg m^-3"
1334+
@test repr("text/plain", (1:10)*°) == "(1:10)°"
13351335
end
13361336
withenv("UNITFUL_FANCY_EXPONENTS" => true) do
1337-
@test repr(1.0 * u"m * s * kg^(-1//2)") == "1.0 m s kg⁻¹ᐟ²"
1337+
@test repr("text/plain", 1.0 * u"m * s * kg^(-1//2)") == "1.0 m s kg⁻¹ᐟ²"
13381338
end
13391339
withenv("UNITFUL_FANCY_EXPONENTS" => nothing) do
1340-
@test repr(1.0 * u"m * s * kg^(-1//2)") ==
1340+
@test repr("text/plain", 1.0 * u"m * s * kg^(-1//2)") ==
13411341
(Sys.isapple() ? "1.0 m s kg⁻¹ᐟ²" : "1.0 m s kg^-1/2")
13421342
end
1343+
1344+
@testset "roundtripping show" begin
1345+
u = u"m/s"
1346+
u2 = eval(Meta.parse(repr(u)))
1347+
@test u == u2
1348+
1349+
q = Quantity(5, u"m")
1350+
q2 = eval(Meta.parse(repr(q)))
1351+
@test q == q2
1352+
1353+
d = u"𝐌*𝐋/𝐓^2"
1354+
d2 = eval(Meta.parse(repr(d)))
1355+
@test d == d2
1356+
end
13431357
end
13441358

13451359
@testset "DimensionError message" begin
@@ -1673,7 +1687,7 @@ end
16731687

16741688
@testset "> Display" begin
16751689
withenv("UNITFUL_FANCY_EXPONENTS" => false) do
1676-
@test repr(3u"dB/Hz") == "[3 dB] Hz^-1"
1690+
@test repr(3u"dB/Hz") == "Quantity{Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}, Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), FreeUnits{(Unitful.Unit{:Hertz, Unitful.Dimensions{(Unitful.Dimension{:Time}(-1//1),)}()}(0, -1//1),), Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}(), nothing}}(Gain{LogInfo{:Decibel, 10, 10}, :?, Int64}(3))"
16771691
@test repr("text/plain", 3u"dB/Hz") == "[3 dB] Hz^-1"
16781692
end
16791693
@test Unitful.abbr(3u"dBm") == "dBm"

0 commit comments

Comments
 (0)