command_line_reporter 3.2.1 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## Command Line Reporter
1
+ ## Command Line Reporter [![Build Status](https://travis-ci.org/wbailey/command_line_reporter.png)](https://travis-ci.org/wbailey/command_line_reporter) [![Code Climate](https://codeclimate.com/github/wbailey/command_line_reporter.png)](https://codeclimate.com/github/wbailey/command_line_reporter) [![Gem Version](https://badge.fury.io/rb/command_line_reporter.png)](http://badge.fury.io/rb/command_line_reporter)
2
2
 
3
3
  This gem provides a DSL that makes it easy to write reports of various types in ruby. It eliminates
4
4
  the need to litter your source with *puts* statements instead providing a more readable, expressive
@@ -10,8 +10,11 @@ interface to your application. Some of the best features include:
10
10
  * Output suppression that makes it easy for your script to support a _quiet_ flag
11
11
  * Capture report output as a string
12
12
 
13
- The latest release uses unicode drawing characters for the table, colors allowing you to distinguish
14
- data in new ways including bold if your terminal supports it.
13
+ The latest releast thanks to a contribution from [Josh Brown](https://github.com/tobijb) allows you
14
+ to choose between UTF8 or ASCII for drawing tables. By default it will use UTF8 if your system
15
+ support it. Here is an example of output you can generate easily with "the reporter":
16
+
17
+ ![Screenshot](http://i.imgur.com/5izCf.png)
15
18
 
16
19
  ### Installation
17
20
 
@@ -94,6 +97,7 @@ There are several methods the mixin provides that do not depend on the formatter
94
97
  * _table(hash) {block}_
95
98
  * The first argument is a hash that defines properties of the table.
96
99
  * _:border_ - true|false indicates whether to include borders around the table cells
100
+ * _:encoding_ - :ascii or :unicode (default unicode)
97
101
  * The second argument is a block which includes calls the to the _row_ method
98
102
  * _row {block}_
99
103
  * _:header_ - Set to true to indicate if this is a header row in the table.
@@ -113,22 +117,22 @@ There are several methods the mixin provides that do not depend on the formatter
113
117
 
114
118
  ### To Do
115
119
 
116
- * Refactor the table structure to use a formatter that produces the current ascii output
117
- * After the formatter is added to a table then create one for html output
120
+ * Add a formatter that supports html output
118
121
  * Add the ability for a column to span across others in a table
119
122
 
120
123
  ### Contributors
121
124
 
122
- * Thanks to [Stefan Frank](https://github.com/mugwump) for raising the issue that he could not
123
- capture report output in a variable as a string
124
- * Thanks to [Mike Gunderloy](https://github.com/ffmike) for suggesting the need for suppressing
125
- output and putting together a fantastic pull request and discussion
126
- * Thanks to [Jason Rogers](https://github.com/jacaetevha) and [Peter
127
- Suschlik](https://github.com/splattael) for their contributions as well on items I missed
125
+ * [Josh Brown](https://github.com/tobijb) added the ability to encode tables in either ascii or utf8
126
+ * [Stefan Frank](https://github.com/mugwump) for raising the issue that he could not capture report
127
+ output in a variable as a string
128
+ * [Mike Gunderloy](https://github.com/ffmike) for suggesting the need for suppressing output and
129
+ putting together a fantastic pull request and discussion
130
+ * [Jason Rogers](https://github.com/jacaetevha) and [Peter Suschlik](https://github.com/splattael)
131
+ for their contributions as well on items I missed
128
132
 
129
133
  ### License
130
134
 
131
- Copyright (c) 2011-2012 Wes Bailey
135
+ Copyright (c) 2011-2014 Wes Bailey
132
136
 
133
137
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
134
138
  associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -1,4 +1,3 @@
1
- require 'options_validator'
2
1
  require 'colored'
3
2
 
4
3
  module CommandLineReporter
@@ -13,7 +12,7 @@ module CommandLineReporter
13
12
 
14
13
  self.text = text.to_s
15
14
 
16
- self.width = options[:width] || 10
15
+ self.width = options[:width] || 10
17
16
  self.align = options[:align] || 'left'
18
17
  self.padding = options[:padding] || 0
19
18
  self.color = options[:color] || nil
@@ -23,8 +22,14 @@ module CommandLineReporter
23
22
 
24
23
  raise ArgumentError unless self.width > 0
25
24
  raise ArgumentError unless self.padding.to_s.match(/^\d+$/)
25
+ end
26
+
27
+ def size
28
+ self.width - 2 * self.padding
29
+ end
26
30
 
27
- self.size = self.width - 2 * self.padding
31
+ def required_width
32
+ self.text.to_s.size + 2 * self.padding
28
33
  end
29
34
 
30
35
  def screen_rows
@@ -1,5 +1,4 @@
1
1
  require 'singleton'
2
- require 'options_validator'
3
2
  require 'colored'
4
3
 
5
4
  module CommandLineReporter
@@ -1,5 +1,4 @@
1
1
  require 'singleton'
2
- require 'options_validator'
3
2
  require 'colored'
4
3
 
5
4
  module CommandLineReporter
@@ -1,11 +1,8 @@
1
- require 'column'
2
- require 'options_validator'
3
-
4
1
  module CommandLineReporter
5
2
  class Row
6
3
  include OptionsValidator
7
4
 
8
- VALID_OPTIONS = [:header, :color, :bold]
5
+ VALID_OPTIONS = [:header, :color, :bold, :encoding]
9
6
  attr_accessor :columns, :border, *VALID_OPTIONS
10
7
 
11
8
  def initialize(options = {})
@@ -16,6 +13,8 @@ module CommandLineReporter
16
13
  self.header = options[:header] || false
17
14
  self.color = options[:color]
18
15
  self.bold = options[:bold] || false
16
+ self.encoding = options[:encoding] || false
17
+
19
18
  end
20
19
 
21
20
  def add(column)
@@ -32,7 +31,7 @@ module CommandLineReporter
32
31
 
33
32
  def output
34
33
  screen_count.times do |sr|
35
- border_char = ("\u2501" == "u2501") ? '|' : "\u2503"
34
+ border_char = ("\u2501" == "u2501" || self.encoding == :ascii) ? '|' : "\u2503"
36
35
  line = (self.border) ? "#{border_char} " : ''
37
36
  self.columns.size.times do |mc|
38
37
  col = self.columns[mc]
@@ -0,0 +1,130 @@
1
+ module CommandLineReporter
2
+ class Table
3
+ include OptionsValidator
4
+
5
+ VALID_OPTIONS = [:border, :width, :encoding]
6
+ attr_accessor :rows, *VALID_OPTIONS
7
+
8
+ def initialize(options = {})
9
+ self.validate_options(options, *VALID_OPTIONS)
10
+
11
+ self.border = options[:border] || false
12
+ self.width = options[:width] || false
13
+ self.encoding = options[:encoding] || CommandLineReporter::DEFAULTS[:encoding]
14
+
15
+ @rows = []
16
+
17
+ raise ArgumentError, "Invalid encoding" unless [ :ascii, :unicode ].include? self.encoding
18
+ end
19
+
20
+ def add(row)
21
+ # Inheritance from the table
22
+ row.border = self.border
23
+
24
+ # Inherit properties from the appropriate row
25
+ inherit_column_attrs(row) if self.rows[0]
26
+
27
+ self.rows << row
28
+ end
29
+
30
+ def output
31
+ return if self.rows.size == 0 # we got here with nothing to print to the screen
32
+ auto_adjust_widths if self.width == :auto
33
+
34
+ puts separator('first') if self.border
35
+ self.rows.each_with_index do |row, index|
36
+ row.output
37
+ puts separator('middle') if self.border && (index != self.rows.size - 1)
38
+ end
39
+ puts separator('last') if self.border
40
+ end
41
+
42
+ def auto_adjust_widths
43
+ column_widths = []
44
+
45
+ self.rows.each do |row|
46
+ row.columns.each_with_index do |col, i|
47
+ column_widths[i] = [ col.required_width, ( column_widths[i] || 0 ) ].max
48
+ end
49
+ end
50
+
51
+ self.rows.each do |row|
52
+ row.columns.each_with_index do |col, i|
53
+ col.width = column_widths[i]
54
+ end
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def separator(type = 'middle')
61
+ left, right, center, bar = use_utf8? ? ascii_separator : utf8_separator
62
+
63
+ left + self.rows[0].columns.map {|c| bar * (c.width + 2)}.join(center) + right
64
+ end
65
+
66
+ def use_utf8?
67
+ self.encoding == :ascii || "\u2501" == "u2501"
68
+ end
69
+
70
+ def ascii_separator
71
+ left = right = center = '+'
72
+ bar = '-'
73
+ [left, right, center, bar]
74
+ end
75
+
76
+ def utf8_separator(type)
77
+ bar = "\u2501"
78
+
79
+ left, right, center = case type
80
+ when 'first'
81
+ ["\u250F", "\u2533", "\u2513"]
82
+ when 'middle'
83
+ ["\u2523", "\u254A", "\u252B"]
84
+ when 'last'
85
+ ["\u2517", "\u253B", "\u251B"]
86
+ end
87
+
88
+ [left, right, center, bar]
89
+ end
90
+
91
+ def inherit_column_attrs(row)
92
+ row.columns.each_with_index do |c,i|
93
+ use_positional_attrs(c, i)
94
+ use_color(row, c, i)
95
+ use_bold(row, c, i)
96
+ end
97
+ end
98
+
99
+ def use_positional_attrs(c, i)
100
+ # The positional attributes are always required to inheret to make sure the table
101
+ # displays properly
102
+ %w{align padding width}.each do |attr|
103
+ val = self.rows[0].columns[i].send(attr)
104
+ c.send(attr + "=", val)
105
+ end
106
+ end
107
+
108
+ def inherit_from
109
+ self.rows[0].header ? 1 : 0
110
+ end
111
+
112
+ def use_color(row, c, i)
113
+ if c.color
114
+ # keep default
115
+ elsif row.color
116
+ c.color = row.color
117
+ else
118
+ c.color = self.rows[inherit_from].columns[i].color
119
+ end
120
+ end
121
+
122
+ def use_bold(row, c, i)
123
+ if row.bold
124
+ c.bold = row.bold
125
+ elsif inherit_from != 1
126
+ c.bold = self.rows[inherit_from].columns[i].bold
127
+ end
128
+ end
129
+ end
130
+ end
@@ -1,3 +1,3 @@
1
1
  module CommandLineReporter
2
- VERSION = '3.2.1'
2
+ VERSION = '3.3.0'
3
3
  end
@@ -1,7 +1,12 @@
1
1
  require 'stringio'
2
- require 'table'
3
2
 
4
- Dir[File.join(File.dirname(__FILE__), '*_formatter.rb')].each {|r| require r}
3
+ require 'command_line_reporter/options_validator'
4
+ require 'command_line_reporter/formatter/progress'
5
+ require 'command_line_reporter/formatter/nested'
6
+ require 'command_line_reporter/row'
7
+ require 'command_line_reporter/column'
8
+ require 'command_line_reporter/table'
9
+ require 'command_line_reporter/version'
5
10
 
6
11
  module CommandLineReporter
7
12
  include OptionsValidator
@@ -12,6 +17,7 @@ module CommandLineReporter
12
17
  :width => 100,
13
18
  :align => 'left',
14
19
  :formatter => 'nested',
20
+ :encoding => :unicode,
15
21
  }
16
22
 
17
23
  def capture_output
@@ -120,6 +126,7 @@ module CommandLineReporter
120
126
  end
121
127
 
122
128
  def row(options = {})
129
+ options[:encoding] ||= @table.encoding
123
130
  @row = CommandLineReporter::Row.new(options)
124
131
  yield
125
132
  @table.add(@row)