Skip to content

Commit fe09a7b

Browse files
authored
display non-compactly arrays with only one column (#22981)
1 parent 5e32423 commit fe09a7b

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

base/show.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,10 @@ end
13871387
function alignment(io::IO, x::Pair)
13881388
s = sprint(0, show, x, env=io)
13891389
if has_tight_type(x) # i.e. use "=>" for display
1390-
left = length(sprint(0, show, x.first, env=io)) + 2isa(x.first, Pair) # 2 for parens
1390+
iocompact = IOContext(io, :compact => get(io, :compact, true))
1391+
left = length(sprint(0, show, x.first, env=iocompact))
1392+
left += 2 * !isdelimited(iocompact, x.first) # for parens around p.first
1393+
left += !get(io, :compact, false) # spaces are added around "=>"
13911394
(left+1, length(s)-left-1) # +1 for the "=" part of "=>"
13921395
else
13931396
(0, length(s)) # as for x::Any
@@ -1741,7 +1744,7 @@ function showarray(io::IO, X::AbstractArray, repr::Bool = true; header = true)
17411744
if repr && ndims(X) == 1
17421745
return show_vector(io, X, "[", "]")
17431746
end
1744-
if !haskey(io, :compact)
1747+
if !haskey(io, :compact) && length(indices(X, 2)) > 1
17451748
io = IOContext(io, :compact => true)
17461749
end
17471750
if !repr && get(io, :limit, false) && eltype(X) === Method

test/complex.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,10 +969,10 @@ end
969969
@test sprint((io, x) -> show(io, MIME("text/plain"), x), a) ==
970970
join([
971971
"4-element Array{Complex{Float64},1}:",
972-
" 1.0+1.0e-10im",
973-
" 2.0e-15-2.0e-5im ",
974-
" 1.0e-15+2.0im ",
975-
" 1.0+2.0e-15im"], "\n")
972+
" 1.0 + 1.0e-10im",
973+
" 2.0e-15 - 2.0e-5im ",
974+
" 1.0e-15 + 2.0im ",
975+
" 1.0 + 2.0e-15im"], "\n")
976976
end
977977

978978
@testset "corner cases of division, issue #22983" begin

test/show.jl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,13 +846,37 @@ end
846846
end
847847

848848
@testset "alignment for pairs" begin # (#22899)
849-
@test replstr([1=>22,33=>4]) == "2-element Array{Pair{$Int,$Int},1}:\n 1=>22\n 33=>4 "
849+
@test replstr([1=>22,33=>4]) == "2-element Array{Pair{$Int,$Int},1}:\n 1 => 22\n 33 => 4 "
850850
# first field may have "=>" in its representation
851851
@test replstr(Pair[(1=>2)=>3, 4=>5]) ==
852-
"2-element Array{Pair,1}:\n (1=>2)=>3\n 4=>5"
852+
"2-element Array{Pair,1}:\n (1=>2) => 3\n 4 => 5"
853853
@test replstr(Any[Dict(1=>2)=> (3=>4), 1=>2]) ==
854-
"2-element Array{Any,1}:\n Dict(1=>2)=>(3=>4)\n 1=>2 "
854+
"2-element Array{Any,1}:\n Dict(1=>2) => (3=>4)\n 1 => 2 "
855855
# left-alignment when not using the "=>" symbol
856856
@test replstr(Pair{Integer,Int64}[1=>2, 33=>4]) ==
857857
"2-element Array{Pair{Integer,Int64},1}:\n Pair{Integer,Int64}(1, 2) \n Pair{Integer,Int64}(33, 4)"
858858
end
859+
860+
@testset "display arrays non-compactly when size(⋅, 2) == 1" begin
861+
# 0-dim
862+
@test replstr(zeros(Complex{Int})) == "0-dimensional Array{Complex{$Int},0}:\n0 + 0im"
863+
A = Array{Pair}(); A[] = 1=>2
864+
@test replstr(A) == "0-dimensional Array{Pair,0}:\n1 => 2"
865+
# 1-dim
866+
@test replstr(zeros(Complex{Int}, 2)) ==
867+
"2-element Array{Complex{$Int},1}:\n 0 + 0im\n 0 + 0im"
868+
@test replstr([1=>2, 3=>4]) == "2-element Array{Pair{$Int,$Int},1}:\n 1 => 2\n 3 => 4"
869+
# 2-dim
870+
@test replstr(zeros(Complex{Int}, 2, 1)) ==
871+
"2×1 Array{Complex{$Int},2}:\n 0 + 0im\n 0 + 0im"
872+
@test replstr(zeros(Complex{Int}, 1, 2)) ==
873+
"1×2 Array{Complex{$Int},2}:\n 0+0im 0+0im"
874+
@test replstr([1=>2 3=>4]) == "1×2 Array{Pair{$Int,$Int},2}:\n 1=>2 3=>4"
875+
@test replstr([1=>2 for x in 1:2, y in 1:1]) ==
876+
"2×1 Array{Pair{$Int,$Int},2}:\n 1 => 2\n 1 => 2"
877+
# 3-dim
878+
@test replstr(zeros(Complex{Int}, 1, 1, 1)) ==
879+
"1×1×1 Array{Complex{$Int},3}:\n[:, :, 1] =\n 0 + 0im"
880+
@test replstr(zeros(Complex{Int}, 1, 2, 1)) ==
881+
"1×2×1 Array{Complex{$Int},3}:\n[:, :, 1] =\n 0+0im 0+0im"
882+
end

0 commit comments

Comments
 (0)