33 print_tree(io::IO, tree; kwargs...)
44 print_tree(f::Function, io::IO, tree; kwargs...)
55
6- # Usage
7- Prints an ASCII formatted representation of the `tree` to the given `io` object.
8- By default all children will be printed up to a maximum level of 5, though this
9- value can be overriden by the `maxdepth` parameter. Nodes that are truncated are
10- indicated by a vertical ellipsis below the truncated node, this indication can be
11- turned off by providing `indicate_truncation=false` as a kwarg. The charset to use in
12- printing can be customized using the `charset` keyword argument.
13- You can control the printing of individual nodes by passing a function `f(io, node)`;
14- the default is [`AbstractTrees.printnode`](@ref).
6+ Print a text representation of `tree` to the given `io` object.
7+
8+ # Arguments
9+
10+ * `f::Function` - custom implementation of [`printnode`](@ref) to use. Should have the
11+ signature `f(io::IO, node)`.
12+ * `maxdepth::Integer = 5` - truncate printing of subtrees at this depth.
13+ * `indicate_truncation::Bool = true` - print a vertical ellipsis character beneath
14+ truncated nodes.
15+ * `charset::TreeCharSet` - [`TreeCharSet`](@ref) to use to print branches.
1516
1617# Examples
18+
1719```julia
1820julia> print_tree(stdout, Dict("a"=>"b","b"=>['c','d']))
1921Dict{String,Any}("b"=>['c','d'],"a"=>"b")
@@ -50,7 +52,7 @@ Print a single node. The default is to show a compact representation of `node`.
5052Override this if you want nodes printed in a custom way in [`print_tree`](@ref),
5153or if you want your print function to print part of the tree by default.
5254
53- # Example
55+ # Examples
5456
5557```
5658struct MyNode{T}
6062AbstractTrees.printnode(io::IO, node::MyNode) = print(io, "MyNode(\$ (node.data))")
6163```
6264"""
63- printnode (io:: IO , node) = show (IOContext (io, :compact => true ), node)
65+ printnode (io:: IO , node) = show (IOContext (io, :compact => true , :limit => true ), node)
66+
67+
68+ """
69+ repr_node(node; context=nothing)
70+
71+ Get the string representation of a node using [`printnode`](@ref). This works
72+ analagously to `Base.repr`.
73+
74+ `context` is an `IO` or `IOContext` object whose attributes are used for the
75+ I/O stream passed to `printnode`.
76+ """
77+ function repr_node (node; context= nothing )
78+ buf = IOBuffer ()
79+ io = context === nothing ? buf : IOContext (buf, context)
80+ printnode (io, node)
81+ return String (take! (buf))
82+ end
6483
6584
85+ """
86+ TreeCharSet
87+
88+ Set of characters (or strings) used to pretty-print tree branches in
89+ [`print_tree`](@ref).
90+ """
6691struct TreeCharSet
6792 mid
6893 terminator
@@ -71,12 +96,22 @@ struct TreeCharSet
7196 trunc
7297end
7398
74- # Default charset
75- TreeCharSet () = TreeCharSet (' ├' ,' └' ,' │' ,' ─' ,' ⋮' )
76- TreeCharSet (mid, term, skip, dash) = TreeCharSet (mid, term, skip, dash, ' ⋮' )
99+ """ Default `charset` argument used by [`print_tree`](@ref)."""
100+ const DEFAULT_CHARSET = TreeCharSet (' ├' , ' └' , ' │' , ' ─' , ' ⋮' )
101+ """ Charset using only ASCII characters."""
102+ const ASCII_CHARSET = TreeCharSet (" +" , " \\ " , " |" , " --" , " ..." )
103+
104+ function TreeCharSet ()
105+ Base. depwarn (" The 0-argument constructor of TreeCharSet is deprecated, use AbstractTrees.DEFAULT_CHARSET instead." , :TreeCharSet )
106+ return DEFAULT_CHARSET
107+ end
77108
78109
79- function print_prefix (io, depth, charset, active_levels)
110+ """
111+ Print tree branches in the initial part of a [`print_tree`](@ref) line, before
112+ the node itself is printed.
113+ """
114+ function print_prefix (io:: IO , depth:: Int , charset:: TreeCharSet , active_levels)
80115 for current_depth in 0 : (depth- 1 )
81116 if current_depth in active_levels
82117 print (io,charset. skip," " ^ (textwidth (charset. dash)+ 1 ))
@@ -87,7 +122,7 @@ function print_prefix(io, depth, charset, active_levels)
87122end
88123
89124function _print_tree (printnode:: Function , io:: IO , tree; maxdepth = 5 , indicate_truncation = true ,
90- depth = 0 , active_levels = Int[], charset = TreeCharSet () , withinds = false ,
125+ depth = 0 , active_levels = Int[], charset = DEFAULT_CHARSET , withinds = false ,
91126 inds = [], from = nothing , to = nothing , roottree = tree)
92127 nodebuf = IOBuffer ()
93128 isa (io, IOContext) && (nodebuf = IOContext (nodebuf, io))
@@ -148,6 +183,8 @@ print_tree(tree, args...; kwargs...) = print_tree(stdout::IO, tree, args...; kwa
148183
149184
150185"""
186+ repr_tree(tree; context=nothing, kw...)
187+
151188Get the string result of calling [`print_tree`](@ref) with the supplied arguments.
152189
153190The `context` argument works as it does in `Base.repr`.
0 commit comments