diff --git a/.gitignore b/.gitignore
index 01be5cf..ea325b8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,6 @@ tmp
doc
vendor
/.bundle
+
+# tempfiles
+*~
diff --git a/Gemfile.lock b/Gemfile.lock
index 401ad0e..593f378 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- terminal-table (2.0.0)
+ terminal-table (3.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
GEM
@@ -14,19 +14,19 @@ GEM
coderay (~> 1.1)
method_source (~> 1.0)
rake (13.0.1)
- rspec (3.9.0)
- rspec-core (~> 3.9.0)
- rspec-expectations (~> 3.9.0)
- rspec-mocks (~> 3.9.0)
- rspec-core (3.9.3)
- rspec-support (~> 3.9.3)
- rspec-expectations (3.9.3)
+ rspec (3.10.0)
+ rspec-core (~> 3.10.0)
+ rspec-expectations (~> 3.10.0)
+ rspec-mocks (~> 3.10.0)
+ rspec-core (3.10.0)
+ rspec-support (~> 3.10.0)
+ rspec-expectations (3.10.0)
diff-lcs (>= 1.2.0, < 2.0)
- rspec-support (~> 3.9.0)
- rspec-mocks (3.9.1)
+ rspec-support (~> 3.10.0)
+ rspec-mocks (3.10.0)
diff-lcs (>= 1.2.0, < 2.0)
- rspec-support (~> 3.9.0)
- rspec-support (3.9.4)
+ rspec-support (~> 3.10.0)
+ rspec-support (3.10.0)
term-ansicolor (1.7.1)
tins (~> 1.0)
tins (1.0.1)
@@ -34,6 +34,7 @@ GEM
PLATFORMS
ruby
+ x86_64-linux
DEPENDENCIES
bundler (~> 2)
@@ -45,4 +46,4 @@ DEPENDENCIES
tins (~> 1.0.0)
BUNDLED WITH
- 2.1.4
+ 2.2.0
diff --git a/History.rdoc b/History.rdoc
index d23248e..d7dcdbc 100644
--- a/History.rdoc
+++ b/History.rdoc
@@ -1,3 +1,39 @@
+3.0.0 / 2020-01-22
+
+- Support for (optional) Unicode border styles on tables. In order to support decent looking Unicode borders, different types of intersections get different types of intersection characters. This has the side effect of subtle formatting differences even for the ASCII table border case due to removal of certain intersections near colspans.
+
+For example, previously the output of a table may be:
+ +------+-----+
+ | Title |
+ +------+-----+
+ | Char | Num |
+ +------+-----+
+ | a | 1 |
+ | b | 2 |
+ | c | 3 |
+ +------+-----+
+
+And now the `+` character above the word Title is removed, as it is no longer considered an intersection:
+
+ +------------+
+ | Title |
+ +------+-----+
+ | Char | Num |
+ +------+-----+
+ | a | 1 |
+ | b | 2 |
+ +------+-----+
+
+- The default border remains an ASCII border for backwards compatibility, however multiple border classes are included / documented, and user defined border types can be applied as needed.
+
+In support of this update, the following issues were addressed:
+- colspan creates conflict with colorize (#95)
+- Use nice UTF box-drawing characters by default (#99)
+ - Note that `AsciiBorder` is stll the default
+- Border-left and border-right style (#100)
+- Helper function to style as Markdown (#111)
+ - Achieved using `MarkdownBorder`
+
2.0.0 / 2020-10-28
==================
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b584328
--- /dev/null
+++ b/README.md
@@ -0,0 +1,391 @@
+[
](https://github.com/tj/terminal-table/actions)
+
+# Terminal Table
+
+## Description
+
+Terminal Table is a fast and simple, yet feature rich table generator
+written in Ruby. It supports ASCII and Unicode formatted tables.
+
+## Installation
+
+```
+$ gem install terminal-table
+```
+## Usage
+
+### Basics
+
+To use Terminal Table:
+
+```ruby
+require 'terminal-table'
+```
+To generate a table, provide an array of arrays (which are interpreted as
+rows):
+
+```ruby
+rows = []
+rows << ['One', 1]
+rows << ['Two', 2]
+rows << ['Three', 3]
+table = Terminal::Table.new :rows => rows
+
+# > puts table
+#
+# +-------+---+
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# +-------+---+
+```
+The constructor can also be given a block which is either yielded the Table
+object or instance evaluated:
+
+```ruby
+table = Terminal::Table.new do |t|
+ t.rows = rows
+end
+
+table = Terminal::Table.new do
+ self.rows = rows
+end
+```
+Adding rows one by one:
+
+```ruby
+table = Terminal::Table.new do |t|
+ t << ['One', 1]
+ t.add_row ['Two', 2]
+end
+```
+To add separators between rows:
+
+```ruby
+table = Terminal::Table.new do |t|
+ t << ['One', 1]
+ t << :separator
+ t.add_row ['Two', 2]
+ t.add_separator
+ t.add_row ['Three', 3]
+end
+
+# > puts table
+#
+# +-------+---+
+# | One | 1 |
+# +-------+---+
+# | Two | 2 |
+# +-------+---+
+# | Three | 3 |
+# +-------+---+
+```
+Cells can handle multiline content:
+
+```ruby
+table = Terminal::Table.new do |t|
+ t << ['One', 1]
+ t << :separator
+ t.add_row ["Two\nDouble", 2]
+ t.add_separator
+ t.add_row ['Three', 3]
+end
+
+# > puts table
+#
+# +--------+---+
+# | One | 1 |
+# +--------+---+
+# | Two | 2 |
+# | Double | |
+# +--------+---+
+# | Three | 3 |
+# +--------+---+
+```
+### Head
+
+To add a head to the table:
+
+```ruby
+table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows
+
+# > puts table
+#
+# +-------+--------+
+# | Word | Number |
+# +-------+--------+
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# +-------+--------+
+```
+### Title
+
+To add a title to the table:
+
+```ruby
+table = Terminal::Table.new :title => "Cheatsheet", :headings => ['Word', 'Number'], :rows => rows
+
+# > puts table
+#
+# +------------+--------+
+# | Cheatsheet |
+# +------------+--------+
+# | Word | Number |
+# +------------+--------+
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# +------------+--------+
+```
+### Alignment
+
+To align the second column to the right:
+
+```ruby
+table.align_column(1, :right)
+
+# > puts table
+#
+# +-------+--------+
+# | Word | Number |
+# +-------+--------+
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# +-------+--------+
+```
+To align an individual cell, you specify the cell value in a hash along the
+alignment:
+
+```ruby
+table << ["Four", {:value => 4.0, :alignment => :center}]
+
+# > puts table
+#
+# +-------+--------+
+# | Word | Number |
+# +-------+--------+
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# | Four | 4.0 |
+# +-------+--------+
+```
+### Style
+
+To specify style options:
+
+```ruby
+table = Terminal::Table.new :headings => ['Word', 'Number'], :rows => rows, :style => {:width => 80}
+
+# > puts table
+#
+# +--------------------------------------+---------------------------------------+
+# | Word | Number |
+# +--------------------------------------+---------------------------------------+
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# +--------------------------------------+---------------------------------------+
+```
+And change styles on the fly:
+
+```ruby
+table.style = {:width => 40, :padding_left => 3, :border_x => "=", :border_i => "x"}
+
+# > puts table
+#
+# x====================x=================x
+# | Cheatsheet |
+# x====================x=================x
+# | Word | Number |
+# x====================x=================x
+# | One | 1 |
+# | Two | 2 |
+# | Three | 3 |
+# x====================x=================x
+```
+You can also use styles to add a separator after every row:
+
+```ruby
+table = Terminal::Table.new do |t|
+ t.add_row [1, 'One']
+ t.add_row [2, 'Two']
+ t.add_row [3, 'Three']
+ t.style = {:all_separators => true}
+end
+
+# > puts table
+#
+# +---+-------+
+# | 1 | One |
+# +---+-------+
+# | 2 | Two |
+# +---+-------+
+# | 3 | Three |
+# +---+-------+
+```
+You can also use styles to disable top and bottom borders of the table.
+
+```ruby
+table = Terminal::Table.new do |t|
+ t.headings = ['id', 'name']
+ t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]
+ t.style = { :border_top => false, :border_bottom => false }
+end
+
+# > puts table
+# | id | name |
+# +----+-------+
+# | 1 | One |
+# | 2 | Two |
+# | 3 | Three |
+```
+
+And also to disable left and right borders of the table.
+
+```ruby
+table = Terminal::Table.new do |t|
+ t.headings = ['id', 'name']
+ t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]
+ t.style = { :border_left => false, :border_right => false }
+end
+
+# > puts table
+# ----+-------
+# id | name
+# ----+-------
+# 1 | One
+# 2 | Two
+# 3 | Three
+# ----+-------
+```
+
+To change the default style options:
+
+```ruby
+Terminal::Table::Style.defaults = {:width => 80}
+```
+All Table objects created afterwards will inherit these defaults.
+
+### Constructor options and setter methods
+
+Valid options for the constructor are `:rows`, `:headings`, `:style` and `:title` -
+and all options can also be set on the created table object by their setter
+method:
+
+```ruby
+table = Terminal::Table.new
+table.title = "Cheatsheet"
+table.headings = ['Word', 'Number']
+table.rows = rows
+table.style = {:width => 40}
+```
+
+## New Formatting
+
+### Unicode Table Borders
+A support for Unicode 'box art' borders presented a challenge, as the basics of terminal-table only handled three border types: horizontal (x), vertical (y), and intersection (i). For proper box-art, it became necessary to enable different types of corners/edges for multiple intersection types.
+
+For the sake of backward compatiblity, the previous interface is still supported, this gem has been around a long time and making breaking changes would be inconvenient. The new interface is required for any complex and/or unicode style bordering. A few variations on border style are supported via some new classes and creation of additional classes (or modification of characters used in existing ones) will allow for customized border types.
+
+The simplest way to use an alternate border is one of the following:
+```
+table.style = { :border => :unicode }
+table.style = { :border => :unicode_round }
+table.style = { :border => :unicode_thick_edge }
+```
+
+These are a convenience wrapper around setting border using an instance of a class that inherits from Table::Terminal::Border
+```
+table.style = { :border => Terminal::Table::UnicodeBorder.new() }
+table.style = { :border => Terminal::Table::UnicodeRoundBorder.new() }
+table.style = { :border => Terminal::Table::UnicodeThickEdgeBorder.new() }
+```
+
+If you define a custom class and wish to use the symbol shortcut, you must namespace within `Terminal::Table` and end your class name with `Border`.
+
+### Markdown Compatiblity
+Per popular request, markdown formatted tables can be generated by using the following border style:
+
+```
+table.style = { :border => :markdown }
+```
+
+### Ascii Borders
+Ascii borders are default, but can be explicitly set with:
+```
+table.style = { :border => :ascii }
+```
+
+### Customizing Borders
+Inside the `UnicodeBorder` class, there are definitions for a variety of corner/intersection types.
+
+```
+@data = {
+nil => nil,
+nw: "┌", nx: "─", n: "┬", ne: "┐",
+yw: "│", y: "│", ye: "│",
+aw: "╞", ax: "═", ai: "╪", ae: "╡", ad: '╤', au: "╧",
+bw: "┝", bx: "━", bi: "┿", be: "┥", bd: '┯', bu: "┷",
+w: "├", x: "─", i: "┼", e: "┤", dn: "┬", up: "┴",
+sw: "└", sx: "─", s: "┴", se: "┘",
+}
+```
+
+Note that many are defined as directional (:nw == north-west), others defined in terms of 'x' or 'y'.
+The border that separates headings (below each heading) is of type `:double` and is defined with 'a*' entries.
+Alternate `:heavy` types that can be applied to separators can be defined with 'b*' entries.
+
+When defining a new set of borders, it's probably easiest to define a new class that inherits from UnicodeBorder and replaces the @data Hash.
+However, these corners can be these can be overridden with:
+
+```
+table.style = {border: :unicode}
+table.style.border[:nw] = '*' # override the north-west corner of the table
+```
+
+### Customizing row separators
+
+Row-separators can now be customized in a variety of ways. The default separator's border_type is referred to as `:mid`. Additional `:strong` / `:strong_a` and `:strong_b` separator styles can be applied to separate sections (e.g. header/footer/title).
+
+The separator border_type may be specified when a user-defined separator added. Alternatively, borders may be adjusted after the table's rows are elaborated, but before the table is rendered.
+
+Separator `border_type`s can be adjusted to be heavy, use double-lines, and different dash/dot styles. The border type should be one of:
+
+ div dash dot3 dot4
+ thick thick_dash thick_dot3 thick_dot4
+ heavy heavy_dash heavy_dot3 heavy_dot4
+ bold bold_dash bold_dot3 bold_dot4
+ double
+
+
+To manually set the separator border_type, the `add_separator` method may be called.
+```ruby
+add_separator(border_type: :heavy_dash)
+```
+
+Alternatively, if `style: :all_separators` is used, it may be necessary to elaborate the Rows prior to rendering.
+```ruby
+table = Terminal::Table.new do |t|
+ t.add_row [1, 'One']
+ t.add_row [2, 'Two']
+ t.add_row [3, 'Three']
+ t.style = {:all_separators => true}
+end
+rows = table.elaborate_rows
+rows[2].border_type = :heavy # modify separator row: emphasize below title
+puts table.render
+```
+
+## More examples
+
+For more examples, please see the `examples` directory included in the
+source distribution.
+
+## Author
+
+TJ Holowaychuk
+
+Unicode table support by Ben Bowers https://github.com/nanobowers
diff --git a/examples/examples.rb b/examples/examples.rb
old mode 100644
new mode 100755
diff --git a/examples/examples_unicode.rb b/examples/examples_unicode.rb
new file mode 100755
index 0000000..ad8455f
--- /dev/null
+++ b/examples/examples_unicode.rb
@@ -0,0 +1,89 @@
+#!/bin/env ruby
+
+$:.unshift File.dirname(__FILE__) + '/../lib'
+require 'terminal-table/import'
+
+Terminal::Table::Style.defaults = { :border => :unicode_round }
+# Terminal::Table::UnicodeThickEdgeBorder.new()
+
+puts
+puts table(['a', 'b'], [1, 2], [3, 4])
+
+puts
+puts table(['name', 'content'], ['ftp.example.com', '1.1.1.1'], ['www.example.com', '|lalalala|lalala|'])
+
+puts
+t = table ['a', 'b']
+t.style = {:padding_left => 2, :width => 80}
+t << [1, 2]
+t << [3, 4]
+t << :separator
+t << [4, 6]
+puts t
+
+puts
+user_table = table do |v|
+ v.title = "Contact Information"
+ v.headings = 'First Name', 'Last Name', 'Email'
+ v << %w( TJ Holowaychuk tj@vision-media.ca )
+ v << %w( Bob Someone bob@vision-media.ca )
+ v << %w( Joe Whatever bob@vision-media.ca )
+end
+puts user_table
+
+puts
+user_table = table do |v|
+ v.style.width = 80
+ v.headings = 'First Name', 'Last Name', 'Email'
+ v << %w( TJ Holowaychuk tj@vision-media.ca )
+ v << %w( Bob Someone bob@vision-media.ca )
+ v << %w( Joe Whatever bob@vision-media.ca )
+end
+puts user_table
+
+puts
+user_table = table do
+ self.headings = 'First Name', 'Last Name', 'Email'
+ add_row ['TJ', 'Holowaychuk', 'tj@vision-media.ca']
+ add_row ['Bob', 'Someone', 'bob@vision-media.ca']
+ add_row ['Joe', 'Whatever', 'joe@vision-media.ca']
+ add_separator
+ add_row ['Total', { :value => '3', :colspan => 2, :alignment => :right }]
+ align_column 1, :center
+end
+puts user_table
+
+puts
+user_table = table do
+ self.headings = ['First Name', 'Last Name', {:value => 'Phones', :colspan => 2, :alignment => :center}]
+ #add_row ['Bob', 'Someone', '123', '456']
+ add_row [{:value => "Bob Someone", :colspan => 3, :alignment => :center}, '123456']
+ add_row :separator
+ add_row ['TJ', 'Holowaychuk', {:value => "No phones\navaiable", :colspan => 2, :alignment => :center}]
+ add_row :separator
+ add_row ['Joe', 'Whatever', '4324', '343242']
+end
+puts user_table
+
+rows = []
+rows << ['Lines', 100]
+rows << ['Comments', 20]
+rows << ['Ruby', 70]
+rows << ['JavaScript', 30]
+puts table([nil, 'Lines'], *rows)
+
+rows = []
+rows << ['Lines', 100]
+rows << ['Comments', 20]
+rows << ['Ruby', 70]
+rows << ['JavaScript', 30]
+puts table(nil, *rows)
+
+rows = []
+rows << ['Lines', 100]
+rows << ['Comments', 20]
+rows << ['Ruby', 70]
+rows << ['JavaScript', 30]
+table = table([{ :value => 'Stats', :colspan => 2, :alignment => :center }], *rows)
+table.align_column 1, :right
+puts table
diff --git a/examples/issue100.rb b/examples/issue100.rb
new file mode 100755
index 0000000..41527be
--- /dev/null
+++ b/examples/issue100.rb
@@ -0,0 +1,34 @@
+#!/usr/bin/env ruby
+
+# Methods to suppress left/right borders using border_left & border_right
+
+require_relative "../lib/terminal-table"
+table = Terminal::Table.new do |t|
+ t.headings = ['id', 'name']
+ t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]
+ t.style = { :border_left => false, :border_top => false, :border_bottom => false }
+end
+
+puts table
+puts
+
+# no right
+table.style = {:border_right => false }
+puts table
+puts
+
+# no right
+table.style = {:border_left => true }
+puts table
+puts
+
+table.style.border = Terminal::Table::UnicodeBorder.new
+puts table
+
+
+table.style = {:border_right => false, :border_left => true }
+puts table
+
+table.style = {:border_right => true, :border_left => false }
+puts table
+
diff --git a/examples/issue111.rb b/examples/issue111.rb
new file mode 100755
index 0000000..a7a6c71
--- /dev/null
+++ b/examples/issue111.rb
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+require_relative "../lib/terminal-table"
+puts Terminal::Table.new(headings: ['heading A', 'heading B'], rows: [['a', 'b'], ['a', 'b']], style: {border: Terminal::Table::MarkdownBorder.new})
+
diff --git a/examples/issue95.rb b/examples/issue95.rb
new file mode 100755
index 0000000..e6ddbf8
--- /dev/null
+++ b/examples/issue95.rb
@@ -0,0 +1,42 @@
+#!/usr/bin/env ruby
+require 'colorize'
+require_relative '../lib/terminal-table.rb'
+
+original_sample_data = [
+["Sep 2016", 33, [-38, -53.52], 46, [-25, -35.21]],
+["Oct 2016", 35, [2, 6.06], 50, [4, 8.69]]
+]
+
+table = Terminal::Table.new headings: ["Month".cyan,"Monthly IT".cyan,"IT Difference OPM".cyan,
+"Monthly OOT".cyan,"OOT Difference OPM".cyan], rows: original_sample_data
+
+table.style = { padding_left: 2, padding_right: 2, border_x: "-".blue, border_y: "|".blue, border_i: "+".blue }
+
+puts table
+
+puts ""
+puts "^ good table"
+puts "v wonky table"
+puts ""
+
+split_column_sample_data = [
+["Sep 2016", 33, -38, -53.52, 46, -25, -35.21],
+["Oct 2016", 35, 2, 6.06, 50, 4, 8.69]
+]
+
+table = Terminal::Table.new headings: ["Month".cyan,"Monthly IT".cyan,
+{value: "IT Difference OPM".cyan, colspan: 2}, "Monthly OOT".cyan,
+{value: "OOT Difference OPM".cyan, colspan: 2}], rows: split_column_sample_data
+
+table.style = { padding_left: 2, padding_right: 2, border_x: "-".blue, border_y: "|".blue, border_i: "+".blue }
+
+puts table
+
+
+table = Terminal::Table.new headings: ["Month","Monthly IT",
+{value: "IT Difference OPM", colspan: 2}, "Monthly OOT",
+{value: "OOT Difference OPM", colspan: 2}], rows: split_column_sample_data
+
+table.style = { padding_left: 2, padding_right: 2, border_x: "-".blue, border_y: "|".cyan, border_i: "+" }
+
+puts table
diff --git a/examples/strong_separator.rb b/examples/strong_separator.rb
new file mode 100755
index 0000000..52ad195
--- /dev/null
+++ b/examples/strong_separator.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+require_relative "../lib/terminal-table"
+
+#
+# An example of how to manually add separators with non-default
+# border_type to enable a footer row.
+#
+table = Terminal::Table.new do |t|
+ # set the style
+ t.style = { border: :unicode_thick_edge }
+
+ # header row
+ t.headings = ['fruit', 'count']
+
+ # some row data
+ t.add_row ['apples', 7]
+ t.add_row ['bananas', 19]
+ t.add_separator border_type: :strong
+ # footer row
+ t.add_row ['total', 26]
+end
+
+puts table.render
diff --git a/lib/terminal-table.rb b/lib/terminal-table.rb
index c8238e1..ebea259 100644
--- a/lib/terminal-table.rb
+++ b/lib/terminal-table.rb
@@ -21,6 +21,6 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
-%w(cell row separator style table table_helper version).each do |file|
- require "terminal-table/#{file}"
+%w(cell row separator style table table_helper util version).each do |file|
+ require_relative "./terminal-table/#{file}"
end
diff --git a/lib/terminal-table/cell.rb b/lib/terminal-table/cell.rb
index 15b8a62..4073604 100644
--- a/lib/terminal-table/cell.rb
+++ b/lib/terminal-table/cell.rb
@@ -57,7 +57,7 @@ def lines
def render(line = 0)
left = " " * @table.style.padding_left
right = " " * @table.style.padding_right
- display_width = Unicode::DisplayWidth.of(escape(lines[line]))
+ display_width = Unicode::DisplayWidth.of(Util::ansi_escape(lines[line]))
render_width = lines[line].to_s.size - display_width + width
align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
end
@@ -68,7 +68,7 @@ def render(line = 0)
# removes all ANSI escape sequences (e.g. color)
def value_for_column_width_recalc
- lines.map{ |s| escape(s) }.max_by{ |s| Unicode::DisplayWidth.of(s) }
+ lines.map{ |s| Util::ansi_escape(s) }.max_by{ |s| Unicode::DisplayWidth.of(s) }
end
##
@@ -81,14 +81,6 @@ def width
end
inner_width + padding
end
-
- ##
- # removes all ANSI escape sequences (e.g. color)
- def escape(line)
- line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
- gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
- gsub(/(\x03|\x1a)/, '')
- end
end
end
end
diff --git a/lib/terminal-table/row.rb b/lib/terminal-table/row.rb
index 73dc589..2b20768 100644
--- a/lib/terminal-table/row.rb
+++ b/lib/terminal-table/row.rb
@@ -36,17 +36,31 @@ def height
end
def render
- y = @table.style.border_y
+ vleft, vcenter, vright = @table.style.vertical
(0...height).to_a.map do |line|
- y + cells.map do |cell|
+ vleft + cells.map do |cell|
cell.render(line)
- end.join(y) + y
+ end.join(vcenter) + vright
end.join("\n")
end
def number_of_columns
@cells.collect(&:colspan).inject(0, &:+)
end
+
+ # used to find indices where we have table '+' crossings.
+ # in cases where the colspan > 1, then we will skip over some numbers
+ # if colspan is always 1, then the list should be incrementing by 1.
+ #
+ # skip 0 entry, because it's the left side.
+ # skip last entry, because it's the right side.
+ # we only care about "+/T" style crossings.
+ def crossings
+ idx = 0
+ @cells[0...-1].map { |c| idx += c.colspan }
+ end
+
end
+
end
end
diff --git a/lib/terminal-table/separator.rb b/lib/terminal-table/separator.rb
index f09c2f7..0413925 100644
--- a/lib/terminal-table/separator.rb
+++ b/lib/terminal-table/separator.rb
@@ -2,13 +2,65 @@ module Terminal
class Table
class Separator < Row
+ ##
+ # `prevrow`, `nextrow` contain references to adjacent rows.
+ #
+ # `border_type` is a symbol used to control which type of border is used
+ # on the separator (:top for top-edge, :bot for bottom-edge,
+ # :div for interior, and :strong for emphasized-interior)
+ #
+ # `implicit` is false for user-added separators, and true for
+ # implicit/auto-generated separators.
+
+ def initialize(*args, border_type: :div, implicit: false)
+ super
+ @prevrow, @nextrow = nil, nil
+ @border_type = border_type
+ @implicit = implicit
+ end
+
+ attr_accessor :border_type
+ attr_reader :implicit
+
def render
- arr_x = (0...@table.number_of_columns).to_a.map do |i|
- @table.style.border_x * (@table.column_width(i) + @table.cell_padding)
+ left_edge, ctrflat, ctrud, right_edge, ctrdn, ctrup = @table.style.horizontal(border_type)
+
+ prev_crossings = @prevrow.respond_to?(:crossings) ? @prevrow.crossings : []
+ next_crossings = @nextrow.respond_to?(:crossings) ? @nextrow.crossings : []
+ rval = [left_edge]
+ numcols = @table.number_of_columns
+ (0...numcols).each do |idx|
+ rval << ctrflat * (@table.column_width(idx) + @table.cell_padding)
+ pcinc = prev_crossings.include?(idx+1)
+ ncinc = next_crossings.include?(idx+1)
+ border_center = if pcinc && ncinc
+ ctrud
+ elsif pcinc
+ ctrup
+ elsif ncinc
+ ctrdn
+ elsif !ctrud.empty?
+ # special case if the center-up-down intersection is empty
+ # which happens when verticals/intersections are removed. in that case
+ # we do not want to replace with a flat element so return empty-string in else block
+ ctrflat
+ else
+ ''
+ end
+ rval << border_center if idx < numcols-1
end
- border_i = @table.style.border_i
- border_i + arr_x.join(border_i) + border_i
+
+ rval << right_edge
+ rval.join
+ end
+
+ # Save off neighboring rows, so that we can use them later in determining
+ # which types of table edges to use.
+ def save_adjacent_rows(prevrow, nextrow)
+ @prevrow = prevrow
+ @nextrow = nextrow
end
+
end
end
end
diff --git a/lib/terminal-table/style.rb b/lib/terminal-table/style.rb
index 315b076..dc89737 100644
--- a/lib/terminal-table/style.rb
+++ b/lib/terminal-table/style.rb
@@ -1,5 +1,171 @@
+# coding: utf-8
+require 'forwardable'
+
module Terminal
class Table
+
+ class Border
+
+ attr_accessor :data, :top, :bottom, :left, :right
+ def initialize
+ @top, @bottom, @left, @right = true, true, true, true
+ end
+ def []=(key, val)
+ @data[key] = val
+ end
+ def [](key)
+ @data[key]
+ end
+ def initialize_dup(other)
+ super
+ @data = other.data.dup
+ end
+ def remove_verticals
+ self.class.const_get("VERTICALS").each { |key| @data[key] = "" }
+ self.class.const_get("INTERSECTIONS").each { |key| @data[key] = "" }
+ end
+ def remove_horizontals
+ self.class.const_get("HORIZONTALS").each { |key| @data[key] = "" }
+ end
+
+ # If @left, return the edge else empty-string.
+ def maybeleft(key) ; @left ? @data[key] : '' ; end
+
+ # If @right, return the edge else empty-string.
+ def mayberight(key) ; @right ? @data[key] : '' ; end
+
+ end
+
+ class AsciiBorder < Border
+ HORIZONTALS = %i[x]
+ VERTICALS = %i[y]
+ INTERSECTIONS = %i[i]
+
+ def initialize
+ super
+ @data = { x: "-", y: "|", i: "+" }
+ end
+
+ # Get vertical border elements
+ # @return [Array] 3-element list of [left, center, right]
+ def vertical
+ [maybeleft(:y), @data[:y], mayberight(:y)] # left, center, right
+ end
+
+ # Get horizontal border elements
+ # @return [Array] a 6 element list of: [i-left, horizontal-bar, i-up/down, i-right, i-down, i-up]
+ def horizontal(_type)
+ x, i = @data[:x], @data[:i]
+ [maybeleft(:i), x, i, mayberight(:i), i, i]
+ end
+ end
+
+ class MarkdownBorder < AsciiBorder
+ def initialize
+ super
+ @top, @bottom = false, false
+ @data = { x: "-", y: "|", i: "|" }
+ end
+ end
+
+ class UnicodeBorder < Border
+
+ ALLOWED_SEPARATOR_BORDER_STYLES = %i[
+ top bot
+ div dash dot3 dot4
+ thick thick_dash thick_dot3 thick_dot4
+ heavy heavy_dash heavy_dot3 heavy_dot4
+ bold bold_dash bold_dot3 bold_dot4
+ double
+ ]
+
+ HORIZONTALS = %i[x sx ax bx nx bx_dot3 bx_dot4 bx_dash x_dot3 x_dot4 x_dash]
+ VERTICALS = %i[y yw ye]
+ INTERSECTIONS = %i[nw n ne nd
+ aw ai ae ad au
+ bw bi be bd bu
+ w i e dn up
+ sw s se su]
+ def initialize
+ super
+ @data = {
+ nil => nil,
+ nw: "┌", nx: "─", n: "┬", ne: "┐",
+ yw: "│", y: "│", ye: "│",
+ aw: "╞", ax: "═", ai: "╪", ae: "╡", ad: '╤', au: "╧", # double
+ bw: "┝", bx: "━", bi: "┿", be: "┥", bd: '┯', bu: "┷", # heavy/bold/thick
+ w: "├", x: "─", i: "┼", e: "┤", dn: "┬", up: "┴", # normal div
+ sw: "└", sx: "─", s: "┴", se: "┘",
+ # alternative dots/dashes
+ x_dot4: '┈', x_dot3: '┄', x_dash: '╌',
+ bx_dot4: '┉', bx_dot3: '┅', bx_dash: '╍',
+ }
+ end
+ # Get vertical border elements
+ # @return [Array] 3-element list of [left, center, right]
+ def vertical
+ [maybeleft(:yw), @data[:y], mayberight(:ye)]
+ end
+
+ # Get horizontal border elements
+ # @return [Array] a 6 element list of: [i-left, horizontal-bar, i-up/down, i-right, i-down, i-up]
+ def horizontal(type)
+ raise ArgumentError, "Border type is #{type.inspect}, must be one of #{ALLOWED_SEPARATOR_BORDER_STYLES.inspect}" unless ALLOWED_SEPARATOR_BORDER_STYLES.include?(type)
+ lookup = case type
+ when :top
+ [:nw, :nx, :n, :ne, :n, nil]
+ when :bot
+ [:sw, :sx, :s, :se, nil, :s]
+ when :double
+ # typically used for the separator below the heading row or above a footer row)
+ [:aw, :ax, :ai, :ae, :ad, :au]
+ when :thick, :thick_dash, :thick_dot3, :thick_dot4,
+ :heavy, :heavy_dash, :heavy_dot3, :heavy_dot4,
+ :bold, :bold_dash, :bold_dot3, :bold_dot4
+ # alternate thick/bold border
+ xref = type.to_s.sub(/^(thick|heavy|bold)/,'bx').to_sym
+ [:bw, xref, :bi, :be, :bd, :bu]
+ when :dash, :dot3, :dot4
+ # alternate thin dividers
+ xref = "x_#{type}".to_sym
+ [:w, xref, :i, :e, :dn, :up]
+ else # :div (center, non-emphasized)
+ [:w, :x, :i, :e, :dn, :up]
+ end
+ rval = lookup.map { |key| @data.fetch(key) }
+ rval[0] = '' unless @left
+ rval[3] = '' unless @right
+ rval
+ end
+ end
+
+ # Unicode Border With rounded edges
+ class UnicodeRoundBorder < UnicodeBorder
+ def initialize
+ super
+ @data.merge!({nw: '╭', ne: '╮', sw: '╰', se: '╯'})
+ end
+ end
+
+ # Unicode Border with thick outer edges
+ class UnicodeThickEdgeBorder < UnicodeBorder
+ def initialize
+ super
+ @data = {
+ nil => nil,
+ nw: "┏", nx: "━", n: "┯", ne: "┓", nd: nil,
+ yw: "┃", y: "│", ye: "┃",
+ aw: "┣", ax: "═", ai: "╪", ae: "┫", ad: '╤', au: "╧", # double
+ bw: "┣", bx: "━", bi: "┿", be: "┫", bd: '┯', bu: "┷", # heavy/bold/thick
+ w: "┠", x: "─", i: "┼", e: "┨", dn: "┬", up: "┴", # normal div
+ sw: "┗", sx: "━", s: "┷", se: "┛", su: nil,
+ # alternative dots/dashes
+ x_dot4: '┈', x_dot3: '┄', x_dash: '╌',
+ bx_dot4: '┉', bx_dot3: '┅', bx_dash: '╍',
+ }
+ end
+ end
+
# A Style object holds all the formatting information for a Table object
#
# To create a table with a certain style, use either the constructor
@@ -22,20 +188,51 @@ class Table
# Terminal::Table::Style.defaults = {:width => 80}
#
class Style
+ extend Forwardable
+ def_delegators :@border, :vertical, :horizontal, :remove_verticals, :remove_horizontals
+
@@defaults = {
- :border_x => "-", :border_y => "|", :border_i => "+",
- :border_top => true, :border_bottom => true,
+ :border => AsciiBorder.new,
:padding_left => 1, :padding_right => 1,
:margin_left => '',
:width => nil, :alignment => nil,
- :all_separators => false
+ :all_separators => false,
}
- attr_accessor :border_x
- attr_accessor :border_y
- attr_accessor :border_i
- attr_accessor :border_top
- attr_accessor :border_bottom
+ ## settors/gettor for legacy ascii borders
+ def border_x=(val) ; @border[:x] = val ; end
+ def border_y=(val) ; @border[:y] = val ; end
+ def border_i=(val) ; @border[:i] = val ; end
+ def border_y ; @border[:y] ; end
+ def border_y_width ; Util::ansi_escape(@border[:y]).length ; end
+
+ # Accessor for instance of Border
+ attr_reader :border
+ def border=(val)
+ if val.is_a? Symbol
+ # convert symbol name like :foo_bar to get class FooBarBorder
+ klass_str = val.to_s.split('_').collect(&:capitalize).join + "Border"
+ begin
+ klass = Terminal::Table::const_get(klass_str)
+ @border = klass.new
+ rescue NameError
+ raise "Cannot lookup class Terminal::Table::#{klass_str} from symbol #{val.inspect}"
+ end
+ else
+ @border = val
+ end
+ end
+
+ def border_top=(val) ; @border.top = val ; end
+ def border_bottom=(val) ; @border.bottom = val ; end
+ def border_left=(val) ; @border.left = val ; end
+ def border_right=(val) ; @border.right = val ; end
+
+ def border_top ; @border.top ; end
+ def border_bottom ; @border.bottom ; end
+ def border_left ; @border.left ; end
+ def border_right ; @border.right ; end
+
attr_accessor :padding_left
attr_accessor :padding_right
@@ -47,23 +244,30 @@ class Style
attr_accessor :all_separators
-
+
def initialize options = {}
apply self.class.defaults.merge(options)
end
def apply options
- options.each { |m, v| __send__ "#{m}=", v }
+ options.each do |m, v|
+ __send__ "#{m}=", v
+ end
end
-
+
class << self
def defaults
- @@defaults
+ klass_defaults = @@defaults.dup
+ # border is an object that needs to be duplicated on instantiation,
+ # otherwise everything will be referencing the same object-id.
+ klass_defaults[:border] = klass_defaults[:border].dup
+ klass_defaults
end
-
+
def defaults= options
@@defaults = defaults.merge(options)
end
+
end
def on_change attr
@@ -74,6 +278,7 @@ def on_change attr
yield attr.to_sym, value
end
end
+
end
end
end
diff --git a/lib/terminal-table/table.rb b/lib/terminal-table/table.rb
index 947a207..e252068 100644
--- a/lib/terminal-table/table.rb
+++ b/lib/terminal-table/table.rb
@@ -7,9 +7,10 @@ class Table
attr_reader :headings
##
- # Generates a ASCII table with the given _options_.
+ # Generates a ASCII/Unicode table with the given _options_.
def initialize options = {}, &block
+ @elaborated = false
@headings = []
@rows = []
@column_widths = []
@@ -47,12 +48,12 @@ def add_row array
##
# Add a separator.
- def add_separator
- self << :separator
+ def add_separator(border_type: :div)
+ @rows << Separator.new(self, border_type: border_type)
end
def cell_spacing
- cell_padding + style.border_y.length
+ cell_padding + style.border_y_width
end
def cell_padding
@@ -119,28 +120,57 @@ def headings= arrays
end
##
- # Render the table.
+ # Elaborate rows to form an Array of Rows and Separators with adjacency properties added.
+ #
+ # This is separated from the String rendering so that certain features may be tweaked
+ # before the String is built.
- def render
- separator = Separator.new(self)
- buffer = style.border_top ? [separator] : []
+ def elaborate_rows
+
+ buffer = style.border_top ? [Separator.new(self, border_type: :top, implicit: true)] : []
unless @title.nil?
buffer << Row.new(self, [title_cell_options])
- buffer << separator
+ buffer << Separator.new(self, implicit: true)
end
@headings.each do |row|
unless row.cells.empty?
buffer << row
- buffer << separator
+ buffer << Separator.new(self, border_type: :double, implicit: true)
end
end
if style.all_separators
- buffer += @rows.product([separator]).flatten
+ @rows.each_with_index do |row, idx|
+ # last separator is bottom, others are :div
+ border_type = (idx == @rows.size - 1) ? :bot : :div
+ buffer << row
+ buffer << Separator.new(self, border_type: border_type, implicit: true)
+ end
else
buffer += @rows
- buffer << separator if style.border_bottom
+ buffer << Separator.new(self, border_type: :bot, implicit: true) if style.border_bottom
+ end
+
+ # After all implicit Separators are inserted we need to save off the
+ # adjacent rows so that we can decide what type of intersections to use
+ # based on column spans in the adjacent row(s).
+ buffer.each_with_index do |r, idx|
+ if r.is_a?(Separator)
+ prev_row = idx > 0 ? buffer[idx - 1] : nil
+ next_row = buffer.fetch(idx + 1, nil)
+ r.save_adjacent_rows(prev_row, next_row)
+ end
end
- buffer.map { |r| style.margin_left + r.render.rstrip }.join("\n")
+
+ @elaborated = true
+ @rows = buffer
+ end
+
+ ##
+ # Render the table.
+
+ def render
+ elaborate_rows unless @elaborated
+ @rows.map { |r| style.margin_left + r.render.rstrip }.join("\n")
end
alias :to_s :render
@@ -182,7 +212,7 @@ def == other
private
def columns_width
- column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
+ column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y_width
end
def recalc_column_widths
@@ -301,7 +331,7 @@ def recalc_column_widths
full_width = dp[n_cols][0].keys.first
unless style.width.nil?
- new_width = style.width - space_width - style.border_y.length
+ new_width = style.width - space_width - style.border_y_width
if new_width < full_width
raise "Table width exceeds wanted width " +
"of #{style.width} characters."
diff --git a/lib/terminal-table/util.rb b/lib/terminal-table/util.rb
new file mode 100644
index 0000000..a584c3d
--- /dev/null
+++ b/lib/terminal-table/util.rb
@@ -0,0 +1,13 @@
+module Terminal
+ class Table
+ module Util
+ # removes all ANSI escape sequences (e.g. color)
+ def ansi_escape(line)
+ line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
+ gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
+ gsub(/(\x03|\x1a)/, '')
+ end
+ module_function :ansi_escape
+ end
+ end
+end
diff --git a/lib/terminal-table/version.rb b/lib/terminal-table/version.rb
index 9f81813..d6d78f8 100644
--- a/lib/terminal-table/version.rb
+++ b/lib/terminal-table/version.rb
@@ -1,5 +1,5 @@
module Terminal
class Table
- VERSION = '2.0.0'
+ VERSION = '3.0.0'
end
end
diff --git a/spec/style_spec.rb b/spec/style_spec.rb
new file mode 100644
index 0000000..2e3005f
--- /dev/null
+++ b/spec/style_spec.rb
@@ -0,0 +1,29 @@
+# encoding: utf-8
+require 'spec_helper'
+
+module Terminal
+ describe Table::Style do
+
+ before :each do
+ @table = Table.new
+ end
+
+ border_map = {
+ :markdown => Terminal::Table::MarkdownBorder,
+ :ascii => Terminal::Table::AsciiBorder,
+ :unicode => Terminal::Table::UnicodeBorder,
+ :unicode_thick_edge => Terminal::Table::UnicodeThickEdgeBorder,
+ :unicode_round => Terminal::Table::UnicodeRoundBorder,
+ }
+
+ border_map.each do |name_sym, klass|
+ it "should enumerate a #{name_sym} table" do
+ @table.style.border = name_sym
+ @table.style.border.should be_a klass
+ @table.style.border = klass.new
+ @table.style.border.should be_a klass
+ end
+ end
+
+ end
+end
diff --git a/spec/table_spec.rb b/spec/table_spec.rb
index 7194c19..bebc5aa 100755
--- a/spec/table_spec.rb
+++ b/spec/table_spec.rb
@@ -41,7 +41,7 @@ module Terminal
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 88 |
- +---+---+----+
+ +-------+----+
EOF
end
@@ -87,7 +87,7 @@ module Terminal
@table.headings = ['Char', 'Num']
@table << ['a', 1]
separator = Terminal::Table::Separator.new(@table)
- separator.render.should eq '+------+-----+'
+ separator.render.should eq '+------------+'
end
it "should add separator" do
@@ -131,6 +131,7 @@ module Terminal
@table << ['b', 2]
@table << ['c', 3]
@table.style.padding_right.should eq 2
+
@table.render.should eq <<-EOF.deindent
yyx======x=====x
yy:Char :Num :
@@ -150,6 +151,7 @@ module Terminal
@table << ['c', 3]
@table.style.width = 21
@table.style.alignment = :right
+
@table.render.should eq <<-EOF.deindent
+-----------+-------+
| Character | Num |
@@ -195,7 +197,7 @@ module Terminal
@table << ['b', 2]
@table << ['c', 3]
@table.render.should eq <<-EOF.deindent
- +------+-----+
+ +------------+
| Title |
+------+-----+
| Char | Num |
@@ -418,7 +420,7 @@ module Terminal
@table.headings = ['one', { :value => 'two', :alignment => :center, :colspan => 2}]
@table.rows = [['a', 1, 2], ['b', 3, 4], ['c', 3, 4]]
@table.render.should eq <<-EOF.deindent
- +-----+---+---+
+ +-----+-------+
| one | two |
+-----+---+---+
| a | 1 | 2 |
@@ -438,7 +440,7 @@ module Terminal
| a | 1 | 2 |
| b | 3 | 4 |
| joined | c |
- +-----+-----+-------+
+ +-----------+-------+
EOF
end
@@ -452,7 +454,7 @@ module Terminal
| a | 1 | 2 |
| b | 3 | 4 |
| c | joined |
- +-----+-----+-------+
+ +-----+-------------+
EOF
end
@@ -466,7 +468,7 @@ module Terminal
| a | 1 | 2 |
| b | 3 | 4 |
| c | joined that is very very long |
- +-----+---------------+---------------+
+ +-----+-------------------------------+
EOF
end
@@ -477,7 +479,7 @@ module Terminal
+-------+---+---+
| 12345 | 2 | 3 |
| 123456789 | 4 |
- +-------+---+---+
+ +-----------+---+
EOF
end
@@ -494,7 +496,7 @@ module Terminal
| 123456789 | 4 | |
| 123 | 444444444 |
| 0 | |
- +-------+---+-----+-----+
+ +-----------------+-----+
EOF
end
@@ -509,7 +511,7 @@ module Terminal
| 4 | 5555555 |
| 66666666 | 777 |
| 20202020202020202020 |
- +-------+-------+------+
+ +----------------------+
EOF
end
@@ -533,7 +535,7 @@ module Terminal
@table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
@table.render.should eq <<-EOF.deindent
- +------+---+---+---+
+ +------+-----------+
| name | values |
+------+---+---+---+
| a | 1 | 2 | 3 |
@@ -560,7 +562,7 @@ module Terminal
end
@table.render.should eq <<-EOF.deindent
- +--------+----+----+-------+------+-----+----+
+ +--------+---------+--------------+----------+
| name | Values | Other values | Column 3 |
+--------+----+----+-------+------+-----+----+
| row #1 | 0 | 1 | 2 | 3 | 4 | 5 |
@@ -572,11 +574,11 @@ module Terminal
it "should render a table with only X cell borders" do
@table.style = {:border_x => "-", :border_y => "", :border_i => ""}
+ #@table.style.remove_verticals # new methodology is to use remove_verticals method.
@table.headings = ['name', { :value => 'values', :alignment => :right, :colspan => 3}]
@table.headings = ['name', { :value => 'values', :colspan => 3}]
@table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
-
@table.render.should eq <<-EOF.strip
---------------
name values
@@ -612,7 +614,7 @@ module Terminal
@table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
@table.render.should eq <<-EOF.deindent
- +------+---+---+---+
+ +------+-----------+
| name | values |
+------+---+---+---+
| a | 1 | 2 | 3 |
@@ -665,5 +667,34 @@ module Terminal
| c | 3 |
EOF
end
+
+ it "should allow to not generate left and right border" do
+ @table.style = { :border_left => false, :border_right => false }
+ @table.headings = ['name', 'value']
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
+ @table.render.should eq <<-EOF.chomp
+------+-------
+ name | value
+------+-------
+ a | 1
+ b | 2
+ c | 3
+------+-------
+ EOF
+ end
+
+ it "create markdown compatible tables" do
+ @table.style.border = :markdown # Terminal::Table::MarkdownBorder.new
+ @table.headings = ['name', 'value']
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
+ @table.render.should eq <<-EOF.deindent
+| name | value |
+|------|-------|
+| a | 1 |
+| b | 2 |
+| c | 3 |
+ EOF
+ end
+
end
end
diff --git a/spec/unicode_table_spec.rb b/spec/unicode_table_spec.rb
new file mode 100755
index 0000000..cf517ec
--- /dev/null
+++ b/spec/unicode_table_spec.rb
@@ -0,0 +1,811 @@
+# encoding: utf-8
+require 'spec_helper'
+
+module Terminal
+ describe Table do
+ before :each do
+ @table = Table.new
+ @table.style = { :border => Terminal::Table::UnicodeBorder.new() }
+
+ end
+
+ it "should select columns" do
+ @table << ['foo', 'bar']
+ @table << ['big foo', 'big foo bar']
+ @table.column(1).should eq ['bar', 'big foo bar']
+ end
+
+ it "should select the column with headings at an index" do
+ @table << [1,2,3]
+ @table << [4,5,6]
+ @table.column_with_headings(2).should eq [3,6]
+ end
+
+ #it "should select the columns with colspans > 1 in the index" do
+ # @table << [1,{:value => 2, :colspan => 2}]
+ # @table << [{:value => 3, :colspan => 2}, 4]
+ #end
+
+ it "should account for the colspan when selecting columns" do
+ @table << [1,2,3]
+ @table << [{:value => "4,5", :colspan => 2}, 6]
+ @table.column_with_headings(0).should eq [1,"4,5"]
+ @table.column_with_headings(1).should eq [2,"4,5"]
+ @table.column_with_headings(2).should eq [3,6]
+ end
+
+ it "should render tables with colspan properly" do
+ @table << [1,2,3]
+ @table << [4,5,6]
+ @table << [{:value => "7", :colspan => 2}, 88]
+ @table.render.should eq <<-EOF.deindent
+ ┌───┬───┬────┐
+ │ 1 │ 2 │ 3 │
+ │ 4 │ 5 │ 6 │
+ │ 7 │ 88 │
+ └───────┴────┘
+ EOF
+ end
+
+ it "should count columns" do
+ @table << [1, {:value => "2", :colspan => 2}]
+ @table << [{:value => "3", :colspan => 2}, 4]
+ @table.number_of_columns.should eq 3
+ end
+
+ it "should iterate columns" do
+ @table << [1, 2, 3]
+ @table << [4, 5, 6]
+ @table.columns.should eq [[1, 4], [2, 5], [3, 6]]
+ end
+
+ it "should select columns" do
+ @table.headings = ['one', 'two']
+ @table << ['foo', 'bar']
+ @table << ['big foo', 'big foo bar']
+ @table.column(1).should eq ['bar', 'big foo bar']
+ end
+
+ it "should select columns when using hashes" do
+ @table.headings = ['one', 'two']
+ @table.rows = [[{ :value => 'a', :align => :left }, 1], ['b', 2], ['c', 3]]
+ @table.column(0).should eq ['a', 'b', 'c']
+ end
+
+ it "should find column length" do
+ @table << ['foo', 'bar', 'a']
+ @table << ['big foo', 'big foo bar']
+ @table.column_width(1).should eq 11
+ end
+
+ it "should find column length with headings" do
+ @table.headings = ['one', 'super long heading']
+ @table << ['foo', 'bar', 'a']
+ @table << ['big foo', 'big foo bar']
+ @table.column_width(1).should eq 18
+ end
+
+ it "should render separators" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ separator = Terminal::Table::Separator.new(@table)
+ # For UnicodeBorder this now defaults to a :div border_type
+ separator.render.should eq '├────────────┤'
+ end
+
+ it "should add separator" do
+ @table << ['a', 1]
+ @table.add_separator
+ @table << ['b', 2]
+ @table.rows.size.should eq 2
+ end
+
+ it "should render an empty table properly" do
+ @table.render.should eq <<-EOF.deindent
+ ┌┐
+ └┘
+ EOF
+ end
+
+ it "should render properly" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬─────┐
+ │ Char │ Num │
+ ╞══════╪═════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └──────┴─────┘
+ EOF
+ end
+
+ it "should render styles properly" do
+ @table.headings = ['Char', 'Num']
+ @table.style = {
+ :padding_left => 0, :padding_right => 2,
+ :margin_left => 'y' * 2
+ }
+ @table.style.border[:ai] = "x"
+ @table.style.border[:y] = ":"
+ @table.style.border[:nw] = "1"
+ @table.style.border[:ne] = "2"
+ @table.style.border[:sw] = "3"
+ @table.style.border[:se] = "4"
+ @table.style.border[:s] = "^"
+ @table.style.border[:aw] = "5"
+ @table.style.border[:ae] = "6"
+
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.style.padding_right.should eq 2
+
+ @table.render.should eq <<-EOF.deindent
+ yy1──────┬─────2
+ yy│Char :Num │
+ yy5══════x═════6
+ yy│a :1 │
+ yy│b :2 │
+ yy│c :3 │
+ yy3──────^─────4
+ EOF
+ end
+
+
+ it "should render default alignment properly" do
+ @table.headings = ['Character', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.style.width = 21
+ @table.style.alignment = :right
+
+ @table.render.should eq <<-EOF.deindent
+ ┌───────────┬───────┐
+ │ Character │ Num │
+ ╞═══════════╪═══════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └───────────┴───────┘
+ EOF
+ end
+
+ it "should render width properly" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.style.width = 21
+ @table.render.should eq <<-EOF.deindent
+ ┌─────────┬─────────┐
+ │ Char │ Num │
+ ╞═════════╪═════════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └─────────┴─────────┘
+ EOF
+ end
+
+ it "should raise an error if the table width exceeds style width" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table << ['d', 'x' * 22]
+ @table.style.width = 21
+ lambda { @table.render }.should raise_error "Table width exceeds wanted width of 21 characters."
+ end
+
+ it "should render title properly" do
+ @table.title = "Title"
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.render.should eq <<-EOF.deindent
+ ┌────────────┐
+ │ Title │
+ ├──────┬─────┤
+ │ Char │ Num │
+ ╞══════╪═════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └──────┴─────┘
+ EOF
+ end
+
+ it "should render properly without headings" do
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.render.should eq <<-EOF.deindent
+ ┌───┬───┐
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └───┴───┘
+ EOF
+ end
+
+ it "should render separators" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table.add_separator
+ @table << ['c', 3]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬─────┐
+ │ Char │ Num │
+ ╞══════╪═════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ ├──────┼─────┤
+ │ c │ 3 │
+ └──────┴─────┘
+ EOF
+ end
+
+ it "should align columns with separators" do
+ @table.headings = ['Char', 'Num']
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table.add_separator
+ @table << ['c', 3]
+ @table.align_column 1, :right
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬─────┐
+ │ Char │ Num │
+ ╞══════╪═════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ ├──────┼─────┤
+ │ c │ 3 │
+ └──────┴─────┘
+ EOF
+ end
+
+
+ it "should render properly using block syntax" do
+ table = Terminal::Table.new do |t|
+ t << ['a', 1]
+ t << ['b', 2]
+ t << ['c', 3]
+ end
+ table.render.should eq <<-EOF.deindent
+ +---+---+
+ | a | 1 |
+ | b | 2 |
+ | c | 3 |
+ +---+---+
+ EOF
+ end
+
+ it "should render properly using instance_eval block syntax" do
+ table = Terminal::Table.new do
+ add_row ['a', 1]
+ add_row ['b', 2]
+ add_row ['c', 3]
+ end
+ table.render.should eq <<-EOF.deindent
+ +---+---+
+ | a | 1 |
+ | b | 2 |
+ | c | 3 |
+ +---+---+
+ EOF
+ end
+
+ it "should render multi-row headings properly" do
+ @table.headings = [['Char', 'Num'], [{ :value => "2nd heading", :colspan => 2 }]]
+ @table << ['a', 1]
+ @table << ['b', 2]
+ @table << ['c', 3]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬──────┐
+ │ Char │ Num │
+ ╞══════╧══════╡
+ │ 2nd heading │
+ ╞══════╤══════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └──────┴──────┘
+ EOF
+ end
+
+ it "should allows a hash of options for creation" do
+ headings = ['Char', 'Num']
+ rows = [['a', 1], ['b', 2], ['c', 3]]
+ Terminal::Table.new(:rows => rows, :headings => headings).render.should eq <<-EOF.deindent
+ +------+-----+
+ | Char | Num |
+ +------+-----+
+ | a | 1 |
+ | b | 2 |
+ | c | 3 |
+ +------+-----+
+ EOF
+ end
+
+ it "should flex for large cells" do
+ @table.headings = ['Just some characters', 'Num']
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────────────────────┬─────┐
+ │ Just some characters │ Num │
+ ╞══════════════════════╪═════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └──────────────────────┴─────┘
+ EOF
+ end
+
+ it "should allow alignment of headings and cells" do
+ @table.headings = ['Characters', {:value => 'Nums', :alignment => :right} ]
+ @table << [{:value => 'a', :alignment => :center}, 1]
+ @table << ['b', 222222222222222]
+ @table << ['c', 3]
+ @table.render.should eq <<-EOF.deindent
+ ┌────────────┬─────────────────┐
+ │ Characters │ Nums │
+ ╞════════════╪═════════════════╡
+ │ a │ 1 │
+ │ b │ 222222222222222 │
+ │ c │ 3 │
+ └────────────┴─────────────────┘
+ EOF
+
+ end
+
+ it "should align columns, but allow specifics to remain" do
+ @table.headings = ['Just some characters', 'Num']
+ @table.rows = [[{:value => 'a', :alignment => :left}, 1], ['b', 2], ['c', 3]]
+ @table.align_column 0, :center
+ @table.align_column 1, :right
+ @table.render.should eq <<-EOF.deindent
+ ┌──────────────────────┬─────┐
+ │ Just some characters │ Num │
+ ╞══════════════════════╪═════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └──────────────────────┴─────┘
+ EOF
+ end
+
+ describe "#==" do
+ it "should be equal to itself" do
+ t = Table.new
+ t.should eq t
+ end
+
+ # it "should be equal with two empty tables" do
+ # table_one = Table.new
+ # table_two = Table.new
+ #
+ # table_one.should eq table_two
+ # table_two.should eq table_one
+ # end
+
+ it "should not be equal with different headings" do
+ table_one = Table.new
+ table_two = Table.new
+
+ table_one.headings << "a"
+
+ table_one.should_not eq table_two
+ table_two.should_not eq table_one
+ end
+
+ it "should not be equal with different rows" do
+ table_one = Table.new
+ table_two = Table.new
+
+ table_one.should_not eq table_two
+ table_two.should_not eq table_one
+ end
+
+ it "should not be equal if the other object does not respond_to? :headings" do
+ table_one = Table.new
+ table_two = Object.new
+ allow(table_two).to receive(:rows).and_return([])
+ table_one.should_not eq table_two
+ end
+
+ it "should not be equal if the other object does not respond_to? :rows" do
+ table_one = Table.new
+ table_two = Object.new
+ allow(table_two).to receive(:rows).and_return([])
+ table_one.should_not eq table_two
+ end
+ end
+
+ it "should handle colspan inside heading" do
+ @table.headings = ['one', { :value => 'two', :alignment => :center, :colspan => 2}]
+ @table.rows = [['a', 1, 2], ['b', 3, 4], ['c', 3, 4]]
+ @table.render.should eq <<-EOF.deindent
+ ┌─────┬───────┐
+ │ one │ two │
+ ╞═════╪═══╤═══╡
+ │ a │ 1 │ 2 │
+ │ b │ 3 │ 4 │
+ │ c │ 3 │ 4 │
+ └─────┴───┴───┘
+ EOF
+ end
+
+ it "should handle colspan inside cells" do
+ @table.headings = ['one', 'two', 'three']
+ @table.rows = [['a', 1, 2], ['b', 3, 4], [{:value => "joined", :colspan => 2,:alignment => :center}, 'c']]
+ @table.render.should eq <<-EOF.deindent
+ ┌─────┬─────┬───────┐
+ │ one │ two │ three │
+ ╞═════╪═════╪═══════╡
+ │ a │ 1 │ 2 │
+ │ b │ 3 │ 4 │
+ │ joined │ c │
+ └───────────┴───────┘
+ EOF
+ end
+
+ it "should handle colspan inside cells regardless of colspan position" do
+ @table.headings = ['one', 'two', 'three']
+ @table.rows = [['a', 1, 2], ['b', 3, 4], ['c', {:value => "joined", :colspan => 2,:alignment => :center}]]
+ @table.render.should eq <<-EOF.deindent
+ ┌─────┬─────┬───────┐
+ │ one │ two │ three │
+ ╞═════╪═════╪═══════╡
+ │ a │ 1 │ 2 │
+ │ b │ 3 │ 4 │
+ │ c │ joined │
+ └─────┴─────────────┘
+ EOF
+ end
+
+ it "should handle overflowing colspans" do
+ @table.headings = ['one', 'two', 'three']
+ @table.rows = [['a', 1, 2], ['b', 3, 4], ['c', {:value => "joined that is very very long", :colspan => 2,:alignment => :center}]]
+ @table.render.should eq <<-EOF.deindent
+ ┌─────┬───────────────┬───────────────┐
+ │ one │ two │ three │
+ ╞═════╪═══════════════╪═══════════════╡
+ │ a │ 1 │ 2 │
+ │ b │ 3 │ 4 │
+ │ c │ joined that is very very long │
+ └─────┴───────────────────────────────┘
+ EOF
+ end
+
+ it "should only increase column size for multi-column if it is unavoidable" do
+ @table << [12345,2,3]
+ @table << [{:value => 123456789, :colspan => 2}, 4]
+ @table.render.should eq <<-EOF.deindent
+ ┌───────┬───┬───┐
+ │ 12345 │ 2 │ 3 │
+ │ 123456789 │ 4 │
+ └───────────┴───┘
+ EOF
+ end
+
+ it "should handle extreme case with complex table layout" do
+ @table << [1, 2, 33, 4]
+ @table << [12345, {:value => 54321, :colspan => 2}, '']
+ @table << [{:value => 123456789, :colspan => 2}, 4, '']
+ @table << [{:value => 123, :colspan => 2}, {:value => 444444444, :colspan => 2}]
+ @table << [{:value => 0, :colspan => 3}, '']
+ @table.render.should eq <<-EOF.deindent
+ ┌───────┬───┬─────┬─────┐
+ │ 1 │ 2 │ 33 │ 4 │
+ │ 12345 │ 54321 │ │
+ │ 123456789 │ 4 │ │
+ │ 123 │ 444444444 │
+ │ 0 │ │
+ └─────────────────┴─────┘
+ EOF
+ end
+
+ it "should try to evenly divide contents over colspans" do
+ @table << [1, 2, 3]
+ @table << [4, {:value => 5555555, :colspan => 2}]
+ @table << [{:value => 66666666 , :colspan => 2}, 777]
+ @table << [{:value => 20202020202020202020, :colspan => 3}]
+ @table.render.should eq <<-EOF.deindent
+ ┌───────┬───────┬──────┐
+ │ 1 │ 2 │ 3 │
+ │ 4 │ 5555555 │
+ │ 66666666 │ 777 │
+ │ 20202020202020202020 │
+ └──────────────────────┘
+ EOF
+ end
+
+ it "should handle colspan 1" do
+ @table.headings = ['name', { :value => 'values', :colspan => 1}]
+ @table.rows = [['a', 1], ['b', 4], ['c', 7]]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬────────┐
+ │ name │ values │
+ ╞══════╪════════╡
+ │ a │ 1 │
+ │ b │ 4 │
+ │ c │ 7 │
+ └──────┴────────┘
+ EOF
+ end
+
+ it "should handle big colspan" do
+ @table.headings = ['name', { :value => 'values', :alignment => :right, :colspan => 3}]
+ @table.headings = ['name', { :value => 'values', :colspan => 3}]
+ @table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
+
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬───────────┐
+ │ name │ values │
+ ╞══════╪═══╤═══╤═══╡
+ │ a │ 1 │ 2 │ 3 │
+ │ b │ 4 │ 5 │ 6 │
+ │ c │ 7 │ 8 │ 9 │
+ └──────┴───┴───┴───┘
+ EOF
+ end
+
+ it "should handle multiple colspan" do
+ @table.headings = [
+ 'name',
+ { :value => 'Values', :alignment => :right, :colspan => 2},
+ { :value => 'Other values', :alignment => :right, :colspan => 2},
+ { :value => 'Column 3', :alignment => :right, :colspan => 2}
+ ]
+
+ 3.times do |row_index|
+ row = ["row ##{row_index+1}"]
+ 6.times do |i|
+ row << row_index*6 + i
+ end
+ @table.add_row(row)
+ end
+
+ @table.render.should eq <<-EOF.deindent
+ ┌────────┬─────────┬──────────────┬──────────┐
+ │ name │ Values │ Other values │ Column 3 │
+ ╞════════╪════╤════╪═══════╤══════╪═════╤════╡
+ │ row #1 │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │
+ │ row #2 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │
+ │ row #3 │ 12 │ 13 │ 14 │ 15 │ 16 │ 17 │
+ └────────┴────┴────┴───────┴──────┴─────┴────┘
+ EOF
+ end
+
+ it "should render a table with only X cell borders" do
+ #@table.style = {:border_x => "-", :border_y => "", :border_i => ""}
+ @table.style.remove_verticals # new methodology is to use remove_verticals method.
+
+ @table.headings = ['name', { :value => 'values', :alignment => :right, :colspan => 3}]
+ @table.headings = ['name', { :value => 'values', :colspan => 3}]
+ @table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
+ @table.render.should eq <<-EOF.strip
+───────────────
+ name values
+═══════════════
+ a 1 2 3
+ b 4 5 6
+ c 7 8 9
+───────────────
+ EOF
+ end
+
+ it "should render a table without cell borders" do
+ #@table.style = {:border_x => "", :border_y => "", :border_i => ""}
+ @table.style.remove_verticals # new methodology is to use remove_verticals method.
+ @table.style.remove_horizontals # new methodology is to use remove_horizontal method.
+
+ @table.headings = ['name', { :value => 'values', :alignment => :right, :colspan => 3}]
+ @table.headings = ['name', { :value => 'values', :colspan => 3}]
+ @table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
+
+ @table.render.should eq <<-EOF
+
+ name values
+
+ a 1 2 3
+ b 4 5 6
+ c 7 8 9
+ EOF
+ end
+
+ it "should render a table with all separators" do
+ @table.style = {:all_separators => true}
+ @table.headings = ['name', { :value => 'values', :alignment => :right, :colspan => 3}]
+ @table.headings = ['name', { :value => 'values', :colspan => 3}]
+ @table.rows = [['a', 1, 2, 3], ['b', 4, 5, 6], ['c', 7, 8, 9]]
+
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬───────────┐
+ │ name │ values │
+ ╞══════╪═══╤═══╤═══╡
+ │ a │ 1 │ 2 │ 3 │
+ ├──────┼───┼───┼───┤
+ │ b │ 4 │ 5 │ 6 │
+ ├──────┼───┼───┼───┤
+ │ c │ 7 │ 8 │ 9 │
+ └──────┴───┴───┴───┘
+ EOF
+ end
+
+ it "should render a table with fullwidth characters" do
+ @table.headings = ['COL 1', 'COL 2', 'COL 3', 'COL4']
+ @table << ['中文', 'にっぽんご', '한국어', 'ABC']
+ @table << ['Chinese','Japanese','Korean', '.......']
+ @table.render.should eq <<-EOF.deindent
+ ┌─────────┬────────────┬────────┬──────────┐
+ │ COL 1 │ COL 2 │ COL 3 │ COL4 │
+ ╞═════════╪════════════╪════════╪══════════╡
+ │ 中文 │ にっぽんご │ 한국어 │ ABC │
+ │ Chinese │ Japanese │ Korean │ ....... │
+ └─────────┴────────────┴────────┴──────────┘
+ EOF
+ end
+
+ it "should allow to not generate top border" do
+ @table.style = { :border_top => false }
+ @table.headings = ['name', 'value']
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
+ @table.render.should eq <<-EOF.deindent
+ │ name │ value │
+ ╞══════╪═══════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ └──────┴───────┘
+ EOF
+ end
+
+ it "should allow to not generate bottom border" do
+ @table.style = { :border_bottom => false }
+ @table.headings = ['name', 'value']
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬───────┐
+ │ name │ value │
+ ╞══════╪═══════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ EOF
+ end
+
+ it "should be able to make a footer row" do
+ @table.headings = ['name', 'value']
+ @table.rows = [['a', 1], ['b', 2], ['c', 3]]
+ @table.add_separator(border_type: :double)
+ @table.add_row ['tot', 6]
+ @table.render.should eq <<-EOF.deindent
+ ┌──────┬───────┐
+ │ name │ value │
+ ╞══════╪═══════╡
+ │ a │ 1 │
+ │ b │ 2 │
+ │ c │ 3 │
+ ╞══════╪═══════╡
+ │ tot │ 6 │
+ └──────┴───────┘
+ EOF
+ end
+
+ it "should be able to change separators after elaboration" do
+ @table.style = { all_separators: true }
+ @table.title = 'Animals'
+ @table.headings = ['name', 'value']
+ @table.rows = [['rat', 1], ['cat', 2], ['dog', 3]]
+ @table.add_row ['totl', 6]
+ # Elaborate here, and we will modify the border-types
+ # post elaboration.
+ rows = @table.elaborate_rows
+ rows[2].border_type = :heavy # emphasize below title
+ rows[4].border_type = :div # de-emphasize below header
+ rows[-3].border_type = :double # emphasize above footer
+ @table.render.should eq <<-EOF.deindent
+ ┌──────────────┐
+ │ Animals │
+ ┝━━━━━━┯━━━━━━━┥
+ │ name │ value │
+ ├──────┼───────┤
+ │ rat │ 1 │
+ ├──────┼───────┤
+ │ cat │ 2 │
+ ├──────┼───────┤
+ │ dog │ 3 │
+ ╞══════╪═══════╡
+ │ totl │ 6 │
+ └──────┴───────┘
+ EOF
+ end
+
+ it "should be able manually specify separator borders" do
+ @table.style = { border: :unicode_round }
+ @table.title = 'Animals'
+ @table.headings = ['name', 'value']
+ @table.add_row ['rat', 1]
+ @table.add_separator
+ @table.add_row ['cat', 2]
+ @table.add_separator(border_type: :bold)
+ @table.add_row ['dog', 3]
+ @table.add_separator(border_type: :double)
+ @table.add_row ['totl', 6]
+ @table.render.should eq <<-EOF.deindent
+ ╭──────────────╮
+ │ Animals │
+ ├──────┬───────┤
+ │ name │ value │
+ ╞══════╪═══════╡
+ │ rat │ 1 │
+ ├──────┼───────┤
+ │ cat │ 2 │
+ ┝━━━━━━┿━━━━━━━┥
+ │ dog │ 3 │
+ ╞══════╪═══════╡
+ │ totl │ 6 │
+ ╰──────┴───────╯
+ EOF
+ end
+
+ it "should test many separator borders" do
+ @table.style = { border: :unicode_thick_edge }
+ @table.title = 'Borders'
+ @table.headings = ['name', 'value']
+ @table.add_row ['1st', 1]
+ @table.add_separator(border_type: :heavy)
+ @table.add_separator(border_type: :heavy_dash)
+ @table.add_separator(border_type: :heavy_dot3)
+ @table.add_separator(border_type: :heavy_dot4)
+ @table.add_separator(border_type: :thick)
+ @table.add_separator(border_type: :thick_dash)
+ @table.add_separator(border_type: :thick_dot3)
+ @table.add_separator(border_type: :thick_dot4)
+ @table.add_separator(border_type: :bold)
+ @table.add_separator(border_type: :bold_dash)
+ @table.add_separator(border_type: :bold_dot3)
+ @table.add_separator(border_type: :bold_dot4)
+ @table.add_separator(border_type: :dash)
+ @table.add_separator(border_type: :dot3)
+ @table.add_separator(border_type: :dot4)
+ @table.add_separator(border_type: :double)
+ @table.add_row ['last', 'N']
+ @table.render.should eq <<-EOF.deindent
+ ┏━━━━━━━━━━━━━━┓
+ ┃ Borders ┃
+ ┠──────┬───────┨
+ ┃ name │ value ┃
+ ┣══════╪═══════┫
+ ┃ 1st │ 1 ┃
+ ┣━━━━━━┿━━━━━━━┫
+ ┣╍╍╍╍╍╍┿╍╍╍╍╍╍╍┫
+ ┣┅┅┅┅┅┅┿┅┅┅┅┅┅┅┫
+ ┣┉┉┉┉┉┉┿┉┉┉┉┉┉┉┫
+ ┣━━━━━━┿━━━━━━━┫
+ ┣╍╍╍╍╍╍┿╍╍╍╍╍╍╍┫
+ ┣┅┅┅┅┅┅┿┅┅┅┅┅┅┅┫
+ ┣┉┉┉┉┉┉┿┉┉┉┉┉┉┉┫
+ ┣━━━━━━┿━━━━━━━┫
+ ┣╍╍╍╍╍╍┿╍╍╍╍╍╍╍┫
+ ┣┅┅┅┅┅┅┿┅┅┅┅┅┅┅┫
+ ┣┉┉┉┉┉┉┿┉┉┉┉┉┉┉┫
+ ┠╌╌╌╌╌╌┼╌╌╌╌╌╌╌┨
+ ┠┄┄┄┄┄┄┼┄┄┄┄┄┄┄┨
+ ┠┈┈┈┈┈┈┼┈┈┈┈┈┈┈┨
+ ┣══════╪═══════┫
+ ┃ last │ N ┃
+ ┗━━━━━━┷━━━━━━━┛
+ EOF
+ end
+
+ end
+end