@@ -98,10 +98,13 @@ struct TreeCharSet
9898 terminator
9999 skip
100100 dash
101+ trunc
101102end
102103
103104# Default charset
104- TreeCharSet () = TreeCharSet (' ├' ,' └' ,' │' ,' ─' )
105+ TreeCharSet () = TreeCharSet (' ├' ,' └' ,' │' ,' ─' ,' ⋮' )
106+ TreeCharSet (mid, term, skip, dash) = TreeCharSet (mid, term, skip, dash, ' ⋮' )
107+
105108
106109function print_prefix (io, depth, charset, active_levels)
107110 for current_depth in 0 : (depth- 1 )
@@ -113,8 +116,9 @@ function print_prefix(io, depth, charset, active_levels)
113116 end
114117end
115118
116- function _print_tree (printnode:: Function , io:: IO , tree, maxdepth = 5 ; depth = 0 , active_levels = Int[],
117- charset = TreeCharSet (), withinds = false , inds = [], from = nothing , to = nothing , roottree = tree)
119+ function _print_tree (printnode:: Function , io:: IO , tree, maxdepth = 5 ; indicate_truncation = true ,
120+ depth = 0 , active_levels = Int[], charset = TreeCharSet (), withinds = false ,
121+ inds = [], from = nothing , to = nothing , roottree = tree)
118122 nodebuf = IOBuffer ()
119123 isa (io, IOContext) && (nodebuf = IOContext (nodebuf, io))
120124 if withinds
@@ -132,23 +136,31 @@ function _print_tree(printnode::Function, io::IO, tree, maxdepth = 5; depth = 0,
132136 c = isa (treekind (roottree), IndexedTree) ?
133137 childindices (roottree, tree) : children (roottree, tree)
134138 if c != = ()
135- s = Iterators. Stateful (from === nothing ? pairs (c) : Iterators. Rest (pairs (c), from))
136- while ! isempty (s)
137- ind, child = popfirst! (s)
138- ind === to && break
139- active = false
140- child_active_levels = active_levels
141- print_prefix (io, depth, charset, active_levels)
142- if isempty (s)
143- print (io, charset. terminator)
144- else
145- print (io, charset. mid)
146- child_active_levels = push! (copy (active_levels), depth)
139+ if depth < maxdepth
140+ s = Iterators. Stateful (from === nothing ? pairs (c) : Iterators. Rest (pairs (c), from))
141+ while ! isempty (s)
142+ ind, child = popfirst! (s)
143+ ind === to && break
144+ active = false
145+ child_active_levels = active_levels
146+ print_prefix (io, depth, charset, active_levels)
147+ if isempty (s)
148+ print (io, charset. terminator)
149+ else
150+ print (io, charset. mid)
151+ child_active_levels = push! (copy (active_levels), depth)
152+ end
153+ print (io, charset. dash, ' ' )
154+ print_tree (printnode, io, child, maxdepth;
155+ indicate_truncation= indicate_truncation, depth = depth + 1 ,
156+ active_levels = child_active_levels, charset = charset, withinds= withinds,
157+ inds = withinds ? [inds; ind] : [], roottree = roottree)
147158 end
148- print (io, charset. dash, ' ' )
149- print_tree (printnode, io, child; depth = depth + 1 ,
150- active_levels = child_active_levels, charset = charset, withinds= withinds,
151- inds = withinds ? [inds; ind] : [], roottree = roottree)
159+ elseif indicate_truncation
160+ print_prefix (io, depth, charset, active_levels)
161+ println (io, charset. trunc)
162+ print_prefix (io, depth, charset, active_levels)
163+ println (io)
152164 end
153165 end
154166end
@@ -164,23 +176,31 @@ print_tree(tree, args...; kwargs...) = print_tree(stdout::IO, tree, args...; kwa
164176# Usage
165177Prints an ASCII formatted representation of the `tree` to the given `io` object.
166178By default all children will be printed up to a maximum level of 5, though this
167- value can be overriden by the `maxdepth` parameter. The charset to use in
179+ value can be overriden by the `maxdepth` parameter. Nodes that are truncated are
180+ indicated by a vertical ellipsis below the truncated node, this indication can be
181+ turned off by providing `indicate_truncation=false` as a kwarg. The charset to use in
168182printing can be customized using the `charset` keyword argument.
169183You can control the printing of individual nodes by passing a function `f(io, node)`;
170184the default is [`AbstractTrees.printnode`](@ref).
171185
172186# Examples
173187```julia
174- julia> print_tree(STDOUT, Dict("a"=>"b","b"=>['c','d']))
188+ julia> print_tree(stdout, Dict("a"=>"b","b"=>['c','d']))
175189Dict{String,Any}("b"=>['c','d'],"a"=>"b")
176190├─ b
177191│ ├─ c
178192│ └─ d
179193└─ a
180194 └─ b
181195
182- julia> print_tree(STDOUT,Dict("a"=>"b","b"=>['c','d']);
183- charset = TreeCharSet('+','\\ ','|',"--"))
196+ julia> print_tree(stdout, '0'=>'1'=>'2'=>'3', 2)
197+ '0'
198+ └─ '1'
199+ └─ '2'
200+ ⋮
201+
202+ julia> print_tree(stdout, Dict("a"=>"b","b"=>['c','d']);
203+ charset = TreeCharSet('+','\\ ','|',"--","⋮"))
184204Dict{String,Any}("b"=>['c','d'],"a"=>"b")
185205+-- b
186206| +-- c
0 commit comments