In my VSCode REPL I want the normal display of StructArray
instances to be PrettyTables.pretty_table
. What should I do?
Hi @jar1 !
Did you try to overload the show
method? It should work.
1 Like
I can pirate show
as you suggest.
julia> Base.show(io::IO, ::MIME"text/plain", x::StructArray) = pretty_table(io, x)
julia> StructArray(a=rand(5), b=rand(5))
ββββββββββββ¬βββββββββββ
β a β b β
β Float64 β Float64 β
ββββββββββββΌβββββββββββ€
β 0.557374 β 0.415028 β
β 0.809813 β 0.628098 β
β 0.248757 β 0.320603 β
β 0.762648 β 0.207177 β
β 0.478039 β 0.693585 β
ββββββββββββ΄βββββββββββ
However that seems like a much bigger change than is necessary or safe. I donβt want to overload it for all output, because that could have adverse side effects, since Iβm not aware of what other programs in my runtime are using show
in one way or another. I only want to overload the display in my REPL.
If StructArray does not support changing the printing mechanism, I think overloading show is the only way.
2 Likes
This seems to work. It changes the repl display without changing other outputs. Itβs still piracy of display
and it relies on nonpublic REPL.outstream
but still it seems better than pirating show
.
import REPL
function Base.display(r::REPL.REPLDisplay, x::StructArray)
io = REPL.outstream(r.repl)
println(io, """StructArray{$(join(propertynames(x), ", "))}""")
pretty_table(io, x)
end
julia> StructArray(a=rand(5), b=rand(5))
StructArray{a, b}
βββββββββββββ¬ββββββββββββ
β a β b β
β Float64 β Float64 β
βββββββββββββΌββββββββββββ€
β 0.165508 β 0.75791 β
β 0.440973 β 0.460919 β
β 0.0858889 β 0.392867 β
β 0.471203 β 0.0932289 β
β 0.410608 β 0.433334 β
βββββββββββββ΄ββββββββββββ
julia> let io = IOBuffer()
show(io, "text/plain", StructArray(a=rand(5), b=rand(5)))
seekstart(io)
println(read(io, String))
end;
5-element StructArray(::Vector{Float64}, ::Vector{Float64}) with eltype @NamedTuple{a::Float64, b::Float64}:
(a = 0.6178495422742597, b = 0.1238638495845299)
(a = 0.30289555128884693, b = 0.27873172204566277)
(a = 0.4013228628193214, b = 0.6438433505704091)
(a = 0.8092062230983714, b = 0.3295303757547783)
(a = 0.5775566148192897, b = 0.6535287178798409)