diff --git a/src/categorical_algebra/ACSetInterface.jl b/src/categorical_algebra/ACSetInterface.jl index 4c67c2b77..f1b279df8 100644 --- a/src/categorical_algebra/ACSetInterface.jl +++ b/src/categorical_algebra/ACSetInterface.jl @@ -285,20 +285,20 @@ end pretty_tables(acs::ACSet; kw...) = pretty_tables(stdout, acs; kw...) -function Base.show(io::IO, ::MIME"text/plain", acs::ACSet) - print(io, "ACSet") +function Base.show(io::IO, ::MIME"text/plain", acs::T) where T <: ACSet + print(io, T) print(io, " with elements ") - join(io, ["$ob = 1:$(nparts(acs,ob))" for ob in keys(tables(acs))], ", ") + join(io, ["$ob = $(parts(acs,ob))" for ob in keys(tables(acs))], ", ") println(io) pretty_tables(io, acs) end -function Base.show(io::IO, ::MIME"text/html", acs::ACSet) +function Base.show(io::IO, ::MIME"text/html", acs::T) where T <: ACSet println(io, "
") print(io, "") - print(io, "ACSet") + print(io, T) print(io, " with elements ") - join(io, ["$ob = 1:$(nparts(acs,ob))" for ob in keys(tables(acs))], ", ") + join(io, ["$ob = $(parts(acs,ob))" for ob in keys(tables(acs))], ", ") println(io, "") pretty_tables(io, acs, backend=Val(:html), standalone=false) println(io, "
") diff --git a/src/categorical_algebra/CSetDataStructures.jl b/src/categorical_algebra/CSetDataStructures.jl index 60ab66362..d475d670a 100644 --- a/src/categorical_algebra/CSetDataStructures.jl +++ b/src/categorical_algebra/CSetDataStructures.jl @@ -5,6 +5,7 @@ export @acset_type, @abstract_acset_type, @declare_schema, StructACSet, StructCS using MLStyle using StaticArrays using Reexport +import Tables @reexport using ..ACSetInterface using ..IndexUtils @@ -13,8 +14,6 @@ using ...Theories: SchemaDesc, SchemaDescType, CSetSchemaDescType, SchemaDescTyp ob_num, codom_num, attr, attrtype @reexport using ...Theories: FreeSchema using ...Meta: strip_lines -import Tables - # StructACSet Struct Generation ############################### @@ -601,19 +600,23 @@ end # Printing ########## -function Base.show(io::IO, acs::StructACSet{S,Ts,idxed}) where {S,Ts,idxed} +function Base.show(io::IO, acs::T) where {S,T<:StructACSet{S}} s = SchemaDesc(S) - print(io, "ACSet") - println(io, "(") - join(io, vcat( - [ " $ob = 1:$(nparts(acs,ob))" for ob in s.obs ], - [ " $attr_type = $(Ts.parameters[i])" for (i, attr_type) in enumerate(s.attrtypes) ], - [ " $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 diff --git a/src/categorical_algebra/FinSets.jl b/src/categorical_algebra/FinSets.jl index fe402d35b..080226aa3 100644 --- a/src/categorical_algebra/FinSets.jl +++ b/src/categorical_algebra/FinSets.jl @@ -11,7 +11,7 @@ using DataStructures: OrderedDict, IntDisjointSets, union!, find_root! using Reexport import StaticArrays using StaticArrays: StaticVector, SVector, SizedVector, similar_type -import Tables +import Tables, PrettyTables @reexport using ..Sets using ...GAT, ...Theories, ...CSetDataStructures, ...Graphs @@ -86,6 +86,16 @@ FinSet(nt::NamedTuple) = TabularSet(nt) Base.iterate(set::TabularSet, args...) = iterate(Tables.rows(set.table), args...) Base.length(set::TabularSet) = Tables.rowcount(set.table) +Base.show(io::IO, set::TabularSet) = print(io, "TabularSet($(set.table))") + +function Base.show(io::IO, ::MIME"text/plain", set::TabularSet{T}) where T + print(io, "$(length(set))-element TabularSet{$T}") + if !get(io, :compact, false) + println(io, ":") + PrettyTables.pretty_table(io, set.table, nosubheader=true) + end +end + # Discrete categories #-------------------- diff --git a/test/categorical_algebra/CSetDataStructures.jl b/test/categorical_algebra/CSetDataStructures.jl index 9e1d1c9b2..ef02175ac 100644 --- a/test/categorical_algebra/CSetDataStructures.jl +++ b/test/categorical_algebra/CSetDataStructures.jl @@ -73,17 +73,20 @@ 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 startswith(s, "ACSet") +@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 startswith(s, "ACSet") +@test occursin("DDS", s) @test occursin("X = 1:3", s) @test occursin("│ X │", s) @@ -199,12 +202,11 @@ du = disjoint_union(d, d2) # Pretty printing of data attributes. s = sprint(show, d) -@test startswith(s, "ACSet") -@test occursin("R = Int64", s) +@test occursin("Dendrogram{Int64}:", s) @test occursin("height : X → R = ", s) s = sprint(show, MIME"text/plain"(), d) -@test startswith(s, "ACSet") +@test occursin("Dendrogram{Int64}", s) # Allow type inheritance for data attributes. d_abs = Dendrogram{Number}() diff --git a/test/categorical_algebra/FinSets.jl b/test/categorical_algebra/FinSets.jl index cd9eb5ebf..a146b7832 100644 --- a/test/categorical_algebra/FinSets.jl +++ b/test/categorical_algebra/FinSets.jl @@ -25,6 +25,8 @@ set = FinSet(Set(1:2:5)) set = FinSet((x=[1,3,5], y=['a','b','c'])) @test length(set) == 3 @test map(NamedTuple, set) == [(x=1, y='a'), (x=3, y='b'), (x=5, y='c')] +@test startswith(sshow(set), "TabularSet(") +@test startswith(sprint(show, MIME("text/plain"), set), "3-element TabularSet") # Discrete categories #####################