Search code examples
juliadisplayread-eval-print-looppretty-print

Julia: which environment variable/setting controls the number of elements printed for an array in the repl?


When I print

rand(1_000_000)

It prints the first N lines and prints the last N lines. How is this N determined and how do I control this N?

enter image description here


Solution

  • The size comes is calculated by Base.displaysize(::IO), which you can see should report the size of your terminal for stdout, and reports the "standard" size for IOBuffers:

    julia> Base.displaysize(stdout)
    (19, 81)
    
    
    julia> Base.displaysize(IOBuffer())
    (24, 80)
    
    julia> Base.displaysize()
    (24, 80)
    

    This is called in the full show() method for showing arrays at the REPL: show(io::IO, ::MIME"text/plain", X::AbstractArray), inside print_matrix, here:

        if !get(io, :limit, false)
            screenheight = screenwidth = typemax(Int)
        else
            sz = displaysize(io)
            screenheight, screenwidth = sz[1] - 4, sz[2]
        end
    

    https://github.com/NHDaly/julia/blob/879fef402835c1727aac52bafae686b5913aec2d/base/arrayshow.jl#L159-L164

    Note though that in that function, io is actually an IOContext, so as @Fengyang Wang describes in this answer: https://stackoverflow.com/a/40794864/751061, you can also manually set the displaysize on the IOContext if you want to control it yourself (updated for julia 1.0):

    julia> show(IOContext(stdout, :limit=>true, :displaysize=>(10,10)), MIME("text/plain"), rand(1_000_000))
    1000000-element Array{Float64,1}:
     0.5684598962187111
     0.2779754727011845
     0.22165656934386813
     ⋮
     0.3574516963850929
     0.914975294703998
    

    Finally, to close the loop, displaying a value at the REPL turns into show(io, MIME("text/plain"), v), via display: https://github.com/NHDaly/julia/blob/879fef402835c1727aac52bafae686b5913aec2d/base/multimedia.jl#L319