Skip to content

Commit

Permalink
Merge pull request #7147 from mbauman/dict-print
Browse files Browse the repository at this point in the history
Restore old Associative show method as print
  • Loading branch information
JeffBezanson committed Jun 9, 2014
2 parents e97bb5e + 85026ab commit d7552f3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
44 changes: 23 additions & 21 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ function summary(t::Associative)
string(typeof(t), " with ", n, (n==1 ? " entry" : " entries"))
end

function showcompact{K,V}(io::IO, t::Associative{K,V})
print(io, summary(t))
if !isempty(t)
print(io, ": ")
delims = (K == V == Any) ? ('{', '}') : ('[', ']')
function show{K,V}(io::IO, t::Associative{K,V})
if isempty(t)
print(io, typeof(t),"()")
else
if K === Any && V === Any
delims = ['{','}']
else
delims = ['[',']']
end
print(io, delims[1])
first = true
for (k, v) in t
first || print(io, ',')
first = false
showcompact(io, k)
show(io, k)
print(io, "=>")
showcompact(io, v)
show(io, v)
end
print(io, delims[2])
end
Expand Down Expand Up @@ -57,13 +61,14 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
end
end

function showdict{K,V}(io::IO, t::Associative{K,V}, limit_output::Bool = false,
showdict(t::Associative; kw...) = showdict(STDOUT, t; kw...)
function showdict{K,V}(io::IO, t::Associative{K,V}; limit::Bool = false,
rows = tty_rows()-3, cols = tty_cols())
print(io, summary(t))
isempty(t) && return
print(io, ":")

if limit_output
if limit
rows < 2 && (print(io, ""); return)
cols < 12 && (cols = 12) # Minimum widths of 2 for key, 4 for value
cols -= 6 # Subtract the widths of prefix " " separator " => "
Expand All @@ -81,9 +86,9 @@ function showdict{K,V}(io::IO, t::Associative{K,V}, limit_output::Bool = false,

for (i, (k, v)) in enumerate(t)
print(io, "\n ")
limit_output && i > rows && (print(io, rpad("", keylen), " => ⋮"); break)
limit && i > rows && (print(io, rpad("", keylen), " => ⋮"); break)

if limit_output
if limit
key = rpad(_truncate_at_width_or_chars(ks[i], keylen, "\r\n"), keylen)
else
key = sprint(show, k)
Expand All @@ -92,16 +97,13 @@ function showdict{K,V}(io::IO, t::Associative{K,V}, limit_output::Bool = false,
print(io, " => ")

val = sprint(show, v)
if limit_output
if limit
val = _truncate_at_width_or_chars(val, cols - keylen, "\r\n")
end
print(io, val)
end
end

show{K,V}(io::IO, t::Associative{K,V}) = showdict(io, t, false)
showlimited{K,V}(io::IO, t::Associative{K,V}) = showdict(io, t, true)

immutable KeyIterator{T<:Associative}
dict::T
end
Expand All @@ -112,15 +114,15 @@ end
summary{T<:Union(KeyIterator,ValueIterator)}(iter::T) =
string(T.name, " for a ", summary(iter.dict))

show(io::IO, iter::Union(KeyIterator,ValueIterator)) = showkv(io, iter, false)
showlimited(io::IO, iter::Union(KeyIterator,ValueIterator)) = showkv(io, iter, true)
show(io::IO, iter::Union(KeyIterator,ValueIterator)) = show(io, collect(iter))

function showkv{T<:Union(KeyIterator,ValueIterator)}(io::IO, iter::T, limit_output::Bool = false,
showkv(iter::Union(KeyIterator,ValueIterator); kw...) = showkv(STDOUT, iter; kw...)
function showkv{T<:Union(KeyIterator,ValueIterator)}(io::IO, iter::T; limit::Bool = false,
rows = tty_rows()-3, cols = tty_cols())
print(io, summary(iter))
isempty(iter) && return
print(io, ". ", T<:KeyIterator ? "Keys" : "Values", ":")
if limit_output
if limit
rows < 2 && (print(io, ""); return)
cols < 4 && (cols = 4)
cols -= 2 # For prefix " "
Expand All @@ -129,10 +131,10 @@ function showkv{T<:Union(KeyIterator,ValueIterator)}(io::IO, iter::T, limit_outp

for (i, v) in enumerate(iter)
print(io, "\n ")
limit_output && i >= rows && (print(io, ""); break)
limit && i >= rows && (print(io, ""); break)

str = sprint(show, v)
limit_output && (str = _truncate_at_width_or_chars(str, cols, "\r\n"))
limit && (str = _truncate_at_width_or_chars(str, cols, "\r\n"))
print(io, str)
end
end
Expand Down
6 changes: 6 additions & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ function writemime(io::IO, ::MIME"text/plain", v::DataType)
end
end

writemime(io::IO, ::MIME"text/plain", t::Associative) =
showdict(io, t, limit=true)
writemime(io::IO, ::MIME"text/plain", t::Union(KeyIterator,ValueIterator)) =
showkv(io, t, limit=true)


# showing exception objects as descriptive error messages

showerror(io::IO, e) = show(io, e)
Expand Down
6 changes: 3 additions & 3 deletions test/collections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ for d in (["\n" => "\n", "1" => "\n", "\n" => "2"],
for cols in (12, 40, 80), rows in (2, 10, 24)
# Ensure output is limited as requested
s = IOBuffer()
Base.showdict(s, d, true, rows, cols)
Base.showdict(s, d, limit=true, rows=rows, cols=cols)
out = split(takebuf_string(s),'\n')
for line in out[2:end]
@test strwidth(line) <= cols
Expand All @@ -188,7 +188,7 @@ for d in (["\n" => "\n", "1" => "\n", "\n" => "2"],

for f in (keys, values)
s = IOBuffer()
Base.showkv(s, f(d), true, rows, cols)
Base.showkv(s, f(d), limit=true, rows=rows, cols=cols)
out = split(takebuf_string(s),'\n')
for line in out[2:end]
@test strwidth(line) <= cols
Expand All @@ -197,7 +197,7 @@ for d in (["\n" => "\n", "1" => "\n", "\n" => "2"],
end
end
# Simply ensure these do not throw errors
Base.showdict(IOBuffer(), d, false)
Base.showdict(IOBuffer(), d, limit=false)
@test !isempty(summary(d))
@test !isempty(summary(keys(d)))
@test !isempty(summary(values(d)))
Expand Down

0 comments on commit d7552f3

Please sign in to comment.