Skip to content

Commit

Permalink
Revert "Table#line replaces #horizontal rule, which is now "soft-depr…
Browse files Browse the repository at this point in the history
…ecated""

This reverts commit bc1bb1c.
  • Loading branch information
matt-harvey committed Dec 16, 2019
1 parent bc1bb1c commit d718ef6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 106 deletions.
52 changes: 1 addition & 51 deletions README.md
Expand Up @@ -71,9 +71,7 @@ Tabulo has also been ported to Crystal (with some modifications): see [Tablo](ht
* [Accessing cell values](#accessing-cell-values)
* [Accessing the underlying enumerable](#accessing-sources)
* [Transposing rows and columns](#transposition)
* [Configuring borders](#borders)
* [Preset border options](#preset-borders)
* [Adding dividing lines between rows](#row-dividers)
* [Border configuration](#borders)
* [Comparison with other libraries](#motivation)
* [Contributing](#contributing)
* [License](#license)
Expand Down Expand Up @@ -658,9 +656,6 @@ represented in that column. This can be configured, however, along with other as
<a name="borders"></a>
### Configuring borders

<a name="preset-borders"></a>
#### Preset border options

You can configure the kind of border and divider characters that are used when the table is printed.
This is done using the `border` option passed to `Table.new`. The options are as follows.

Expand Down Expand Up @@ -750,51 +745,6 @@ but without a bottom border:
| 3 | false | true |
```

<a name="row-dividers"></a>
##### Adding dividing lines between rows

The preset border options described above do not include dividing lines between rows in the table
body. Rather, a method `#line` is provided, which can be called at any time while stepping through
a table, to print a horizontal line at that point. This allows dividing lines to be placed between
rows or groups of rows as desired. For example, if we wanted a horizontal line to appear after
every three rows, we could achieve that as follows:

```ruby
table = Tabulo::Table.new(1..9, :itself, :even?, :odd?, border: :modern)

table.each_with_index do |row, index|
if index != 0 && index % 3 == 0
puts table.line(:middle)
end

puts row
end

puts table.line(:bottom)
```

```
┌──────────────┬──────────────┬──────────────┐
│ itself │ even? │ odd? │
├──────────────┼──────────────┼──────────────┤
│ 1 │ false │ true │
│ 2 │ true │ false │
│ 3 │ false │ true │
├──────────────┼──────────────┼──────────────┤
│ 4 │ true │ false │
│ 5 │ false │ true │
│ 6 │ true │ false │
├──────────────┼──────────────┼──────────────┤
│ 7 │ false │ true │
│ 8 │ true │ false │
│ 9 │ false │ true │
└──────────────┴──────────────┴──────────────┘
```

Note the `#line` method takes an argument, `:top`, `:middle` or `:bottom`, describing the position at
which the line is intended to be printed. This ensures the correct characters will be used from the
configured border option's character set.

<a name="motivation"></a>
## Comparison with other libraries

Expand Down
2 changes: 1 addition & 1 deletion lib/tabulo/border.rb
Expand Up @@ -112,7 +112,7 @@ def self.from(initializer, styler = nil)
end

# @!visibility private
def line(column_widths, position = :bottom)
def horizontal_rule(column_widths, position = :bottom)
left, center, right, segment =
case position
when :top
Expand Down
36 changes: 5 additions & 31 deletions lib/tabulo/table.rb
Expand Up @@ -230,7 +230,7 @@ def add_column(label, header: nil, align_header: nil, align_body: nil,
# display in a fixed-width font.
def to_s
if column_registry.any?
bottom_edge = line(:bottom)
bottom_edge = horizontal_rule(:bottom)
rows = map(&:to_s)
bottom_edge.empty? ? join_lines(rows) : join_lines(rows + [bottom_edge])
else
Expand Down Expand Up @@ -272,30 +272,6 @@ def formatted_header
format_row(cells, @wrap_header_cells_to)
end

# @param [:top, :middle, :bottom] align_body Specifies the position for which
# the resulting horizontal dividing line is intended to be printed.
# This determines the border characters that are used to construct the line.
# @return [String] an "ASCII" graphical representation of a horizontal
# dividing line suitable for printing at the top, bottom or middle of the
# table.
# @example Print a horizontal divider between each pair of rows, and again
# at the bottom:
#
# table.each_with_index do |row, i|
# puts table.line(:middle) unless i == 0
# puts row
# end
# puts table.line(:bottom)
#
# It may be that `:top`, `:middle` and `:bottom` all look the same. Whether
# this is the case depends on the characters used for the table border.
def line(position)
column_widths = column_registry.map { |_, column| column.width + total_column_padding }
@border_instance.line(column_widths, position)
end

# @deprecated Use {#line} instead
#
# @param [:top, :middle, :bottom] align_body (:bottom) Specifies the position
# for which the resulting horizontal dividing line is intended to be printed.
# This determines the border characters that are used to construct the line.
Expand All @@ -314,10 +290,8 @@ def line(position)
# It may be that `:top`, `:middle` and `:bottom` all look the same. Whether
# this is the case depends on the characters used for the table border.
def horizontal_rule(position = :bottom)
# We don't print a deprecation warning here, as it would excessively "garble" existing
# tables that call this method between rows.
# Deprecation.warn("`Tabulo::Table#horizontal_rule'", "`#line'")
line(position)
column_widths = column_registry.map { |_, column| column.width + total_column_padding }
@border_instance.horizontal_rule(column_widths, position)
end

# Reset all the column widths so that each column is *just* wide enough to accommodate
Expand Down Expand Up @@ -450,9 +424,9 @@ def formatted_body_row(source, header: nil)
inner = format_row(cells, @wrap_body_cells_to)
if header
join_lines([
line(header == :top ? :top : :middle),
horizontal_rule(header == :top ? :top : :middle),
formatted_header,
line(:middle),
horizontal_rule(:middle),
inner,
].reject(&:empty?))
else
Expand Down
8 changes: 4 additions & 4 deletions spec/border_spec.rb
Expand Up @@ -102,15 +102,15 @@
end
end

describe "#line" do
describe "#horizontal_rule" do
it "returns a horizontal line suitable for rendering within a table with the indicated column widths, "\
"at the indicated position, with" do
border = Tabulo::Border.from(:modern)
column_widths = [3, 5, 12]

expect(border.line(column_widths, :top)).to eq("┌───┬─────┬────────────┐")
expect(border.line(column_widths, :middle)).to eq("├───┼─────┼────────────┤")
expect(border.line(column_widths, :bottom)).to eq("└───┴─────┴────────────┘")
expect(border.horizontal_rule(column_widths, :top)).to eq("┌───┬─────┬────────────┐")
expect(border.horizontal_rule(column_widths, :middle)).to eq("├───┼─────┼────────────┤")
expect(border.horizontal_rule(column_widths, :bottom)).to eq("└───┴─────┴────────────┘")
end
end

Expand Down
19 changes: 0 additions & 19 deletions spec/table_spec.rb
Expand Up @@ -1151,19 +1151,6 @@
expect(table.formatted_header).to eq("| N | Doubled |") end
end

describe "#line" do
let(:border) { :modern }

it "returns a horizontal line made up of the horizontal rule character, and appropriately placed "\
"corner characters, of an appropriate width for the table, suitable for the printing at the passed position" do
aggregate_failures do
expect(table.line(:top)).to eq("┌──────────────┬──────────────┐")
expect(table.line(:middle)).to eq("├──────────────┼──────────────┤")
expect(table.line(:bottom)).to eq("└──────────────┴──────────────┘")
end
end
end

describe "#horizontal_rule" do
let(:border) { :modern }

Expand All @@ -1176,12 +1163,6 @@
expect(table.horizontal_rule).to eq("└──────────────┴──────────────┘")
end
end

it "does not print a deprecation warning (even though it's deprecated)" do
expect(Tabulo::Deprecation).not_to receive(:warn)

table.horizontal_rule
end
end

describe "#pack" do
Expand Down

0 comments on commit d718ef6

Please sign in to comment.