Skip to content

Commit 9b22b75

Browse files
author
Christopher Doris
committed
better compact printing of Py
1 parent 71666ca commit 9b22b75

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/Core/Py.jl

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,23 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
168168
end
169169
hasprefix = (get(io, :typeinfo, Any) != Py)::Bool
170170
compact = get(io, :compact, false)::Bool
171+
limit = get(io, :limit, true)::Bool
172+
if compact
173+
# compact should output a single line, which we force by replacing newline
174+
# characters with spaces
175+
str = replace(str, "\n" => " ")
176+
end
171177
multiline = '\n' in str
172-
prefix =
173-
hasprefix ?
174-
compact ? "Py:$(multiline ? '\n' : ' ')" : "Python:$(multiline ? '\n' : ' ')" : ""
178+
prefix = !hasprefix ? "" : compact ? "Py: " : multiline ? "Python:\n" : "Python: "
175179
print(io, prefix)
176180
h, w = displaysize(io)
177-
if get(io, :limit, true)
181+
if limit
182+
# limit: fit the printed text into the display size
178183
h, w = displaysize(io)
179184
h = max(h - 3, 5) # use 3 fewer lines to allow for the prompt, but always allow at least 5 lines
180185
if multiline
186+
# multiline: we truncate each line to the width of the screen, and skip
187+
# middle lines if there are too many for the height
181188
h -= 1 # for the prefix
182189
lines = split(str, '\n')
183190
function printlines(io, lines, w)
@@ -218,7 +225,25 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
218225
println(io)
219226
printlines(io, lines[end-h1+1:end], w)
220227
end
228+
elseif compact
229+
# compact: we print up to one screen width, skipping characters in the
230+
# middle if the string is too long for the width
231+
maxlen = w - length(prefix)
232+
if length(str) maxlen
233+
print(io, str)
234+
else
235+
gap = " ... "
236+
gaplen = length(gap)
237+
w0 = cld(maxlen - gaplen, 2)
238+
i0 = nextind(str, 1, w0 - 1)
239+
i1 = prevind(str, ncodeunits(str), maxlen - gaplen - w0 - 1)
240+
print(io, str[begin:i0])
241+
printstyled(io, gap, color = :light_black)
242+
print(io, str[i1:end])
243+
end
221244
else
245+
# single-line: we print up to one screenfull, skipping characters in the
246+
# middle if the string is too long. We skip a whole line of characters.
222247
maxlen = h * w - length(prefix)
223248
if length(str) maxlen
224249
print(io, str)

0 commit comments

Comments
 (0)