Skip to content

Commit b32f1f4

Browse files
authored
further tweak tensor and block show (#322)
1 parent 43ebfc4 commit b32f1f4

3 files changed

Lines changed: 99 additions & 21 deletions

File tree

src/tensors/abstracttensor.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -652,22 +652,22 @@ function Base.show(io::IO, mime::MIME"text/plain", t::AbstractTensorMap)
652652
# 1) show summary: typically d₁×d₂×… ← d₃×d₄×… $(typeof(t))
653653
summary(io, t)
654654

655-
# case without `\n`:
656-
if get(io, :compact, true)
655+
if get(io, :compact, false)
656+
# case without `\n`:
657657
print(io, "(…, ")
658658
show(io, mime, space(t))
659659
print(io, ')')
660-
return nothing
660+
else
661+
# case with `\n`
662+
# 2) show spaces
663+
println(io, ':')
664+
println(io, " codomain: ", codomain(t))
665+
println(io, " domain: ", domain(t))
666+
# 3) show data
667+
println(io, " blocks: ")
668+
(numlines, numcols) = get(io, :displaysize, displaysize(io))
669+
newio = IOContext(io, :displaysize => (numlines - 4, numcols))
670+
show_blocks(newio, mime, blocks(t))
661671
end
662-
663-
# case with `\n`
664-
# 2) show spaces
665-
println(io, ':')
666-
println(io, " codomain: ", codomain(t))
667-
println(io, " domain: ", domain(t))
668-
669-
# 3) [optional]: show data
670-
println(io, "\n\n blocks: ")
671-
show_blocks(io, mime, blocks(t))
672672
return nothing
673673
end

src/tensors/blockiterator.jl

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,40 @@ function foreachblock(f, t::AbstractTensorMap; scheduler = nothing)
4545
return nothing
4646
end
4747

48-
function show_blocks(io, mime::MIME"text/plain", iter)
49-
first = true
50-
for (c, b) in iter
51-
first || print(io, "\n\n")
52-
print(io, " * ", c, " => ")
53-
show(io, mime, b)
54-
first = false
48+
function show_blocks(io, mime::MIME"text/plain", iter; maytruncate::Bool = true)
49+
if maytruncate && get(io, :limit, false)
50+
numlinesleft, numcols = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
51+
numlinesleft -= 2 # lines of headers have already been subtracted, but not the 2 spare lines for old and new prompts
52+
minlinesperblock = 7 # aim to have at least this many lines per printed block (= 5 lines for the actual matrix)
53+
minnumberofblocks = min(3, length(iter)) # aim to show at least this many blocks
54+
truncateblocks = sum(cb -> min(size(cb[2], 1) + 2, minlinesperblock), iter; init = 0) > numlinesleft
55+
maxnumlinesperblock = max(div(numlinesleft - 2 * truncateblocks, minnumberofblocks), minlinesperblock)
56+
# aim to show at least minnumberofblocks, but not if this means that there would be less than minlinesperblock
57+
# deduct two lines for a truncation message (and newline) if needed
58+
for (n, (c, b)) in enumerate(iter)
59+
n == 1 || print(io, "\n\n")
60+
numlinesneeded = min(size(b, 1) + 2, maxnumlinesperblock)
61+
if numlinesleft >= numlinesneeded + 2 * truncateblocks
62+
# we can still print at least this block, and have two lines for
63+
# the truncation message (and its newline) if it is required
64+
print(io, " * ", c, " => ")
65+
newio = IOContext(io, :displaysize => (maxnumlinesperblock - 1 + 3, numcols))
66+
# subtract 1 line for the newline, but add 3 because of how matrices are printed
67+
show(newio, mime, b)
68+
numlinesleft -= numlinesneeded
69+
else
70+
print(io, " * ", " \u2026 [output of ", length(iter) - n + 1, " more block(s) truncated]")
71+
break
72+
end
73+
end
74+
else
75+
first = true
76+
for (c, b) in iter
77+
first || print(io, "\n\n")
78+
print(io, " * ", c, " => ")
79+
show(io, mime, b)
80+
first = false
81+
end
5582
end
5683
return nothing
5784
end
@@ -73,7 +100,9 @@ end
73100
function Base.show(io::IO, mime::MIME"text/plain", b::BlockIterator)
74101
summary(io, b)
75102
println(io, ":")
76-
show_blocks(io, mime, b)
103+
(numlines, numcols) = get(io, :displaysize, displaysize(io))::Tuple{Int, Int}
104+
newio = IOContext(io, :displaysize => (numlines - 1, numcols))
105+
show_blocks(newio, mime, b; maytruncate = false)
77106
return nothing
78107
end
79108

test/tensors/tensors.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,52 @@ end
624624
end
625625
end
626626
end
627+
628+
@timedtestset "show tensors" begin
629+
for V in (ℂ^2, Z2Space(0 => 2, 1 => 2), SU2Space(0 => 2, 1 => 2))
630+
t1 = ones(Float32, V V, V)
631+
t2 = randn(ComplexF64, V V V)
632+
# test unlimited output
633+
for t in (t1, t2, t1', t2')
634+
output = IOBuffer()
635+
summary(output, t)
636+
print(output, ":\n codomain: ")
637+
show(output, MIME("text/plain"), codomain(t))
638+
print(output, "\n domain: ")
639+
show(output, MIME("text/plain"), domain(t))
640+
print(output, "\n blocks: \n")
641+
first = true
642+
for (c, b) in blocks(t)
643+
first || print(output, "\n\n")
644+
print(output, " * ")
645+
show(output, MIME("text/plain"), c)
646+
print(output, " => ")
647+
show(output, MIME("text/plain"), b)
648+
first = false
649+
end
650+
outputstr = String(take!(output))
651+
@test outputstr == sprint(show, MIME("text/plain"), t)
652+
end
653+
654+
# test limited output with a single block
655+
t = randn(Float64, V V, V)' # we know there is a single space in the codomain, so that blocks have 2 rows
656+
output = IOBuffer()
657+
summary(output, t)
658+
print(output, ":\n codomain: ")
659+
show(output, MIME("text/plain"), codomain(t))
660+
print(output, "\n domain: ")
661+
show(output, MIME("text/plain"), domain(t))
662+
print(output, "\n blocks: \n")
663+
c = unit(sectortype(t))
664+
b = block(t, c)
665+
print(output, " * ")
666+
show(output, MIME("text/plain"), c)
667+
print(output, " => ")
668+
show(output, MIME("text/plain"), b)
669+
if length(blocks(t)) > 1
670+
print(output, "\n\n * … [output of 1 more block(s) truncated]")
671+
end
672+
outputstr = String(take!(output))
673+
@test outputstr == sprint(show, MIME("text/plain"), t; context = (:limit => true, :displaysize => (12, 100)))
674+
end
675+
end

0 commit comments

Comments
 (0)