Skip to content

Commit

Permalink
Add colwidth option to print_table
Browse files Browse the repository at this point in the history
  • Loading branch information
indirect committed Jun 22, 2010
1 parent e73958d commit f65dc03
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/thor/shell/basic.rb
Expand Up @@ -81,16 +81,20 @@ def no?(statement, color=nil)
# Array[Array[String, String, ...]]
#
# ==== Options
# ident<Integer>:: Ident the first column by ident value.
# ident<Integer>:: Indent the first column by ident value.
# colwidth<Integer>:: Force the first column to colwidth spaces wide.
#
def print_table(table, options={})
return if table.empty?

formats, ident = [], options[:ident].to_i
formats, ident, colwidth = [], options[:ident].to_i, options[:colwidth]
options[:truncate] = terminal_width if options[:truncate] == true

0.upto(table.first.length - 2) do |i|
maxima = table.max{ |a,b| a[i].size <=> b[i].size }[i].size
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
formats << "%-#{maxima + 2}s"
end

Expand Down
9 changes: 9 additions & 0 deletions spec/shell/basic_spec.rb
Expand Up @@ -129,6 +129,15 @@ def shell
abc #123 firs...
#0 empty
xyz #786 last...
TABLE
end

it "honors the colwidth option" do
content = capture(:stdout){ shell.print_table(@table, :colwidth => 10)}
content.must == <<-TABLE
abc #123 first three
#0 empty
xyz #786 last three
TABLE
end
end
Expand Down

0 comments on commit f65dc03

Please sign in to comment.