Skip to content

Commit

Permalink
refactored away the override of to_s, added suppression of output to …
Browse files Browse the repository at this point in the history
…support --quiet flags and working on the examples for the wiki
  • Loading branch information
wbailey committed Jan 16, 2012
1 parent bfcbe2b commit 6935205
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -2,7 +2,7 @@

This gem provides an simple way to add RSpec like formatting of the output of your ruby scripts. It
eliminates the need to litter your code with *puts* statements instead providing a cleaner, more
ruby like way of reporting progress to the user throught the command line interface.
ruby like way of reporting progress to the user through the command line interface.

With the release of Version 2.0, it is now possible to produce tables with and without borders. See
the section on *Tables* for more examples.
Expand Down Expand Up @@ -290,7 +290,7 @@ This produces the very simple output with 2 rows and 3 columns of data:
```

Notice how the properties of the columns for the second and third rows have been inherited from the
first like in HTML. This makes it a lot easier to write in freeform. What if you have data to
first like in HTML. This makes it a lot easier to write in free-form. What if you have data to
iterate over and have text that is wider than the column width you have selected? Not a problem as
the following example demonstrates all of the combinations of the various options:

Expand Down
40 changes: 40 additions & 0 deletions examples/quiet.rb
@@ -0,0 +1,40 @@
require 'command_line_reporter'
require 'ostruct'
require 'optparse'

class Example
include CommandLineReporter

def initialize
self.formatter = 'progress'
end

def run(options = {})
x = 0

suppress_output if options.quiet

report do
10.times do
x += 1
sleep 0.1
formatter.progress
end
end

restore_output if options.quiet
end
end


options = OpenStruct.new({:quiet => false})

OptionParser.new do |opts|
opts.banner = "Usage: ruby -I lib example/quiet.rb [-q|--quiet]"

opts.on('-q', '--quiet', 'do not print any output') do
options.quiet = true
end
end.parse!

Example.new.run(options)
16 changes: 15 additions & 1 deletion examples/simple.rb
Expand Up @@ -15,7 +15,21 @@ def run
end

footer(:title => 'Values', :width => 15)
%w(x y z).each {|v| aligned("#{v}: #{eval v}")}

table do
row do
column('x', :width => 5, :align => 'right')
column(x.to_s, :width => 10)
end
row do
column('y')
column(y.to_s)
end
row do
column('z')
column(z.to_s)
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion examples/table.rb
Expand Up @@ -24,7 +24,7 @@ def run
vertical_spacing(2)
end

header(:title => 'A simple example of how column properties are inhereted from the first row')
header(:title => 'A simple example of how column properties are inherited from the first row')

table(:border => true) do
row do
Expand Down
11 changes: 10 additions & 1 deletion lib/command_line_reporter.rb
@@ -1,3 +1,4 @@
require 'stringio'
require 'table'

Dir[File.join(File.dirname(__FILE__), '*_formatter.rb')].each {|r| require r}
Expand All @@ -12,6 +13,14 @@ module CommandLineReporter
:align => 'left',
}

def suppress_output
$stdout = StringIO.new
end

def restore_output
$stdout = STDOUT
end

def formatter=(type = 'nested')
name = type.capitalize + 'Formatter'
klass = %W{CommandLineReporter #{name}}.inject(Kernel) {|s,c| s.const_get(c)}
Expand Down Expand Up @@ -85,7 +94,7 @@ def aligned(text, options = {})
def table(options = {})
@table = Table.new(options)
yield
@table.to_s
@table.output
end

def row(options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/row.rb
Expand Up @@ -17,7 +17,7 @@ def separator
@sep ||= '+' + self.columns.map {|c| '-' * (c.width + 2)}.join('+') + '+'
end

def to_s
def output
screen_count.times do |sr|
line = (self.border) ? '| ' : ''
self.columns.size.times do |mc|
Expand Down
4 changes: 2 additions & 2 deletions lib/table.rb
Expand Up @@ -32,12 +32,12 @@ def add(row)
self.rows << row
end

def to_s
def output
return if self.rows.size == 0 # we got here with nothing to print to the screen

puts self.rows[0].separator if self.border
self.rows.each do |row|
row.to_s
row.output
puts self.rows[0].separator if self.border
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
@@ -1,3 +1,3 @@
module CommandLineReporter
VERSION = '2.0'
VERSION = '2.1'
end
22 changes: 11 additions & 11 deletions spec/row_spec.rb
Expand Up @@ -18,7 +18,7 @@
end
end

describe '#to_s' do
describe '#output' do
before :each do
@cols = [
Column.new('asdf'),
Expand All @@ -41,46 +41,46 @@

context 'no border' do
context 'no wrap' do
it 'prints a single column' do
it 'outputs a single column' do
subject.add(@cols[0])
subject.should_receive(:puts).with(/^asdf#{@six_pieces}/)
subject.to_s
subject.output
end
it 'prints three columns' do
it 'outputs three columns' do
subject.add(@cols[0])
subject.add(@cols[1])
subject.add(@cols[2])
subject.should_receive(:puts).with(/^asdf#{@six_spaces}#{@one_space}#{@three_spaces}qwer#{@three_spaces}#{@one_space}#{@six_spaces}zxcv $/)
subject.to_s
subject.output
end
end

context 'with wrapping' do
it 'prints a single column' do
it 'outputs a single column' do
subject.add(@cols[3])
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}$/)
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}$/)
subject.should_receive(:puts).with(/^#{@five_xs}#{@six_spaces}$/)
subject.to_s
subject.output
end

it 'prints multiple columns of the same size' do
it 'outputs multiple columns of the same size' do
subject.add(@cols[3])
subject.add(@cols[4])
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}#{@ten_xs}#{@one_space}$/)
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}#{@ten_xs}#{@one_space}$/)
subject.should_receive(:puts).with(/^#{@five_xs}#{@nine_spaces}#{@five_xs}#{@three_spaces}$/)
subject.to_s
subject.output
end

it 'prints multiple columns with different size' do
it 'outputs multiple columns with different sizes' do
subject.add(@cols[5])
subject.add(@cols[3])
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}#{@ten_xs}#{@one_space}$/)
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}#{@ten_xs}#{@one_space}$/)
subject.should_receive(:puts).with(/^#{@ten_xs}#{@one_space}#{@five_xs}#{@six_spaces}$/)
subject.should_receive(:puts).with(/^#{@five_xs} {5,17}$/)
subject.to_s
subject.output
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/table_spec.rb
Expand Up @@ -38,7 +38,7 @@
row.add(cols[1])
t.add(row)
output = capture_stdout {
t.to_s
t.output
}
end
end
Expand Down

0 comments on commit 6935205

Please sign in to comment.