11"""
2- print_tree(tree; kwargs ...)
3- print_tree(io::IO, tree; kwargs ...)
4- print_tree(f::Function, io::IO, tree; kwargs ...)
2+ print_tree(tree; kw ...)
3+ print_tree(io::IO, tree; kw ...)
4+ print_tree(f::Function, g::Function, io::IO, tree; kw ...)
55
66Print a text representation of `tree` to the given `io` object.
77
88# Arguments
99
1010* `f::Function` - custom implementation of [`printnode`](@ref) to use. Should have the
11- signature `f(io::IO, node)`.
11+ signature `f(io::IO, node; kw...)`.
12+ * `g::Function` - custom implementation of [`print_child_key`](@ref) to use. Should have the
13+ signature `g(io::IO, key;)`.
1214* `io::IO` - IO stream to write to.
1315* `tree` - tree to print.
1416* `maxdepth::Integer = 5` - truncate printing of subtrees at this depth.
@@ -82,7 +84,7 @@ Print a compact representation of a single node. By default, this prints `nodev
8284**OPTIONAL**: This can be extended for custom types and controls how nodes are shown
8385in [`print_tree`](@ref).
8486"""
85- printnode (io:: IO , node) = show (IOContext (io, :compact => true , :limit => true ), nodevalue (node))
87+ printnode (io:: IO , node; kw ... ) = show (IOContext (io, :compact => true , :limit => true ), nodevalue (node))
8688
8789
8890"""
@@ -185,17 +187,18 @@ print_child_key(io::IO, key::CartesianIndex) = show(io, Tuple(key))
185187
186188branchwidth (cs:: TreeCharSet ) = sum (textwidth .((cs. mid, cs. dash)))
187189
188- function print_tree (printnode:: Function , io:: IO , node;
190+ function print_tree (printnode:: Function , print_child_key :: Function , io:: IO , node;
189191 maxdepth:: Integer = 5 ,
190192 indicate_truncation:: Bool = true ,
191193 charset:: TreeCharSet = TreeCharSet (),
192194 printkeys:: Union{Bool,Nothing} = nothing ,
193195 depth:: Integer = 0 ,
194196 prefix:: AbstractString = " " ,
197+ kw...
195198 )
196199 # Get node representation as string
197200 buf = IOBuffer ()
198- printnode (IOContext (buf, io), node)
201+ printnode (IOContext (buf, io), node; kw ... )
199202 str = String (take! (buf))
200203
201204 # Copy buffer to output, prepending prefix to each line
@@ -259,14 +262,14 @@ function print_tree(printnode::Function, io::IO, node;
259262 child_prefix *= " " ^ (textwidth (key_str) + textwidth (charset. pair))
260263 end
261264
262- print_tree (printnode, io, child;
265+ print_tree (printnode, print_child_key, io, child;
263266 maxdepth= maxdepth, indicate_truncation= indicate_truncation, charset= charset,
264267 printkeys= printkeys, depth= depth+ 1 , prefix= child_prefix
265268 )
266269 end
267270end
268271
269- print_tree (io:: IO , node; kw... ) = print_tree (printnode, io, node; kw... )
272+ print_tree (io:: IO , node; kw... ) = print_tree (printnode, print_child_key, io, node; kw... )
270273print_tree (node; kw... ) = print_tree (stdout , node; kw... )
271274
272275
@@ -278,10 +281,10 @@ Get the string result of calling [`print_tree`](@ref) with the supplied argument
278281
279282The `context` argument works as it does in `Base.repr`.
280283"""
281- repr_tree (tree; context= nothing , kw... ) = repr_tree (printnode, tree; context= nothing , kw... )
282- function repr_tree (f, tree; context= nothing , kw... )
284+ repr_tree (tree; context= nothing , kw... ) = repr_tree (printnode, print_child_key, tree; context= nothing , kw... )
285+ function repr_tree (f:: Function , g :: Function , tree; context= nothing , kw... )
283286 buf = IOBuffer ()
284287 io = context === nothing ? buf : IOContext (buf, context)
285- print_tree (f, io, tree; kw... )
288+ print_tree (f, g, io, tree; kw... )
286289 return String (take! (buf))
287290end
0 commit comments