diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..5ace460 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e7c1abe --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: CI +on: [push] + +jobs: + test: + if: "!contains(github.event.head_commit.message, 'ci skip')" + + continue-on-error: ${{ endsWith(matrix.ruby, 'head') || matrix.ruby == 'debug' }} + + strategy: + fail-fast: false + + matrix: + os: [ubuntu] + ruby: [2.4, 2.5, 2.6, 2.7, '3.0', 3.1, 3.2] + + runs-on: ${{ matrix.os }}-latest + + steps: + - uses: actions/checkout@v3 + + - uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + ruby-version: ${{ matrix.ruby }} + + - run: bundle install + - run: bundle exec rspec diff --git a/.gitignore b/.gitignore index 863ddb8..b91d764 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,9 @@ pkg tmp *.cache doc +vendor +/.bundle Gemfile.lock + +# tempfiles +*~ diff --git a/History.rdoc b/History.rdoc index 58a96f8..06432e1 100644 --- a/History.rdoc +++ b/History.rdoc @@ -1,3 +1,60 @@ +3.0.2 / 2021-09-19 +================== + +- fix align_column for nil values and colspan + +3.0.1 / 2021-05-10 +================== + +- Support for unicode-display_width 2.0 +- Fix issue where last row of an empty table changed format + +3.0.0 / 2020-01-27 +================== + +- 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 +================== + +- Drops official support for Ruby 1.9.x with and of life on 2015-02-23 +- Drops official support for Ruby 2.0.x with and of life on 2016-02-24 +- Drops official support for Ruby 2.1.x with and of life on 2017-03-31 +- Drops official support for Ruby 2.2.x with and of life on 2018-03-31 +- Drops official support for Ruby 2.3.x with and of life on 2019-03-31 + 1.8.0 / 2017-05-16 ================== diff --git a/README.md b/README.md new file mode 100644 index 0000000..73b0a12 --- /dev/null +++ b/README.md @@ -0,0 +1,417 @@ +[![CI status](https://github.com/tj/terminal-table/workflows/CI/badge.svg)](https://github.com/tj/terminal-table/actions) +[![Gem Version](https://badge.fury.io/rb/terminal-table.svg)](https://badge.fury.io/rb/terminal-table) + +# 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 = TerminalTable::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 = TerminalTable::Table.new do |t| + t.rows = rows +end + +table = TerminalTable::Table.new do + self.rows = rows +end +``` +Adding rows one by one: + +```ruby +table = TerminalTable::Table.new do |t| + t << ['One', 1] + t.add_row ['Two', 2] +end +``` +To add separators between rows: + +```ruby +table = TerminalTable::Table.new do |t| + t << ['One', 1] # Using << (push) as an alias for add_row + t << :separator # Using << with :separator as an alias for add_separator + t.add_row ['Two', 2] + t.add_separator # Note - this version allows setting the separator's border_type + t.add_row ['Three', 3] +end + +# > puts table +# +# +-------+---+ +# | One | 1 | +# +-------+---+ +# | Two | 2 | +# +-------+---+ +# | Three | 3 | +# +-------+---+ +``` +Cells can handle multiline content: + +```ruby +table = TerminalTable::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 = TerminalTable::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 = TerminalTable::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 = TerminalTable::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 +# | 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 = TerminalTable::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 = TerminalTable::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 = TerminalTable::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 +TerminalTable::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 = TerminalTable::Table.new +table.title = "Cheatsheet" +table.headings = ['Word', 'Number'] +table.rows = rows +table.style = {:width => 40} +``` + +## New Formatting + +### Unicode Table Borders +Support for Unicode 'box art' borders presented a challenge, as the original 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, as this gem has been around a long time and making breaking changes would have been 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::TerminalTable::Border +``` +table.style = { :border => TerminalTable::Table::UnicodeBorder.new() } +table.style = { :border => TerminalTable::Table::UnicodeRoundBorder.new() } +table.style = { :border => TerminalTable::Table::UnicodeThickEdgeBorder.new() } +``` + +If you define a custom class and wish to use the symbol shortcut, you must namespace within `TerminalTable::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 and divider types. + +```ruby +@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: '╍', +} +``` + +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 elements can be these can be overridden by poking setting the Hash, should the need arise: + +``` +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 `:div`. Additional separator border types (e.g. `:double`, `:heavy`, `:dash` - see full list below) can be applied to separate the sections (e.g. header/footer/title). + +The separator's `border_type` may be specified when a user-defined separator is 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 at the table level, it may be necessary to elaborate the implicit Separator rows prior to rendering. +```ruby +table = TerminalTable::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 +``` + +## Example: Displaying a small CSV spreadsheet + +This example code demonstrates using Terminal-table and CSV to display a small spreadsheet. + +```ruby +#!/usr/bin/env ruby +require "csv" +require "terminal-table" +use_stdin = ARGV[0].nil? || (ARGV[0] == '-') +io_object = use_stdin ? $stdin : File.open(ARGV[0], 'r') +csv = CSV.new(io_object) +csv_array = csv.to_a +user_table = TerminalTable::Table.new do |v| + v.style = { :border => :unicode_round } # >= v3.0.0 + v.title = "Some Title" + v.headings = csv_array[0] + v.rows = csv_array[1..-1] +end +puts user_table +``` + +See also `examples/show_csv_table.rb` in the source distribution. + +## 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/README.rdoc b/README.rdoc deleted file mode 100755 index 912b424..0000000 --- a/README.rdoc +++ /dev/null @@ -1,247 +0,0 @@ -= TerminalTable Table - -== Description - -TerminalTable Table is a fast and simple, yet feature rich ASCII table generator written in Ruby. - -== Installation - - $ gem install terminal-table - -== Usage - -=== Basics - -To use TerminalTable Table: - - require 'terminal-table' - -To generate a table, provide an array of arrays (which are interpreted as rows): - - rows = [] - rows << ['One', 1] - rows << ['Two', 2] - rows << ['Three', 3] - table = TerminalTable::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: - - table = TerminalTable::Table.new do |t| - t.rows = rows - end - - table = TerminalTable::Table.new do - self.rows = rows - end - -Adding rows one by one: - - table = TerminalTable::Table.new do |t| - t << ['One', 1] - t.add_row ['Two', 2] - end - -To add separators between rows: - - table = TerminalTable::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: - - table = TerminalTable::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: - - table = TerminalTable::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: - - table = TerminalTable::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: - - 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: - - 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: - - table = TerminalTable::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: - - 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: - - table = TerminalTable::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 - - table = TerminalTable::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 | - -To change the default style options: - - TerminalTable::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: - - table = TerminalTable::Table.new - table.title = "Cheatsheet" - table.headings = ['Word', 'Number'] - table.rows = rows - table.style = {:width => 40} - -== More examples - -For more examples, please see the examples/examples.rb file included in the source distribution. - -== Author - -TJ Holowaychuk diff --git a/examples/data.csv b/examples/data.csv new file mode 100644 index 0000000..2af8d47 --- /dev/null +++ b/examples/data.csv @@ -0,0 +1,4 @@ +First Name,Last Name,Email +TJ,Holowaychuk,tj@vision-media.ca +Bob,Someone,bob@vision-media.ca +Joe,Whatever,joe@vision-media.ca 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..56717e3 --- /dev/null +++ b/examples/examples_unicode.rb @@ -0,0 +1,89 @@ +#!/usr/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/issue118.rb b/examples/issue118.rb new file mode 100755 index 0000000..e94eb71 --- /dev/null +++ b/examples/issue118.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby + +require_relative '../lib/terminal-table' + +puts Terminal::Table.new(headings: ['a', 'b', 'c', 'd'], style: { border: :unicode }) + +puts + +tbl = Terminal::Table.new do |t| + t.style = { border: :unicode } + t.add_separator + t.add_separator + t.add_row ['x','y','z'] + t.add_separator + t.add_separator +end +puts tbl + +puts + +puts Terminal::Table.new(headings: [['a', 'b', 'c', 'd'], ['cat','dog','frog','mouse']], style: { border: :unicode }) + +puts + +puts Terminal::Table.new(headings: ['a', 'b', 'c', 'd']) + +puts + +tbl = Terminal::Table.new do |t| + t.add_separator + t.add_separator + t.add_row ['x','y','z'] + t.add_separator + t.add_separator +end +puts tbl 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/show_csv_table.rb b/examples/show_csv_table.rb new file mode 100755 index 0000000..1bb368a --- /dev/null +++ b/examples/show_csv_table.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require "csv" +$LOAD_PATH << "#{__dir__}/../lib" +require "terminal-table" + +# +# Usage: +# ./show_csv_table.rb data.csv +# cat data.csv | ./show_csv_table.rb +# cat data.csv | ./show_csv_table.rb - +# +# +# Reads a CSV from $stdin if no argument given, or argument is '-' +# otherwise interprets first cmdline argument as the CSV filename +# +use_stdin = ARGV[0].nil? || (ARGV[0] == '-') +io_object = use_stdin ? $stdin : File.open(ARGV[0], 'r') +csv = CSV.new(io_object) + +# +# Convert to an array for use w/ terminal-table +# The assumption is that this is a pretty small spreadsheet. +# +csv_array = csv.to_a + +user_table = Terminal::Table.new do |v| + v.style = { :border => :unicode_round } # >= v3.0.0 + v.title = "Some Title" + v.headings = csv_array[0] + v.rows = csv_array[1..-1] +end + +puts user_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 e269234..cfa7573 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 ## @@ -82,12 +82,12 @@ def width 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)/, '') + def inspect + fields = %i[alignment colspan index value width].map do |name| + val = self.instance_variable_get('@'+name.to_s) + "@#{name}=#{val.inspect}" + end.join(', ') + return "#<#{self.class} #{fields}>" end end end diff --git a/lib/terminal-table/row.rb b/lib/terminal-table/row.rb index 8eec4e0..faef8d0 100644 --- a/lib/terminal-table/row.rb +++ b/lib/terminal-table/row.rb @@ -12,7 +12,7 @@ class Row ## # Initialize with _width_ and _options_. - def initialize table, array = [] + def initialize table, array = [], **_kwargs @cell_index = 0 @table = table @cells = [] @@ -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 bed51db..90c34fd 100644 --- a/lib/terminal-table/separator.rb +++ b/lib/terminal-table/separator.rb @@ -2,13 +2,65 @@ module TerminalTable 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 0d1fce8..5485808 100644 --- a/lib/terminal-table/style.rb +++ b/lib/terminal-table/style.rb @@ -1,5 +1,171 @@ +# coding: utf-8 +require 'forwardable' + module TerminalTable 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 # TerminalTable::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 = TerminalTable::Table::const_get(klass_str) + @border = klass.new + rescue NameError + raise "Cannot lookup class TerminalTable::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 @@ -53,17 +250,24 @@ def initialize 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 old mode 100755 new mode 100644 index c99275c..2c5edbd --- 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 = [] @@ -26,10 +27,8 @@ def initialize options = {}, &block # Align column _n_ to the given _alignment_ of :center, :left, or :right. def align_column n, alignment - r = rows - column(n).each_with_index do |col, i| - cell = r[i][n] - next unless cell + # nil forces the column method to return the cell itself + column(n, nil).each do |cell| cell.alignment = alignment unless cell.alignment? end end @@ -47,12 +46,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 +118,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 +210,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 +329,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..e8c2d16 --- /dev/null +++ b/lib/terminal-table/util.rb @@ -0,0 +1,13 @@ +module TerminalTable + 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 dd1a2b8..e14ff06 100644 --- a/lib/terminal-table/version.rb +++ b/lib/terminal-table/version.rb @@ -1,5 +1,5 @@ module TerminalTable class Table - VERSION = '1.8.0' + VERSION = '3.0.2' end end diff --git a/spec/style_spec.rb b/spec/style_spec.rb new file mode 100644 index 0000000..a7f710e --- /dev/null +++ b/spec/style_spec.rb @@ -0,0 +1,29 @@ +# encoding: utf-8 +require 'spec_helper' + +module TerminalTable + describe Table::Style do + + before :each do + @table = Table.new + end + + border_map = { + :markdown => TerminalTable::Table::MarkdownBorder, + :ascii => TerminalTable::Table::AsciiBorder, + :unicode => TerminalTable::Table::UnicodeBorder, + :unicode_thick_edge => TerminalTable::Table::UnicodeThickEdgeBorder, + :unicode_round => TerminalTable::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 c0106d4..34fabc9 100755 --- a/spec/table_spec.rb +++ b/spec/table_spec.rb @@ -41,7 +41,7 @@ module TerminalTable | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 88 | - +---+---+----+ + +-------+----+ EOF end @@ -87,7 +87,7 @@ module TerminalTable @table.headings = ['Char', 'Num'] @table << ['a', 1] separator = TerminalTable::Table::Separator.new(@table) - separator.render.should eq '+------+-----+' + separator.render.should eq '+------------+' end it "should add separator" do @@ -131,6 +131,7 @@ module TerminalTable @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 TerminalTable @table << ['c', 3] @table.style.width = 21 @table.style.alignment = :right + @table.render.should eq <<-EOF.deindent +-----------+-------+ | Character | Num | @@ -195,7 +197,7 @@ module TerminalTable @table << ['b', 2] @table << ['c', 3] @table.render.should eq <<-EOF.deindent - +------+-----+ + +------------+ | Title | +------+-----+ | Char | Num | @@ -353,20 +355,44 @@ module TerminalTable 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.rows = [[nil, 0], [{:value => 'a', :alignment => :left}, nil], ['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 | + | | 0 | + | a | | | b | 2 | | c | 3 | +----------------------+-----+ EOF end + it "should align columns taking colspan into account" do + @table.headings = ['xxx'] * 4 + @table.rows = [ + [1, 2, 3, 4], + [5, {:value => 6, :colspan => 2}, 7], + [{:value => 8, :colspan => 2}, 9, :a], + [{:value => :b, :colspan => 2}, {:value => :c, :colspan => 2}], + [{:value => :d, :colspan => 3}, :e], + ] + @table.align_column 3, :right + @table.render.should eq <<-EOF.deindent + +-----+-----+-----+-----+ + | xxx | xxx | xxx | xxx | + +-----+-----+-----+-----+ + | 1 | 2 | 3 | 4 | + | 5 | 6 | 7 | + | 8 | 9 | a | + | b | c | + | d | e | + +-----------------+-----+ + EOF + end + describe "#==" do it "should be equal to itself" do t = Table.new @@ -418,7 +444,7 @@ module TerminalTable @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 +464,7 @@ module TerminalTable | a | 1 | 2 | | b | 3 | 4 | | joined | c | - +-----+-----+-------+ + +-----------+-------+ EOF end @@ -452,7 +478,7 @@ module TerminalTable | a | 1 | 2 | | b | 3 | 4 | | c | joined | - +-----+-----+-------+ + +-----+-------------+ EOF end @@ -466,7 +492,7 @@ module TerminalTable | a | 1 | 2 | | b | 3 | 4 | | c | joined that is very very long | - +-----+---------------+---------------+ + +-----+-------------------------------+ EOF end @@ -477,7 +503,7 @@ module TerminalTable +-------+---+---+ | 12345 | 2 | 3 | | 123456789 | 4 | - +-------+---+---+ + +-----------+---+ EOF end @@ -494,7 +520,7 @@ module TerminalTable | 123456789 | 4 | | | 123 | 444444444 | | 0 | | - +-------+---+-----+-----+ + +-----------------+-----+ EOF end @@ -509,7 +535,7 @@ module TerminalTable | 4 | 5555555 | | 66666666 | 777 | | 20202020202020202020 | - +-------+-------+------+ + +----------------------+ EOF end @@ -533,7 +559,7 @@ module TerminalTable @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 +586,7 @@ module TerminalTable end @table.render.should eq <<-EOF.deindent - +--------+----+----+-------+------+-----+----+ + +--------+---------+--------------+----------+ | name | Values | Other values | Column 3 | +--------+----+----+-------+------+-----+----+ | row #1 | 0 | 1 | 2 | 3 | 4 | 5 | @@ -572,11 +598,11 @@ module TerminalTable 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 +638,7 @@ module TerminalTable @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 +691,34 @@ module TerminalTable | 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..09397f6 --- /dev/null +++ b/spec/unicode_table_spec.rb @@ -0,0 +1,851 @@ +# encoding: utf-8 +require 'spec_helper' + +module TerminalTable + describe Table do + before :each do + @table = Table.new + @table.style = { :border => TerminalTable::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 = TerminalTable::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 = TerminalTable::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 = TerminalTable::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]] + TerminalTable::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 + + it "should allow headings with no rows (issue #118.a)" do + @table.headings = ['a', 'b', 'c', 'd'] + @table.render.should eq <<-EOF.deindent + ┌───┬───┬───┬───┐ + │ a │ b │ c │ d │ + ╞═══╧═══╧═══╧═══╡ + └───────────────┘ + EOF + end + + it "should allow multiple headings with no rows (issue #118.b)" do + @table.headings = [['a', 'b', 'c', 'd'], ['cat','dog','frog','mouse']] + @table.render.should eq <<-EOF.deindent + ┌─────┬─────┬──────┬───────┐ + │ a │ b │ c │ d │ + ╞═════╪═════╪══════╪═══════╡ + │ cat │ dog │ frog │ mouse │ + ╞═════╧═════╧══════╧═══════╡ + └──────────────────────────┘ + EOF + end + + it "should not create spurrious vertical intersections for adjacent separators (issue #118.c)" do + @table.add_separator + @table.add_separator + @table.add_row ['x','y','z'] + @table.add_separator + @table.add_separator + @table.render.should eq <<-EOF.deindent + ┌───────────┐ + ├───────────┤ + ├───┬───┬───┤ + │ x │ y │ z │ + ├───┴───┴───┤ + ├───────────┤ + └───────────┘ + EOF + end + + + end +end diff --git a/terminal-table.gemspec b/terminal-table.gemspec index 1ab1b20..1c4d761 100644 --- a/terminal-table.gemspec +++ b/terminal-table.gemspec @@ -16,11 +16,11 @@ Gem::Specification.new do |spec| spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } spec.require_paths = ["lib"] - spec.add_development_dependency "bundler", "~> 1.10" - spec.add_development_dependency "rake", "~> 10.0" + spec.add_development_dependency "bundler", "~> 2" + spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "rspec", ">= 3.0" spec.add_development_dependency "term-ansicolor" spec.add_development_dependency "pry" - spec.add_runtime_dependency "unicode-display_width", ["~> 1.1", ">= 1.1.1"] + spec.add_runtime_dependency "unicode-display_width", [">= 1.1.1", "< 3"] end