Skip to content
Wes Bailey edited this page Jul 21, 2017 · 8 revisions

The first thing you want to do is be able to run the examples. Clone the repository:

$ git clone git@github.com:wbailey/command_line_reporter.git
...
$ cd command_line_reporter
$ ruby -I lib example/simple.rb
...

examples/nested.rb

require 'command_line_reporter'

class Example
  include CommandLineReporter

  def initialize
    self.formatter = 'nested'
    self.formatter.complete_string = 'done'
  end

  def run
    x,y,z = 0,0,0

    report(message: 'calculating first expression') do
      x = 2 + 2
      sleep 1

      2.times do
        report(message: 'calculating second expression') do
          y = 10 - x
          sleep 1

          10.times do |i|
            report(message: 'pixelizing', type: 'inline', complete: "#{i*10+10}%") do
              z = x + y
              sleep 1
            end
          end
        end
      end
    end

    horizontal_rule(width: 20)
    %w(x y z).each {|v| aligned "#{v}: #{eval v}"}
  end
end

Example.new.run
calculating first expression
  calculating second expression
    pixelizing...10%
    pixelizing...20%
    pixelizing...30%
    pixelizing...40%
    pixelizing...50%
    pixelizing...60%
    pixelizing...70%
    pixelizing...80%
    pixelizing...90%
    pixelizing...100%
  done
  calculating second expression
    pixelizing...10%
    pixelizing...20%
    pixelizing...30%
    pixelizing...40%
    pixelizing...50%
    pixelizing...60%
    pixelizing...70%
    pixelizing...80%
    pixelizing...90%
    pixelizing...100%
  done
done
--------------------
x: 4
y: 6
z: 10

examples/progress.rb

require 'command_line_reporter'

class Example
  include CommandLineReporter

  NYAN_CHARS = "****[;::;<](^-^)"

  def initialize
    self.formatter = 'progress'
  end

  def run
    x = 0

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

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

    y = 0

    report do
      10.times do
        y += 1
        sleep 0.1
        progre#{y*10}%")
      end
    end

    report do
      3.times do
        progress(\\")
        sleep 0.1
        progress(/")
        sleep 0.1
        progress(-")
        sleep 0.1
      end
    end

    report(color: 'red') do
      100.times do
        progress(erase_chars + NYAN_CHARS)
        sleep 0.1
      end
    end

    aligned "x: #{x}"
    aligned "y: #{y}"
  end

  def erase_chars
    "\10" * NYAN_CHARS.size + " "
  end
end

Example.new.run
..............................................................................................................
100%
-
                                                                                                    ****[;::;<](^-^)
x: 110
y: 10

examples/quiet.rb

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
        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)
..........

examples/simple.rb

require 'command_line_reporter'

include CommandLineReporter

class Example
  def initialize
    self.formatter = 'progress'
  end

  def run
    report do
      sum = 0
      10.times do
        sum += 10
        progress
      end
      vertical_spacing
      aligned("Sum: #{sum}")
    end
  end
end

Example.new.run
..........
Sum: 100

examples/table.rb

require 'command_line_reporter'

class Example
  include CommandLineReporter

  def run
    header(title: 'TABLE EXAMPLES - Borders, Wrapping, Alignment and Padding', align: 'center', width: 70)

    2.times do |j|
      header title: "Table #{j}", align: 'center', width: 65

      table border: j % 2 == 0  do
        3.times do
          row do
            i = 0
            3.times do
              i += 10
              column 'x' * (0 + rand(50)), align: %w[left right center][rand(3)], width: i, padding: rand(5) 
            end
          end
        end
      end

      vertical_spacing 2
    end

    header title: 'An example of a table with a header row.  The color and border properties are not inherited'

    table border: true do
      row header: true, color: 'red'  do
        column 'MY NAME IS REALLY LONG AND WILL WRAP AND HOPE', width: 20, align: 'center', color: 'blue'
        column 'ADDRESS', width: 30, padding: 5
        column 'CITY', width: 15
      end
      row color: 'green', bold: true do
        column 'Ceaser'
        column '1 Appian Way'
        column 'Rome'
      end
      row do
        column 'Richard Feynman'
        column '1 Golden Gate'
        column 'Quantum Field'
      end
    end

    vertical_spacing 2
    header title: 'The same table with the properties inherited from the first row'

    table border: true do
      row color: 'red' do
        column 'MY NAME IS REALLY LONG AND WILL WRAP AND HOPE', width: 20, align: 'center', color: 'blue'
        column 'ADDRESS', width: 30, padding: 5
        column 'CITY', width: 15
      end
      row color: 'green', bold: true do
        column 'Ceaser'
        column '1 Appian Way'
        column 'Rome'
      end
      row do
        column 'Richard Feynman'
        column '1 Golden Gate'
        column 'Quantum Field'
      end
    end
  end
end

Example.new.run
      TABLE EXAMPLES - Borders, Wrapping, Alignment and Padding

                             Table 0

+------------+----------------------+--------------------------------+
|   xxxxxx   |    xxxxxxxxxxxxxxxxx |                   xxxxxxxxxx   | 
|   xxxxxx   |                      |                                | 
|   xxxxxx   |                      |                                | 
|   xxxxxx   |                      |                                | 
|   xxxxxx   |                      |                                | 
|   xxxxxx   |                      |                                | 
|   xxxxxx   |                      |                                | 
|        x   |                      |                                | 
+------------+----------------------+--------------------------------+
|   xxxxxx   |            xxxxxxxxx |   xxxxxxxxxxxxxxxxxxxxxxxxxx   | 
|   xxxxxx   |                      |                      xxxxxxx   | 
|   xxxxxx   |                      |                                | 
|        x   |                      |                                | 
+------------+----------------------+--------------------------------+
|   xxxxxx   | xxxxxxxxxxxxxxxxxxxx |                   xxxxxxxxxx   | 
|            | xxxxxxxxxxxxxxxxxxxx |                                | 
|            |                   xx |                                | 
+------------+----------------------+--------------------------------+


                             Table 1

   xxxx           xxxxxxx           xxxxxxxxxxxxxxxxxxxxxx     
   x                                xxxxxxxxxxxxxxxxxxxxx      
   xxxx      xxxxxxxxxxxxxxxx       xxxxxxxxxxxx               
   xxxx            xxxx                                        
   xxxx                                                        
   xxxx                                                        
   xxxx                                                        
   xxxx                                                        
   xxxx                                                        
   x                                                           
   xxxx          xxxxxxxx           xxxxxxxxxxxxxxxxxxxxxx     
   xxxx                             x                          
   xxxx                                                        
   xx                                                          


An example of a table with a header row.  The color and border properties are not inherited

+----------------------+--------------------------------+-----------------+
| MY NAME IS REALLY LO |      ADDRESS                   | CITY            | 
| NG AND WILL WRAP AND |                                |                 | 
|          HOPE        |                                |                 | 
+----------------------+--------------------------------+-----------------+
|        Ceaser        |      1 Appian Way              | Rome            | 
+----------------------+--------------------------------+-----------------+
|    Richard Feynman   |      1 Golden Gate             | Quantum Field   | 
+----------------------+--------------------------------+-----------------+


The same table with the properties inherited from the first row

+----------------------+--------------------------------+-----------------+
| MY NAME IS REALLY LO |      ADDRESS                   | CITY            | 
| NG AND WILL WRAP AND |                                |                 | 
|          HOPE        |                                |                 | 
+----------------------+--------------------------------+-----------------+
|        Ceaser        |      1 Appian Way              | Rome            | 
+----------------------+--------------------------------+-----------------+
|    Richard Feynman   |      1 Golden Gate             | Quantum Field   | 
+----------------------+--------------------------------+-----------------+