Skip to content

Commit

Permalink
COSMETIC: Compact variant of default show method for acsets.
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters committed Oct 28, 2021
1 parent 5fc09d7 commit 2eb8dc3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
25 changes: 15 additions & 10 deletions src/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,21 @@ end

function Base.show(io::IO, acs::T) where {S,T<:StructACSet{S}}
s = SchemaDesc(S)
print(io, T)
println(io, "(")
join(io, Iterators.flatten((
(" $ob = $(parts(acs,ob))" for ob in s.obs),
(" $f : $(s.doms[f])$(s.codoms[f]) = $(subpart(acs,f))"
for f in s.homs),
(" $a : $(s.doms[a])$(s.codoms[a]) = $(subpart(acs,a))"
for a in s.attrs),
)), ",\n")
print(io, ")")
if get(io, :compact, false)
print(io, nameof(T))
print(io, ": ")
join(io, ("$ob = $(nparts(acs,ob))" for ob in s.obs), ", ")
else
print(io, T)
println(io, ":")
join(io, Iterators.flatten((
(" $ob = $(parts(acs,ob))" for ob in s.obs),
(" $f : $(s.doms[f])$(s.codoms[f]) = $(subpart(acs,f))"
for f in s.homs),
(" $a : $(s.doms[a])$(s.codoms[a]) = $(subpart(acs,a))"
for a in s.attrs),
)), "\n")
end
end

# TODO: implement Tables interface
Expand Down
11 changes: 7 additions & 4 deletions test/categorical_algebra/CSetDataStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ rem_parts!(dds, :X, [1,4])
@test incident(dds, 1, ) == [1,2]
@test incident(dds, 2, ) == []

## Pretty printing.

# Pretty printing.
dds = DDS()
add_parts!(dds, :X, 3, Φ=[2,3,3])
s = sprint(show, dds)
@test occursin("DDS(", s)
@test occursin("DDS:", s)
@test occursin("X = 1:3", s)
@test occursin("Φ : X → X = ", s)
s = sprint(show, dds, context=:compact => true)
@test occursin("DDS:", s)
@test !occursin("\n", s)
@test occursin("X = 3", s)

s = sprint(show, MIME"text/plain"(), dds)
@test occursin("DDS", s)
Expand Down Expand Up @@ -199,7 +202,7 @@ du = disjoint_union(d, d2)

# Pretty printing of data attributes.
s = sprint(show, d)
@test occursin("Dendrogram{Int64}(", s)
@test occursin("Dendrogram{Int64}:", s)
@test occursin("height : X → R = ", s)

s = sprint(show, MIME"text/plain"(), d)
Expand Down

0 comments on commit 2eb8dc3

Please sign in to comment.