Skip to content

Commit

Permalink
display non-compactly arrays with only one column (#22981)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet authored Aug 3, 2017
1 parent 5e32423 commit fe09a7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
7 changes: 5 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,10 @@ end
function alignment(io::IO, x::Pair)
s = sprint(0, show, x, env=io)
if has_tight_type(x) # i.e. use "=>" for display
left = length(sprint(0, show, x.first, env=io)) + 2isa(x.first, Pair) # 2 for parens
iocompact = IOContext(io, :compact => get(io, :compact, true))
left = length(sprint(0, show, x.first, env=iocompact))
left += 2 * !isdelimited(iocompact, x.first) # for parens around p.first
left += !get(io, :compact, false) # spaces are added around "=>"
(left+1, length(s)-left-1) # +1 for the "=" part of "=>"
else
(0, length(s)) # as for x::Any
Expand Down Expand Up @@ -1741,7 +1744,7 @@ function showarray(io::IO, X::AbstractArray, repr::Bool = true; header = true)
if repr && ndims(X) == 1
return show_vector(io, X, "[", "]")
end
if !haskey(io, :compact)
if !haskey(io, :compact) && length(indices(X, 2)) > 1
io = IOContext(io, :compact => true)
end
if !repr && get(io, :limit, false) && eltype(X) === Method
Expand Down
8 changes: 4 additions & 4 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -969,10 +969,10 @@ end
@test sprint((io, x) -> show(io, MIME("text/plain"), x), a) ==
join([
"4-element Array{Complex{Float64},1}:",
" 1.0+1.0e-10im",
" 2.0e-15-2.0e-5im ",
" 1.0e-15+2.0im ",
" 1.0+2.0e-15im"], "\n")
" 1.0 + 1.0e-10im",
" 2.0e-15 - 2.0e-5im ",
" 1.0e-15 + 2.0im ",
" 1.0 + 2.0e-15im"], "\n")
end

@testset "corner cases of division, issue #22983" begin
Expand Down
30 changes: 27 additions & 3 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,37 @@ end
end

@testset "alignment for pairs" begin # (#22899)
@test replstr([1=>22,33=>4]) == "2-element Array{Pair{$Int,$Int},1}:\n 1=>22\n 33=>4 "
@test replstr([1=>22,33=>4]) == "2-element Array{Pair{$Int,$Int},1}:\n 1 => 22\n 33 => 4 "
# first field may have "=>" in its representation
@test replstr(Pair[(1=>2)=>3, 4=>5]) ==
"2-element Array{Pair,1}:\n (1=>2)=>3\n 4=>5"
"2-element Array{Pair,1}:\n (1=>2) => 3\n 4 => 5"
@test replstr(Any[Dict(1=>2)=> (3=>4), 1=>2]) ==
"2-element Array{Any,1}:\n Dict(1=>2)=>(3=>4)\n 1=>2 "
"2-element Array{Any,1}:\n Dict(1=>2) => (3=>4)\n 1 => 2 "
# left-alignment when not using the "=>" symbol
@test replstr(Pair{Integer,Int64}[1=>2, 33=>4]) ==
"2-element Array{Pair{Integer,Int64},1}:\n Pair{Integer,Int64}(1, 2) \n Pair{Integer,Int64}(33, 4)"
end

@testset "display arrays non-compactly when size(⋅, 2) == 1" begin
# 0-dim
@test replstr(zeros(Complex{Int})) == "0-dimensional Array{Complex{$Int},0}:\n0 + 0im"
A = Array{Pair}(); A[] = 1=>2
@test replstr(A) == "0-dimensional Array{Pair,0}:\n1 => 2"
# 1-dim
@test replstr(zeros(Complex{Int}, 2)) ==
"2-element Array{Complex{$Int},1}:\n 0 + 0im\n 0 + 0im"
@test replstr([1=>2, 3=>4]) == "2-element Array{Pair{$Int,$Int},1}:\n 1 => 2\n 3 => 4"
# 2-dim
@test replstr(zeros(Complex{Int}, 2, 1)) ==
"2×1 Array{Complex{$Int},2}:\n 0 + 0im\n 0 + 0im"
@test replstr(zeros(Complex{Int}, 1, 2)) ==
"1×2 Array{Complex{$Int},2}:\n 0+0im 0+0im"
@test replstr([1=>2 3=>4]) == "1×2 Array{Pair{$Int,$Int},2}:\n 1=>2 3=>4"
@test replstr([1=>2 for x in 1:2, y in 1:1]) ==
"2×1 Array{Pair{$Int,$Int},2}:\n 1 => 2\n 1 => 2"
# 3-dim
@test replstr(zeros(Complex{Int}, 1, 1, 1)) ==
"1×1×1 Array{Complex{$Int},3}:\n[:, :, 1] =\n 0 + 0im"
@test replstr(zeros(Complex{Int}, 1, 2, 1)) ==
"1×2×1 Array{Complex{$Int},3}:\n[:, :, 1] =\n 0+0im 0+0im"
end

0 comments on commit fe09a7b

Please sign in to comment.