Skip to content

Commit

Permalink
Allow printed tables to have implicit columns.
Browse files Browse the repository at this point in the history
Closes rails#185.
  • Loading branch information
eventualbuddha committed Oct 19, 2011
1 parent aa739da commit d29b75e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/thor/shell/basic.rb
Expand Up @@ -112,8 +112,10 @@ def print_table(table, options={})
formats << "%-#{colwidth + 2}s" if colwidth
start = colwidth ? 1 : 0

start.upto(table.first.length - 2) do |i|
maxima ||= table.max{|a,b| a[i].size <=> b[i].size }[i].size
colcount = table.max{|a,b| a.size <=> b.size }.size

start.upto(colcount - 2) do |i|
maxima ||= table.map {|row| row[i] ? row[i].size : 0 }.max
formats << "%-#{maxima + 2}s"
end

Expand Down
10 changes: 10 additions & 0 deletions spec/shell/basic_spec.rb
Expand Up @@ -147,6 +147,16 @@ def shell
abc #123 first three
#0 empty
xyz #786 last three
TABLE
end

it "prints tables with implicit columns" do
2.times { @table.first.pop }
content = capture(:stdout){ shell.print_table(@table) }
content.should == <<-TABLE
abc
#0 empty
xyz #786 last three
TABLE
end
end
Expand Down

0 comments on commit d29b75e

Please sign in to comment.