Skip to content

Commit 4062cf2

Browse files
committed
🐛 [Text] Missing support for colors in column labels
1 parent 19d0b57 commit 4062cf2

File tree

7 files changed

+128
-12
lines changed

7 files changed

+128
-12
lines changed

docs/src/man/text/text_backend.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,12 @@ contains the following fields:
225225
- `stubhead_label::Crayon`: Crayon with the style for the stubhead label.
226226
- `row_label::Crayon`: Crayon with the style for the row labels.
227227
- `row_group_label::Crayon`: Crayon with the style for the row group label.
228-
- `first_line_column_label::Crayon`: Crayon with the style for the first column label lines.
229-
- `column_label::Crayon`: Crayon with the style for the rest of the column labels.
228+
- `first_line_column_label::Union{Crayon, Vector{Crayon}}`: Crayon or crayons with the style
229+
for the first column label lines. If a vector of crayons is passed, it must have the
230+
same length as the number columns in the table.
231+
- `column_label::Union{Crayon, Vector{Crayon}}`: Crayon or crayons with the style for the
232+
rest of the column labels. If a vector of crayons is passed, it must have the same
233+
length as the number of columns in the table.
230234
- `first_line_merged_column_label::Crayon`: Crayon with the style for the merged cells at
231235
the first column label line.
232236
- `merged_column_label::Crayon`: Crayon with the style for the merged cells at the rest of

src/backends/text/documentation.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,12 @@ contains the following fields:
234234
- `stubhead_label::Crayon`: Crayon with the style for the stubhead label.
235235
- `row_label::Crayon`: Crayon with the style for the row labels.
236236
- `row_group_label::Crayon`: Crayon with the style for the row group label.
237-
- `first_line_column_label::Crayon`: Crayon with the style for the first column label lines.
238-
- `column_label::Crayon`: Crayon with the style for the rest of the column labels.
237+
- `first_line_column_label::Union{Crayon, Vector{Crayon}}`: Crayon or crayons with the style
238+
for the first column label lines. If a vector of crayons is passed, it must have the
239+
same length as the number columns in the table.
240+
- `column_label::Union{Crayon, Vector{Crayon}}`: Crayon or crayons with the style for the
241+
rest of the column labels. If a vector of crayons is passed, it must have the same
242+
length as the number of columns in the table.
239243
- `first_line_merged_column_label::Crayon`: Crayon with the style for the merged cells at
240244
the first column label line.
241245
- `merged_column_label::Crayon`: Crayon with the style for the merged cells at the rest of

src/backends/text/text_backend.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,26 @@ function _text__print_table(
107107
))
108108

109109
(j > table_data.num_columns) && throw(ArgumentError(
110-
"The column index in the alignment anchor regex must be less than the number of columns ($table_data.num_columns)."
110+
"The column index in the alignment anchor regex must be less than the number of columns ($(table_data.num_columns))."
111111
))
112112
end
113113
end
114114

115+
# Check the style variables.
116+
if style.first_line_column_label isa Vector
117+
length(style.first_line_column_label) != table_data.num_columns &&
118+
throw(ArgumentError(
119+
"The length of `first_line_column_label` in `style` must be equal to the number of columns ($(table_data.num_columns))."
120+
))
121+
end
122+
123+
if style.column_label isa Vector
124+
length(style.column_label) != table_data.num_columns &&
125+
throw(ArgumentError(
126+
"The length of `column_label` in `style` must be equal to the number of columns ($(table_data.num_columns))."
127+
))
128+
end
129+
115130
# == Table Fitting in the Display ======================================================
116131

117132
# Process the horizontal lines at column labels.
@@ -1162,9 +1177,17 @@ function _text__print_table(
11621177
cell_width = printed_data_column_widths[jr]
11631178
rendered_cell = column_labels[ir, jr]
11641179
decoration = if ir == 1
1165-
style.first_line_column_label
1180+
if style.first_line_column_label isa Crayon
1181+
style.first_line_column_label
1182+
else
1183+
style.first_line_column_label[jr]
1184+
end
11661185
else
1167-
style.column_label
1186+
if style.column_label isa Crayon
1187+
style.column_label
1188+
else
1189+
style.column_label[jr]
1190+
end
11681191
end
11691192

11701193
cell === _IGNORE_CELL && continue

src/backends/text/types.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ Define the style of the tables printed with the text back end.
200200
- `stubhead_label::Crayon`: Crayon with the style for the stubhead label.
201201
- `row_label::Crayon`: Crayon with the style for the row labels.
202202
- `row_group_label::Crayon`: Crayon with the style for the row group label.
203-
- `first_line_column_label::Crayon`: Crayon with the style for the first column label lines.
204-
- `column_label::Crayon`: Crayon with the style for the rest of the column labels.
203+
- `first_line_column_label::Union{Crayon, Vector{Crayon}}`: Crayon or crayons with the style
204+
for the first column label lines. If a vector of crayons is passed, it must have the
205+
same length as the number columns in the table.
206+
- `column_label::Union{Crayon, Vector{Crayon}}`: Crayon or crayons with the style for the
207+
rest of the column labels. If a vector of crayons is passed, it must have the same
208+
length as the number of columns in the table.
205209
- `first_line_merged_column_label::Crayon`: Crayon with the style for the merged cells at
206210
the first column label line.
207211
- `merged_column_label::Crayon`: Crayon with the style for the merged cells at the rest of
@@ -213,16 +217,19 @@ Define the style of the tables printed with the text back end.
213217
- `omitted_cell_summary::Crayon`: Crayon with the style for the omitted cell summary.
214218
- `table_border::Crayon`: Crayon with the style for the table border.
215219
"""
216-
@kwdef struct TextTableStyle
220+
@kwdef struct TextTableStyle{
221+
TFCL<:Union{Crayon, Vector{Crayon}},
222+
TCL<:Union{Crayon, Vector{Crayon}}
223+
}
217224
title::Crayon = _TEXT__BOLD
218225
subtitle::Crayon = _TEXT__DEFAULT
219226
row_number_label::Crayon = _TEXT__BOLD
220227
row_number::Crayon = _TEXT__DEFAULT
221228
stubhead_label::Crayon = _TEXT__BOLD
222229
row_label::Crayon = _TEXT__BOLD
223230
row_group_label::Crayon = _TEXT__BOLD
224-
first_line_column_label::Crayon = _TEXT__BOLD
225-
column_label::Crayon = _TEXT__DARK_GRAY
231+
first_line_column_label::TFCL = _TEXT__BOLD
232+
column_label::TCL = _TEXT__DARK_GRAY
226233
first_line_merged_column_label::Crayon = _TEXT__BOLD_UNDERLINE
227234
merged_column_label::Crayon = _TEXT__DARK_GRAY_UNDERLINE
228235
summary_row_cell::Crayon = _TEXT__DEFAULT

src/precompile.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ PrecompileTools.@setup_workload begin
9797

9898
pretty_table(types)
9999

100+
# .. Text Table Styles .............................................................
101+
102+
pretty_table(
103+
matrix;
104+
style = TextTableStyle(
105+
first_line_column_label = [crayon"bold yellow" for i = 1:10]
106+
)
107+
)
108+
109+
pretty_table(
110+
matrix;
111+
style = TextTableStyle(
112+
column_label = [crayon"bold yellow" for i = 1:10]
113+
)
114+
)
115+
116+
pretty_table(
117+
matrix;
118+
style = TextTableStyle(
119+
first_line_column_label = [crayon"bold yellow" for i = 1:10],
120+
column_label = [crayon"bold yellow" for i = 1:10]
121+
)
122+
)
123+
100124
# -- Options Used in DataFrames.jl -------------------------------------------------
101125

102126
style = TextTableStyle(row_label = Crayon())

test/backends/text/colors.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## Description #############################################################################
2+
#
3+
# Text Back End: Tests related with colors.
4+
#
5+
############################################################################################
6+
7+
@testset "Colors" verbose = true begin
8+
@testset "Decoration of Column Labels" begin
9+
matrix = ones(3, 3)
10+
11+
expected = """
12+
┌────────┬────────┬────────┐
13+
\e[33;1m Col. 1 \e[0m│\e[33;1m Col. 2 \e[0m│\e[33;1m Col. 3 \e[0m│
14+
├────────┼────────┼────────┤
15+
│ 1.0 │ 1.0 │ 1.0 │
16+
│ 1.0 │ 1.0 │ 1.0 │
17+
│ 1.0 │ 1.0 │ 1.0 │
18+
└────────┴────────┴────────┘
19+
"""
20+
21+
result = pretty_table(
22+
String,
23+
matrix;
24+
color = true,
25+
style = TextTableStyle(; first_line_column_label = crayon"bold yellow")
26+
)
27+
28+
@test result == expected
29+
30+
expected = """
31+
┌────────┬────────┬────────┐
32+
\e[33;1m Col. 1 \e[0m│\e[34;1m Col. 2 \e[0m│\e[31;1m Col. 3 \e[0m│
33+
├────────┼────────┼────────┤
34+
│ 1.0 │ 1.0 │ 1.0 │
35+
│ 1.0 │ 1.0 │ 1.0 │
36+
│ 1.0 │ 1.0 │ 1.0 │
37+
└────────┴────────┴────────┘
38+
"""
39+
40+
result = pretty_table(
41+
String,
42+
matrix;
43+
color = true,
44+
style = TextTableStyle(; first_line_column_label = [
45+
crayon"bold yellow"
46+
crayon"bold blue"
47+
crayon"bold red"
48+
])
49+
)
50+
51+
@test result == expected
52+
end
53+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ end
6767
@testset "Text Back End Test" verbose = true begin
6868
include("./backends/text/alignment.jl")
6969
include("./backends/text/circular_reference.jl")
70+
include("./backends/text/colors.jl")
7071
include("./backends/text/cropping.jl")
7172
include("./backends/text/custom_cells.jl")
7273
include("./backends/text/default.jl")

0 commit comments

Comments
 (0)