# Use PrettyTables.pretty\_table for StructArray display

**URL:** <https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797>\
**Category:** General Usage\
**Created:** [November 19, 2024, 1:07am UTC](https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797 "2024-11-19T01:07:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 19, 2024, 1:07am UTC](https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797/1 "2024-11-19T01:07:44Z")

</div>

In my VSCode REPL I want the normal display of `StructArray` instances to be `PrettyTables.pretty_table`. What should I do?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [November 19, 2024, 3:17am UTC](https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797/2 "2024-11-19T03:17:01Z")

</div>

Hi @jar1 !

Did you try to overload the `show` method? It should work.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 19, 2024, 4:55am UTC](https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797/3 "2024-11-19T04:55:30Z")

</div>

I can pirate `show` as you suggest.

```julia
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.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [November 19, 2024, 11:35am UTC](https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797/4 "2024-11-19T11:35:38Z")

</div>

If StructArray does not support changing the printing mechanism, I think overloading show is the only way.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [November 19, 2024, 7:06pm UTC](https://discourse.julialang.org/t/use-prettytables-pretty-table-for-structarray-display/122797/5 "2024-11-19T19:06:26Z")

</div>

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`.

```julia
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)

```
