Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 755
xiaodai Avatar asked Sep 12 '19 00:09

xiaodai


2 Answers

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

like image 170
NHDaly Avatar answered Nov 09 '22 03:11

NHDaly


If you want the setting to stick around in your REPL session:

Base.active_repl.options.iocontext[:displaysize] = (100, 80)

That will tell Julia to use 100 lines and 80 columns. You can go back to the default with:

Base.active_repl.options.iocontext[:displaysize] = displaysize(stdout)

To change the display size for a single call to show, you can do as others have suggested:

show(IOContext(stdout, :limit=>true, :displaysize=>(100,80)),
     MIME("text/plain"),
     thing)
like image 32
Matt Kramer Avatar answered Nov 09 '22 04:11

Matt Kramer