@@ -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 ] < 3 size (S, 2 ))
363+ if ! get (io, :limit , false ) || (screen[1 ] < size (S, 1 ) + 4 ) | (screen[2 ] < 3 size (S, 2 ))
360364 _show_with_braille_patterns (io, S)
361365 else
362366 _show_with_dotted_zeros (io, S)
407411const brailleBlocks = UInt16[' ⠁' , ' ⠂' , ' ⠄' , ' ⡀' , ' ⠈' , ' ⠐' , ' ⠠' , ' ⢀' ]
408412function _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 ÷ 2 w, m ÷ 4 h) + 1
425- char_h, char_w = fld1 (m, 4 scale ), fld1 (n , 2 scale)
431+ scale = maximum ( cld .( size (S), ( 4 h, 2 w)))
432+ char_h, char_w = cld .( size (S ), ( 4 scale , 2 scale) )
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
0 commit comments