Skip to content

Commit 7364ef3

Browse files
committed
further tweak tensor and block show
1 parent 7c68aec commit 7364ef3

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/tensors/abstracttensor.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,22 +652,21 @@ 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))
672-
return nothing
673672
end

src/tensors/blockiterator.jl

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,32 @@ function foreachblock(f, t::AbstractTensorMap; scheduler = nothing)
4646
end
4747

4848
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
49+
numlinesleft, numcols = get(io, :displaysize, displaysize(io))
50+
# lines of headers should already have been subtracted, but not the 3 spare lines for old and new prompts
51+
maxnumlinesperblock = max(div(numlinesleft - 4, min(3, length(iter))), 7)
52+
# aim to show at least 3 blocks, but not if this means that there
53+
# would be less than 7 lines per block (= 5 lines for the actual matrix)
54+
# we deduct 4 lines to leave space for a truncation message and prompts
55+
for (n, (c, b)) in enumerate(iter)
56+
n == 1 || print(io, "\n\n")
57+
if get(io, :limit, false)
58+
numlinesneeded = min(size(b, 1) + 2, maxnumlinesperblock)
59+
if numlinesleft >= numlinesneeded + 4
60+
# we can still print at least this block, and have one line
61+
# for the truncation message and 3 more lines for old and new prompts
62+
print(io, " * ", c, " => ")
63+
newio = IOContext(io, :displaysize => (maxnumlinesperblock - 1 + 3, numcols))
64+
# subtract 1 line for the newline, but add 3 because of how matrices are printed
65+
show(newio, mime, b)
66+
numlinesleft -= numlinesneeded
67+
else
68+
print(io, " * ", " \u2026 [output of ", length(iter) - n + 1, " more block(s) truncated]")
69+
break
70+
end
71+
else
72+
print(io, " * ", c, " => ")
73+
show(io, mime, b)
74+
end
5575
end
5676
return nothing
5777
end
@@ -73,7 +93,9 @@ end
7393
function Base.show(io::IO, mime::MIME"text/plain", b::BlockIterator)
7494
summary(io, b)
7595
println(io, ":")
76-
show_blocks(io, mime, b)
96+
(numlines, numcols) = get(io, :displaysize, displaysize(io))
97+
newio = IOContext(io, :displaysize => (numlines - 1, numcols))
98+
show_blocks(newio, mime, b)
7799
return nothing
78100
end
79101

0 commit comments

Comments
 (0)