@@ -2,9 +2,10 @@ mutable struct ColumnTable
22 col_names:: Vector{Symbol}
33 name_to_idx:: Dict{Symbol,Int}
44 cols:: Vector{AbstractVector}
5+ metadata:: Dict{Symbol,Any}
56end
67
7- function ColumnTable (col_names:: Vector{Symbol} , cols:: Vector{AbstractVector} )
8+ function ColumnTable (col_names:: Vector{Symbol} , cols:: Vector{AbstractVector} ; metadata = Dict {Symbol,Any} () )
89 length (col_names) == length (cols) || error (" Column names and columns must have the same length." )
910 nrows = isempty (cols) ? 0 : length (cols[1 ])
1011 @inbounds for i in 2 : length (cols)
@@ -14,17 +15,17 @@ function ColumnTable(col_names::Vector{Symbol}, cols::Vector{AbstractVector})
1415 @inbounds for i in eachindex (col_names)
1516 name_to_idx[col_names[i]] = i
1617 end
17- ColumnTable (col_names, name_to_idx, cols)
18+ ColumnTable (col_names, name_to_idx, cols, Dict {Symbol,Any} ( pairs (metadata)) )
1819end
1920
20- function ColumnTable (; kwargs... )
21+ function ColumnTable (; metadata = Dict {Symbol,Any} (), kwargs... )
2122 names_ = Symbol[]
2223 cols_ = AbstractVector[]
2324 for (k, v) in pairs (kwargs)
2425 push! (names_, k)
2526 push! (cols_, v)
2627 end
27- ColumnTable (names_, cols_)
28+ ColumnTable (names_, cols_; metadata = metadata )
2829end
2930
3031@inline function _column_idx (table:: ColumnTable , name:: Symbol )
@@ -39,14 +40,14 @@ Base.size(table::ColumnTable) = (isempty(table.cols) ? 0 : length(table.cols[1])
3940Base. length (table:: ColumnTable ) = first (size (table))
4041
4142function Base. getproperty (table:: ColumnTable , name:: Symbol )
42- if name === :col_names || name === :name_to_idx || name === :cols
43+ if name === :col_names || name === :name_to_idx || name === :cols || name === :metadata
4344 return getfield (table, name)
4445 end
4546 return table. cols[_column_idx (table, name)]
4647end
4748
4849function Base. setproperty! (table:: ColumnTable , name:: Symbol , values)
49- if name === :col_names || name === :name_to_idx || name === :cols
50+ if name === :col_names || name === :name_to_idx || name === :cols || name === :metadata
5051 setfield! (table, name, values)
5152 return values
5253 end
@@ -75,7 +76,7 @@ Base.getindex(table::ColumnTable, ::Colon, col::Int) = table.cols[col]
7576
7677function Base. copy (table:: ColumnTable )
7778 new_cols = AbstractVector[copy (col) for col in table. cols]
78- ColumnTable (copy (table. col_names), new_cols)
79+ ColumnTable (copy (table. col_names), new_cols; metadata = copy (table . metadata) )
7980end
8081
8182function Base.:(== )(a:: ColumnTable , b:: ColumnTable )
@@ -84,7 +85,8 @@ function Base.:(==)(a::ColumnTable, b::ColumnTable)
8485 @inbounds for i in eachindex (a. cols)
8586 a. cols[i] == b. cols[i] || return false
8687 end
87- return true
88+ a. metadata == b. metadata || return false
89+ true
8890end
8991
9092function Base. isequal (a:: ColumnTable , b:: ColumnTable )
@@ -93,7 +95,8 @@ function Base.isequal(a::ColumnTable, b::ColumnTable)
9395 @inbounds for i in eachindex (a. cols)
9496 isequal (a. cols[i], b. cols[i]) || return false
9597 end
96- return true
98+ isequal (a. metadata, b. metadata) || return false
99+ true
97100end
98101
99102function Base. sort! (table:: ColumnTable , by:: Symbol ; kwargs... )
@@ -112,3 +115,33 @@ Tables.columnnames(table::ColumnTable) = Tuple(table.col_names)
112115Tables. getcolumn (table:: ColumnTable , i:: Int ) = table. cols[i]
113116Tables. getcolumn (table:: ColumnTable , name:: Symbol ) = table. cols[_column_idx (table, name)]
114117Tables. schema (table:: ColumnTable ) = Tables. Schema (Tuple (table. col_names), Tuple (eltype .(table. cols)))
118+
119+ function Base. show (io:: IO , :: MIME"text/plain" , table:: ColumnTable )
120+ nrows, ncols = size (table)
121+ title = " ColumnTable($(nrows) x $(ncols) )"
122+
123+ syms = get (table. metadata, :symbols , nothing )
124+ scales = get (table. metadata, :scales , nothing )
125+ syms === nothing || print (io, " Symbols: " , syms, " \n " )
126+ scales === nothing || print (io, " Scales: " , scales, " \n " )
127+
128+ if ncols == 0
129+ print (io, title)
130+ return
131+ end
132+
133+ t_format = PrettyTables. TextTableFormat (
134+ borders= PrettyTables. text_table_borders__unicode_rounded
135+ )
136+
137+ PrettyTables. pretty_table (
138+ io,
139+ table;
140+ backend= :text ,
141+ title= title,
142+ table_format= t_format,
143+ row_number_column_label= " Row" ,
144+ row_labels= 1 : nrows,
145+ vertical_crop_mode= :middle ,
146+ )
147+ end
0 commit comments