From 76ca8152cdcf8c5ce37015be1603236612f84787 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Thu, 9 Jun 2016 10:32:30 +0200 Subject: [PATCH] Set :multiline=>false in showcompact() and use it for enum printing This provides a simple way of printing the short representation of a type. Cf. #16817. --- base/Enums.jl | 2 +- base/docs/helpdb/Base.jl | 12 +++++++----- base/show.jl | 13 +++++++++++-- doc/stdlib/io-network.rst | 6 ++++-- test/show.jl | 13 +++++++++++++ 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/base/Enums.jl b/base/Enums.jl index b1e1647182b600..39581d411f758c 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -97,7 +97,7 @@ macro enum(T,syms...) print(io, x) else print(io, x, "::") - show(IOContext(io, :multiline => false), typeof(x)) + showcompact(io, typeof(x)) print(io, " = ", Int(x)) end end diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 23aa66990e2747..0c34aec897a879 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -2855,12 +2855,14 @@ Show an expression and result, returning the result. """ showcompact(x) -Show a more compact representation of a value. +Show a compact representation of a value. -This is used for printing array elements. If a new type has a different compact representation, -it should test `get(io, :compact, false)` in its normal `show` method. -A compact representation should skip any type information, which would be redundant -with that printed once for the whole array. +This is used for printing array elements without repeating type information (which would +be redundant with that printed once for the whole array), and without line breaks inside +the representation of an element. + +To offer a compact representation different from its standard one, a custom type should +test `get(io, :compact, false)` in its normal `show` method. """ showcompact diff --git a/base/show.jl b/base/show.jl index a2d97d766f6bb3..b56a80681351c4 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1542,9 +1542,18 @@ end showcompact(x) = showcompact(STDOUT, x) function showcompact(io::IO, x) if get(io, :compact, false) - show(io, x) + if !get(io, :multiline, false) + show(io, x) + else + show(IOContext(io, :multiline => false), x) + end else - show(IOContext(io, :compact => true), x) + if get(io, :multiline, false) + show(IOContext(io, :compact => true), x) + else + show(IOContext(IOContext(io, :compact => true), + :multiline => false), x) + end end end diff --git a/doc/stdlib/io-network.rst b/doc/stdlib/io-network.rst index 3c3944a74c26f2..987ee3017410ca 100644 --- a/doc/stdlib/io-network.rst +++ b/doc/stdlib/io-network.rst @@ -434,9 +434,11 @@ Text I/O .. Docstring generated from Julia source - Show a more compact representation of a value. + Show a compact representation of a value. - This is used for printing array elements. If a new type has a different compact representation, it should test ``get(io, :compact, false)`` in its normal ``show`` method. A compact representation should skip any type information, which would be redundant with that printed once for the whole array. + This is used for printing array elements without repeating type information (which would be redundant with that printed once for the whole array), and without line breaks inside the representation of an element. + + To offer a compact representation different from its standard one, a custom type should test ``get(io, :compact, false)`` in its normal ``show`` method. .. function:: showall(x) diff --git a/test/show.jl b/test/show.jl index 0903408571386f..472068e07b4efa 100644 --- a/test/show.jl +++ b/test/show.jl @@ -447,3 +447,16 @@ end # PR #16651 @test !contains(repr(ones(10,10)), "\u2026") @test contains(sprint((io,x)->show(IOContext(io,:limit=>true), x), ones(30,30)), "\u2026") + +# showcompact() also sets multiline=>false (#16817) +let io = IOBuffer() + @enum TestShowEnum a b + showcompact(io, TestShowEnum) + @test takebuf_string(io) == "TestShowEnum" + showcompact(IOContext(io, :multiline=>true), TestShowEnum) + @test takebuf_string(io) == "TestShowEnum" + showcompact(IOContext(io, :compact=>true), TestShowEnum) + @test takebuf_string(io) == "TestShowEnum" + showcompact(IOContext(IOContext(io, :compact=>true), :multiline=>true), TestShowEnum) + @test takebuf_string(io) == "TestShowEnum" +end