Giter VIP home page Giter VIP logo

command_line_reporter's Introduction

Command Line Reporter Build Status Code Climate Gem Version

This gem provides a DSL that makes it easy to write reports of various types in ruby. It eliminates the need to litter your source with puts statements, instead providing a more readable, expressive interface to your application. Some of the best features include:

  • Formatters that automatically indicate progress
  • Table syntax similar to HTML that makes it trivial to format your data in rows and columns
  • Easily created headers and footers for your report
  • Output suppression that makes it easy for your script to support a quiet flag
  • Capture report output as a string

The latest release, thanks to a contribution from Josh Brown, allows you to choose between UTF8 or ASCII for drawing tables. By default it will use UTF8 if your system supports it. Here is an example of output you can generate easily with "the reporter":

Screenshot

Installation

It is up on rubygems.org so add it to your bundle in the Gemfile

gem 'command_line_reporter', '>=3.0'

or do it the old fashioned way:

gem install command_line_reporter

Usage

The gem provides a mixin that can be included in your scripts.

require 'command_line_reporter'

class MyReport
  include CommandLineReporter
  ...
end

The Wiki has all of the documentation necessary for getting you started.

API Reference

There are several methods the mixin provides that do not depend on the formatter used:

  • header(hash) and footer(hash)
    • :title - The title text for the section. Default: 'Report'
    • :width - The width in characters for the section. Default: 100
    • :align - 'left'|'right'|'center' align the title text. Default: 'left'
    • :spacing - Number of vertical lines to leave as spacing after|before the header|footer. Default: 1
    • :timestamp - Include a line indicating the timestamp below|above the header|footer text. Either true|false. Default: false
    • :rule - true|false indicates whether to include a horizontal rule below|above the header|footer. Default: false
    • :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
    • :bold - true|false to boldface the font
  • report(hash) {block}
    • The first argument is a hash that defines the options for the method. See the details in the formatter section for allowed values.
    • The second argument is a block of ruby code that you want executed within the context of the reporter. Any ruby code is allowed. See the examples that follow in the formatter sections for details.
  • formatter=(string)
    • Factory method indicating the formatter you want your application to use. At present the 2 formatters are (Default: 'nested'):
    • 'progress' - Use the progress formatter
    • 'nested' - Use the nested (or documentation) formatter
  • horizontal_rule(hash)
    • :char - The character used to build the rule. Default: '-'
    • :width - The width in characters of the rule. Default: 100
    • :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
    • :bold - true|false to boldface the font
  • vertical_spacing(int)
    • Number of blank lines to output. Default: 1
  • datetime(hash)
    • :align - 'left'|'center'|'right' alignment of the timestamp. Default: 'left'
    • :width - The width of the string in characters. Default: 100
    • :format - Any allowed format from #strftime#. Default: %Y-%m-%d %H:%I:%S%p
    • :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
    • :bold - true|false to boldface the font
  • aligned(string, hash)
    • text - String to display
    • :align - 'left'|'right'|'center' align the string text. Default: 'left'
    • :width - The width in characters of the string text. Default: 100
    • :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
    • :bold - true|false to boldface the font
  • table(hash) {block}
    • The first argument is a hash that defines properties of the table.
      • :border - true|false indicates whether to include borders around the table cells
      • :encoding - :ascii or :unicode (default unicode)
    • The second argument is a block which includes calls the to the row method
  • row {block}
    • :header - Set to true to indicate if this is a header row in the table.
    • :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
    • :bold - true|false to boldface the font
  • column(string, hash)
    • text - String to display in the table cell
    • options - The options to define the column
      • :span - defines the number of columns to span across in the table
      • :width - defines the width of the column
      • :padding - The number of spaces to put on both the left and right of the text.
      • :align - Allowed values are left|right|center
      • :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
      • :bold - true|false to boldface the font
      • :wrap - character|word to break multi-line cells on character or word boundary.
  • suppress_output - Suppresses output stream that goes to STDOUT
  • capture_output - Captures all of the output stream to a string and restores output to STDOUT
  • restore_output - Restores the output stream to STDOUT

To Do

  • Add a formatter that supports html output

Contributors

  • Dennis Ideler submitted pull requests getting CLR up to date and fixing issues with border colors
  • Josh Brown added the ability to encode tables in either ascii or utf8
  • Stefan Frank for raising the issue that he could not capture report output in a variable as a string
  • Mike Gunderloy for suggesting the need for suppressing output and putting together a fantastic pull request and discussion
  • Jason Rogers and Peter Suschlik for their contributions as well on items I missed

License

Copyright (c) 2011-2017 Wes Bailey

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

command_line_reporter's People

Contributors

ardell avatar bmarini avatar carrum-ci avatar dideler avatar jwoffindin avatar lastobelus avatar maxmeyer avatar rrrene avatar splattael avatar tobijb avatar wbailey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

command_line_reporter's Issues

undefined method `align' for nil:NilClass (NoMethodError)

I want to have a header with full width, but have the error below:

code:

require 'command_line_reporter'
class AccountInfo
  include CommandLineReporter

  def initialize
    table(border: true, width: 80) do
      row header: true do
        column('Account Info', width: 80)
      end
      row do
        column('balance', width: 40)
        column('9999', width: 40)
      end
    end
  end
end
AccountInfo.new

error:

~/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:111:in `block in use_positional_attrs': undefined method `align' for nil:NilClass (NoMethodError)
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:110:in `each'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:110:in `use_positional_attrs'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:99:in `block in inherit_column_attrs'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:98:in `each'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:98:in `each_with_index'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:98:in `inherit_column_attrs'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter/table.rb:25:in `add'
	from /Users/victor/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter.rb:123:in `row'
	from account_info.rb:10:in `block in initialize'
	from ~/.rvm/gems/ruby-2.3.1/gems/command_line_reporter-4.0.0/lib/command_line_reporter.rb:115:in `table'
	from account_info.rb:6:in `initialize'
	from account_info.rb:17:in `new'
	from account_info.rb:17:in `<main>'

Report output is throwing some wild characters

My sample.rb

require 'command_line_reporter'
include CommandLineReporter

header title: 'STARTING SCRIPT', width: 80, align: 'center', rule: true, bold: true, timestamp: true

When I run this file ruby sample.rb I see below characters instead of a horizontal line

image

Am I missing on setting any encoders?

Using command_line_reporter v4.0.3, Ruby 2.6.3 on Windows 10

Don't inherit column width for last column of a row

The code here currently sets width (along with some other attributes) on every column to the width of the corresponding column of the first row of the table.

That works well for all columns except the last when the lines are close to wrapping in a terminal window. With the current setup, if the first row is wider than the user's terminal, it will wrap, and because of the above behavior, so will all the other lines because they'll be right-padded with spaces to match the width of the first row.

I'd propose changing the code to only inherit width for the last column of a row if the text in that column is longer than the first rows's width. Either that, or just don't right-pad the last column of each row on output regardless of the column's width.

reporting into a string?!

I love it! I'm doing lots of import-scripts and the reporter is extremely handy for that. But I'm also having some import-jobs that run in delayed_job: they do report into the logs, but I'm also storing a report in the database to display it on the website (in a simple

-block).  Is there a simple way to report everything into a string and show this string later?!

Don't print colors when not on a TTY

When piping to another command or to a file the colors shouldn't be printed. Otherwise the output when running commands in a cron job for example is pretty horrible. Here's a snippet of a command I run via cron:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ ^[[1m^[[34m Repo Name ^[[0m^[[0m ┃ ^[[1m^[[31mOpen PRs ^[[0m^[[0m ┃ ^[[1m^[[31mIssues ^[[0m^[[0m ┃ ^[[1m^[[31mOldest PR ^[[0m^[[0m ┃ ^[[1m^[[31mOldest Iss^[[0m^[[0m ┃

Issue with newrelic_rpm

Hi Wes!

I updated my newrelic_rpm from 3.6.6.147 to 3.9.1.236, and started getting:
"..../gems/command_line_reporter-3.3.2/lib/command_line_reporter.rb:45:in `rescue in formatter='"

Looks like the newrelic gem is doing this "@log.formatter = Proc.new ..." around line 167 of agent_logger.rb, which is conflicting with your formatter= method.

I tried a quick hack with this:

38 def formatter=(type = 'nested')
39 return type if type.class != String
40 name = type.capitalize + 'Formatter'
41 klass = %W{CommandLineReporter #{name}}.inject(Kernel) {|s,c| s.const_get(c)}
42
43 # Each formatter is a singleton that responds to #instance
44 @Formatter = klass.instance
45 rescue
46 raise ArgumentError, 'Invalid formatter specified'
47 end

which seemed to stop things from crashing, but haven't had much time to see if this is appropriate or not. Any thoughts?

Borders not working

Hi, my borders are invisible with:

table border: true do
    # ...
end

See this screenshot.

Ubuntu 12.04.4, Ruby 2.1.1, XTerm. Tested with stable and your GitHub master branch.

Conflict with colorize

When using colorize gem, the following error is thrown:

TypeError: no implicit conversion of Hash into String
/Users/user/.rvm/gems/ruby-2.3.1/gems/colored-1.2/lib/colored.rb:70:in `colorize'
/Users/user/.rvm/gems/ruby-2.3.1/gems/colorize-0.8.1/lib/colorize/class_methods.rb:98:in `block (2 levels) in color_methods'

.ruby-version should not be in version control

The file .ruby-version should not be checked into your Git repo. It might cause Travis CI to run all your commands with the Ruby version specified in .ruby-version and therefore ignoring the versions you specified in .travis.yml.

For more information, see the recommended .gitignore file, especially the section

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

Warnings when using command_line_reporter

Running ruby with warnings yields the following warnings:

/Users/me/Documents/tool/vendor/bundle/ruby/2.1.0/gems/command_line_reporter-3.3.6/lib/command_line_reporter/formatter/progress.rb:10: warning: `*' interpreted as argument prefix
/Users/me/Documents/tool/vendor/bundle/ruby/2.1.0/gems/command_line_reporter-3.3.6/lib/command_line_reporter/formatter/progress.rb:33: warning: method redefined; discarding old indicator
/Users/me/Documents/tool/vendor/bundle/ruby/2.1.0/gems/command_line_reporter-3.3.6/lib/command_line_reporter/formatter/nested.rb:36: warning: method redefined; discarding old message_string
/Users/me/Documents/tool/vendor/bundle/ruby/2.1.0/gems/command_line_reporter-3.3.6/lib/command_line_reporter/formatter/nested.rb:40: warning: method redefined; discarding old complete_string
/Users/me/Documents/tool/vendor/bundle/ruby/2.1.0/gems/command_line_reporter-3.3.6/lib/command_line_reporter/formatter/nested.rb:44: warning: method redefined; discarding old indent_size
/Users/me/Documents/tool/vendor/bundle/ruby/2.1.0/gems/command_line_reporter-3.3.6/lib/command_line_reporter/column.rb:27: warning: method redefined; discarding old size

spacing == 0 issue

Param spacing => 0 doesn't work for table (only if param >0 it works ok)

Fixed for myself by adding

if lines != 0

to vertical_spacing function:

  def vertical_spacing(lines = 1)
    puts "\n" * lines if lines != 0
  rescue
    raise ArgumentError
  end

Missing Ruby version requirement in GemSpec

This gem will install without error on Ruby 1.9.3 since it does not specify a requirement towards the Ruby version. It will however not work with Ruby 1.9.3 because you are using the %i[array of symbols] literal introduced in Ruby 2.0.

My suggestion would be to run your CI tests against

rvm:
- 2.0.0
- 2.1.9
- 2.2.7
- 2.3.4
- 2.4.1

and to add a Ruby version requirement to your GemSpec:

   gem.required_ruby_version = '~> 2.0'

You could also adapt the syntax of your gem to work on Ruby 1.9.3 if you want still want to support that version.

Table only prints if called from instance method

I noticed that a table is only printed if ran from an instance method. For example, this works

require 'command_line_reporter'

class Example
  include CommandLineReporter

  def run
    table do
      ...
    end
  end
end

Example.new.run

But the following does not

require 'command_line_reporter'

class Example
  include CommandLineReporter

  def self.run
    table do
      ...
    end
  end
end

Example.run

You'll get an error like

example.rb:7:in `run': undefined method `table' for Example:Class (NoMethodError)
    from example.rb:27:in `<main>'

If this is intentional, then the usage example in the README is a bit misleading and could be clarified.

require 'command_line_reporter'

class MyReport
  include CommandLineReporter
  ...
end

Separator issue

separator function doesn't return it correctly for me:

def separator(type = 'middle')
  left, center, right, bar = use_utf8? ? utf8_separator(type) : ascii_separator

  separator_str = left + self.rows[0].columns.map {|c| bar * (c.width + 2)}.join(center) + right
  separator_str.send(self.border_color) if self.border_color
end

But it works ok if i add to end of the function

     return separator_str string

Table formatting broken in v3.3.3

Commit a59d00e breaks the table formatting, and v3.3.3 which was just released contains that commit.

Before (GitHub slightly messes up the alignment)

┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ NAME                 ┃                   ADDRESS      ┃ CITY            ┃ 
┣━━━━━━━━━━━━━━━━━━━━━━╊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╊━━━━━━━━━━━━━━━━━┫
┃ Caesar               ┃              1 Appian Way      ┃ Rome            ┃ 
┣━━━━━━━━━━━━━━━━━━━━━━╊━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╊━━━━━━━━━━━━━━━━━┫
┃ Richard Feynman      ┃             1 Golden Gate      ┃ Quantum Field   ┃ 
┗━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━┛

After


┃ NAME                 ┃                   ADDRESS      ┃ CITY            ┃ 

┃ Caesar               ┃              1 Appian Way      ┃ Rome            ┃ 

┃ Richard Feynman      ┃             1 Golden Gate      ┃ Quantum Field   ┃ 


I first noticed this when I was developing from HEAD for my patches, but I assumed the fault was somehow in my dev environment. Then I upgraded the gem from v3.3.2 to v3.3.3 and noticed the issue again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.