Skip to content

Commit c35dd0e

Browse files
authored
Merge pull request JuliaCollections#124 from FedeClaudi/master
Increase customization in `print_tree`
2 parents 7aa242f + cfd36b8 commit c35dd0e

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/printing.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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
66
Print 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
8385
in [`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

186188
branchwidth(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
267270
end
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...)
270273
print_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
279282
The `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))
287290
end

test/printing.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,10 @@ end
190190
""")
191191

192192
# Test printnode override
193-
str = repr_tree(tree) do io, s
194-
if s isa BoxNode
195-
print(io, s.s)
196-
else
197-
AbstractTrees.printnode(io, s)
198-
end
199-
end
193+
_f(io, s) = s isa BoxNode ? print(io, s.s) : AbstractTrees.printnode(io, s)
194+
_g(io, k) = AbstractTrees.print_child_key(io, k)
200195

196+
str = repr_tree(_f, _g, tree)
201197
@test endswith(str, """
202198
├─ "foo"
203199
├─ bar

0 commit comments

Comments
 (0)