diff --git a/docs/src/conversion.md b/docs/src/conversion.md index e440b50d..d1363e76 100644 --- a/docs/src/conversion.md +++ b/docs/src/conversion.md @@ -146,13 +146,13 @@ julia> [1.0u"m", 2.0u"m"] julia> [1.0u"m", 2.0u"cm"] 2-element Vector{Quantity{Float64, š‹, Unitful.FreeUnits{(m,), š‹, nothing}}}: - 1.0 m + 1.0 m 0.02 m julia> [1.0u"m", 2.0] 2-element Vector{Quantity{Float64}}: 1.0 m - 2.0 + 2.0 ``` In the first case, an array with a concrete type is created. Good diff --git a/src/display.jl b/src/display.jl index c85af809..b899382b 100644 --- a/src/display.jl +++ b/src/display.jl @@ -252,3 +252,19 @@ superscript(i::Integer) = map(repr(i)) do c c == '0' ? '\u2070' : error("unexpected character") end + +if isdefined(Base, :alignment_from_show) + printed_length(io, x) = Base.alignment_from_show(io, x) +else + printed_length(io, x) = length(sprint(show, x, context=io)) +end + +function Base.alignment(io::IO, x::Quantity) + if isunitless(unit(x)) + return Base.alignment(io, x.val) + end + length = printed_length(io, x) + left, _ = Base.alignment(io, x.val) + left += BracketStyle(x.val) != NoBrackets() + return left, length - left +end diff --git a/src/logarithm.jl b/src/logarithm.jl index 86a717e3..9abb399b 100644 --- a/src/logarithm.jl +++ b/src/logarithm.jl @@ -315,6 +315,17 @@ function Base.show(io::IO, x::Level) nothing end +function Base.alignment(io::IO, x::Gain) + length = printed_length(io, x) + left, _ = Base.alignment(io, x.val) + return left, length - left +end +function Base.alignment(io::IO, x::Level) + length = printed_length(io, x) + left, _ = Base.alignment(io, ustrip(x)) + return left, length - left +end + BracketStyle(::Type{<:Union{Level,Gain}}) = SquareBrackets() """ diff --git a/test/runtests.jl b/test/runtests.jl index dd32a39d..8b905fc9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1728,6 +1728,12 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0") @test repr(1.0 * u"m * s * kg^(-1//2)") == (Sys.isapple() ? "1.0 m s kgā»Ā¹įŸĀ²" : "1.0 m s kg^-1/2") end + @test Base.alignment(stdout, (1//3)m) == Base.alignment(stdout, 1//3) .+ (0, 2) + @test Base.alignment(stdout, (2+3im)m) == Base.alignment(stdout, 2+3im) .+ (1, 3) + @test Base.alignment(stdout, 3.0dB) == Base.alignment(stdout, 3.0) .+ (0, 3) + @test Base.alignment(stdout, 3.0dBm) == Base.alignment(stdout, 3.0) .+ (0, 4) + @test Base.alignment(stdout, 3.0dB*s) == Base.alignment(stdout, 3.0) .+ (1, 6) + @test Base.alignment(stdout, 3.0dBm*s) == Base.alignment(stdout, 3.0) .+ (1, 7) end @testset "DimensionError message" begin