Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove white space #66

Merged
merged 1 commit into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions lib/terminal-table/cell.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

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
Expand All @@ -24,15 +23,15 @@ def initialize options = nil
@index = options.fetch :index
@table = options.fetch :table
end

def alignment?
[email protected]?
end

def alignment
@alignment || @table.style.alignment || :left
end

def alignment=(val)
supported = %w(left center right)
if supported.include?(val.to_s)
Expand All @@ -41,37 +40,37 @@ def alignment=(val)
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
render_width = lines[line].to_s.size - escape(lines[line]).size + 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| 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|
Expand All @@ -81,7 +80,7 @@ def width
end

##
# removes all ANSI escape sequences (e.g. color)
# 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]/, '').
Expand Down
6 changes: 3 additions & 3 deletions lib/terminal-table/separator.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module Terminal
class Table
class Separator < Row

def render
arr_x = ([email protected]_of_columns).to_a.map do |i|
@table.style.border_x * (@table.column_width(i) + @table.cell_padding)
@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
end
1 change: 0 additions & 1 deletion lib/terminal-table/style.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module Terminal
class Table
# A Style object holds all the formatting information for a Table object
Expand Down
1 change: 0 additions & 1 deletion lib/terminal-table/table.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module Terminal
class Table

Expand Down
16 changes: 8 additions & 8 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,11 @@ module Terminal

@table.render.should == <<-EOF.strip
---------------
name values
name values
---------------
a 1 2 3
b 4 5 6
c 7 8 9
a 1 2 3
b 4 5 6
c 7 8 9
---------------
EOF
end
Expand All @@ -564,11 +564,11 @@ module Terminal

@table.render.should == <<-EOF

name values
name values

a 1 2 3
b 4 5 6
c 7 8 9
a 1 2 3
b 4 5 6
c 7 8 9
EOF
end

Expand Down