@@ -61,87 +61,79 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
6161end
6262
6363showdict (t:: Associative ; kw... ) = showdict (STDOUT, t; kw... )
64- function showdict {K,V} (io:: IO , t:: Associative{K,V} ; limit :: Bool = false , compact = false ,
64+ function showdict {K,V} (io:: IO , t:: Associative{K,V} ; compact = false ,
6565 sz= (s = tty_size (); (s[1 ]- 3 , s[2 ])))
66- shown_set = get (task_local_storage (), :SHOWNSET , nothing )
67- if shown_set === nothing
68- shown_set = ObjectIdDict ()
69- task_local_storage (:SHOWNSET , shown_set)
70- end
71- t in keys (shown_set) && (print (io, " #= circular reference =#" ); return )
72-
73- try
74- shown_set[t] = true
75- if compact
76- # show in a Julia-syntax-like form: Dict(k=>v, ...)
77- if isempty (t)
78- print (io, typeof (t), " ()" )
66+ (:SHOWN_SET => t) in io && (print (io, " #= circular reference =#" ); return )
67+
68+ recur_io = IOContext (io, :SHOWN_SET => t)
69+ limit:: Bool = limit_output (io)
70+ if compact
71+ # show in a Julia-syntax-like form: Dict(k=>v, ...)
72+ if isempty (t)
73+ print (io, typeof (t), " ()" )
74+ else
75+ if isleaftype (K) && isleaftype (V)
76+ print (io, typeof (t). name)
7977 else
80- if isleaftype (K) && isleaftype (V)
81- print (io, typeof (t). name)
82- else
83- print (io, typeof (t))
84- end
85- print (io, ' (' )
86- first = true
87- n = 0
88- for (k, v) in t
89- first || print (io, ' ,' )
90- first = false
91- show (io, k)
92- print (io, " =>" )
93- show (io, v)
94- n+= 1
95- limit && n >= 10 && (print (io, " …" ); break )
96- end
97- print (io, ' )' )
78+ print (io, typeof (t))
79+ end
80+ print (io, ' (' )
81+ first = true
82+ n = 0
83+ for (k, v) in t
84+ first || print (io, ' ,' )
85+ first = false
86+ show (recur_io, k)
87+ print (io, " =>" )
88+ show (recur_io, v)
89+ n+= 1
90+ limit && n >= 10 && (print (io, " …" ); break )
9891 end
99- return
92+ print (io, ' ) ' )
10093 end
94+ return
95+ end
10196
102- # Otherwise show more descriptively, with one line per key/value pair
103- rows, cols = sz
104- print (io, summary (t))
105- isempty (t) && return
106- print (io, " :" )
107- if limit
108- rows < 2 && (print (io, " …" ); return )
109- cols < 12 && (cols = 12 ) # Minimum widths of 2 for key, 4 for value
110- cols -= 6 # Subtract the widths of prefix " " separator " => "
111- rows -= 2 # Subtract the summary and final ⋮ continuation lines
112-
113- # determine max key width to align the output, caching the strings
114- ks = Array (AbstractString, min (rows, length (t)))
115- keylen = 0
116- for (i, k) in enumerate (keys (t))
117- i > rows && break
118- ks[i] = sprint (show, k)
119- keylen = clamp (length (ks[i]), keylen, div (cols, 3 ))
120- end
97+ # Otherwise show more descriptively, with one line per key/value pair
98+ rows, cols = sz
99+ print (io, summary (t))
100+ isempty (t) && return
101+ print (io, " :" )
102+ if limit
103+ rows < 2 && (print (io, " …" ); return )
104+ cols < 12 && (cols = 12 ) # Minimum widths of 2 for key, 4 for value
105+ cols -= 6 # Subtract the widths of prefix " " separator " => "
106+ rows -= 2 # Subtract the summary and final ⋮ continuation lines
107+
108+ # determine max key width to align the output, caching the strings
109+ ks = Array (AbstractString, min (rows, length (t)))
110+ keylen = 0
111+ for (i, k) in enumerate (keys (t))
112+ i > rows && break
113+ ks[i] = sprint (0 , show, k, env= recur_io)
114+ keylen = clamp (length (ks[i]), keylen, div (cols, 3 ))
121115 end
116+ end
122117
123- for (i, (k, v)) in enumerate (t)
124- print (io, " \n " )
125- limit && i > rows && (print (io, rpad (" ⋮" , keylen), " => ⋮" ); break )
118+ for (i, (k, v)) in enumerate (t)
119+ print (io, " \n " )
120+ limit && i > rows && (print (io, rpad (" ⋮" , keylen), " => ⋮" ); break )
126121
127- if limit
128- key = rpad (_truncate_at_width_or_chars (ks[i], keylen, " \r\n " ), keylen)
129- else
130- key = sprint (show, k)
131- end
132- print (io , key)
133- print (io, " => " )
122+ if limit
123+ key = rpad (_truncate_at_width_or_chars (ks[i], keylen, " \r\n " ), keylen)
124+ else
125+ key = sprint (0 , show, k, env = recur_io )
126+ end
127+ print (recur_io , key)
128+ print (io, " => " )
134129
135- if limit
136- val = with_output_limit (()-> sprint (show, v))
137- val = _truncate_at_width_or_chars (val, cols - keylen, " \r\n " )
138- print (io, val)
139- else
140- show (io, v)
141- end
130+ if limit
131+ val = sprint (0 , show, v, env= recur_io)
132+ val = _truncate_at_width_or_chars (val, cols - keylen, " \r\n " )
133+ print (io, val)
134+ else
135+ show (recur_io, v)
142136 end
143- finally
144- delete! (shown_set, t)
145137 end
146138end
147139
@@ -158,8 +150,9 @@ summary{T<:Union{KeyIterator,ValueIterator}}(iter::T) =
158150show (io:: IO , iter:: Union{KeyIterator,ValueIterator} ) = show (io, collect (iter))
159151
160152showkv (iter:: Union{KeyIterator,ValueIterator} ; kw... ) = showkv (STDOUT, iter; kw... )
161- function showkv {T<:Union{KeyIterator,ValueIterator}} (io:: IO , iter:: T ; limit :: Bool = false ,
153+ function showkv {T<:Union{KeyIterator,ValueIterator}} (io:: IO , iter:: T ;
162154 sz= (s = tty_size (); (s[1 ]- 3 , s[2 ])))
155+ limit:: Bool = limit_output (io)
163156 rows, cols = sz
164157 print (io, summary (iter))
165158 isempty (iter) && return
@@ -176,7 +169,7 @@ function showkv{T<:Union{KeyIterator,ValueIterator}}(io::IO, iter::T; limit::Boo
176169 limit && i >= rows && (print (io, " ⋮" ); break )
177170
178171 if limit
179- str = with_output_limit (() -> sprint (show, v) )
172+ str = sprint (0 , show, v, env = io )
180173 str = _truncate_at_width_or_chars (str, cols, " \r\n " )
181174 print (io, str)
182175 else
0 commit comments