Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add styler option #3

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spec/column_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe Tablo::Column do
align_header: Tablo::Justify::Left,
align_body: Tablo::Justify::Left,
formatter: ->(n : Tablo::CellType) { "%.2f" % n.as(Int32) },
styler: ->(n : Tablo::CellType) { n.to_s },
extractor: ->(n : Array(Tablo::CellType)) { n[0].as(Tablo::CellType) })

describe "#initialize" do
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ def add_columns_ndf(t : Tablo::Table)
t
end

def add_columns_ndfs(t : Tablo::Table)
t.add_column("N") { |n| n[0] }
t.add_column("Double") { |n| n[0].as(Number) * 2 }
t.add_column("Triple", formatter: ->(val : Tablo::CellType) { "%.2f" % val }, styler: ->(val : Tablo::CellType) { "\e[31m#{val}\e[0m" }) { |n| n[0].as(Number) * 3 }
t
end

def add_columns_ndn(t : Tablo::Table)
t.add_column("N") { |n| n[0] }
t.add_column("Double") { |n| n[0].as(Number) * 2 }
Expand Down
21 changes: 21 additions & 0 deletions spec/table_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,27 @@ describe "Tablo::Table" do
end
# <<<<< formatter parameter

# >>>>> styler parameter
describe "`styler` param" do
it "styles the formatted cell value for display, without affecting the width calculations" do
t13a = add_columns_ndfs(mktable_5i32(default_header_alignment: Tablo::Justify::Center))
t13a.to_s.should eq \
%(+--------------+--------------+--------------+
| N | Double | Triple |
+--------------+--------------+--------------+
| 1 | 2 | \e[31m3.00\e[0m |
| 2 | 4 | \e[31m6.00\e[0m |
| 3 | 6 | \e[31m9.00\e[0m |
| 4 | 8 | \e[31m12.00\e[0m |
| 5 | 10 | \e[31m15.00\e[0m |
+--------------+--------------+--------------+).gsub(/^ +/m, "")
top_right_body_cell = t13a.first.to_a.last
top_right_body_cell.should eq(3)
top_right_body_cell.should be_a(Int32)
end
end
# <<<<< styler parameter

# >>>>> extractor parameter
describe "'extractor' parameter is mandatory and must be provided as a block" do
t14a = add_columns_5n(mktable_5i32(default_header_alignment: Tablo::Justify::Center))
Expand Down
17 changes: 12 additions & 5 deletions src/column.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Tablo
@align_header : Justify,
@align_body : Justify,
@formatter : CellType -> String,
@styler : CellType -> String,
@extractor : Array(CellType) -> CellType)
end

Expand All @@ -25,7 +26,7 @@ module Tablo
type_alignment = infer_alignment(cell_datum) # Compute alignment from cell type
@align_header = type_alignment if @align_header.none?
@align_body = type_alignment if @align_body.none?
infilled_subcells(formatted_content, @align_body)
infilled_subcells(formatted_content, @align_body, true)
end

# Format the cell using the formatter proc
Expand All @@ -40,17 +41,23 @@ module Tablo

# Calculates the number of lines necessary to deal with cell contents and
# column width, and fills each resulting subcell with aligned data
private def infilled_subcells(str, alignment)
private def infilled_subcells(str, alignment, apply_styler = false)
str.split("\n").flat_map do |substr|
num_subsubcells = [1, (substr.size.to_f / width).ceil].max
(0...num_subsubcells).map do |i|
align_cell_content(substr[i * width, width], alignment)
align_cell_content(substr[i * width, width], alignment, apply_styler)
end
end
end

# Calculate alignment and padding of a (sub)cell
private def align_cell_content(content, alignment)
private def align_cell_content(content, alignment, apply_styler)
styled_content =
if apply_styler
@styler.call(content)
else
content
end
padding = [width - content.size, 0].max
left_padding, right_padding =
case alignment
Expand All @@ -62,7 +69,7 @@ module Tablo
half_padding = padding // 2
[padding - half_padding, half_padding]
end
"#{" " * left_padding}#{content}#{" " * right_padding}"
"#{" " * left_padding}#{styled_content}#{" " * right_padding}"
end

# Calculate alignment of a (sub)cell
Expand Down
2 changes: 2 additions & 0 deletions src/table.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Tablo
def add_column(label, header = nil, align_header = Justify::None,
align_body = Justify::None, width = nil,
formatter : CellType -> String = ->(n : CellType) { n.to_s },
styler : CellType -> String = ->(n : CellType) { n.to_s },
&extractor : Array(CellType) -> CellType)
if column_registry.has_key?(label)
raise InvalidColumnLabelError.new("Column label already used in this table.")
Expand All @@ -67,6 +68,7 @@ module Tablo
align_header,
align_body,
formatter,
styler,
extractor
)
end
Expand Down