Avoiding very long output

Occasionally, for custom types, I forget to define a show method that respects limit. This can happen accidentally even with the best intentions, eg with unfortunate combinations of nested types.

This can lead to very long output in the REPL, which not all terminals and interfaces are happy with.

I am wondering it it would be possible to somehow customize the REPL or the display infrastructure to catch this. Eg when working the REPL, somehow impose a sanity check of cutting off at eg line 100, or after 10000 characters, whichever comes first.

But I don’t know where to start, is it possible to hook into the existing mechanisms for this?

One way would be to first print into an IOBuffer within your show method and then to count the number of lines from the IOBuffer before truncating and printing it to the display output. If you don’t want to manually code this into each show method, then it would have to be coded into the stdout?

I think you can register a custom AbstractDisplay to do this. See: https://docs.julialang.org/en/latest/base/io-network/#Multimedia-I/O-1

If you want an example, I think ElectronDisplay.jl is a good one: https://github.com/queryverse/ElectronDisplay.jl/blob/master/src/ElectronDisplay.jl

1 Like