Skip to content

Commit aafe26d

Browse files
authored
Refactor the Transforms show (#7)
1 parent fa4fa8d commit aafe26d

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

src/TransformsBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module TransformsBase
66

77
import AbstractTrees
88

9+
include("ioutils.jl")
910
include("interface.jl")
1011
include("sequential.jl")
1112
include("identity.jl")

src/interface.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,21 @@ reapply(transform::Transform, object, cache) = apply(transform, object) |> first
123123

124124
(transform::Transform)(object) = apply(transform, object) |> first
125125

126+
# -----------
127+
# IO METHODS
128+
# -----------
129+
130+
Base.summary(io::IO, transform::Transform) = print(io, "$(prettyname(transform)) transform")
131+
126132
function Base.show(io::IO, transform::Transform)
127-
T = typeof(transform)
128-
vals = getfield.(Ref(transform), fieldnames(T))
129-
strs = repr.(vals, context=io)
130-
print(io, "$(nameof(T))($(join(strs, ", ")))")
133+
name = prettyname(transform)
134+
ioctx = IOContext(io, :compact => true)
135+
print(io, "$name(")
136+
printfields(ioctx, transform, singleline=true)
137+
print(io, ")")
131138
end
132139

133140
function Base.show(io::IO, ::MIME"text/plain", transform::Transform)
134-
T = typeof(transform)
135-
fnames = fieldnames(T)
136-
len = length(fnames)
137-
print(io, "$(nameof(T)) transform")
138-
for (i, field) in enumerate(fnames)
139-
div = i == len ? "\n└─ " : "\n├─ "
140-
val = getfield(transform, field)
141-
str = repr(val, context=io)
142-
print(io, "$div$field = $str")
143-
end
141+
summary(io, transform)
142+
printfields(io, transform)
144143
end

src/ioutils.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ------------------------------------------------------------------
2+
# Licensed under the MIT License. See LICENSE in the project root.
3+
# ------------------------------------------------------------------
4+
5+
prettyname(obj) = prettyname(typeof(obj))
6+
function prettyname(T::Type)
7+
name = string(T)
8+
name = replace(name, r"{.*" => "")
9+
replace(name, r".*\." => "")
10+
end
11+
12+
printfields(io, obj; kwargs...) = printfields(io, obj, fieldnames(typeof(obj)); kwargs...)
13+
14+
function printfields(io, obj, fnames; singleline=false)
15+
if singleline
16+
vals = map(enumerate(fnames)) do (i, field)
17+
val = getfield(obj, i)
18+
str = repr(val, context=io)
19+
"$field: $str"
20+
end
21+
join(io, vals, ", ")
22+
else
23+
len = length(fnames)
24+
for (i, field) in enumerate(fnames)
25+
div = i == len ? "\n└─ " : "\n├─ "
26+
val = getfield(obj, i)
27+
str = repr(val, context=io)
28+
print(io, "$div$field: $str")
29+
end
30+
end
31+
end

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ using Test
4343
@test lastindex(T) == 2
4444
@test T[begin] == TestTransform()
4545
@test T[end] == Identity()
46+
47+
T1 = Identity()
48+
T2 = TestTransform()
49+
T3 = TestTransform() TestTransform()
50+
@test sprint(show, T1) == "Identity()"
51+
@test sprint(show, MIME("text/plain"), T1) == "Identity transform"
52+
@test sprint(show, T2) == "TestTransform()"
53+
@test sprint(show, MIME("text/plain"), T2) == "TestTransform transform"
54+
@test sprint(show, T3) == "TestTransform() → TestTransform()"
55+
@test sprint(show, MIME("text/plain"), T3) == """
56+
SequentialTransform
57+
├─ TestTransform()
58+
└─ TestTransform()"""
4659
end

0 commit comments

Comments
 (0)