From d718ef60da3c89142a92cf658197ea8398f5964c Mon Sep 17 00:00:00 2001 From: Matthew Harvey Date: Mon, 16 Dec 2019 20:48:10 +1100 Subject: [PATCH] Revert "Table#line replaces #horizontal rule, which is now "soft-deprecated"" This reverts commit bc1bb1c491fd367ed7eb3a1154bb04dc1b58858d. --- README.md | 52 +------------------------------------------- lib/tabulo/border.rb | 2 +- lib/tabulo/table.rb | 36 +++++------------------------- spec/border_spec.rb | 8 +++---- spec/table_spec.rb | 19 ---------------- 5 files changed, 11 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 718750c..2e3d69c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -658,9 +656,6 @@ represented in that column. This can be configured, however, along with other as ### Configuring borders - -#### 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. @@ -750,51 +745,6 @@ but without a bottom border: | 3 | false | true | ``` - -##### 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. - ## Comparison with other libraries diff --git a/lib/tabulo/border.rb b/lib/tabulo/border.rb index a5a4f67..472c9c0 100644 --- a/lib/tabulo/border.rb +++ b/lib/tabulo/border.rb @@ -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 diff --git a/lib/tabulo/table.rb b/lib/tabulo/table.rb index 1d32db2..58c365d 100644 --- a/lib/tabulo/table.rb +++ b/lib/tabulo/table.rb @@ -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 @@ -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. @@ -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 @@ -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 diff --git a/spec/border_spec.rb b/spec/border_spec.rb index f189f66..bd7945b 100644 --- a/spec/border_spec.rb +++ b/spec/border_spec.rb @@ -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 diff --git a/spec/table_spec.rb b/spec/table_spec.rb index f8c749f..a34eca2 100644 --- a/spec/table_spec.rb +++ b/spec/table_spec.rb @@ -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 } @@ -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