Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle styled show methods? #244

Closed
joshday opened this issue May 25, 2024 · 2 comments
Closed

How to handle styled show methods? #244

joshday opened this issue May 25, 2024 · 2 comments

Comments

@joshday
Copy link

joshday commented May 25, 2024

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
Copy link
Author

joshday commented May 25, 2024

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

@joshday joshday closed this as completed May 25, 2024
@ronisbr
Copy link
Owner

ronisbr commented May 25, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants