Skip to content

Commit 1f01f25

Browse files
committed
🐛 Summary rows must infer the name from the functions
1 parent 1d7f9bd commit 1f01f25

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

src/main.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ function _pretty_table(
399399
end
400400

401401
if !isnothing(summary_rows) && isnothing(summary_row_labels)
402-
summary_row_labels = SummaryLabelIterator(length(summary_rows))
402+
summary_row_labels = SummaryLabelIterator(summary_rows)
403403
end
404404

405405
if show_first_column_label_only

src/precompile.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ PrecompileTools.@setup_workload begin
8080
# -- Text --------------------------------------------------------------------------
8181

8282
pretty_table(matrix)
83+
pretty_table(matrix; summary_rows = [sum, sum, sum])
8384

8485
pretty_table(
8586
types;

src/types.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,15 @@ end
262262
# Type to lazily construct the summary row / column label if the user does not pass this
263263
# information.
264264
struct SummaryLabelIterator <: AbstractVector{String}
265-
length::Int
265+
summary_rows::Vector{Any}
266266
end
267267

268-
Base.size(s::SummaryLabelIterator) = (s.length,)
269-
Base.getindex(::SummaryLabelIterator, i::Int) = "Summary $i"
268+
Base.size(s::SummaryLabelIterator) = (length(s.summary_rows),)
269+
function Base.getindex(s::SummaryLabelIterator, i::Int)
270+
f_str = string(s.summary_rows[i])
271+
first(f_str) == '#' && return "Summary $i"
272+
return f_str
273+
end
270274

271275
# == PrettyTable ===========================================================================
272276

test/internal/cell_alignment.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
row_labels = ["Row 1", "Row 2", "Row 3"],
2121
row_group_labels = [2 => "Row Group"],
2222
summary_rows = [(data, i) -> i, (data, i) -> 2i],
23-
summary_row_labels = ["Summary 1", "Summary 2"],
2423
footnotes = [(:data, 1, 1) => "Footnote", (:data, 2, 2) => "Footnote"],
2524
source_notes = "Source Notes",
2625
title_alignment = :r,

test/internal/cell_data.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
9 10 11 12
1515
]
1616

17+
summary_rows = [(data, j) -> 20j, (data, j) -> 30j, sum]
18+
1719
td = PrettyTables.TableData(
1820
;
1921
data,
@@ -25,8 +27,8 @@
2527
row_number_column_label = "Row Number",
2628
row_labels = ["Row 1", "Row 2", "Row 3"],
2729
row_group_labels = [2 => "Row Group"],
28-
summary_rows = [(data, j) -> 20j, (data, j) -> 30j, sum],
29-
summary_row_labels = PrettyTables.SummaryLabelIterator(3),
30+
summary_rows = summary_rows,
31+
summary_row_labels = PrettyTables.SummaryLabelIterator(summary_rows),
3032
footnotes = [(:data, 1, 1) => "Footnote 1", (:data, 2, 2) => "Footnote 2"],
3133
source_notes = "Source Notes",
3234
data_alignment = [:l, :c, :r, :l],
@@ -132,7 +134,7 @@
132134

133135
action, rs, ps = PrettyTables._next(ps, td)
134136
cell = PrettyTables._current_cell(action, ps, td)
135-
@test cell == "Summary $i"
137+
@test cell == (i == 3 ? "sum" : "Summary $i")
136138

137139
for j in 1:4
138140
action, rs, ps = PrettyTables._next(ps, td)
@@ -179,6 +181,7 @@
179181
5 6 7 8
180182
9 10 11 12
181183
]
184+
summary_rows = [(data, j) -> 20j, (data, j) -> 30j, sum]
182185

183186
td = PrettyTables.TableData(
184187
;
@@ -191,8 +194,8 @@
191194
row_number_column_label = "Row Number",
192195
row_labels = ["Row 1", "Row 2", "Row 3"],
193196
row_group_labels = [2 => "Row Group"],
194-
summary_rows = [(data, j) -> 20j, (data, j) -> 30j, sum],
195-
summary_row_labels = PrettyTables.SummaryLabelIterator(3),
197+
summary_rows = summary_rows,
198+
summary_row_labels = PrettyTables.SummaryLabelIterator(summary_rows),
196199
footnotes = [(:data, 1, 1) => "Footnote 1", (:data, 2, 2) => "Footnote 2"],
197200
source_notes = "Source Notes",
198201
merge_column_label_cells = [MergeCells(1, 2, 2, "Merged #1"), MergeCells(2, 1, 2, "Merged #2")],
@@ -312,7 +315,7 @@
312315

313316
action, rs, ps = PrettyTables._next(ps, td)
314317
cell = PrettyTables._current_cell(action, ps, td)
315-
@test cell == "Summary $i"
318+
@test cell == (i == 3 ? "sum" : "Summary $i")
316319

317320
for j in 1:4
318321
action, rs, ps = PrettyTables._next(ps, td)

test/internal/print_state.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@
269269
@testset "Bottom Vertical Cropping" begin
270270
# == Create the Table Data =====================================================
271271

272+
summary_rows = [(data, i) -> i, (data, i) -> 2i]
273+
272274
td = PrettyTables.TableData(
273275
;
274276
data = rand(6, 4),
@@ -280,8 +282,8 @@
280282
row_number_column_label = "Row Number",
281283
row_labels = ["Row 1", "Row 2", "Row 3"],
282284
row_group_labels = [2 => "Row Group 1", 4 => "Row Group 1"],
283-
summary_rows = [(data, i) -> i, (data, i) -> 2i],
284-
summary_row_labels = PrettyTables.SummaryLabelIterator(2),
285+
summary_rows = summary_rows,
286+
summary_row_labels = PrettyTables.SummaryLabelIterator(summary_rows),
285287
footnotes = [(:data, 1, 1) => "Footnote", (:data, 2, 2) => "Footnote"],
286288
source_notes = "Source Notes",
287289
num_rows = 6,
@@ -504,6 +506,8 @@
504506
@testset "Middle Vertical Cropping" begin
505507
# == Create the Table Data =====================================================
506508

509+
summary_rows = [(data, i) -> i, (data, i) -> 2i]
510+
507511
td = PrettyTables.TableData(
508512
;
509513
data = rand(6, 4),
@@ -515,8 +519,8 @@
515519
row_number_column_label = "Row Number",
516520
row_labels = ["Row 1", "Row 2", "Row 3"],
517521
row_group_labels = [2 => "Row Group 1", 6 => "Row Group 2"],
518-
summary_rows = [(data, i) -> i, (data, i) -> 2i],
519-
summary_row_labels = PrettyTables.SummaryLabelIterator(2),
522+
summary_rows = summary_rows,
523+
summary_row_labels = PrettyTables.SummaryLabelIterator(summary_rows),
520524
footnotes = [(:data, 1, 1) => "Footnote", (:data, 2, 2) => "Footnote"],
521525
source_notes = "Source Notes",
522526
num_rows = 6,
@@ -792,6 +796,8 @@
792796

793797
# == Create the Table Data =====================================================
794798

799+
summary_rows = [(data, i) -> i, (data, i) -> 2i]
800+
795801
td = PrettyTables.TableData(
796802
;
797803
data = rand(3, 4),
@@ -802,8 +808,8 @@
802808
show_row_number_column = true,
803809
row_number_column_label = "Row Number",
804810
row_labels = ["Row 1", "Row 2", "Row 3"],
805-
summary_rows = [(data, i) -> i, (data, i) -> 2i],
806-
summary_row_labels = ["Summary 1", "Summary 2"],
811+
summary_rows = summary_rows,
812+
summary_row_labels = PrettyTables.SummaryLabelIterator(summary_rows),
807813
footnotes = [(:data, 1, 1) => "Footnote", (:data, 2, 2) => "Footnote"],
808814
source_notes = "Source Notes",
809815
num_rows = 3,

0 commit comments

Comments
 (0)