Skip to content

Commit

Permalink
Set :multiline=>false in showcompact() and use it for enum printing
Browse files Browse the repository at this point in the history
This provides a simple way of printing the short representation
of a type. Cf. #16817.
  • Loading branch information
nalimilan committed Jun 9, 2016
1 parent 53040a0 commit 76ca815
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 76ca815

Please sign in to comment.