@@ -45,13 +45,40 @@ function foreachblock(f, t::AbstractTensorMap; scheduler = nothing)
4545 return nothing
4646end
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, " * " , " \u 2026 [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
5784end
73100function 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
78107end
79108
0 commit comments