terminal-table-1.4.5/0000755000004100000410000000000012252257265014461 5ustar www-datawww-dataterminal-table-1.4.5/Rakefile0000644000004100000410000000064512252257265016133 0ustar www-datawww-data require 'rubygems' require 'rake' require 'echoe' require './lib/terminal-table.rb' Echoe.new("terminal-table", Terminal::Table::VERSION) do |p| p.author = "TJ Holowaychuk" p.email = "tj@vision-media.ca" p.summary = "Simple, feature rich ascii table generation library" p.url = "http://github.com/visionmedia/terminal-table" p.runtime_dependencies = [] end Dir['tasks/**/*.rake'].sort.each { |lib| load lib }terminal-table-1.4.5/examples/0000755000004100000410000000000012252257265016277 5ustar www-datawww-dataterminal-table-1.4.5/examples/examples.rb0000644000004100000410000000410412252257265020441 0ustar www-datawww-data$:.unshift File.dirname(__FILE__) + '/../lib' require 'terminal-table/import' puts puts table(['a', 'b'], [1, 2], [3, 4]) 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-1.4.5/terminal-table.gemspec0000644000004100000410000000410112252257265020722 0ustar www-datawww-data# -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = %q{terminal-table} s.version = "1.4.5" s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= s.authors = ["TJ Holowaychuk", "Scott J. Goldman"] s.date = %q{2012-3-15} s.description = %q{Simple, feature rich ascii table generation library} s.email = %q{tj@vision-media.ca} s.extra_rdoc_files = ["README.rdoc", "lib/terminal-table.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/version.rb", "lib/terminal-table/row.rb", "lib/terminal-table/separator.rb", "lib/terminal-table/style.rb", "lib/terminal-table/table_helper.rb", "lib/terminal-table/version.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"] s.files = ["History.rdoc", "Manifest", "README.rdoc", "Rakefile", "Todo.rdoc", "examples/examples.rb", "lib/terminal-table.rb", "lib/terminal-table/cell.rb", "lib/terminal-table/core_ext.rb", "lib/terminal-table/import.rb", "lib/terminal-table/table.rb", "lib/terminal-table/table_helper.rb", "lib/terminal-table/version.rb", "lib/terminal-table/row.rb", "lib/terminal-table/separator.rb", "lib/terminal-table/style.rb", "spec/cell_spec.rb", "spec/core_ext_spec.rb", "spec/import_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/table_spec.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "terminal-table.gemspec"] s.homepage = %q{http://github.com/visionmedia/terminal-table} s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Terminal-table", "--main", "README.rdoc"] s.require_paths = ["lib"] s.rubyforge_project = %q{terminal-table} s.rubygems_version = %q{1.3.5} s.summary = %q{Simple, feature rich ascii table generation library} if s.respond_to? :specification_version= then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then else end else end end terminal-table-1.4.5/Todo.rdoc0000644000004100000410000000030612252257265016236 0ustar www-datawww-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: * Nothingterminal-table-1.4.5/History.rdoc0000644000004100000410000000171212252257265016774 0ustar www-datawww-data1.4.5 / 2012-3-15 ================== * Add color support. * Various bugfixes 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-1.4.5/spec/0000755000004100000410000000000012252257265015413 5ustar www-datawww-dataterminal-table-1.4.5/spec/core_ext_spec.rb0000644000004100000410000000057412252257265020570 0ustar www-datawww-data require File.dirname(__FILE__) + '/spec_helper' describe String do describe "#align" do it "should center" do 'foo'.align(:center, 10).should == ' foo ' end it "should align left" do 'foo'.align(:left, 10).should == 'foo ' end it "should align right" do 'foo'.align(:right, 10).should == ' foo' end end end terminal-table-1.4.5/spec/spec.opts0000644000004100000410000000000712252257265017251 0ustar www-datawww-data--colorterminal-table-1.4.5/spec/spec_helper.rb0000644000004100000410000000017412252257265020233 0ustar www-datawww-data require File.dirname(__FILE__) + '/../lib/terminal-table' class String def deindent strip.gsub(/^ */, '') end end terminal-table-1.4.5/spec/cell_spec.rb0000644000004100000410000000350712252257265017676 0ustar www-datawww-datarequire 'rubygems' require 'term/ansicolor' class String; include Term::ANSIColor; end require File.dirname(__FILE__) + '/spec_helper' describe Terminal::Table do Cell = Terminal::Table::Cell it "should default alignment to the left" do cell = Cell.new :value => 'foo', :table => Terminal::Table.new, :index => 0 cell.value.should == 'foo' cell.alignment.should == :left end it "should allow overriding of alignment" do cell = Cell.new :value => 'foo', :alignment => :center, :table => Terminal::Table.new, :index => 0 cell.value.should == 'foo' cell.alignment.should == :center end it "should allow :left, :right and :center for alignment" do @cell = Cell.new :value => 'foo', :table => Terminal::Table.new, :index => 0 @cell.alignment = :left @cell.alignment = :right @cell.alignment = :center lambda { @cell.alignment = "foo" }.should raise_error end it "should allow multiline content" do cell = Cell.new :value => "barrissimo\n"+"foo".yellow, :table => Terminal::Table.new, :index => 0 cell.value.should == "barrissimo\n"+"foo".yellow cell.lines.should == ['barrissimo','foo'.yellow] cell.value_for_column_width_recalc.should == 'barrissimo' cell.render(0).should == " barrissimo " end it "should allow colorized content" do cell = Cell.new :value => "foo".red, :table => Terminal::Table.new, :index => 0 cell.value.should == "\e[31mfoo\e[0m" cell.value_for_column_width_recalc.should == 'foo' cell.render.should == " \e[31mfoo\e[0m " end it "should render padding properly" do @table = Terminal::Table.new(:rows => [['foo', '2'], ['3', '4']], :style => {:padding_right => 3}) cell = @table.rows.first.cells.first cell.value.should == 'foo' cell.alignment.should == :left cell.render.should == " foo " end end terminal-table-1.4.5/spec/table_spec.rb0000644000004100000410000003604112252257265020045 0ustar www-datawww-data require File.dirname(__FILE__) + '/spec_helper' module Terminal describe Table do before :each do @table = Table.new end it "should select columns" do @table << ['foo', 'bar'] @table << ['big foo', 'big foo bar'] @table.column(1).should == ['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 == [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 == [1,"4,5"] @table.column_with_headings(1).should == [2,"4,5"] @table.column_with_headings(2).should == [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 == <<-EOF.deindent +---+---+----+ | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 88 | +---+---+----+ EOF end it "should count columns" do @table << [1, 2, 3] @table.number_of_columns.should == 3 end it "should iterate columns" do @table << [1, 2, 3] @table << [4, 5, 6] @table.columns.should == [[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 == ['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 == ['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 == 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 == 18 end it "should render separators" do @table.headings = ['Char', 'Num'] @table << ['a', 1] separator = Terminal::Table::Separator.new(@table) separator.render.should == '+------+-----+' end it "should add separator" do @table << ['a', 1] @table.add_separator @table << ['b', 2] @table.rows.size.should == 2 end it "should render an empty table properly" do @table.render.should == <<-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 == <<-EOF.deindent +------+-----+ | Char | Num | +------+-----+ | a | 1 | | b | 2 | | c | 3 | +------+-----+ EOF end it "should render styles properly" do @table.headings = ['Char', 'Num'] @table.style = {:border_x => "=", :border_y => ":", :border_i => "x", :padding_left => 0, :padding_right => 2} @table << ['a', 1] @table << ['b', 2] @table << ['c', 3] @table.style.padding_right.should == 2 @table.render.should == <<-EOF.deindent x======x=====x :Char :Num : x======x=====x :a :1 : :b :2 : :c :3 : x======x=====x 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 == <<-EOF.deindent +---------+---------+ | Char | Num | +---------+---------+ | a | 1 | | b | 2 | | c | 3 | +---------+---------+ EOF 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 == <<-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 == <<-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 == <<-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 == <<-EOF.deindent +------+-----+ | Char | Num | +------+-----+ | a | 1 | | b | 2 | +------+-----+ | c | 3 | +------+-----+ EOF end it "should render properly using block syntax" do table = Terminal::Table.new do |t| t << ['a', 1] t << ['b', 2] t << ['c', 3] end table.render.should == <<-EOF.deindent +---+---+ | a | 1 | | b | 2 | | c | 3 | +---+---+ EOF end it "should render properly using instance_eval block syntax" do table = Terminal::Table.new do add_row ['a', 1] add_row ['b', 2] add_row ['c', 3] end table.render.should == <<-EOF.deindent +---+---+ | a | 1 | | b | 2 | | c | 3 | +---+---+ EOF end it "should allows a hash of options for creation" do headings = ['Char', 'Num'] rows = [['a', 1], ['b', 2], ['c', 3]] Terminal::Table.new(:rows => rows, :headings => headings).render.should == <<-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 == <<-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 == <<-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 == <<-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 == t end # it "should be equal with two empty tables" do # table_one = Table.new # table_two = Table.new # # table_one.should == table_two # table_two.should == 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 == table_two table_two.should_not == table_one end it "should not be equal with different rows" do table_one = Table.new table_two = Table.new table_one.should_not == table_two table_two.should_not == 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 table_two.stub!(:rows).and_return([]) table_one.should_not == 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 table_two.stub!(:rows).and_return([]) table_one.should_not == 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 == <<-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 == <<-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 == <<-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 == <<-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 == <<-EOF.deindent +-------+---+---+ | 12345 | 2 | 3 | | 123456789 | 4 | +-------+---+---+ 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 == <<-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 == <<-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 == <<-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.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 == <<-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.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 == <<-EOF name values a 1 2 3 b 4 5 6 c 7 8 9 EOF end end end terminal-table-1.4.5/spec/import_spec.rb0000644000004100000410000000043312252257265020264 0ustar www-datawww-data require File.dirname(__FILE__) + '/spec_helper' require "terminal-table/import" describe Object do describe "#table" do it "should allow creation of a terminal table" do table(['foo', 'bar'], ['a', 'b'], [1, 2]).should be_instance_of(Terminal::Table) end end end terminal-table-1.4.5/README.rdoc0000644000004100000410000001376712252257265016305 0ustar www-datawww-data= Terminal Table == Description Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby. == Installation Install http://gemcutter.org and execute: $ 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 specifify 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 To change the default style options: Terminal::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 == License (The MIT License) 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, an d/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. terminal-table-1.4.5/lib/0000755000004100000410000000000012252257265015227 5ustar www-datawww-dataterminal-table-1.4.5/lib/terminal-table.rb0000644000004100000410000000240212252257265020452 0ustar www-datawww-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. #++ $:.unshift File.dirname(__FILE__) %w(version core_ext table cell row separator style table_helper).each do |file| require "terminal-table/#{file}" endterminal-table-1.4.5/lib/terminal-table/0000755000004100000410000000000012252257265020127 5ustar www-datawww-dataterminal-table-1.4.5/lib/terminal-table/core_ext.rb0000644000004100000410000000022212252257265022260 0ustar www-datawww-data class String def align position, length self.__send__ position, length end alias_method :left, :ljust alias_method :right, :rjust endterminal-table-1.4.5/lib/terminal-table/style.rb0000644000004100000410000000304612252257265021617 0ustar www-datawww-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 => "+", :padding_left => 1, :padding_right => 1, :width => nil } attr_accessor :border_x attr_accessor :border_y attr_accessor :border_i attr_accessor :padding_left attr_accessor :padding_right attr_accessor :width 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 end end endterminal-table-1.4.5/lib/terminal-table/table_helper.rb0000644000004100000410000000031412252257265023100 0ustar www-datawww-datamodule 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-1.4.5/lib/terminal-table/table.rb0000644000004100000410000001173012252257265021545 0ustar www-datawww-data module Terminal class Table attr_reader :title attr_reader :headings ## # Generates a ASCII table with the given _options_. def initialize options = {}, &block @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 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] 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 recalc_column_widths row 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| cell = row[n] 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 width = @column_widths[n] || 0 width + additional_column_widths[n].to_i 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.cells.size }.max end ## # Set the headings def headings= array @headings = Row.new(self, array) recalc_column_widths @headings end ## # Render the table. def render separator = Separator.new(self) buffer = [separator] unless @title.nil? buffer << Row.new(self, [title_cell_options]) buffer << separator end unless @headings.cells.empty? buffer << @headings buffer << separator end buffer += @rows buffer << separator buffer.map { |r| r.render }.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 recalc_column_widths Row.new(self, [title_cell_options]) 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 additional_column_widths return [] if style.width.nil? spacing = style.width - columns_width if spacing < 0 raise "Table width exceeds wanted width of #{wanted} characters." else per_col = spacing / number_of_columns arr = (1...number_of_columns).to_a.map { |i| per_col } other_cols = arr.inject(0) { |s, i| s + i } arr << spacing - other_cols arr end end def recalc_column_widths row return if row.is_a? Separator i = 0 row.cells.each do |cell| colspan = cell.colspan cell_value = cell.value_for_column_width_recalc colspan.downto(1) do |j| cell_length = cell_value.to_s.length if colspan > 1 spacing_length = cell_spacing * (colspan - 1) length_in_columns = (cell_length - spacing_length) cell_length = (length_in_columns.to_f / colspan).ceil end if @column_widths[i].to_i < cell_length @column_widths[i] = cell_length end i = i + 1 end end 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 end end terminal-table-1.4.5/lib/terminal-table/separator.rb0000644000004100000410000000054412252257265022457 0ustar www-datawww-datamodule 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 endterminal-table-1.4.5/lib/terminal-table/import.rb0000644000004100000410000000010012252257265021755 0ustar www-datawww-data require 'terminal-table' include Terminal::Table::TableHelper terminal-table-1.4.5/lib/terminal-table/version.rb0000644000004100000410000000007712252257265022145 0ustar www-datawww-data module Terminal class Table VERSION = '1.4.5' end end terminal-table-1.4.5/lib/terminal-table/row.rb0000644000004100000410000000176112252257265021270 0ustar www-datawww-datamodule 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 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 end end endterminal-table-1.4.5/lib/terminal-table/cell.rb0000644000004100000410000000442512252257265021400 0ustar www-datawww-data module Terminal class Table class Cell ## # Cell width. attr_reader :width ## # 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 || :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 lines @value.to_s.split(/\n/) end ## # Render the cell. def render(line = 0) left = " " * @table.style.padding_left right = " " * @table.style.padding_right render_width = lines[line].to_s.size - escape(lines[line]).size + width "#{left}#{lines[line]}#{right}".align(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| s.size } 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-1.4.5/metadata.yml0000644000004100000410000000367312252257265016775 0ustar www-datawww-data--- !ruby/object:Gem::Specification rubygems_version: 0.9.4 specification_version: 1 name: terminal-table version: !ruby/object:Gem::Version version: 1.4.5 date: 2012-03-15 00:00:00 +00:00 summary: Simple, feature rich ascii table generation library require_paths: - lib email: tj@vision-media.ca homepage: http://github.com/visionmedia/terminal-table rubyforge_project: terminal-table description: Simple, feature rich ascii table generation library autorequire: default_executable: bindir: bin has_rdoc: false required_ruby_version: !ruby/object:Gem::Version::Requirement requirements: - - ">" - !ruby/object:Gem::Version version: 0.0.0 version: platform: ruby signing_key: cert_chain: post_install_message: authors: - TJ Holowaychuk - Scott J. Goldman files: - History.rdoc - Manifest - README.rdoc - Rakefile - Todo.rdoc - examples/examples.rb - lib/terminal-table.rb - lib/terminal-table/cell.rb - lib/terminal-table/core_ext.rb - lib/terminal-table/import.rb - lib/terminal-table/table.rb - lib/terminal-table/table_helper.rb - lib/terminal-table/version.rb - lib/terminal-table/row.rb - lib/terminal-table/separator.rb - lib/terminal-table/style.rb - spec/cell_spec.rb - spec/core_ext_spec.rb - spec/import_spec.rb - spec/spec.opts - spec/spec_helper.rb - spec/table_spec.rb - tasks/docs.rake - tasks/gemspec.rake - tasks/spec.rake - terminal-table.gemspec test_files: [] rdoc_options: - --line-numbers - --inline-source - --title - Terminal-table - --main - README.rdoc extra_rdoc_files: - README.rdoc - lib/terminal-table.rb - lib/terminal-table/cell.rb - lib/terminal-table/core_ext.rb - lib/terminal-table/import.rb - lib/terminal-table/table.rb - lib/terminal-table/version.rb - lib/terminal-table/row.rb - lib/terminal-table/separator.rb - lib/terminal-table/style.rb - lib/terminal-table/table_helper.rb - tasks/docs.rake - tasks/gemspec.rake - tasks/spec.rake executables: [] extensions: [] requirements: [] dependencies: [] terminal-table-1.4.5/Manifest0000644000004100000410000000075612252257265016162 0ustar www-datawww-dataHistory.rdoc Manifest README.rdoc Rakefile Todo.rdoc examples/examples.rb lib/terminal-table.rb lib/terminal-table/cell.rb lib/terminal-table/core_ext.rb lib/terminal-table/heading.rb lib/terminal-table/import.rb lib/terminal-table/table.rb lib/terminal-table/table_helper.rb lib/terminal-table/version.rb spec/cell_spec.rb spec/core_ext_spec.rb spec/import_spec.rb spec/spec.opts spec/spec_helper.rb spec/table_spec.rb tasks/docs.rake tasks/gemspec.rake tasks/spec.rake terminal-table.gemspec terminal-table-1.4.5/tasks/0000755000004100000410000000000012252257265015606 5ustar www-datawww-dataterminal-table-1.4.5/tasks/docs.rake0000644000004100000410000000043412252257265017403 0ustar www-datawww-data namespace :docs do desc 'Remove rdoc products' task :remove => [:clobber_docs] desc 'Build docs, and open in browser for viewing (specify BROWSER)' task :open => [:docs] do browser = ENV["BROWSER"] || "safari" sh "open -a #{browser} doc/index.html" end endterminal-table-1.4.5/tasks/spec.rake0000644000004100000410000000124112252257265017402 0ustar www-datawww-data require 'spec/rake/spectask' desc "Run all specifications" Spec::Rake::SpecTask.new(:spec) do |t| t.libs << "lib" t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"] end namespace :spec do desc "Run all specifications verbosely" Spec::Rake::SpecTask.new(:verbose) do |t| t.libs << "lib" t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"] end desc "Run specific specification verbosely (specify SPEC)" Spec::Rake::SpecTask.new(:select) do |t| t.libs << "lib" t.spec_files = [ENV["SPEC"]] t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"] end endterminal-table-1.4.5/tasks/gemspec.rake0000644000004100000410000000007412252257265020076 0ustar www-datawww-data desc 'Build gemspec file' task :gemspec => [:build_gemspec]