ronisbr / PrettyTables.jl

Print data in formatted tables.
MIT License
404 stars 38 forks source link

How to handle styled show methods? #244

Closed joshday closed 4 months ago

joshday commented 4 months ago

How should one implement a styled show method that works with PrettyTables? The IO passed to the show method indicates colors are okay, but PrettyTables prints the ANSI codes. I'm sure this is due to the (very cool) highlighting features of PrettyTables, but how would one handle both colors in the REPL and no ANSI codes in the pretty_table?

Maybe PrettyTables should at least be passing IOContext(io, :color => false) to the show methods of the elements?

julia> using PrettyTables

julia> struct A end

julia> Base.show(io::IO, ::A) = (@info(get(io, :color, false)); printstyled(io, "AAAA", color=:red))

julia> A()
[ Info: true
AAAA  # this is red

julia> pretty_table([A()])
[ Info: true
┌──────────────────┐
│           Col. 1 │
├──────────────────┤
│ \e[31mAAAA\e[39m │
└──────────────────┘
joshday commented 4 months ago

For anyone who finds yourself at this issue, read the response by @ronisbr in the linked issue at OnlineStats.

ronisbr commented 4 months ago

Hi @joshday !

I replied to that original thread! In PrettyTables.jl, you just need to wrap the string inside an AnsiTextCell:

julia> pretty_table([AnsiTextCell(sprint(print, A(); context = :color => true))])
Captura de Tela 2024-05-25 às 10 39 13

However, I think the source of the original problem is really how print is used.