better compact printing of Py by cjdoris · Pull Request #684 · JuliaPy/PythonCall.jl · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/releasenotes.md
33 changes: 29 additions & 4 deletions src/Core/Py.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,23 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
end
hasprefix = (get(io, :typeinfo, Any) != Py)::Bool
compact = get(io, :compact, false)::Bool
limit = get(io, :limit, true)::Bool
if compact
# compact should output a single line, which we force by replacing newline
# characters with spaces
str = replace(str, "\n" => " ")
end
multiline = '\n' in str
prefix =
hasprefix ?
compact ? "Py:$(multiline ? '\n' : ' ')" : "Python:$(multiline ? '\n' : ' ')" : ""
prefix = !hasprefix ? "" : compact ? "Py: " : multiline ? "Python:\n" : "Python: "
print(io, prefix)
h, w = displaysize(io)
if get(io, :limit, true)
if limit
# limit: fit the printed text into the display size
h, w = displaysize(io)
h = max(h - 3, 5) # use 3 fewer lines to allow for the prompt, but always allow at least 5 lines
if multiline
# multiline: we truncate each line to the width of the screen, and skip
# middle lines if there are too many for the height
h -= 1 # for the prefix
lines = split(str, '\n')
function printlines(io, lines, w)
Expand Down Expand Up @@ -218,7 +225,25 @@ function Base.show(io::IO, ::MIME"text/plain", o::Py)
println(io)
printlines(io, lines[end-h1+1:end], w)
end
elseif compact
# compact: we print up to one screen width, skipping characters in the
# middle if the string is too long for the width
maxlen = w - length(prefix)
if length(str) ≤ maxlen
print(io, str)
else
gap = " ... "
gaplen = length(gap)
w0 = cld(maxlen - gaplen, 2)
i0 = nextind(str, 1, w0 - 1)
i1 = prevind(str, ncodeunits(str), maxlen - gaplen - w0 - 1)
print(io, str[begin:i0])
printstyled(io, gap, color = :light_black)
print(io, str[i1:end])
end
else
# single-line: we print up to one screenfull, skipping characters in the
# middle if the string is too long. We skip a whole line of characters.
maxlen = h * w - length(prefix)
if length(str) ≤ maxlen
print(io, str)
Expand Down
3 changes: 3 additions & 0 deletions test/Core.jl
Loading