Skip to content

Commit

Permalink
print_matrix fix on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Aug 24, 2020
1 parent aa8bf7e commit e02a0a8
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/arrayshow.jl
Expand Up @@ -24,6 +24,112 @@ for DT in [:Integer, :HalfInteger]
end
end

function Base.print_matrix(io::IO, X::Union{AbstractHalfIntegerVecOrMat, AdjOrTransAbsHalfIntVecOrMat,
HalfIntSubArray},
pre::AbstractString = " ", # pre-matrix string
sep::AbstractString = " ", # separator between elements
post::AbstractString = "", # post-matrix string
hdots::AbstractString = " \u2026 ",
vdots::AbstractString = "\u22ee",
ddots::AbstractString = " \u22f1 ",
hmod::Integer = 5, vmod::Integer = 5)

hmod, vmod = Int(hmod)::Int, Int(vmod)::Int
if !(get(io, :limit, false)::Bool)
screenheight = screenwidth = typemax(Int)
else
sz = displaysize(io)::Tuple{Int,Int}
screenheight, screenwidth = sz[1] - 4, sz[2]
end
screenwidth -= length(pre)::Int + length(post)::Int
presp = repeat(" ", length(pre)::Int) # indent each row to match pre string
postsp = ""
@assert textwidth(hdots) == textwidth(ddots)
sepsize = length(sep)::Int
rowsA, colsA = UnitRange(axes(X,1)), UnitRange(axes(X,2))
m, n = length(rowsA)::Int, length(colsA)::Int
# To figure out alignments, only need to look at as many rows as could
# fit down screen. If screen has at least as many rows as A, look at A.
# If not, then we only need to look at the first and last chunks of A,
# each half a screen height in size.
halfheight = div(screenheight,2)
if m > screenheight
rowsA = [rowsA[(0:halfheight-1) .+ firstindex(rowsA)]; rowsA[(end-div(screenheight-1,2)+1):end]]
end
# Similarly for columns, only necessary to get alignments for as many
# columns as could conceivably fit across the screen
maxpossiblecols = div(screenwidth, 1+sepsize)
if n > maxpossiblecols
colsA = [colsA[(0:maxpossiblecols-1) .+ firstindex(colsA)]; colsA[(end-maxpossiblecols+1):end]]
end
A = Base.alignment(io, X, rowsA, colsA, screenwidth, screenwidth, sepsize)
# Nine-slicing is accomplished using print_matrix_row repeatedly
if m <= screenheight # rows fit vertically on screen
if n <= length(A) # rows and cols fit so just print whole matrix in one piece
for i in rowsA
print(io, i == first(rowsA) ? pre : presp)
Base.print_matrix_row(io, X,A,i,colsA,sep)
print(io, i == last(rowsA) ? post : postsp)
if i != last(rowsA); println(io); end
end
else # rows fit down screen but cols don't, so need horizontal ellipsis
c = div(screenwidth-length(hdots)::Int+1,2)+1 # what goes to right of ellipsis
Ralign = reverse(Base.alignment(io, X, rowsA, reverse(colsA), c, c, sepsize)) # alignments for right
c = screenwidth - sum(map(sum,Ralign)) - (length(Ralign)-1)*sepsize - length(hdots)::Int
Lalign = Base.alignment(io, X, rowsA, colsA, c, c, sepsize) # alignments for left of ellipsis
for i in rowsA
print(io, i == first(rowsA) ? pre : presp)
Base.print_matrix_row(io, X,Lalign,i,colsA[1:length(Lalign)],sep)
print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots)::Int))
Base.print_matrix_row(io, X, Ralign, i, (n - length(Ralign)) .+ colsA, sep)
print(io, i == last(rowsA) ? post : postsp)
if i != last(rowsA); println(io); end
end
end
else # rows don't fit so will need vertical ellipsis
if n <= length(A) # rows don't fit, cols do, so only vertical ellipsis
for i in rowsA
print(io, i == first(rowsA) ? pre : presp)
Base.print_matrix_row(io, X,A,i,colsA,sep)
print(io, i == last(rowsA) ? post : postsp)
if i != rowsA[end] || i == rowsA[halfheight]; println(io); end
if i == rowsA[halfheight]
print(io, i == first(rowsA) ? pre : presp)
print_matrix_vdots(io, vdots, A, sep, vmod, 1, false)
print(io, i == last(rowsA) ? post : postsp * '\n')
end
end
else # neither rows nor cols fit, so use all 3 kinds of dots
c = div(screenwidth-length(hdots)::Int+1,2)+1
Ralign = reverse(Base.alignment(io, X, rowsA, reverse(colsA), c, c, sepsize))
c = screenwidth - sum(map(sum,Ralign)) - (length(Ralign)-1)*sepsize - length(hdots)::Int
Lalign = Base.alignment(io, X, rowsA, colsA, c, c, sepsize)
r = mod((length(Ralign)-n+1),vmod) # where to put dots on right half
for i in rowsA
print(io, i == first(rowsA) ? pre : presp)
Base.print_matrix_row(io, X,Lalign,i,colsA[1:length(Lalign)],sep)
print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots)::Int))
Base.print_matrix_row(io, X,Ralign,i,(n-length(Ralign)).+colsA,sep)
print(io, i == last(rowsA) ? post : postsp)
if i != rowsA[end] || i == rowsA[halfheight]; println(io); end
if i == rowsA[halfheight]
print(io, i == first(rowsA) ? pre : presp)
print_matrix_vdots(io, vdots, Lalign, sep, vmod, 1, true)
print(io, ddots)
print_matrix_vdots(io, vdots, Ralign, sep, vmod, r, false)
print(io, i == last(rowsA) ? post : postsp * '\n')
end
end
end
if isempty(rowsA)
print(io, pre)
print(io, vdots)
length(colsA) > 1 && print(io, " ", ddots)
print(io, post)
end
end
end

function Base.show(io::IO, X::AbstractHalfIntegerWrapper)
show(io, parent(X))
end
Expand Down

2 comments on commit e02a0a8

@jishnub
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/20114

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" e02a0a8d0bca1008ba79d45b0b0713b5b61a4682
git push origin v0.1.5

Please sign in to comment.