# \`show\` and \`displaysize\`

**URL:** <https://discourse.julialang.org/t/show-and-displaysize/86126>\
**Category:** General Usage\
**Tags:** repl, display\
**Created:** [August 22, 2022, 10:03am UTC](https://discourse.julialang.org/t/show-and-displaysize/86126 "2022-08-22T10:03:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [August 22, 2022, 10:04am UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/1 "2022-08-22T10:04:00Z")

</div>

Why do `show(stdout, "text/plain", x)` respect the `displaysize` in one case but not the other ? Is there a way to gather the representation respecting `displaysize` as a string, using `sprint` for example?

```julia-repl
julia> displaysize(stdout)
(14, 111)

julia> L = collect(1:15) # respect displaysize
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  ⋮
 12
 13
 14
 15

julia> show(stdout, "text/plain", L) # not respecting displaysize
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
julia> M = sparse(ones(1000, 1000)) # respect displaysize
1000×1000 SparseMatrixCSC{Float64, Int64} with 1000000 stored entries:
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿

julia> show(stdout, "text/plain", M) # respect displaysize
1000×1000 SparseMatrixCSC{Float64, Int64} with 1000000 stored entries:
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿

julia> print(sprint(show, "text/plain", M; context=stdout)) # not respecting displaysize, despite I pass stdout in context...
1000×1000 SparseMatrixCSC{Float64, Int64} with 1000000 stored entries:
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 22, 2022, 11:47am UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/2 "2022-08-22T11:47:43Z")

</div>

> [@etienne\_dg](#):
>
> `show(stdout, "text/plain", L) # not respecting displaysize`

When the REPL outputs something ([here](https://github.com/JuliaLang/julia/blob/e0056c8d3087827b7c8e2b0648deeb8dc531da5f/stdlib/REPL/src/REPL.jl#L262)), it uses an [`IOContext`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext) to tell `show` to limit output. You can do the same thing with

```julia
show(IOContext(stdout, :limit=>true), "text/plain", L)

```

> [@etienne\_dg](#):
>
> Is there a way to gather the representation respecting `displaysize` as a string, using `sprint` for example?

Use the [`context` keyword parameter of `sprint`](https://docs.julialang.org/en/v1/base/io-network/#Base.sprint) correctly — it should be something like `:limit=>true` as explained in the docs, not `stdout`:

```julia
sprint(show, "text/plain", L; context=:limit=>true)

```

or you can alternatively use `repr`:

```julia
repr("text/plain", L; context=:limit=>true)

```

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [August 22, 2022, 1:26pm UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/3 "2022-08-22T13:26:09Z")

</div>

```julia-repl
julia> show(IOContext(stdout, :limit=>true), "text/plain", L)
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  ⋮
 12
 13
 14
 15

```

nice!  
So `show(stdout, "text/plain", M)` respecting the displaysize is a bug?

However, I would expect these ones to respect the displaysize:

```julia
julia> print(sprint(show, "text/plain", L; context=:limit=>true)) # not respecting displaysize
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
julia> print(sprint(show, "text/plain", L; context=IOContext(stdout, :limit=>true))) # not respecting displaysize
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
julia> print(repr("text/plain", L; context=:limit=>true)) # not respecting displaysize
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
julia> print(repr("text/plain", L; context=IOContext(stdout, :limit=>true))) # not respecting displaysize
15-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 22, 2022, 7:53pm UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/4 "2022-08-22T19:53:38Z")

</div>

> [@etienne\_dg](#):
>
> So `show(stdout, "text/plain", M)` respecting the displaysize is a bug?

I’m not sure what you mean. `show(stdout, "text/plain", M)` does _not_ limit output. In your example above, `L = collect(1:15) # respect displaysize` was not calling `show` by itself, it was using the REPL’s implicit call to `display`, which calls `show` with `:limit=>true`.

> [@etienne\_dg](#):
>
> However, I would expect these ones to respect the displaysize:

They work for me. Maybe the display size is not what you think? Try `L = collect(1:1000)` and you should see the ` ⋮` in the output.

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [August 22, 2022, 8:51pm UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/5 "2022-08-22T20:51:29Z")

</div>

> [@stevengj](#):
>
> I’m not sure what you mean. `show(stdout, "text/plain", M)` does _not_ limit output.

`show(stdout, "text/plain", L)` does not limit the size of L (when `L = collect(1:15)`, but it does for M (with `M = sparse(ones(1000, 1000))`), so I don’t know if that expected…

> [@stevengj](#):
>
> They work for me. Maybe the display size is not what you think? Try `L = collect(1:1000)` and you should see the ` ⋮` in the output.

Indeed, it crops the output, but not at `displaysize`, the output is still greater than the height of the REPL. There is a difference between calling `show(IOContext(stdout, :limit=>true), "text/plain", L)` and `print(sprint(show, "text/plain", L; context=IOContext(stdout, :limit=>true)))`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 22, 2022, 9:26pm UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/6 "2022-08-22T21:26:38Z")

</div>

> [@etienne\_dg](#):
>
> `show(stdout, "text/plain", L)` does not limit the size of L (when `L = collect(1:15)`, but it does for M (with `M = sparse(ones(1000, 1000))`), so I don’t know if that expected…

Oh, I see — yes, the SparseMatrix.jl `show` method [unconditionally uses `displaysize`](https://github.com/JuliaSparse/SparseArrays.jl/blob/3b1ce54fe4575913140cc4f3b001d456da6091e9/src/sparsematrix.jl#L304-L305) without checking `get(io, :limit, false)`. Arguably this is a bug.

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [August 23, 2022, 8:43am UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/7 "2022-08-23T08:43:32Z")

</div>

I don’t know anymore what is going on…  
Is `stdout` changed to something else than the REPL out stream when calling `sprint` or `repr`?

```julia
julia> print(sprint(show, "text/plain", L; context=IOContext(stdout, :limit=>true)))
1000-element Vector{Int64}:
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
    ⋮
  992
  993
  994
  995
  996
  997
  998
  999
 1000
julia> displaysize(IOContext(stdout, :limit=>true))
(14, 111)

julia> print(sprint(show, "text/plain", L; context=IOContext(stdout, :limit=>true, :displaysize => (14, 111))))

1000-element Vector{Int64}:
    1
    2
    3
    4
    5
    ⋮
  997
  998
  999
 1000

```

---

<div class="post-metadata">

**Author:** ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)\
**Post date:** [August 23, 2022, 11:40am UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/8 "2022-08-23T11:40:28Z")

</div>

> [@etienne\_dg](#):
>
> Is `stdout` changed to something else than the REPL out stream when calling `sprint` or `repr`?

Ok, I think this is the case: [https://github.com/JuliaLang/julia/blob/fa986d9e0525eedd7706929d549c61179e6c6090/base/stream.jl#L546](https://github.com/JuliaLang/julia/blob/fa986d9e0525eedd7706929d549c61179e6c6090/base/stream.jl#L546)  
Thanks for your help!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 23, 2022, 2:57pm UTC](https://discourse.julialang.org/t/show-and-displaysize/86126/9 "2022-08-23T14:57:17Z")

</div>

> [@etienne\_dg](#):
>
> Is `stdout` changed to something else than the REPL out stream when calling `sprint` or `repr`?

Yes, it’s changed to an `IOBuffer()`.
