Permalink
Browse files

Don't output 2 trailing spaces when printing the last column

  • Loading branch information...
1 parent d72999a commit 6ccb1fec028ab001a63a2c06cd94b4b41f7e1c8b @sferik sferik committed Apr 29, 2012
Showing with 26 additions and 4 deletions.
  1. +13 −3 lib/thor/shell/basic.rb
  2. +13 −1 spec/shell/basic_spec.rb
@@ -125,10 +125,15 @@ def print_table(table, options={})
maximas = []
- start.upto(colcount - 2) do |i|
+ start.upto(colcount - 1) do |i|
maxima = table.map {|row| row[i] ? row[i].to_s.size : 0 }.max
maximas << maxima
- formats << "%-#{maxima + 2}s"
+ if i == colcount -1
+ # Don't output 2 trailing spaces when printing the last column
+ formats << "%-s"
+ else
+ formats << "%-#{maxima + 2}s"
+ end
end
formats[0] = formats[0].insert(0, " " * indent)
@@ -141,7 +146,12 @@ def print_table(table, options={})
maxima = maximas[i]
if column.is_a?(Numeric)
- f = "%#{maxima}s "
+ if i == row.size - 1
+ # Don't output 2 trailing spaces when printing the last column
+ f = "%#{maxima}s"
+ else
+ f = "%#{maxima}s "
+ end
else
f = formats[i]
end
@@ -173,7 +173,7 @@ def shell
TABLE
end
- it "prints a table with small numbers, and right-aligns them" do
+ it "prints a table with small numbers and right-aligns them" do
table = [
["Name", "Number", "Color"],
["Erik", 1, "green"]
@@ -185,6 +185,18 @@ def shell
TABLE
end
+ it "doesn't output extra spaces for right-aligned columns in the last column" do
+ table = [
+ ["Name", "Number"],
+ ["Erik", 1]
+ ]
+ content = capture(:stdout){ shell.print_table(table) }
+ content.should == <<-TABLE
+Name Number
+Erik 1
+TABLE
+ end
+
it "prints a table with big numbers" do
table = [
["Name", "Number", "Color"],

0 comments on commit 6ccb1fe

Please sign in to comment.