Skip to content

Commit

Permalink
show brackets instead of header for arrays in show() and repr(). fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 19, 2014
1 parent 58440b7 commit 394e52b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion base/methodshow.jl
Expand Up @@ -151,7 +151,7 @@ function writemime(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
print(io, "</ul>") print(io, "</ul>")
end end
end end

# override usual show method for Vector{Method}: don't abbreviate long lists # override usual show method for Vector{Method}: don't abbreviate long lists
writemime(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method}) = writemime(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method}) =
showarray(io, mt, limit=false) showarray(io, mt, limit=false)
3 changes: 3 additions & 0 deletions base/repl.jl
Expand Up @@ -23,6 +23,9 @@ function writemime(io::IO, ::MIME"text/plain", v::AbstractVector)
end end
end end


writemime(io::IO, ::MIME"text/plain", v::AbstractArray) =
showarray(io, header=true, repr=false)

function writemime(io::IO, ::MIME"text/plain", v::DataType) function writemime(io::IO, ::MIME"text/plain", v::DataType)
show(io, v) show(io, v)
methods(v) # force constructor creation methods(v) # force constructor creation
Expand Down
53 changes: 23 additions & 30 deletions base/show.jl
Expand Up @@ -770,12 +770,16 @@ function print_matrix_vdots(io::IO,
end end
end end


function print_matrix(io::IO, function print_matrix(io::IO, X::AbstractVecOrMat,
X::AbstractVecOrMat, rows::Integer, cols::Integer, rows::Integer = tty_rows()-4,
pre::String, sep::String, post::String, cols::Integer = tty_cols(),
hdots::String, vdots::String, ddots::String, pre::String = " ",
hmod::Integer, vmod::Integer sep::String = " ",
) post::String = "",
hdots::String = " \u2026 ",
vdots::String = "\u22ee",
ddots::String = " \u22f1 ",
hmod::Integer = 5, vmod::Integer = 5)
cols -= length(pre) + length(post) cols -= length(pre) + length(post)
presp = repeat(" ", length(pre)) presp = repeat(" ", length(pre))
postsp = "" postsp = ""
Expand Down Expand Up @@ -845,13 +849,6 @@ function print_matrix(io::IO,
end end
end end
end end
print_matrix(io::IO, X::AbstractVecOrMat,
rows::Integer, cols::Integer) =
print_matrix(io, X, rows, cols, " ", " ", "",
" \u2026 ", "\u22ee", " \u22f1 ", 5, 5)

print_matrix(io::IO, X::AbstractVecOrMat) =
print_matrix(io, X, tty_rows()-4, tty_cols())


summary(x) = string(typeof(x)) summary(x) = string(typeof(x))


Expand All @@ -862,7 +859,7 @@ dims2string(d) = length(d) == 0 ? "0-dimensional" :
summary(a::AbstractArray) = summary(a::AbstractArray) =
string(dims2string(size(a)), " ", typeof(a)) string(dims2string(size(a)), " ", typeof(a))


function show_nd(io::IO, a::AbstractArray, limit, rows, cols) function show_nd(io::IO, a::AbstractArray, limit, print_matrix)
if isempty(a) if isempty(a)
return return
end end
Expand Down Expand Up @@ -894,7 +891,7 @@ function show_nd(io::IO, a::AbstractArray, limit, rows, cols)
for i = 1:(nd-1); print(io, "$(idxs[i]), "); end for i = 1:(nd-1); print(io, "$(idxs[i]), "); end
println(io, idxs[end], "] =") println(io, idxs[end], "] =")
slice = sub(a, 1:size(a,1), 1:size(a,2), idxs...) slice = sub(a, 1:size(a,1), 1:size(a,2), idxs...)
print_matrix(io, slice, rows, cols) print_matrix(io, slice)
print(io, idxs == tail ? "" : "\n\n") print(io, idxs == tail ? "" : "\n\n")
end end
cartesianmap(print_slice, tail) cartesianmap(print_slice, tail)
Expand All @@ -912,16 +909,6 @@ whos() = whos(r"")
whos(m::Module) = whos(m, r"") whos(m::Module) = whos(m, r"")
whos(pat::Regex) = whos(current_module(), pat) whos(pat::Regex) = whos(current_module(), pat)


function show{T}(io::IO, x::AbstractArray{T,0})
println(io, summary(x),":")
if isassigned(x)
sx = sprint(showcompact, x[])
else
sx = undef_ref_str
end
print(io, sx)
end

# global flag for limiting output # global flag for limiting output
# TODO: this should be replaced with a better mechanism. currently it is only # TODO: this should be replaced with a better mechanism. currently it is only
# for internal use in showing arrays. # for internal use in showing arrays.
Expand All @@ -932,25 +919,31 @@ _limit_output = false
showarray(X::AbstractArray; kw...) = showarray(STDOUT, X; kw...) showarray(X::AbstractArray; kw...) = showarray(STDOUT, X; kw...)
function showarray(io::IO, X::AbstractArray; function showarray(io::IO, X::AbstractArray;
header::Bool=true, limit::Bool=_limit_output, header::Bool=true, limit::Bool=_limit_output,
rows = tty_rows()-4, cols = tty_cols()) rows = tty_rows()-4, cols = tty_cols(), repr=false)
header && print(io, summary(X)) header && print(io, summary(X))
if !isempty(X) if !isempty(X)
header && println(io, ":") header && println(io, ":")
if ndims(X) == 0 if ndims(X) == 0
return showcompact(io, X[]) if isassigned(X)
return showcompact(io, X[])
else
return print(io, undef_ref_str)
end
end end
if !limit if !limit
rows = cols = typemax(Int) rows = cols = typemax(Int)
end end
punct = repr ? ("[", " ", "]") : (" ", " ", "")
if ndims(X)<=2 if ndims(X)<=2
print_matrix(io, X, rows, cols) print_matrix(io, X, rows, cols, punct...)
else else
show_nd(io, X, limit, rows, cols) show_nd(io, X, limit,
(io,slice)->print_matrix(io,slice,rows,cols,punct...))
end end
end end
end end


show(io::IO, X::AbstractArray) = showarray(io, X) show(io::IO, X::AbstractArray) = showarray(io, X, header=false, limit=false, repr=true)


print(io::IO, X::AbstractArray) = writedlm(io, X) print(io::IO, X::AbstractArray) = writedlm(io, X)


Expand Down

0 comments on commit 394e52b

Please sign in to comment.