Skip to content

Commit

Permalink
use multiline=true, limit=false in reprmime and stringmime
Browse files Browse the repository at this point in the history
some small tweaks to output function changes and doc updates
  • Loading branch information
JeffBezanson committed May 23, 2016
1 parent 045488b commit 5bfe0cf
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
6 changes: 3 additions & 3 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2888,9 +2888,9 @@ Show an expression and result, returning the result.
"""
showcompact(x)
Show a more 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, :limit, false)` in its normal `show` method.
Show a more 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.
"""
showcompact

Expand Down
2 changes: 1 addition & 1 deletion base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ svdfact(M::Bidiagonal; thin::Bool=true) = svdfact!(copy(M),thin=thin)

function show(io::IO, M::Bidiagonal)
if get(io, :multiline, false)
invoke(show, (IO, AbstractArray), io, M)
Base.showarray(io, M)
else
println(io, summary(M), ":")
print(io, " diag:")
Expand Down
10 changes: 6 additions & 4 deletions base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,33 @@ mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x)
# format and is returned unmodified. This is useful so that raw data can be
# passed to display(m::MIME, x).

verbose_writemime(io, m, x) = writemime(IOContext(io,multiline=true,limit=false), m, x)

macro textmime(mime)
quote
mimeT = MIME{Symbol($mime)}
# avoid method ambiguities with the general definitions below:
# (Q: should we treat Vector{UInt8} as a String?)
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(writemime, m, x)
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_writemime, m, x)
Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x)

Base.Multimedia.istextmime(::mimeT) = true
if $(mime != "text/plain") # strings are shown escaped for text/plain
Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x
end
Base.Multimedia.reprmime(m::mimeT, x) = sprint(writemime, m, x)
Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_writemime, m, x)
Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x)
end
end

istextmime(::MIME) = false
function reprmime(m::MIME, x)
s = IOBuffer()
writemime(s, m, x)
verbose_writemime(s, m, x)
takebuf_array(s)
end
reprmime(m::MIME, x::Vector{UInt8}) = x
stringmime(m::MIME, x) = base64encode(writemime, m, x)
stringmime(m::MIME, x) = base64encode(verbose_writemime, m, x)
stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x)

# it is convenient to accept strings instead of ::MIME
Expand Down
18 changes: 8 additions & 10 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1434,11 +1434,8 @@ function print_matrix_repr(io, X::AbstractArray)
if compact && !haskey(io, :compact)
io = IOContext(io, :compact => compact)
end
prefix *= "["
ind = " "^length(prefix)
print(io, prefix)
print(io, prefix, "[")
for i=1:size(X,1)
i > 1 && print(io, ind)
for j=1:size(X,2)
j > 1 && print(io, " ")
if !isassigned(X,i,j)
Expand All @@ -1449,14 +1446,15 @@ function print_matrix_repr(io, X::AbstractArray)
end
end
if i < size(X,1)
print(io, ";")
else
print(io, "]")
print(io, "; ")
end
end
print(io, "]")
end

function show(io::IO, X::AbstractArray)
show(io::IO, X::AbstractArray) = showarray(io, X)

function showarray(io::IO, X::AbstractArray)
repr = !get(io, :multiline, false)
if repr && ndims(X) == 1
return show_vector(io, X, "[", "]")
Expand Down Expand Up @@ -1491,8 +1489,8 @@ function show(io::IO, X::AbstractArray)
print_matrix(io, X, punct...)
else
show_nd(io, X,
(io, slice) -> print_matrix(io, slice, punct...),
!repr)
(io, slice) -> print_matrix(io, slice, punct...),
!repr)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/io-network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Text I/O

.. Docstring generated from Julia source
Show a more compact representation of a value. This is used for printing array elements. If a new type has a different compact representation, it should test ``Base.limit_output(io)`` in its normal ``show`` method.
Show a more 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.

.. function:: showall(x)

Expand Down
7 changes: 6 additions & 1 deletion test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ for (i, T) in enumerate(types)
@test takebuf_string(io1) == @sprintf("Nullable{%s}(%s)", T, takebuf_string(io2))

a1 = [x2]
show(IOContext(io1, compact=false), a1)
show(IOContext(io2, compact=false), x2)
@test takebuf_string(io1) ==
@sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2))

show(io1, a1)
show(io2, x2)
show(IOContext(io2, compact=true), x2)
@test takebuf_string(io1) ==
@sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2))
end
Expand Down
4 changes: 3 additions & 1 deletion test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ end
# to test print_range in range.jl
replstrmime(x) = sprint((io,x) -> writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x), x)
@test replstrmime(1:4) == "1:4"
@test replstrmime(linspace(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0"
@test stringmime("text/plain", 1:4) == "1:4"
@test stringmime("text/plain", linspace(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0"
@test repr(linspace(1,5,7)) == "linspace(1.0,5.0,7)"
@test replstrmime(0:100.) == "0.0:1.0:100.0"
# next is to test a very large range, which should be fast because print_range
# only examines spacing of the left and right edges of the range, sufficient
Expand Down
9 changes: 4 additions & 5 deletions test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ let err_str,
err_str = @except_str randn(1)() MethodError
@test contains(err_str, "MethodError: objects of type Array{Float64,1} are not callable")
end
strmime(x) = sprint((io,x)->writemime(IOContext(io,multiline=true), MIME("text/plain"), x), x)
@test strmime(FunctionLike()) == "(::FunctionLike) (generic function with 0 methods)"
@test ismatch(r"^@doc \(macro with \d+ method[s]?\)$", strmime(getfield(Base, Symbol("@doc"))))
@test stringmime("text/plain", FunctionLike()) == "(::FunctionLike) (generic function with 0 methods)"
@test ismatch(r"^@doc \(macro with \d+ method[s]?\)$", stringmime("text/plain", getfield(Base, Symbol("@doc"))))

method_defs_lineno = @__LINE__
Base.Symbol() = throw(ErrorException("1"))
Expand Down Expand Up @@ -292,8 +291,8 @@ let err_str,
@test sprint(show, which(reinterpret(EightBitTypeT{Int32}, 0x54), Tuple{})) == "(::EightBitTypeT)() at $sp:$(method_defs_lineno + 6)"
@test startswith(sprint(show, which(getfield(Base, Symbol("@doc")), Tuple{Vararg{Any}})), "@doc(x...) at boot.jl:")
@test startswith(sprint(show, which(FunctionLike(), Tuple{})), "(::FunctionLike)() at $sp:$(method_defs_lineno + 7)")
@test strmime(FunctionLike()) == "(::FunctionLike) (generic function with 1 method)"
@test strmime(Core.arraysize) == "arraysize (built-in function)"
@test stringmime("text/plain", FunctionLike()) == "(::FunctionLike) (generic function with 1 method)"
@test stringmime("text/plain", Core.arraysize) == "arraysize (built-in function)"

err_str = @except_stackframe Symbol() ErrorException
@test err_str == " in Symbol() at $sn:$(method_defs_lineno + 0)"
Expand Down

0 comments on commit 5bfe0cf

Please sign in to comment.