From cb6cf12ced0630b72c915de984dc388217dc5316 Mon Sep 17 00:00:00 2001 From: Evan Patterson Date: Thu, 28 Oct 2021 10:30:25 -0700 Subject: [PATCH] ENH: Display `TabularSet` using PrettyTables. --- src/categorical_algebra/FinSets.jl | 12 +++++++++++- test/categorical_algebra/FinSets.jl | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) 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/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 #####################