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

pretty printing structs that contain large array fields #6

Open
rambunctiousapple opened this issue Jan 18, 2022 · 1 comment
Open

Comments

@rambunctiousapple
Copy link

Is there a flag to pprint to tell it not to print out large array fields, or to print out the first and last few elements only with an ellipsis ... between? I have structs that contain large vectors. pprint() prints out every element in the vectors which makes the output unreadable.

@xitology
Copy link
Member

It is not supported directly, but it is not particularly hard to customize PrettyPrinting to add an ellipsis. For example, you could override PrettyPrinting.quoteof() for your struct types:

using PrettyPrinting
import PrettyPrinting: quoteof

struct S
    v::Vector{Int}
end

quoteof(v::Vector) =
    length(v) <= 2 ?
        Expr(:vect, quoteof.(v)...) :
        Expr(:vect, quoteof(v[1]), :, quoteof(v[end]))

quoteof(s::S) =
    Expr(:call, :S, quoteof(s.v))

pprintln(S([1,2])) #-> S([1,2])
pprintln(S([1,2,3,4])) #-> S([1, …, 4])

If you want every vector to use this compact notation, you need to add

PrettyPrinting.tile(v::Vector) =
    PrettyPrinting.tile_expr(quoteof(v))

pprintln([[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]) #-> [[1], …, [1, …, 4]]

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