Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Oct 17, 2011
1 parent d814e31 commit 42d4082
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 59 deletions.
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Todo.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/terminal-table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 9 additions & 11 deletions lib/terminal-table/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def initialize options = nil
@table = options.fetch :table
end

def lines
@value.to_s.split(/\n/)
end

##
# Render the cell.

Expand All @@ -44,33 +48,27 @@ def render(line = 0)
end
alias :to_s :render

def lines
@value.to_s.split(/\n/)
end

##
# 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
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
10 changes: 0 additions & 10 deletions lib/terminal-table/heading.rb

This file was deleted.

8 changes: 4 additions & 4 deletions lib/terminal-table/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -48,10 +52,6 @@ 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?
Expand Down
32 changes: 1 addition & 31 deletions lib/terminal-table/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def headings= array
# Render the table.

def render
@seperator = nil
buffer = [separator]
unless @headings.empty?
buffer << @headings.render
Expand Down Expand Up @@ -195,37 +196,6 @@ def recalc_column_widths row
end
end
end

def row_with_hash row
# this method duplicates the multi-column columns in each column they are in
index = 0
row.inject [] do |columns, column|
if column.is_a?(Hash) && column[:colspan] && column[:colspan] > 1
column[:start_index] = index
column[:colspan].times do
columns << column
index += 1
end
else
columns << column
index += 1
end
columns
end
end

def row_to_index row, index
new_index = -1
0.upto(index) do |i|
column = row[i]
if column.is_a?(Hash) && column[:colspan] && column[:colspan] > 1 && index != i
new_index = new_index + column[:colspan]
else
new_index += 1
end
end
return new_index
end

##
# Return headings combined with rows.
Expand Down
1 change: 0 additions & 1 deletion spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ module Terminal
@table << ['b', 2]
@table << ['c', 3]
@table.width = 21
@table.additional_column_widths == 21
@table.render.should == <<-EOF.deindent
+---------+---------+
| Char | Num |
Expand Down

0 comments on commit 42d4082

Please sign in to comment.