Skip to content

Commit c44ee61

Browse files
committed
make ▒▒▒ much harder to encounter
1 parent 1eb9e69 commit c44ee61

3 files changed

Lines changed: 43 additions & 34 deletions

File tree

src/sparsematrix.jl

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,17 @@ function Base.show(io::IO, ::MIME"text/plain", S::AbstractSparseMatrixCSCInclAdj
350350
summary(io, S)
351351
isempty(S) && return
352352

353-
screen = displaysize(io)
354-
get(io, :limit, false) && screen[1] <= 4 && return print(io, ": …")
353+
if get(io, :limit, false)
354+
screen = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
355+
screen[1] <= 4 && return print(io, ": …")
356+
else
357+
screen = typemax(Int), typemax(Int)
358+
end
355359

356360
show_circular(io, S) && return
357361
io = IOContext(io, :compact=>true, :typeinfo=>eltype(S), :SHOWN_SET=>S)
358362

359-
if (screen[1] < size(S, 1) + 4) | (screen[2] < 3size(S, 2))
363+
if !get(io, :limit, false) || (screen[1] < size(S, 1) + 4) | (screen[2] < 3size(S, 2))
360364
_show_with_braille_patterns(io, S)
361365
else
362366
_show_with_dotted_zeros(io, S)
@@ -407,8 +411,11 @@ end
407411
const brailleBlocks = UInt16['', '', '', '', '', '', '', '']
408412
function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSCInclAdjointAndTranspose)
409413
# The maximum number of characters we allow to display the matrix
410-
h, w = get(io, :limit, false) ? displaysize(io) .- (4, 2) : (typemax(Int)÷4, typemax(Int)÷2)
411-
m, n = size(S)
414+
h, w = if get(io, :limit, false)::Bool
415+
get(io, :displysize, displaysize(io)) .- (4, 2)
416+
else
417+
typemax(Int)÷4, typemax(Int)÷2
418+
end::Tuple{Int, Int}
412419

413420
warn = false
414421
try
@@ -421,8 +428,8 @@ function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSCInclAdjoi
421428
# In order to prevent aliasing, we only scale down by full integers.
422429
# While 1 to 1/2 is a big jump, it's less noticeable as you get bigger.
423430
# Note each character has 4 dots of height and 2 dots of width.
424-
scale = max(n÷2w, m÷4h) + 1
425-
char_h, char_w = fld1(m, 4scale), fld1(n, 2scale)
431+
scale = maximum(cld.(size(S), (4h, 2w)))
432+
char_h, char_w = cld.(size(S), (4scale, 2scale))
426433

427434
scale != 1 && print(io, " (displaying at 1/$scale scale)")
428435
println(io, ":")
@@ -439,12 +446,12 @@ function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSCInclAdjoi
439446

440447
if S isa AbstractSparseMatrixCSC
441448
for cords in zip(rinds, cinds)
442-
row, col = fld1.(cords, scale) |>x-> fldmod1.(x, (4, 2))
449+
row, col = cld.(cords, scale) |>x-> fldmod1.(x, (4, 2))
443450
brailleGrid[col[1]+1, row[1]] |= brailleBlocks[row[2] + 4(col[2]-1)]
444451
end
445452
else # swap rows / cols for adj and transpose
446453
for cords in zip(rinds, cinds)
447-
col, row = fld1.(cords, scale) |>x-> fldmod1.(x, (2, 4))
454+
col, row = cld.(cords, scale) |>x-> fldmod1.(x, (2, 4))
448455
brailleGrid[col[1]+1, row[1]] |= brailleBlocks[row[2] + 4(col[2]-1)]
449456
end
450457
end
@@ -457,7 +464,7 @@ function _show_with_dotted_zeros(io::IO, S::AbstractSparseMatrixCSCInclAdjointAn
457464
(S isa Adjoint) | (S isa Transpose) && ((rows, cols) = (cols, rows))
458465

459466
align = [isassigned(vals, ind) ? alignment(io, vals[ind]) : (3, 3) for ind in eachindex(vals)]
460-
colwidths = [max.((0, 0), align[findall(==(col), cols)]...) for col in axes(S,2)]
467+
colwidths = [maximum.((first,last), Ref(align[findall(==(col), cols)]);init=0) for col in axes(S,2)]
461468
displaysize(io)[2] < sum(sum.(colwidths) .+ 2) && return _show_with_braille_patterns(io, S)
462469

463470
println(io, ":")
@@ -476,26 +483,25 @@ function _show_with_dotted_zeros(io::IO, S::AbstractSparseMatrixCSCInclAdjointAn
476483
if isempty(index) # no value here, print an aligned dot
477484
l, r = cld(l+r-1, 2) + 1, div(l+r-1, 2) + 1
478485
print(io, " "^l * "" * (col==axes(S,2)[end] ? "" : " "^r))
479-
else try # print the element with 1 space of buffer on each side
486+
elseif length(index) == 1 # print the element with 1 space of buffer on each side
480487
l, r = (l+1, r+1) .- align[index[]]
481488
print(io, " "^l)
482489
isassigned(vals, index[]) ? show(io, vals[index[]]) : print(io, "#undef")
483490
col == axes(S,2)[end] || print(io, " "^r)
484-
catch # if there's 2+ entries, index[] errors. default to summing
485-
# entries, but print red to warn user that something's wrong
491+
else # default to summing entries, but print red to warn user that something's wrong
486492
elm = any(!isassigned(vals, ind) for ind in index) ? "#undef" :
487-
try repr(sum(vals[index]); context=:compact=>true) catch e "#NaN" end
493+
try repr(sum(vals[index]); context=io) catch e "#NaN" end
488494

489-
if length(elm) <= l+r # give up on alignment, center item in the column
490-
l, r = cld(l+r-length(elm), 2) + 1, div(l+r-length(elm), 2) + 1
491-
else # len of new item does not fit in column
495+
l, r = if textwidth(elm) <= l+r+2 # give up on alignment, just center item
496+
cld(l+r-textwidth(elm), 2) + 1, fld(l+r-textwidth(elm), 2) + 1
497+
else # new item does not fit in column
492498
elm = ""^(l+r)
493-
l, r = 1, 1
499+
1, 1
494500
end
495501

496502
printstyled(io, " "^l, elm, color=:red)
497503
col == axes(S,2)[end] || print(io, " "^r)
498-
end end
504+
end
499505
end
500506
row == axes(S,1)[end] || println(io)
501507
end

test/issues.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -759,14 +759,14 @@ let
759759
@test repr(B) == "sparse([1, 2, 3], [1, 2, 3], $T12960[#undef, #undef, #undef], 3, 3)"
760760
@test occursin(
761761
" #undef ⋅ ⋅\n ⋅ #undef ⋅\n ⋅ ⋅ #undef",
762-
repr(MIME("text/plain"), B),
762+
sprint(show, MIME("text/plain"), B; context=:limit=>true),
763763
)
764764

765765
B[1,2] = T12960()
766766
@test repr(B) == "sparse([1, 1, 2, 3], [1, 2, 2, 3], $T12960[#undef, $T12960(), #undef, #undef], 3, 3)"
767767
@test occursin(
768768
"\n #undef T12960() ⋅\n ⋅ #undef ⋅\n ⋅ ⋅ #undef",
769-
repr(MIME("text/plain"), B),
769+
sprint(show, MIME("text/plain"), B; context=:limit=>true),
770770
)
771771
end
772772

@@ -823,14 +823,16 @@ end
823823

824824
@testset "Issue #618" begin
825825
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [1.0, 1.0, 1.0, 1.0])
826-
@test contains(repr(MIME"text/plain"(), x), "2.0")
827-
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [7.0, 7.0, 1.0, 1.0])
828-
@test contains(repr(MIME"text/plain"(), x), "▒▒▒")
826+
@test contains(sprint(show, MIME"text/plain"(), x; context=:limit=>true), "2.0")
827+
x = SparseMatrixCSC(11, 11, [1; 112; 113; [114 for i=1:9]], [[1 for i=1:111]; 2; 3], [[9 for i=1:111]; 1; 1.])
828+
@test !contains(sprint(show, MIME"text/plain"(), x; context=:limit=>true), "▒▒▒")
829+
x = SparseMatrixCSC(11, 11, [1; 113; 114; [115 for i=1:9]], [[1 for i=1:112]; 2; 3], [[9 for i=1:112]; 1; 1.])
830+
@test contains(sprint(show, MIME"text/plain"(), x; context=:limit=>true), "▒▒▒")
829831
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [7.0, 'o', 1.0, 1.0])
830-
@test contains(repr(MIME"text/plain"(), x), "#NaN")
832+
@test contains(sprint(show, MIME"text/plain"(), x; context=:limit=>true), "#NaN")
831833
v = similar(Any[1, 2, 3, 4]); v[2:4] = [1.0, 1.0, 1.0]
832834
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], v)
833-
@test contains(repr(MIME"text/plain"(), x), "#undef")
835+
@test contains(sprint(show, MIME"text/plain"(), x; context=:limit=>true), "#undef")
834836

835837
x = SparseMatrixCSC(3, 3, [1, 3, 4, 5], [1, 1, 2, 3], [1, 1, 1, 1])
836838
@test 2 == Matrix(x)[1,1]

test/sparsematrix_constructors_indexing.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,7 @@ end
14881488

14891489
@testset "show" begin
14901490
io = IOBuffer()
1491+
repl = IOContext(io, :limit=>true)
14911492

14921493
A = spzeros(Float64, Int64, 0, 0)
14931494
for (transform, showstring) in zip(
@@ -1496,7 +1497,7 @@ end
14961497
"0×0 $Adjoint{Float64, $SparseMatrixCSC{Float64, Int64}} with 0 stored entries",
14971498
"0×0 $Transpose{Float64, $SparseMatrixCSC{Float64, Int64}} with 0 stored entries"
14981499
))
1499-
show(io, MIME"text/plain"(), transform(A))
1500+
show(repl , MIME"text/plain"(), transform(A))
15001501
@test String(take!(io)) == showstring
15011502
end
15021503

@@ -1507,7 +1508,7 @@ end
15071508
"1×1 $Adjoint{Float64, $SparseMatrixCSC{Float64, Int64}} with 1 stored entry:\n 1.0",
15081509
"1×1 $Transpose{Float64, $SparseMatrixCSC{Float64, Int64}} with 1 stored entry:\n 1.0",
15091510
))
1510-
show(io, MIME"text/plain"(), transform(A))
1511+
show(repl , MIME"text/plain"(), transform(A))
15111512
@test String(take!(io)) == showstring
15121513
end
15131514

@@ -1518,7 +1519,7 @@ end
15181519
"2×2 $Adjoint{Float32, $SparseMatrixCSC{Float32, Int64}} with 0 stored entries:\n ⋅ ⋅\n ⋅ ⋅",
15191520
"2×2 $Transpose{Float32, $SparseMatrixCSC{Float32, Int64}} with 0 stored entries:\n ⋅ ⋅\n ⋅ ⋅",
15201521
))
1521-
show(io, MIME"text/plain"(), transform(A))
1522+
show(repl , MIME"text/plain"(), transform(A))
15221523
@test String(take!(io)) == showstring
15231524
end
15241525

@@ -1530,7 +1531,7 @@ end
15301531
"2×1 $Transpose{Float64, $SparseMatrixCSC{Float64, Int64}} with 2 stored entries:\n 1.0\n 2.0",
15311532
),
15321533
("\n[⠉]", "\n[⠃]", "\n[⠃]"))
1533-
show(io, MIME"text/plain"(), transform(A))
1534+
show(repl , MIME"text/plain"(), transform(A))
15341535
@test String(take!(io)) == showstring
15351536
_show_with_braille_patterns(convert(IOContext, io), transform(A))
15361537
@test contains(String(take!(io)), braille)
@@ -1565,7 +1566,7 @@ end
15651566
"2×4 $Transpose{Int64, $SparseMatrixCSC{Int64, Int64}} with 5 stored entries:\n 1 1 ⋅ 1\n ⋅ 1 1 ⋅",
15661567
),
15671568
("\n[⡳]", "\n[⠙⠊]", "\n[⠙⠊]"))
1568-
show(io, MIME"text/plain"(), transform(A))
1569+
show(repl , MIME"text/plain"(), transform(A))
15691570
@test String(take!(io)) == showstring
15701571
_show_with_braille_patterns(convert(IOContext, io), transform(A))
15711572
@test contains(String(take!(io)), braille)
@@ -1582,7 +1583,7 @@ end
15821583
"⎣⠀⠀⎦",
15831584
"[⠑⠑⠀⠀]",
15841585
"[⠑⠑⠀⠀]"))
1585-
show(io, MIME"text/plain"(), transform(A))
1586+
show(repl , MIME"text/plain"(), transform(A))
15861587
@test String(take!(io)) == showstring
15871588
_show_with_braille_patterns(convert(IOContext, io), transform(A))
15881589
@test contains(String(take!(io)), braille)
@@ -1598,7 +1599,7 @@ end
15981599
end
15991600

16001601
# Issue #30589
1601-
@test repr("text/plain", sparse([true true])) == "1×2 $SparseMatrixCSC{Bool, $Int} with 2 stored entries:\n 1 1"
1602+
@test sprint(show, "text/plain", sparse([true true]); context=:limit=>true) == "1×2 $SparseMatrixCSC{Bool, $Int} with 2 stored entries:\n 1 1"
16021603

16031604
function _filled_sparse(m::Integer, n::Integer)
16041605
C = CartesianIndices((m, n))[:]
@@ -1618,7 +1619,7 @@ end
16181619
# horizontal scaling
16191620
ioc = IOContext(io, :displaysize => (80, 4), :limit => true)
16201621
_show_with_braille_patterns(ioc, _filled_sparse(8, 8))
1621-
@test contains(String(take!(io)), "\n[⠿⠇]")
1622+
@test contains(String(take!(io)), "\n[⣿⣿]")
16221623

16231624
_show_with_braille_patterns(ioc, _filled_sparse(8, 16))
16241625
@test contains(String(take!(io)), "\n[⠛⠛]")

0 commit comments

Comments
 (0)