Permalink
Browse files

Merge pull request #13 from rrrene/master

Add arbitrary dimensions (and clean up code)
  • Loading branch information...
2 parents b6274cf + 83a1f90 commit 87c4178134810832095ba839419d32350bcaae64 @scottjg scottjg committed Oct 17, 2011
View
@@ -13,6 +13,7 @@ Simple, feature rich ASCII table generator.
* Simple
* Optional headings
* Alignment of columns, headings, or cells
+* Supports arbitrary width
* Supports column span
* Supports multiline cell content
* Supports colorized cell content
View
@@ -6,7 +6,6 @@
== Minor:
* Programmatically add separator rows
-* Arbitrary dimensions
* Add multi-column sorting
* Change; pre-create Cell and Heading objects to clean up Table a bit
View
@@ -14,6 +14,17 @@
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.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 )
View
@@ -22,6 +22,6 @@
#++
$:.unshift File.dirname(__FILE__)
-%w(version core_ext table cell heading row table_helper).each do |file|
+%w(version core_ext table cell row table_helper).each do |file|
require "terminal-table/#{file}"
end
View
@@ -14,11 +14,6 @@ class Cell
attr_reader :value
##
- # Cell alignment.
-
- attr_accessor :alignment
-
- ##
# Column span.
attr_reader :colspan
@@ -29,48 +24,63 @@ class Cell
def initialize options = nil
@value, options = options, {} unless Hash === options
@value = options.fetch :value, value
- @alignment = options.fetch :alignment, :left
+ @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
- ##
- # Render the cell.
+ def alignment?
+ !@alignment.nil?
+ end
- def render(line = 0)
- " #{lines[line]} ".align alignment, width + 2
+ 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
- alias :to_s :render
def lines
@value.to_s.split(/\n/)
end
+ ##
+ # Render the cell.
+
+ def render(line = 0)
+ " #{lines[line]} ".align(alignment, width + 2)
+ 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
str = lines.sort_by { |s| s.size }.last.to_s
str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
str.gsub(/[\x03|\x1a]/, '')
end
+ ##
+ # Returns the width of this cell
+
def width
- padding = (colspan - 1) * 3
+ padding = (colspan - 1) * (2 + @table.class::Y.length)
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
w + @table.column_width(@index + counter - 1)
end
inner_width + padding
end
-
- ##
- # Cell length.
-
- def length
- value.to_s.size + 2
- end
end
end
end
@@ -1,28 +1,8 @@
class String
def align position, length
- send position, length
+ self.__send__ position, length
end
alias_method :left, :ljust
alias_method :right, :rjust
-end
-
-module Enumerable
- def map_with_index &block
- vals = []
- each_with_index { |v, i| vals << yield(v, i) }
- vals
- end
- alias :collect_with_index :map_with_index
-end
-
-class Object
- def yield_or_eval &block
- return unless block
- if block.arity > 0
- yield self
- else
- self.instance_eval(&block)
- end
- end
end
@@ -1,10 +0,0 @@
-
-module Terminal
- class Table
- class Heading < Cell
- def initialize options
- super
- end
- end
- end
-end
View
@@ -40,6 +40,10 @@ def each &block
cells.each &block unless separator?
end
+ def height
+ cells.map { |c| c.lines.count }.max
+ end
+
def method_missing m, *args, &block
if cells.respond_to?(m)
cells.__send__(m, *args, &block)
@@ -48,17 +52,13 @@ def method_missing m, *args, &block
end
end
- def height
- cells.map { |c| c.lines.count }.max
- end
-
def render
y = Terminal::Table::Y
if separator?
@table.separator
else
(0...height).to_a.map do |line|
- y + self.map_with_index do |cell, i|
+ y + cells.map do |cell|
cell.render(line)
end.join(y) + y
end.join("\n")
Oops, something went wrong.

0 comments on commit 87c4178

Please sign in to comment.