Skip to content

Commit

Permalink
More concise printing of some standard Range types
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Jul 19, 2019
1 parent 2d44add commit b4a8ef5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ function show(io::IO, x::Type{T}) where T<:Unitlike
invoke(show, Tuple{IO, typeof(x)}, IOContext(io, :showoperators=>true), x)
end

function show(io::IO, r::Union{StepRange{T},StepRangeLen{T}}) where T<:Quantity
a,s,b = first(r), step(r), last(r)
U = unit(a)
print(io, '(')
if ustrip(U, s) == 1
show(io, ustrip(U, a):ustrip(U, b))
else
show(io, ustrip(U, a):ustrip(U, s):ustrip(U, b))
end
print(io, ')')
has_unit_spacing(U) && print(io,' ')
show(io, U)
end

function show(io::IO, x::typeof(NoDims))
print(io, "NoDims")
end
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,11 @@ Base.show(io::IO, ::MIME"text/plain", ::Foo) = print(io, "42.0")
# Angular degree printing #253
@test sprint(show, 1.0°) == "1.0°"
@test repr("text/plain", 1.0°) == "1.0°"

# Concise printing of ranges
@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)°"
end

@testset "DimensionError message" begin
Expand Down

0 comments on commit b4a8ef5

Please sign in to comment.