Skip to content

Commit

Permalink
Moderately improved dict printing
Browse files Browse the repository at this point in the history
This is a modest attempt at improving the situation for issue JuliaLang#1759.  I've added
slightly enhanced `summary`s with information about the number of k/v pairs for
Associative and Key/ValueIterator types.

I'm still slightly confused by all the different methods used for show, but
this PR keeps the old behavior (mostly) as `showcompact`. I then added new
functionality in `show` and `showlimited`, using newlines as delimiters between
key/value pairs.  When output is limited, values are truncated at newlines or
the TTY screen edge and a limited number of pairs are printed.

The key and value iterators no longer spit out entire dictionaries when shown.
They instead show a limited {} array of the keys and values.
  • Loading branch information
mbauman committed Mar 10, 2014
1 parent 5957a5f commit 27d5bc2
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,91 @@ function in(p::(Any,Any), a::Associative)
!is(v, secret_table_token) && (v == p[2])
end

function show{K,V}(io::IO, t::Associative{K,V})
if isempty(t)
print(io, typeof(t),"()")
else
function summary(t::Associative)
n = length(t)
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, ": ")
if K === Any && V === Any
delims = ['{','}']
else
delims = ['[',']']
end
print(io, delims[1])
first = true
for (k, v) = t
for (k, v) in t
first || print(io, ',')
first = false
show(io, k)
showcompact(io, k)
print(io, "=>")
show(io, v)
showcompact(io, v)
end
print(io, delims[2])
end
end

function showdict{K,V}(io::IO, t::Associative{K,V}, limit_output::Bool = false,
rows = tty_rows()-4, cols = tty_cols())
print(io,summary(t))
if !isempty(t)
println(io, ": ")
idx = 0
for (k, v) in t
idx < 1 || println(io)
print(" ")
key = sprint(showlimited,k)
print(io, key)
print(io, " => ")

val = sprint(showlimited,v)
if limit_output
subval = SubString(val,1,min(endof(val),cols-endof(key)-8))
newline = searchindex(subval,"\n")
lastidx = newline == 0 ? endof(subval) : newline - 1
if lastidx < endof(val)
val = string(SubString(subval,1,lastidx),"\u2026")
end
end
print(io, val)

if limit_output && idx > rows - 1
print("\n \u22ee => \u22ee")
break
end
idx += 1
end
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
immutable ValueIterator{T<:Associative}
dict::T
end

summary(iter::Union(KeyIterator,ValueIterator)) =
string(split(string(typeof(iter)),"{")[1]," for a ", summary(iter.dict))
function show(io::IO, iter::KeyIterator)
print(io,summary(iter))
println(io,". Keys:")
print(io," ")
show(io,{k for (k,_) in iter.dict})
end
function show(io::IO, iter::ValueIterator)
print(io,summary(iter))
println(io,". Values:")
print(io," ")
show(io,{v for (_,v) in iter.dict})
end

length(v::Union(KeyIterator,ValueIterator)) = length(v.dict)
isempty(v::Union(KeyIterator,ValueIterator)) = isempty(v.dict)
eltype(v::KeyIterator) = eltype(v.dict)[1]
Expand Down

0 comments on commit 27d5bc2

Please sign in to comment.