columnist 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35ce2268e5eb282ea238c82f068afe57dc8140e4
4
- data.tar.gz: cf74dcb33851da6ae27429675ce80d06288f0dc0
3
+ metadata.gz: 0fa0309605840f3f0d3703adf7b0aeec35c32458
4
+ data.tar.gz: da7cd50f03f621888fedd2cb2ac4a42243c098b3
5
5
  SHA512:
6
- metadata.gz: a76d6cb4ee88847ed5dd3ddc465d56cb3fc6f7f886a1e4c16dc80ce787f3ec49d52e6a81ec258250d9c39bce28cab12f31073d3d328e38f478984eecd90082b5
7
- data.tar.gz: e5b603a886e092c61b43472a984027e275b81195db5eff1894b533c01a32a9b17c1ba60bde9f20b2f40079c03eac9e0136c3b9b2c56946f2819a70609db89742
6
+ metadata.gz: d1b9986c7cf20d37fad2b7dc5e52f55571caa290a955c459d4051c42522f4d831a74b37dfa3671d260981849330655dc1510f8fb1224f51d912eb4df43d0f35f
7
+ data.tar.gz: 6c12a03ec438fffffbce01633cea2052c1f3f23fb3231bf72955305b7ed54206064427ab9ce98a112297da9dda5239de74655855b19955bf48c0525f39feaa84
data/README.md CHANGED
@@ -1,30 +1,31 @@
1
1
  ## Columnist
2
2
 
3
- This gem provides a DSL that makes it easy to write reports of various types in ruby. It eliminates
4
- the need to litter your source with *puts* statements, instead providing a more readable, expressive
5
- interface to your application. Some of the best features include:
3
+ This gem provides a DSL that makes it easy to write reports of various types in ruby. It eliminates
4
+ the need to litter your source with `puts` statements, instead providing a more readable, expressive
5
+ interface to your application. Some of the best features include:
6
6
 
7
7
  * Formatters that automatically indicate progress
8
8
  * Table syntax similar to HTML that makes it trivial to format your data in rows and columns
9
9
  * Easily created headers and footers for your report
10
- * Output suppression that makes it easy for your script to support a _quiet_ flag
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, 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
- supports it. Here is an example of output you can generate easily with "the reporter":
13
+ The latest release allows you to choose between UTF8 or ASCII for drawing tables. By default it will
14
+ use UTF8 if your system supports it.
16
15
 
17
- ![Screenshot](http://i.imgur.com/5izCf.png)
16
+ Here is an example of output you can generate easily with **Columnist**:
17
+
18
+ ![Screenshot](https://raw.githubusercontent.com/alb3rtuk/columnist/master/examples/screenshot-1.png)
18
19
 
19
20
  ### Installation
20
21
 
21
- It is up on rubygems.org so add it to your bundle in the Gemfile
22
+ It is up on rubygems.org so add it to your bundle in the Gemfile..
22
23
 
23
24
  ```bash
24
25
  gem 'columnist', '>=1.0'
25
26
  ```
26
27
 
27
- or do it the old fashioned way:
28
+ Or do it the old fashioned way..
28
29
 
29
30
  ```bash
30
31
  gem install columnist
@@ -32,119 +33,132 @@ gem install columnist
32
33
 
33
34
  ### Usage
34
35
 
35
- The gem provides a mixin that can be included in your scripts.
36
+ The gem provides a mixin that can be included in your script as follows:
36
37
 
37
38
  ```ruby
38
39
  require 'columnist'
39
40
 
40
- class MyReport
41
+ class YourClass
41
42
  include Columnist
42
43
  ...
43
44
  end
44
45
  ```
45
46
 
46
- ### [Wiki](https://github.com/alb3rtuk/columnist)
47
+ The best way to get an idea of how it works is to look at an example program:
48
+
49
+ ```ruby
50
+ require 'columnist'
51
+
52
+ class Example
53
+ include Columnist
54
+
55
+ def run
56
+ table(:border => true, :border_color => 39) do
57
+ row do
58
+ column('NAME', :width => 20)
59
+ column('ADDRESS', :width => 30)
60
+ column('CITY', :width => 25, :align => 'right')
61
+ end
62
+ row do
63
+ column('Dean Linden', :color => 'magenta')
64
+ column('12 Appian Way', :color => 'magenta')
65
+ column('New York')
66
+ end
67
+ row do
68
+ column('Ross Joy')
69
+ column('24 Golden Gate Road')
70
+ column('San Francisco')
71
+ end
72
+ row do
73
+ column('Tommy Booy', :color => 202)
74
+ column('6210 Crenshaw', :color => 202)
75
+ column('Los Angeles')
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ Example.new.run
82
+ ```
83
+
84
+ The above code will output the following result:
85
+
86
+ ![Screenshot](https://raw.githubusercontent.com/alb3rtuk/columnist/master/examples/screenshot-3.png)
47
87
 
48
- The [Wiki](https://github.com/alb3rtuk/columnist) has all of the documentation
49
- necessary for getting you started.
88
+ ### Color Reference
89
+
90
+ **Columnist** has 256-color support! Use the chart below for reference.
91
+
92
+ ![Screenshot](https://raw.githubusercontent.com/alb3rtuk/columnist/master/examples/screenshot-2.png)
50
93
 
51
94
  ### API Reference
52
95
 
53
96
  There are several methods the mixin provides that do not depend on the formatter used:
54
97
 
55
- * _header(hash)_ and _footer(hash)_
56
- * _:title_ - The title text for the section. _Default: 'Report'_
57
- * _:width_ - The width in characters for the section. _Default: 100_
58
- * _:align_ - 'left'|'right'|'center' align the title text. _Default: 'left'_
59
- * _:spacing_ - Number of vertical lines to leave as spacing after|before the header|footer.
60
- _Default: 1_
61
- * _:timestamp_ - Include a line indicating the timestamp below|above the header|footer text.
62
- Either true|false. _Default: false_
63
- * _:rule_ - true|false indicates whether to include a horizontal rule below|above the
64
- header|footer. _Default: false_
65
- * _:color_ - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
66
- * _:bold_ - true|false to boldface the font
67
- * _report(hash) {block}_
98
+ * **header(hash)** and **footer(hash)**
99
+ * **:title** - The title text for the section. **Default: 'Report'**
100
+ * **:width** - The width in characters for the section. **Default: 100**
101
+ * **:align** - 'left'|'right'|'center' align the title text. **Default: 'left'**
102
+ * **:spacing** - Number of vertical lines to leave as spacing after|before the header|footer.
103
+ **Default: 1**
104
+ * **:timestamp** - Include a line indicating the timestamp below|above the header|footer text.
105
+ Either true|false. **Default: false**
106
+ * **:rule** - true|false indicates whether to include a horizontal rule below|above the
107
+ header|footer. **Default: false**
108
+ * **:color** - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
109
+ * **:bold** - true|false to boldface the font
110
+ * **report(hash) {block}**
68
111
  * The first argument is a hash that defines the options for the method. See the details in the
69
112
  formatter section for allowed values.
70
113
  * The second argument is a block of ruby code that you want executed within the context of the
71
- reporter. Any ruby code is allowed. See the examples that follow in the formatter sections for
114
+ reporter. Any ruby code is allowed. See the examples that follow in the formatter sections for
72
115
  details.
73
- * _formatter=(string)_
74
- * Factory method indicating the formatter you want your application to use. At present the 2
75
- formatters are (_Default: 'nested'_):
116
+ * **formatter=(string)**
117
+ * Factory method indicating the formatter you want your application to use. At present the 2
118
+ formatters are (**Default: 'nested'**):
76
119
  * 'progress' - Use the progress formatter
77
120
  * 'nested' - Use the nested (or documentation) formatter
78
- * _horizontal_rule(hash)_
79
- * _:char_ - The character used to build the rule. _Default: '-'_
80
- * _:width_ - The width in characters of the rule. _Default: 100_
81
- * _:color_ - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
82
- * _:bold_ - true|false to boldface the font
83
- * _vertical_spacing(int)_
84
- * Number of blank lines to output. _Default: 1_
85
- * _datetime(hash)_
86
- * _:align_ - 'left'|'center'|'right' alignment of the timestamp. _Default: 'left'_
87
- * _:width_ - The width of the string in characters. _Default: 100_
88
- * _:format_ - Any allowed format from #strftime#. _Default: %Y-%m-%d %H:%I:%S%p_
89
- * _:color_ - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
90
- * _:bold_ - true|false to boldface the font
91
- * _aligned(string, hash)_
92
- * _text_ - String to display
93
- * _:align_ - 'left'|'right'|'center' align the string text. _Default: 'left'_
94
- * _:width_ - The width in characters of the string text. _Default: 100_
95
- * _:color_ - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
96
- * _:bold_ - true|false to boldface the font
97
- * _table(hash) {block}_
121
+ * **horizontal_rule(hash)**
122
+ * **:char** - The character used to build the rule. **Default: '-'**
123
+ * **:width** - The width in characters of the rule. **Default: 100**
124
+ * **:color** - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
125
+ * **:bold** - true|false to boldface the font
126
+ * **vertical_spacing(int)**
127
+ * Number of blank lines to output. **Default: 1**
128
+ * **datetime(hash)**
129
+ * **:align** - 'left'|'center'|'right' alignment of the timestamp. **Default: 'left'**
130
+ * **:width** - The width of the string in characters. **Default: 100**
131
+ * **:format** - Any allowed format from #strftime#. **Default: %Y-%m-%d %H:%I:%S%p**
132
+ * **:color** - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
133
+ * **:bold** - true|false to boldface the font
134
+ * **aligned(string, hash)**
135
+ * **text** - String to display
136
+ * **:align** - 'left'|'right'|'center' align the string text. **Default: 'left'**
137
+ * **:width** - The width in characters of the string text. **Default: 100**
138
+ * **:color** - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
139
+ * **:bold** - true|false to boldface the font
140
+ * **table(hash) {block}**
98
141
  * The first argument is a hash that defines properties of the table.
99
- * _:border_ - true|false indicates whether to include borders around the table cells
100
- * _:encoding_ - :ascii or :unicode (default unicode)
101
- * The second argument is a block which includes calls the to the _row_ method
102
- * _row {block}_
103
- * _:header_ - Set to true to indicate if this is a header row in the table.
104
- * _:color_ - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
105
- * _:bold_ - true|false to boldface the font
106
- * _column(string, hash)_
107
- * _text_ - String to display in the table cell
108
- * _options_ - The options to define the column
142
+ * **:border** - true|false indicates whether to include borders around the table cells
143
+ * **:encoding** - :ascii or :unicode (default unicode)
144
+ * The second argument is a block which includes calls the to the**row**method
145
+ * **row {block}**
146
+ * **:header** - Set to true to indicate if this is a header row in the table.
147
+ * **:color** - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
148
+ * **:bold** - true|false to boldface the font
149
+ * **column(string, hash)**
150
+ * **text** - String to display in the table cell
151
+ * **options** - The options to define the column
109
152
  * :width - defines the width of the column
110
153
  * :padding - The number of spaces to put on both the left and right of the text.
111
154
  * :align - Allowed values are left|right|center
112
155
  * :color - The color to use for the terminal output i.e. 'red' or 'blue' or 'green'
113
156
  * :bold - true|false to boldface the font
114
- * _suppress_output_ - Suppresses output stream that goes to STDOUT
115
- * _capture_output_ - Captures all of the output stream to a string and restores output to STDOUT
116
- * _restore_output_ - Restores the output stream to STDOUT
117
-
118
- ### To Do
119
-
120
- * Add a formatter that supports html output
121
- * Add the ability for a column to span across others in a table
122
-
123
- ### Contributors
124
-
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
132
-
133
- ### License
134
-
135
- Copyright (c) 2011-2014 Albert Rannetsperger
136
-
137
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
138
- associated documentation files (the "Software"), to deal in the Software without restriction,
139
- including without limitation the rights to use, copy, modify, merge, publish, distribute,
140
- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
141
- furnished to do so, subject to the following conditions:
157
+ * **suppress_output** - Suppresses output stream that goes to STDOUT
158
+ * **capture_output** - Captures all of the output stream to a string and restores output to STDOUT
159
+ * **restore_output** - Restores the output stream to STDOUT
142
160
 
143
- The above copyright notice and this permission notice shall be included in all copies or substantial
144
- portions of the Software.
161
+ ## Original
145
162
 
146
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
147
- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
148
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
149
- OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
150
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
163
+ The original gem, created by [Wes Bailey](https://github.com/wbailey), can be found here:
164
+ [https://github.com/wbailey/command_line_reporter](https://github.com/wbailey/command_line_reporter)
@@ -0,0 +1,32 @@
1
+ require 'columnist'
2
+
3
+ class Example
4
+ include Columnist
5
+
6
+ def run
7
+ table(:border => true, :border_color => 39) do
8
+ row do
9
+ column('NAME', :width => 20)
10
+ column('ADDRESS', :width => 30)
11
+ column('CITY', :width => 25, :align => 'right')
12
+ end
13
+ row do
14
+ column('Dean Linden', :color => 'magenta')
15
+ column('12 Appian Way', :color => 'magenta')
16
+ column('New York')
17
+ end
18
+ row do
19
+ column('Ross Joy')
20
+ column('24 Golden Gate Road')
21
+ column('San Francisco')
22
+ end
23
+ row do
24
+ column('Tommy Booy', :color => 202)
25
+ column('6210 Crenshaw', :color => 202)
26
+ column('Los Angeles')
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ Example.new.run
Binary file
Binary file
Binary file
@@ -7,7 +7,6 @@ module Columnist
7
7
 
8
8
  def initialize(options = {})
9
9
  self.validate_options(options, *VALID_OPTIONS)
10
-
11
10
  self.columns = []
12
11
  self.border = false
13
12
  self.header = options[:header] || false
@@ -32,10 +31,8 @@ module Columnist
32
31
  def output
33
32
  screen_count.times do |sr|
34
33
  border_char = use_utf8? ? "\u2503" : '|'
35
- border_char = border_char.send(self.border_color) if self.border_color
36
-
34
+ border_char = colorize(border_char, self.border_color)
37
35
  line = (self.border) ? "#{border_char} " : ''
38
-
39
36
  self.columns.size.times do |mc|
40
37
  col = self.columns[mc]
41
38
  # Account for the fact that some columns will have more screen rows than their
@@ -62,10 +59,8 @@ module Columnist
62
59
  else
63
60
  line << self.columns[mc].screen_rows[sr]
64
61
  end
65
-
66
62
  line << ' ' + ((self.border) ? "#{border_char} " : '')
67
63
  end
68
-
69
64
  puts line
70
65
  end
71
66
  end
@@ -79,5 +74,37 @@ module Columnist
79
74
  def use_utf8?
80
75
  self.encoding == :unicode && "\u2501" != "u2501"
81
76
  end
77
+
78
+ def colorize(str, color)
79
+ case color
80
+ when 'red'
81
+ return "\x1B[38;5;9m#{str}\x1B[38;5;256m"
82
+ when 'green'
83
+ return "\x1B[38;5;10m#{str}\x1B[38;5;256m"
84
+ when 'yellow'
85
+ return "\x1B[38;5;11m#{str}\x1B[38;5;256m"
86
+ when 'blue'
87
+ return "\x1B[38;5;33m#{str}\x1B[38;5;256m"
88
+ when 'magenta'
89
+ return "\x1B[38;5;13m#{str}\x1B[38;5;256m"
90
+ when 'cyan'
91
+ return "\x1B[38;5;14m#{str}\x1B[38;5;256m"
92
+ when 'gray'
93
+ return "\x1B[38;5;240m#{str}\x1B[38;5;256m"
94
+ when 'white'
95
+ return "\x1B[38;5;255m#{str}\x1B[38;5;256m"
96
+ when 'black'
97
+ return "\x1B[38;5;0m#{str}\x1B[38;5;256m"
98
+ end
99
+ if is_number?(color)
100
+ str = "\x1B[38;5;#{color}m#{str}\x1B[38;5;256m"
101
+ end
102
+ str
103
+ end
104
+
105
+ def is_number?(str)
106
+ true if Integer(str) rescue false
107
+ end
108
+
82
109
  end
83
110
  end
@@ -7,15 +7,12 @@ module Columnist
7
7
 
8
8
  def initialize(options = {})
9
9
  self.validate_options(options, *VALID_OPTIONS)
10
-
11
10
  self.border = options[:border] || false
12
11
  self.border_color = options[:border_color] || false
13
12
  self.width = options[:width] || false
14
13
  self.encoding = options[:encoding] || Columnist::DEFAULTS[:encoding]
15
-
16
14
  @rows = []
17
-
18
- raise ArgumentError, "Invalid encoding" unless [:ascii, :unicode].include? self.encoding
15
+ raise ArgumentError, 'Invalid encoding' unless [:ascii, :unicode].include? self.encoding
19
16
  end
20
17
 
21
18
  def add(row)
@@ -61,9 +58,8 @@ module Columnist
61
58
 
62
59
  def separator(type = 'middle')
63
60
  left, center, right, bar = use_utf8? ? utf8_separator(type) : ascii_separator
64
-
65
61
  separator_str = left + self.rows[0].columns.map { |c| bar * (c.width + 2) }.join(center) + right
66
- separator_str.send(self.border_color) if self.border_color
62
+ separator_str = colorize(separator_str, self.border_color)
67
63
  end
68
64
 
69
65
  def use_utf8?
@@ -129,5 +125,37 @@ module Columnist
129
125
  c.bold = self.rows[inherit_from].columns[i].bold
130
126
  end
131
127
  end
128
+
129
+ def colorize(str, color)
130
+ case color
131
+ when 'red'
132
+ return "\x1B[38;5;9m#{str}\x1B[38;5;256m"
133
+ when 'green'
134
+ return "\x1B[38;5;10m#{str}\x1B[38;5;256m"
135
+ when 'yellow'
136
+ return "\x1B[38;5;11m#{str}\x1B[38;5;256m"
137
+ when 'blue'
138
+ return "\x1B[38;5;33m#{str}\x1B[38;5;256m"
139
+ when 'magenta'
140
+ return "\x1B[38;5;13m#{str}\x1B[38;5;256m"
141
+ when 'cyan'
142
+ return "\x1B[38;5;14m#{str}\x1B[38;5;256m"
143
+ when 'gray'
144
+ return "\x1B[38;5;240m#{str}\x1B[38;5;256m"
145
+ when 'white'
146
+ return "\x1B[38;5;255m#{str}\x1B[38;5;256m"
147
+ when 'black'
148
+ return "\x1B[38;5;0m#{str}\x1B[38;5;256m"
149
+ end
150
+ if is_number?(color)
151
+ str = "\x1B[38;5;#{color}m#{str}\x1B[38;5;256m"
152
+ end
153
+ str
154
+ end
155
+
156
+ def is_number?(str)
157
+ true if Integer(str) rescue false
158
+ end
159
+
132
160
  end
133
161
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: columnist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albert Rannetsperger
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-09 00:00:00.000000000 Z
12
+ date: 2015-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: colored
@@ -58,6 +58,10 @@ extensions: []
58
58
  extra_rdoc_files: []
59
59
  files:
60
60
  - README.md
61
+ - examples/example.rb
62
+ - examples/screenshot-1.png
63
+ - examples/screenshot-2.png
64
+ - examples/screenshot-3.png
61
65
  - lib/columnist.rb
62
66
  - lib/columnist/column.rb
63
67
  - lib/columnist/formatter/nested.rb