Skip to content

Commit 54fe2fd

Browse files
authored
add option to change the printing style (#91)
* add option to change the printing style * format * additional test * Update runtests.jl * Update introduction.md
1 parent 9b1b478 commit 54fe2fd

File tree

5 files changed

+82
-3
lines changed

5 files changed

+82
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**/.ipynb_checkpoints/
66
**/*.gif
77
**/Manifest.toml
8+
**/LocalPreferences.toml
89
docs/build
910
docs/src/contributing.md
1011
docs/src/license.md

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
name = "RootedTrees"
22
uuid = "47965b36-3f3e-11e9-0dcf-4570dfd42a8c"
33
authors = ["Hendrik Ranocha <mail@ranocha.de> and contributors"]
4-
version = "2.13.0"
4+
version = "2.14.0"
55

66
[deps]
77
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
9+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
910
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1011
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1112

1213
[compat]
1314
Latexify = "0.15"
15+
Preferences = "1.3"
1416
RecipesBase = "1"
1517
Requires = "1"
1618
julia = "1.6"

docs/src/introduction.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ julia> butcher_representation(t)
5050
"[[[τ]τ²]τ⁵]"
5151
```
5252

53+
You can use the function [`RootedTrees.set_printing_style`](@ref) to change the
54+
printing style globally. For example,
55+
```@repl
56+
using RootedTrees
57+
t = rootedtree([1, 2, 3, 4, 3, 3, 2, 2, 2, 2, 2])
58+
RootedTrees.set_printing_style("butcher")
59+
t
60+
RootedTrees.set_printing_style("sequence")
61+
t
62+
```
63+
5364
There are also some simple plot recipes for [Plots.jl](https://github.com/JuliaPlots/Plots.jl).
5465
Thus, you can visualize a rooted tree `t` using `plot(t)` when `using Plots`.
5566

src/RootedTrees.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module RootedTrees
55
using LinearAlgebra: dot
66

77
using Latexify: Latexify
8+
using Preferences: @set_preferences!, @load_preference
89
using RecipesBase: RecipesBase
910
using Requires: @require
1011

@@ -182,8 +183,31 @@ end
182183
#RootedTree{T<:Integer}(sequence::Vector{T}) = RootedTree{T}(sequence, false)
183184

184185
function Base.show(io::IO, t::RootedTree{T}) where {T}
185-
print(io, "RootedTree{", T, "}: ")
186-
show(io, t.level_sequence)
186+
printing_style = @load_preference("printing_style")
187+
if printing_style == "butcher"
188+
print(io, butcher_representation(t))
189+
else
190+
print(io, "RootedTree{", T, "}: ")
191+
show(io, t.level_sequence)
192+
end
193+
end
194+
195+
"""
196+
RootedTrees.set_printing_style(style::String)
197+
198+
Set the printing style of rooted trees. Possible options are
199+
200+
- "butcher": print the [`butcher_representation`](@ref) of rooted trees
201+
- "sequence": print the level sequence representation
202+
203+
This system is based on [Preferences.jl](https://github.com/JuliaPackaging/Preferences.jl).
204+
"""
205+
function set_printing_style(style::String)
206+
if !(style in ("butcher", "sequence"))
207+
throw(ArgumentError("Invalid printing style: \"$(style)\""))
208+
end
209+
210+
@set_preferences!("printing_style"=>style)
187211
end
188212

189213
# comparison

test/runtests.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,47 @@ using Aqua: Aqua
101101
@test !(new_hash in hashes)
102102
end
103103

104+
@testset "printing" begin
105+
io = IOBuffer()
106+
107+
@testset "butcher" begin
108+
@test_nowarn RootedTrees.set_printing_style("butcher")
109+
110+
let t = rootedtree([1])
111+
show(io, t)
112+
@test String(take!(io)) == "τ"
113+
@test butcher_representation(t) == "τ"
114+
end
115+
116+
let t = rootedtree([1, 2, 2, 2, 3])
117+
show(io, t)
118+
@test String(take!(io)) == "[[τ]τ²]"
119+
@test butcher_representation(t) == "[[τ]τ²]"
120+
end
121+
end
122+
123+
@testset "sequence" begin
124+
@test_nowarn RootedTrees.set_printing_style("sequence")
125+
126+
let t = rootedtree([1])
127+
show(io, t)
128+
@test String(take!(io)) != "τ"
129+
@test butcher_representation(t) == "τ"
130+
end
131+
132+
let t = rootedtree([1, 2, 2, 2, 3])
133+
show(io, t)
134+
@test String(take!(io)) != "[[τ]τ²]"
135+
@test butcher_representation(t) == "[[τ]τ²]"
136+
end
137+
end
138+
139+
@testset "nonsense" begin
140+
@test_throws ArgumentError RootedTrees.set_printing_style("nonsense_style")
141+
@test_throws ArgumentError RootedTrees.set_printing_style("even_more_nonsense")
142+
end
143+
end
144+
104145
# see Table 301(I) etc. in butcher2016numerical
105146
@testset "functions on trees" begin
106147
t1 = rootedtree([1])

0 commit comments

Comments
 (0)