Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Style class to hold formatting information

  • Loading branch information...
commit 29ce657b60488129724f8114971651fc6a85cb9f 1 parent 9f0da8c
@rrrene rrrene authored
View
1  examples/examples.rb
@@ -6,6 +6,7 @@
puts
t = table ['a', 'b']
+t.style = {:padding_left => 2, :width => 80}
t << [1, 2]
t << [3, 4]
t << :separator
View
2  lib/terminal-table.rb
@@ -22,6 +22,6 @@
#++
$:.unshift File.dirname(__FILE__)
-%w(version core_ext table cell row table_helper).each do |file|
+%w(version core_ext table cell row style table_helper).each do |file|
require "terminal-table/#{file}"
end
View
6 lib/terminal-table/cell.rb
@@ -56,9 +56,9 @@ def lines
# Render the cell.
def render(line = 0)
- left = " " * table.cell_padding_left
- right = " " * table.cell_padding_right
- "#{left}#{lines[line]}#{right}".align(alignment, width + table.cell_padding)
+ left = " " * @table.style.padding_left
+ right = " " * @table.style.padding_right
+ "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding)
end
alias :to_s :render
View
2  lib/terminal-table/row.rb
@@ -53,7 +53,7 @@ def method_missing m, *args, &block
end
def render
- y = Terminal::Table::Y
+ y = @table.style.border_y
if separator?
@table.separator
else
View
42 lib/terminal-table/style.rb
@@ -0,0 +1,42 @@
+
+module Terminal
+ class Table
+ class Style
+ @@defaults = {
+ :border_x => "-", :border_y => "|", :border_i => "+",
+ :padding_left => 1, :padding_right => 1
+ }
+ ##
+ # Style options.
+
+ attr_accessor :border_x
+ attr_accessor :border_y
+ attr_accessor :border_i
+
+ attr_accessor :padding_left
+ attr_accessor :padding_right
+
+ attr_accessor :width
+
+ ##
+ # Initialize with _options_.
+
+ 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
+end
View
46 lib/terminal-table/table.rb
@@ -8,24 +8,21 @@ class Table
Error = Class.new StandardError
- ##
- # Table characters, x axis, y axis, and intersection.
-
- X, Y, I = '-', '|', '+'
-
attr_reader :title
attr_reader :headings
attr_accessor :width
+ attr_reader :style
+
##
# Generates a ASCII table with the given _options_.
def initialize options = {}, &block
@column_widths = []
+ self.style = options.fetch :style, {}
+ self.title = options.fetch :title, nil
self.headings = options.fetch :headings, []
- @rows = []
- @width = options[:width]
- options.fetch(:rows, []).each { |row| add_row row }
+ self.rows = options.fetch :rows, []
yield_or_eval(&block) if block
end
@@ -57,19 +54,11 @@ def add_separator
end
def cell_spacing
- cell_padding + Y.length
+ cell_padding + style.border_y.length
end
def cell_padding
- cell_padding_left + cell_padding_right
- end
-
- def cell_padding_left
- 1
- end
-
- def cell_padding_right
- 1
+ style.padding_left + style.padding_right
end
##
@@ -155,6 +144,7 @@ def rows
end
def rows= array
+ @rows = []
array.each { |arr| self << arr }
end
@@ -163,12 +153,20 @@ def rows= array
def separator
@separator ||= begin
- I + (0...number_of_columns).to_a.map do |i|
- X * (column_width(i) + cell_padding)
- end.join(I) + I
+ style.border_i + (0...number_of_columns).to_a.map do |i|
+ style.border_x * (column_width(i) + cell_padding)
+ end.join(style.border_i) + style.border_i
end
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])
@@ -187,12 +185,12 @@ def == other
private
def columns_width
- @column_widths.inject(0) { |s, i| s + i + cell_spacing } + Y.length
+ @column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
end
def additional_column_widths
- return [] if @width.nil?
- spacing = @width - columns_width
+ return [] if style.width.nil?
+ spacing = style.width - columns_width
if spacing < 0
raise "Table width exceeds wanted width of #{wanted} characters."
else
View
8 spec/cell_spec.rb
@@ -34,5 +34,13 @@ class String; include Term::ANSIColor; end
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
View
38 spec/table_spec.rb
@@ -115,12 +115,30 @@ module Terminal
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.width = 21
+ @table.style.width = 21
@table.render.should == <<-EOF.deindent
+---------+---------+
| Char | Num |
@@ -466,10 +484,8 @@ module Terminal
end
it "should render a table with only X cell borders" do
- Terminal::Table::X = "-"
- Terminal::Table::Y = ""
- Terminal::Table::I = ""
-
+ @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]]
@@ -483,16 +499,10 @@ module Terminal
c 7 8 9
---------------
EOF
-
- Terminal::Table::X = "-"
- Terminal::Table::Y = "|"
- Terminal::Table::I = "+"
end
it "should render a table without cell borders" do
- Terminal::Table::X = ""
- Terminal::Table::Y = ""
- Terminal::Table::I = ""
+ @table.style = {:border_x => "", :border_y => "", :border_i => ""}
@table.headings = ['name', { :value => 'values', :alignment => :right, :colspan => 3}]
@table.headings = ['name', { :value => 'values', :colspan => 3}]
@@ -506,10 +516,6 @@ module Terminal
b 4 5 6
c 7 8 9
EOF
-
- Terminal::Table::X = "-"
- Terminal::Table::Y = "|"
- Terminal::Table::I = "+"
end
end
end

0 comments on commit 29ce657

Please sign in to comment.
Something went wrong with that request. Please try again.