Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display linebreaks, TextDisplay vs REPLDisplay #43766

Closed
t-bltg opened this issue Jan 11, 2022 · 4 comments · Fixed by #43787
Closed

Display linebreaks, TextDisplay vs REPLDisplay #43766

t-bltg opened this issue Jan 11, 2022 · 4 comments · Fixed by #43787
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects.

Comments

@t-bltg
Copy link
Contributor

t-bltg commented Jan 11, 2022

As explained on discourse, we (ping @mkitti) feel that there might be an inconsistency in display(...) between TextDisplay and REPLDisplay.

Does anyone know why there is no trailing newline for TextDisplay ?

Would the following patch be acceptable ?

--- a/base/multimedia.jl	2022-01-11 21:00:42.765702329 +0100
+++ b/base/multimedia.jl	2022-01-11 21:02:53.377249363 +0100
@@ -239,14 +239,14 @@
 struct TextDisplay <: AbstractDisplay
     io::IO
 end
-display(d::TextDisplay, M::MIME"text/plain", @nospecialize x) = show(d.io, M, x)
+display(d::TextDisplay, M::MIME"text/plain", @nospecialize x) = (show(d.io, M, x); println(d.io))
 display(d::TextDisplay, @nospecialize x) = display(d, MIME"text/plain"(), x)
 
 # if you explicitly call display("text/foo", x), it should work on a TextDisplay:
 displayable(d::TextDisplay, M::MIME) = istextmime(M)
 function display(d::TextDisplay, M::MIME, @nospecialize x)
     displayable(d, M) || throw(MethodError(display, (d, M, x)))
-    show(d.io, M, x)
+    show(d.io, M, x); println(d.io)
 end
 
 import Base: close, flush
@JeffBezanson JeffBezanson added the domain:display and printing Aesthetics and correctness of printed representations of objects. label Jan 12, 2022
@JeffBezanson
Copy link
Sponsor Member

I guess we should do this. Normally I don't like building in extra trailing line breaks, because they're harder to remove than to add yourself. But given the nature of display it seems like the right thing.

@t-bltg
Copy link
Contributor Author

t-bltg commented Jan 12, 2022

Thanks @JeffBezanson for clarifying that.

Basically what I understood for MIME"text/plain" is:

  • use show(...) when writing to files or streams (no trailing newline), easier to work with afterwards, needs println to be added explicitly (machines ?)
  • use display(...) for richest output where the trailing newline is justified (humans ?)

I'll propose the PR and add some tests.

@mkitti
Copy link
Contributor

mkitti commented Jan 12, 2022

I think these line breaks may be necessary here rather than extra.

$ julia -e "A = rand(5,5); display(A); display(A)"
5×5 Matrix{Float64}:
 0.558617   0.833546   0.534481  0.871176   0.81832
 0.667262   0.214333   0.557675  0.469011   0.733549
 0.0167319  0.772786   0.532067  0.0182531  0.362664
 0.162401   0.175737   0.958528  0.528862   0.0345345
 0.28226    0.0938108  0.405553  0.34604    0.9921985×5 Matrix{Float64}:
 0.558617   0.833546   0.534481  0.871176   0.81832
 0.667262   0.214333   0.557675  0.469011   0.733549
 0.0167319  0.772786   0.532067  0.0182531  0.362664
 0.162401   0.175737   0.958528  0.528862   0.0345345
 0.28226    0.0938108  0.405553  0.34604    0.992198$

I think there may be an extra line break here though:

julia> A = rand(5,5); display(A); display(A)
5×5 Matrix{Float64}:
 0.649867     0.498648    0.0749694   0.585428   0.645387
 0.440297     0.326395    0.558343    0.0917279  0.519926
 0.566742     0.573145    7.27435e-6  0.477407   0.418683
 0.584659     0.00708769  0.765285    0.346584   0.439672
 0.000295367  0.244561    0.787006    0.738048   0.736395
5×5 Matrix{Float64}:
 0.649867     0.498648    0.0749694   0.585428   0.645387
 0.440297     0.326395    0.558343    0.0917279  0.519926
 0.566742     0.573145    7.27435e-6  0.477407   0.418683
 0.584659     0.00708769  0.765285    0.346584   0.439672
 0.000295367  0.244561    0.787006    0.738048   0.736395

julia>

@t-bltg
Copy link
Contributor Author

t-bltg commented Jan 12, 2022

On your second example this is just the REPL displaying nothing which looks like a newline no ?

julia> nothing

julia> println()


julia> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants