11# Utilities for displaying tables using PrettyTables.jl
22
3+ @static if pkgversion (PrettyTables). major == 2
4+ # temporarily support PrettyTables v2 until v3 is more broadly established in the ecosystem
5+ const IS_PRETTYTABLES_V2 = true
6+ const PRETTYTABLES_TEXT_FORMAT = (; hlines= :none , vlines= :none )
7+ const PRETTYTABLES_TEXT_STYLE = (; title_crayon= PrettyTables. Crayon ())
8+ _prettytables_printf_formatter (fmt:: String , cols) = PrettyTables. ft_printf (fmt, cols)
9+ else
10+ const IS_PRETTYTABLES_V2 = false
11+ const PRETTYTABLES_TEXT_FORMAT = PrettyTables. TextTableFormat (;
12+ PrettyTables. @text__no_vertical_lines , PrettyTables. @text__no_horizontal_lines
13+ )
14+ const PRETTYTABLES_TEXT_STYLE = PrettyTables. TextTableStyle (;
15+ title= PrettyTables. Crayon ()
16+ )
17+ _prettytables_printf_formatter (fmt:: String , cols) = PrettyTables. fmt__printf (fmt, cols)
18+ end
19+
320# formatting functions for special columns
421# see https://ronisbr.github.io/PrettyTables.jl/stable/man/usage/#Formatters
522
623# Use Printf to format real elements to the number of `sigdigits`.
7- function ft_printf_sigdigits (sigdigits:: Int )
24+ function _prettytables_sigdigits_formatter (sigdigits:: Int )
825 return (v, _, _) -> begin
926 v isa Real || return v
1027 return _printf_with_sigdigits (v, sigdigits)
1128 end
1229end
13- function ft_printf_sigdigits (sigdigits:: Int , columns:: AbstractVector{Int} )
14- isempty (columns) && return ft_printf_sigdigits (sigdigits)
30+ function _prettytables_sigdigits_formatter (sigdigits:: Int , columns:: AbstractVector{Int} )
31+ isempty (columns) && return _prettytables_sigdigits_formatter (sigdigits)
1532 return (v, _, j) -> begin
1633 (v isa Real && j ∈ columns) || return v
1734 return _printf_with_sigdigits (v, sigdigits)
1835 end
1936end
2037
2138# Use Printf to format interval elements to the number of `sigdigits`.
22- function ft_printf_sigdigits_interval (sigdigits:: Int )
39+ function _prettytables_interval_formatter (sigdigits:: Int )
2340 return (v, _, _) -> begin
2441 v isa IntervalSets. AbstractInterval || return v
2542 tuple_string = map (Base. Fix2 (_printf_with_sigdigits, sigdigits), extrema (v))
@@ -32,7 +49,7 @@ function _interval_delimiter(x::IntervalSets.AbstractInterval)
3249 return occursin (" .. " , str) ? " .. " : " .."
3350end
3451
35- function ft_printf_sigdigits_matching_se (data, col:: Int , se_col:: Int ; kwargs... )
52+ function _prettytables_sigdigits_from_se_formatter (data, col:: Int , se_col:: Int ; kwargs... )
3653 se_vals = Tables. getcolumn (data, se_col)
3754 return (v, i, j) -> begin
3855 (v isa Real && col == j && se_vals[i] isa Real) || return v
@@ -61,34 +78,36 @@ function _prettytables_se_formatters(data; sigdigits_se=2)
6178 col ∈ col_names || continue
6279 idx_col = Tables. columnindex (data, col)
6380 idx_col == 0 && continue
64- push! (formatters, ft_printf_sigdigits_matching_se (data, idx_col, idx_se_col))
81+ push! (
82+ formatters, _prettytables_sigdigits_from_se_formatter (data, idx_col, idx_se_col)
83+ )
6584 end
6685 if ! isempty (se_cols_inds)
67- push! (formatters, ft_printf_sigdigits (sigdigits_se, se_cols_inds))
86+ push! (formatters, _prettytables_sigdigits_formatter (sigdigits_se, se_cols_inds))
6887 end
6988 return formatters
7089end
7190
7291function _prettytables_ess_formatter (data)
7392 cols = findall (_is_ess_label, Tables. columnnames (data))
7493 isempty (cols) && return nothing
75- return PrettyTables . ft_printf (" %d" , cols)
94+ return _prettytables_printf_formatter (" %d" , cols)
7695end
7796
7897function _prettytables_rhat_formatter (data)
7998 col_names = Tables. columnnames (data)
8099 cols = findall (x -> (x === :rhat || startswith (string (x), " rhat_" )), col_names)
81100 isempty (cols) && return nothing
82- return PrettyTables . ft_printf (" %.2f" , cols)
101+ return _prettytables_printf_formatter (" %.2f" , cols)
83102end
84103
85- function _default_prettytables_formatters (data; sigdigits_se= 2 , sigdigits_default= 3 )
104+ function _prettytables_default_formatters (data; sigdigits_se= 2 , sigdigits_default= 3 )
86105 formatters = Union{Function,Nothing}[]
87106 push! (formatters, _prettytables_integer_formatter (data))
88107 append! (formatters, _prettytables_se_formatters (data; sigdigits_se))
89108 push! (formatters, _prettytables_ess_formatter (data))
90- push! (formatters, ft_printf_sigdigits (sigdigits_default))
91- push! (formatters, ft_printf_sigdigits_interval (sigdigits_default))
109+ push! (formatters, _prettytables_sigdigits_formatter (sigdigits_default))
110+ push! (formatters, _prettytables_interval_formatter (sigdigits_default))
92111 return filter (! isnothing, formatters)
93112end
94113
@@ -102,14 +121,17 @@ function _text_alignment(data)
102121end
103122
104123function _text_alignment_anchor_regex (data)
105- alignment_anchor_regex = Dict {Int,Vector{Regex}} ()
124+ alignment_anchor_regex = Pair {Int,Vector{Regex}}[]
106125 for (i, k) in enumerate (Tables. columnnames (data))
107126 v = Tables. getcolumn (data, k)
108- if eltype (v) <: Real && ! (eltype (v) <: Integer ) && ! _is_ess_label (k)
109- alignment_anchor_regex[i] = [r" \. " , r" e" , r" ^NaN$" , r" Inf$" ]
127+ patterns = if eltype (v) <: Real && ! (eltype (v) <: Integer ) && ! _is_ess_label (k)
128+ [r" \. " , r" e" , r" ^NaN$" , r" Inf$" ]
110129 elseif eltype (v) <: IntervalSets.AbstractInterval
111- alignment_anchor_regex[i] = [r" \.\. " ]
130+ [r" \.\. " ]
131+ else
132+ continue
112133 end
134+ push! (alignment_anchor_regex, i => patterns)
113135 end
114136 return alignment_anchor_regex
115137end
@@ -123,25 +145,37 @@ function _show_prettytable(
123145 sigdigits_default= 3 ,
124146 extra_formatters= (),
125147 alignment= _text_alignment (data),
126- show_subheader= false ,
127- vcrop_mode= :middle ,
128- show_omitted_cell_summary= true ,
129- row_label_alignment= :l ,
148+ show_first_column_label_only= true ,
149+ vertical_crop_mode= :middle ,
150+ row_label_column_alignment= :l ,
130151 kwargs... ,
131152)
132- formatters = (
153+ formatters = [
133154 extra_formatters... ,
134- _default_prettytables_formatters (data; sigdigits_se, sigdigits_default)... ,
155+ _prettytables_default_formatters (data; sigdigits_se, sigdigits_default)... ,
156+ ]
157+ IS_PRETTYTABLES_V2 && return PrettyTables. pretty_table (
158+ io,
159+ data;
160+ alignment,
161+ formatters= Tuple (formatters),
162+ merge (
163+ (;
164+ show_subheader= (! show_first_column_label_only),
165+ vcrop_mode= vertical_crop_mode,
166+ row_label_alignment= row_label_column_alignment,
167+ ),
168+ kwargs,
169+ )... ,
135170 )
136171 PrettyTables. pretty_table (
137172 io,
138173 data;
139- show_subheader,
140- vcrop_mode,
141- show_omitted_cell_summary,
142- row_label_alignment,
143- formatters,
174+ show_first_column_label_only,
175+ vertical_crop_mode,
176+ row_label_column_alignment,
144177 alignment,
178+ formatters,
145179 kwargs... ,
146180 )
147181 return nothing
@@ -151,32 +185,49 @@ function _show_prettytable(
151185 io:: IO ,
152186 :: MIME"text/plain" ,
153187 data;
154- title_crayon = PrettyTables . Crayon () ,
155- hlines = :none ,
156- vlines = :none ,
157- newline_at_end = false ,
188+ style = PRETTYTABLES_TEXT_STYLE ,
189+ table_format = PRETTYTABLES_TEXT_FORMAT ,
190+ new_line_at_end = false ,
191+ title_alignment = :l ,
158192 alignment_anchor_regex= _text_alignment_anchor_regex (data),
159193 alignment_anchor_fallback= :r ,
160194 kwargs... ,
161195)
162- return _show_prettytable (
196+ IS_PRETTYTABLES_V2 && return _show_prettytable (
163197 io,
164198 data;
165199 backend= Val (:text ),
166- title_crayon,
167- hlines,
168- vlines,
169- newline_at_end,
200+ title_alignment,
201+ alignment_anchor_regex= Dict (alignment_anchor_regex),
202+ alignment_anchor_fallback,
203+ merge ((; style... , table_format... , newline_at_end= new_line_at_end), kwargs)... ,
204+ )
205+ return _show_prettytable (
206+ io,
207+ data;
208+ backend= :text ,
209+ style,
210+ table_format,
211+ new_line_at_end,
212+ title_alignment,
170213 alignment_anchor_regex,
171214 alignment_anchor_fallback,
172215 kwargs... ,
173216 )
174217end
218+
175219function _show_prettytable (
176- io:: IO , :: MIME"text/html" , data; minify= true , max_num_of_rows = 25 , kwargs...
220+ io:: IO , :: MIME"text/html" , data; minify= true , maximum_number_of_rows = 25 , kwargs...
177221)
222+ IS_PRETTYTABLES_V2 && return _show_prettytable (
223+ io,
224+ data;
225+ backend= Val (:html ),
226+ minify,
227+ merge ((; max_num_of_rows= maximum_number_of_rows), kwargs)... ,
228+ )
178229 return _show_prettytable (
179- io, data; backend= Val ( :html ) , minify, max_num_of_rows , kwargs...
230+ io, data; backend= :html , minify, maximum_number_of_rows , kwargs...
180231 )
181232end
182233
0 commit comments