terminal-table-2.0.0/ 0000755 0000041 0000041 00000000000 13771347011 014445 5 ustar www-data www-data terminal-table-2.0.0/Gemfile.lock 0000644 0000041 0000041 00000001651 13771347011 016672 0 ustar www-data www-data PATH
remote: .
specs:
terminal-table (2.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.3)
diff-lcs (1.4.4)
method_source (1.0.0)
pry (0.13.1)
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)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.4)
term-ansicolor (1.7.1)
tins (~> 1.0)
tins (1.0.1)
unicode-display_width (1.7.0)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 2)
pry
rake (~> 13.0)
rspec (>= 3.0)
term-ansicolor
terminal-table!
tins (~> 1.0.0)
BUNDLED WITH
2.1.4
terminal-table-2.0.0/terminal-table.gemspec 0000644 0000041 0000041 00000001767 13771347011 020725 0 ustar www-data www-data # coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'terminal-table/version'
Gem::Specification.new do |spec|
spec.name = "terminal-table"
spec.version = Terminal::Table::VERSION
spec.authors = ["TJ Holowaychuk", "Scott J. Goldman"]
spec.email = ["tj@vision-media.ca"]
spec.summary = "Simple, feature rich ascii table generation library"
spec.homepage = "https://github.com/tj/terminal-table"
spec.license = "MIT"
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", "~> 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"]
end
terminal-table-2.0.0/History.rdoc 0000644 0000041 0000041 00000004175 13771347011 016766 0 ustar www-data www-data 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
==================
* Top and bottom borders can be disabled (@kubakrzempek, #83)
* `unicode-display-width` dependency relaxes (@mvz, #88)
* Readme and docs fixes (@loualrid, #82 and @leoarnold, #86)
* Fixed some test-related warnings (@juanitofatas, #81 and @mvz, #89)
1.7.3 / 2016-09-21
==================
* Fixed compatibility issues for Ruby 1.9, 2.0, 2.1. (@vivekbisen, #80)
1.7.2 / 2016-09-09
==================
* Fix packing table to a minimal width (@vizv, #76)
1.7.1 / 2016-08-29
==================
* Update `unicode-display_width` to fix behavior with signal traps [#78, @jrmhaig]
1.7.0 / 2016-08-29
==================
All props to @vizv for this release!
* Fixed some spec failures
* Added support for full-width characters (East Asian alphabets, etc)
1.6.0 / 2016-06-06
==================
* Added table styles - margin_left, all_separators.
1.4.3 / 2011-10-13
==================
* Optimize for faster table output.
1.4.2 / 2010-01-14
==================
* Fixed some bugs with colspan
=== 1.4.1 / 2009-12-18
* Fix column alignment with separators.
=== 1.4.0 / 2009-12-18
* Can now add :seperator arbitrarily in a table [thanks splattael]
* Fix common typo: seperator -> separator [thanks splattael]
=== 1.3.0 / 2009-10-16
* Major refactoring (functionality remains the same)
=== 1.2.0 / 2009-08-06
* Added colspan support to table
=== 1.1.0 / 2009-08-06
* Added colspan support to table
=== 1.1.0 / 2009-07-13
* Added Table#==
=== 1.0.5 / 2009-03-14
* Allowing nil to be passed to table for headings
* Revised doc to show that rows can be splatted now
* Misc refactoring
=== 1.0.3 / 2009-01-15
* Moved yield or eval to Terminal::Table initialize where it belongs
=== 1.0.0 / 2009-01-13
* Initial release
terminal-table-2.0.0/.gitignore 0000644 0000041 0000041 00000000056 13771347011 016436 0 ustar www-data www-data .DS_Store
pkg
tmp
*.cache
doc
vendor
/.bundle
terminal-table-2.0.0/examples/ 0000755 0000041 0000041 00000000000 13771347011 016263 5 ustar www-data www-data terminal-table-2.0.0/examples/examples.rb 0000644 0000041 0000041 00000004264 13771347011 020434 0 ustar www-data www-data $:.unshift File.dirname(__FILE__) + '/../lib'
require 'terminal-table/import'
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 :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
terminal-table-2.0.0/Manifest 0000644 0000041 0000041 00000001101 13771347011 016127 0 ustar www-data www-data Gemfile
History.rdoc
README.rdoc
Rakefile
Todo.rdoc
examples/examples.rb
lib/terminal-table.rb
lib/terminal-table/cell.rb
lib/terminal-table/import.rb
lib/terminal-table/row.rb
lib/terminal-table/separator.rb
lib/terminal-table/style.rb
lib/terminal-table/table.rb
lib/terminal-table/table_helper.rb
lib/terminal-table/version.rb
spec/cell_spec.rb
spec/row_spec.rb
spec/spec_helper.rb
spec/table_helper_spec.rb
spec/table_spec.rb
tasks/docs.rake
tasks/gemspec.rake
tasks/spec.rake
terminal-table.gemspec
terminal-table.sublime-project
terminal-table.sublime-workspace
Manifest
terminal-table-2.0.0/Rakefile 0000644 0000041 0000041 00000000435 13771347011 016114 0 ustar www-data www-data require 'bundler'
Bundler.setup
Bundler::GemHelper.install_tasks
require 'rake'
require 'rspec/core/rake_task'
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.ruby_opts = %w[-w]
t.rspec_opts = %w[--color]
end
desc "Default: Run specs"
task :default => [:spec]
terminal-table-2.0.0/lib/ 0000755 0000041 0000041 00000000000 13771347011 015213 5 ustar www-data www-data terminal-table-2.0.0/lib/terminal-table/ 0000755 0000041 0000041 00000000000 13771347011 020113 5 ustar www-data www-data terminal-table-2.0.0/lib/terminal-table/cell.rb 0000644 0000041 0000041 00000004667 13771347011 021374 0 ustar www-data www-data require 'unicode/display_width/no_string_ext'
module Terminal
class Table
class Cell
##
# Cell value.
attr_reader :value
##
# Column span.
attr_reader :colspan
##
# Initialize with _options_.
def initialize options = nil
@value, options = options, {} unless Hash === options
@value = options.fetch :value, value
@alignment = options.fetch :alignment, nil
@colspan = options.fetch :colspan, 1
@width = options.fetch :width, @value.to_s.size
@index = options.fetch :index
@table = options.fetch :table
end
def alignment?
!@alignment.nil?
end
def alignment
@alignment || @table.style.alignment || :left
end
def alignment=(val)
supported = %w(left center right)
if supported.include?(val.to_s)
@alignment = val
else
raise "Aligment must be one of: #{supported.join(' ')}"
end
end
def align(val, position, length)
positions = { :left => :ljust, :right => :rjust, :center => :center }
val.public_send(positions[position], length)
end
def lines
@value.to_s.split(/\n/)
end
##
# Render the cell.
def render(line = 0)
left = " " * @table.style.padding_left
right = " " * @table.style.padding_right
display_width = Unicode::DisplayWidth.of(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
alias :to_s :render
##
# Returns the longest line in the cell and
# 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) }
end
##
# Returns the width of this cell
def width
padding = (colspan - 1) * @table.cell_spacing
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
w + @table.column_width(@index + counter - 1)
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
terminal-table-2.0.0/lib/terminal-table/version.rb 0000644 0000041 0000041 00000000076 13771347011 022130 0 ustar www-data www-data module Terminal
class Table
VERSION = '2.0.0'
end
end
terminal-table-2.0.0/lib/terminal-table/separator.rb 0000644 0000041 0000041 00000000536 13771347011 022444 0 ustar www-data www-data module Terminal
class Table
class Separator < Row
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)
end
border_i = @table.style.border_i
border_i + arr_x.join(border_i) + border_i
end
end
end
end
terminal-table-2.0.0/lib/terminal-table/import.rb 0000644 0000041 0000041 00000000207 13771347011 021751 0 ustar www-data www-data require 'terminal-table' #required as some people require this file directly from their Gemfiles
include Terminal::Table::TableHelper
terminal-table-2.0.0/lib/terminal-table/row.rb 0000644 0000041 0000041 00000002031 13771347011 021243 0 ustar www-data www-data module Terminal
class Table
class Row
##
# Row cells
attr_reader :cells
attr_reader :table
##
# Initialize with _width_ and _options_.
def initialize table, array = []
@cell_index = 0
@table = table
@cells = []
array.each { |item| self << item }
end
def add_cell item
options = item.is_a?(Hash) ? item : {:value => item}
cell = Cell.new(options.merge(:index => @cell_index, :table => @table))
@cell_index += cell.colspan
@cells << cell
end
alias << add_cell
def [] index
cells[index]
end
def height
cells.map { |c| c.lines.count }.max || 0
end
def render
y = @table.style.border_y
(0...height).to_a.map do |line|
y + cells.map do |cell|
cell.render(line)
end.join(y) + y
end.join("\n")
end
def number_of_columns
@cells.collect(&:colspan).inject(0, &:+)
end
end
end
end
terminal-table-2.0.0/lib/terminal-table/style.rb 0000644 0000041 0000041 00000004023 13771347011 021577 0 ustar www-data www-data module Terminal
class Table
# A Style object holds all the formatting information for a Table object
#
# To create a table with a certain style, use either the constructor
# option :style, the Table#style object or the Table#style= method
#
# All these examples have the same effect:
#
# # by constructor
# @table = Table.new(:style => {:padding_left => 2, :width => 40})
#
# # by object
# @table.style.padding_left = 2
# @table.style.width = 40
#
# # by method
# @table.style = {:padding_left => 2, :width => 40}
#
# To set a default style for all tables created afterwards use Style.defaults=
#
# Terminal::Table::Style.defaults = {:width => 80}
#
class Style
@@defaults = {
:border_x => "-", :border_y => "|", :border_i => "+",
:border_top => true, :border_bottom => true,
:padding_left => 1, :padding_right => 1,
:margin_left => '',
:width => nil, :alignment => nil,
:all_separators => false
}
attr_accessor :border_x
attr_accessor :border_y
attr_accessor :border_i
attr_accessor :border_top
attr_accessor :border_bottom
attr_accessor :padding_left
attr_accessor :padding_right
attr_accessor :margin_left
attr_accessor :width
attr_accessor :alignment
attr_accessor :all_separators
def initialize options = {}
apply self.class.defaults.merge(options)
end
def apply options
options.each { |m, v| __send__ "#{m}=", v }
end
class << self
def defaults
@@defaults
end
def defaults= options
@@defaults = defaults.merge(options)
end
end
def on_change attr
method_name = :"#{attr}="
old_method = method method_name
define_singleton_method(method_name) do |value|
old_method.call value
yield attr.to_sym, value
end
end
end
end
end
terminal-table-2.0.0/lib/terminal-table/table_helper.rb 0000644 0000041 0000041 00000000314 13771347011 023064 0 ustar www-data www-data module Terminal
class Table
module TableHelper
def table headings = [], *rows, &block
Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
end
end
end
end
terminal-table-2.0.0/lib/terminal-table/table.rb 0000644 0000041 0000041 00000022530 13771347011 021531 0 ustar www-data www-data require 'unicode/display_width/no_string_ext'
module Terminal
class Table
attr_reader :title
attr_reader :headings
##
# Generates a ASCII table with the given _options_.
def initialize options = {}, &block
@headings = []
@rows = []
@column_widths = []
self.style = options.fetch :style, {}
self.headings = options.fetch :headings, []
self.rows = options.fetch :rows, []
self.title = options.fetch :title, nil
yield_or_eval(&block) if block
style.on_change(:width) { require_column_widths_recalc }
end
##
# 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
cell.alignment = alignment unless cell.alignment?
end
end
##
# Add a row.
def add_row array
row = array == :separator ? Separator.new(self) : Row.new(self, array)
@rows << row
require_column_widths_recalc unless row.is_a?(Separator)
end
alias :<< :add_row
##
# Add a separator.
def add_separator
self << :separator
end
def cell_spacing
cell_padding + style.border_y.length
end
def cell_padding
style.padding_left + style.padding_right
end
##
# Return column _n_.
def column n, method = :value, array = rows
array.map { |row|
# for each cells in a row, find the column with index
# just greater than the required one, and go back one.
index = col = 0
row.cells.each do |cell|
break if index > n
index += cell.colspan
col += 1
end
cell = row[col - 1]
cell && method ? cell.__send__(method) : cell
}.compact
end
##
# Return _n_ column including headings.
def column_with_headings n, method = :value
column n, method, headings_with_rows
end
##
# Return columns.
def columns
(0...number_of_columns).map { |n| column n }
end
##
# Return length of column _n_.
def column_width n
column_widths[n] || 0
end
alias length_of_column column_width # for legacy support
##
# Return total number of columns available.
def number_of_columns
headings_with_rows.map { |r| r.number_of_columns }.max || 0
end
##
# Set the headings
def headings= arrays
arrays = [arrays] unless arrays.first.is_a?(Array)
@headings = arrays.map do |array|
row = Row.new(self, array)
require_column_widths_recalc
row
end
end
##
# Render the table.
def render
separator = Separator.new(self)
buffer = style.border_top ? [separator] : []
unless @title.nil?
buffer << Row.new(self, [title_cell_options])
buffer << separator
end
@headings.each do |row|
unless row.cells.empty?
buffer << row
buffer << separator
end
end
if style.all_separators
buffer += @rows.product([separator]).flatten
else
buffer += @rows
buffer << separator if style.border_bottom
end
buffer.map { |r| style.margin_left + r.render.rstrip }.join("\n")
end
alias :to_s :render
##
# Return rows without separator rows.
def rows
@rows.reject { |row| row.is_a? Separator }
end
def rows= array
@rows = []
array.each { |arr| self << arr }
end
def style=(options)
style.apply options
end
def style
@style ||= Style.new
end
def title=(title)
@title = title
require_column_widths_recalc
end
##
# Check if _other_ is equal to self. _other_ is considered equal
# if it contains the same headings and rows.
def == other
if other.respond_to? :render and other.respond_to? :rows
self.headings == other.headings and self.rows == other.rows
end
end
private
def columns_width
column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
end
def recalc_column_widths
@require_column_widths_recalc = false
n_cols = number_of_columns
space_width = cell_spacing
return if n_cols == 0
# prepare rows
all_rows = headings_with_rows
all_rows << Row.new(self, [title_cell_options]) unless @title.nil?
# DP states, dp[colspan][index][split_offset] => column_width.
dp = []
# prepare initial value for DP.
all_rows.each do |row|
index = 0
row.cells.each do |cell|
cell_value = cell.value_for_column_width_recalc
cell_width = Unicode::DisplayWidth.of(cell_value.to_s)
colspan = cell.colspan
# find column width from each single cell.
dp[colspan] ||= []
dp[colspan][index] ||= [0] # add a fake cell with length 0.
dp[colspan][index][colspan] ||= 0 # initialize column length to 0.
# the last index `colspan` means width of the single column (split
# at end of each column), not a width made up of multiple columns.
single_column_length = [cell_width, dp[colspan][index][colspan]].max
dp[colspan][index][colspan] = single_column_length
index += colspan
end
end
# run DP.
(1..n_cols).each do |colspan|
dp[colspan] ||= []
(0..n_cols-colspan).each do |index|
dp[colspan][index] ||= [1]
(1...colspan).each do |offset|
# processed level became reverse map from width => [offset, ...].
left_colspan = offset
left_index = index
left_width = dp[left_colspan][left_index].keys.first
right_colspan = colspan - left_colspan
right_index = index + offset
right_width = dp[right_colspan][right_index].keys.first
dp[colspan][index][offset] = left_width + right_width + space_width
end
# reverse map it for resolution (max width and short offset first).
rmap = {}
dp[colspan][index].each_with_index do |width, offset|
rmap[width] ||= []
rmap[width] << offset
end
# sort reversely and store it back.
dp[colspan][index] = Hash[rmap.sort.reverse]
end
end
resolve = lambda do |colspan, full_width, index = 0|
# stop if reaches the bottom level.
return @column_widths[index] = full_width if colspan == 1
# choose best split offset for partition, or second best result
# if first one is not dividable.
candidate_offsets = dp[colspan][index].collect(&:last).flatten
offset = candidate_offsets[0]
offset = candidate_offsets[1] if offset == colspan
# prepare for next round.
left_colspan = offset
left_index = index
left_width = dp[left_colspan][left_index].keys.first
right_colspan = colspan - left_colspan
right_index = index + offset
right_width = dp[right_colspan][right_index].keys.first
# calculate reference column width, give remaining spaces to left.
total_non_space_width = full_width - (colspan - 1) * space_width
ref_column_width = total_non_space_width / colspan
remainder = total_non_space_width % colspan
rem_left_width = [remainder, left_colspan].min
rem_right_width = remainder - rem_left_width
ref_left_width = ref_column_width * left_colspan +
(left_colspan - 1) * space_width + rem_left_width
ref_right_width = ref_column_width * right_colspan +
(right_colspan - 1) * space_width + rem_right_width
# at most one width can be greater than the reference width.
if left_width <= ref_left_width and right_width <= ref_right_width
# use refernce width (evenly partition).
left_width = ref_left_width
right_width = ref_right_width
else
# the wider one takes its value, shorter one takes the rest.
if left_width > ref_left_width
right_width = full_width - left_width - space_width
else
left_width = full_width - right_width - space_width
end
end
# run next round.
resolve.call(left_colspan, left_width, left_index)
resolve.call(right_colspan, right_width, right_index)
end
full_width = dp[n_cols][0].keys.first
unless style.width.nil?
new_width = style.width - space_width - style.border_y.length
if new_width < full_width
raise "Table width exceeds wanted width " +
"of #{style.width} characters."
end
full_width = new_width
end
resolve.call(n_cols, full_width)
end
##
# Return headings combined with rows.
def headings_with_rows
@headings + rows
end
def yield_or_eval &block
return unless block
if block.arity > 0
yield self
else
self.instance_eval(&block)
end
end
def title_cell_options
{:value => @title, :alignment => :center, :colspan => number_of_columns}
end
def require_column_widths_recalc
@require_column_widths_recalc = true
end
def column_widths
recalc_column_widths if @require_column_widths_recalc
@column_widths
end
end
end
terminal-table-2.0.0/lib/terminal-table.rb 0000644 0000041 0000041 00000002330 13771347011 020436 0 ustar www-data www-data #--
# Copyright (c) 2008-2009 TJ Holowaychuk
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# 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}"
end
terminal-table-2.0.0/README.rdoc 0000644 0000041 0000041 00000013272 13771347011 016260 0 ustar www-data www-data {
}[https://github.com/tj/terminal-table/actions]
= Terminal Table
== Description
Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby.
== Installation
$ gem install terminal-table
== Usage
=== Basics
To use Terminal 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 = 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:
table = Terminal::Table.new do |t|
t.rows = rows
end
table = Terminal::Table.new do
self.rows = rows
end
Adding rows one by one:
table = Terminal::Table.new do |t|
t << ['One', 1]
t.add_row ['Two', 2]
end
To add separators between rows:
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:
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:
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:
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:
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 = 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:
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 = 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
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 |
To change the default style options:
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:
table = Terminal::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
terminal-table-2.0.0/Gemfile 0000644 0000041 0000041 00000000207 13771347011 015737 0 ustar www-data www-data source 'https://rubygems.org'
# Specify your gem's dependencies in test-gem.gemspec
gemspec
gem 'tins', '~> 1.0.0' # Ruby 1.9 compat
terminal-table-2.0.0/.github/ 0000755 0000041 0000041 00000000000 13771347011 016005 5 ustar www-data www-data terminal-table-2.0.0/.github/workflows/ 0000755 0000041 0000041 00000000000 13771347011 020042 5 ustar www-data www-data terminal-table-2.0.0/.github/workflows/ci.yml 0000644 0000041 0000041 00000001061 13771347011 021156 0 ustar www-data www-data 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]
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: ${{ matrix.ruby }}
- run: bundle install
- run: bundle exec rspec
terminal-table-2.0.0/Todo.rdoc 0000644 0000041 0000041 00000000306 13771347011 016222 0 ustar www-data www-data
== Major:
* Nothing
== Minor:
* Programmatically add separator rows
* Add multi-column sorting
* Change; pre-create Cell and Heading objects to clean up Table a bit
== Brainstorming:
* Nothing terminal-table-2.0.0/LICENSE.txt 0000644 0000041 0000041 00000002123 13771347011 016266 0 ustar www-data www-data The MIT License (MIT)
Copyright (c) 2008-2017 TJ Holowaychuk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.