`display` and line breaks in `TextDisplay` and `LineEditREPL`

When I use display in a script and execute Julia from the command line julia script.jl, there seem to be missing line breaks after the display results. How do I make the “script” and REPL output look more similar?

$ julia -e "A = rand(Int8, 4, 3); display(A); display(A)"
4×3 Matrix{Int8}:
   -4  -60  -10
  -66   17  -64
 -113   50  -57
  -70   92   104×3 Matrix{Int8}:
   -4  -60  -10
  -66   17  -64
 -113   50  -57
  -70   92   10$

Meanwhile executing the REPL has correct line breaks:

julia> A = rand(Int8, 4, 3); display(A); display(A)
4×3 Matrix{Int8}:
  73  -58  124
   0   56  -73
 -70   37  101
 -41   25  -49
4×3 Matrix{Int8}:
  73  -58  124
   0   56  -73
 -70   37  101
 -41   25  -49

julia> 

If I pop! the REPL.LineEditREPL from Base.Multimedia.displays then I get a similar result.

julia> typeof(pop!(Base.Multimedia.displays))
REPL.REPLDisplay{REPL.LineEditREPL}
julia> A = rand(Int8, 4, 3); display(A); display(A)
4×3 Matrix{Int8}:
   75   120  -78
   82    93   36
   54   113   65
 -120  -116  -854×3 Matrix{Int8}:
   75   120  -78
   82    93   36
   54   113   65
 -120  -116  -85

What is the expectation here for TextDisplay versus LineEditREPL? Is there a bug here?

There is an additional println(io) for REPLDisplay, where there is none after the call to show() for TextDisplay.

This is also annoying for UnicodePlots plots, since running a script with $ julia <script.jl> will miss newlines after plots, whereas julia> include("<script.jl>") won’t.

Do you think we should open a PR for having these definitions instead:

Base.display(d::TextDisplay, M::MIME"text/plain", @nospecialize x) = (show(d.io, M, x); println(d.io))
function display(d::TextDisplay, M::MIME, @nospecialize x)
    displayable(d, M) || throw(MethodError(display, (d, M, x)))
    show(d.io, M, x); println(d.io)
end

?

I feel like this would be more consistent.

1 Like

For reference: opened issue, PR.