columnist 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +111 -97
- data/examples/example.rb +32 -0
- data/examples/screenshot-1.png +0 -0
- data/examples/screenshot-2.png +0 -0
- data/examples/screenshot-3.png +0 -0
- data/lib/columnist/row.rb +33 -6
- data/lib/columnist/table.rb +34 -6
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fa0309605840f3f0d3703adf7b0aeec35c32458
|
4
|
+
data.tar.gz: da7cd50f03f621888fedd2cb2ac4a42243c098b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
4
|
-
the need to litter your source with
|
5
|
-
interface to your application.
|
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
|
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
|
14
|
-
|
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
|
-
|
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
|
-
|
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
|
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
|
41
|
+
class YourClass
|
41
42
|
include Columnist
|
42
43
|
...
|
43
44
|
end
|
44
45
|
```
|
45
46
|
|
46
|
-
|
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
|
-
|
49
|
-
|
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
|
-
*
|
56
|
-
*
|
57
|
-
*
|
58
|
-
*
|
59
|
-
*
|
60
|
-
|
61
|
-
*
|
62
|
-
Either true|false.
|
63
|
-
*
|
64
|
-
header|footer.
|
65
|
-
*
|
66
|
-
*
|
67
|
-
*
|
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.
|
114
|
+
reporter. Any ruby code is allowed. See the examples that follow in the formatter sections for
|
72
115
|
details.
|
73
|
-
*
|
74
|
-
* Factory method indicating the formatter you want your application to use.
|
75
|
-
formatters are (
|
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
|
-
*
|
79
|
-
*
|
80
|
-
*
|
81
|
-
*
|
82
|
-
*
|
83
|
-
*
|
84
|
-
* Number of blank lines to output.
|
85
|
-
*
|
86
|
-
*
|
87
|
-
*
|
88
|
-
*
|
89
|
-
*
|
90
|
-
*
|
91
|
-
*
|
92
|
-
*
|
93
|
-
*
|
94
|
-
*
|
95
|
-
*
|
96
|
-
*
|
97
|
-
*
|
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
|
-
*
|
100
|
-
*
|
101
|
-
* The second argument is a block which includes calls the to the
|
102
|
-
*
|
103
|
-
*
|
104
|
-
*
|
105
|
-
*
|
106
|
-
*
|
107
|
-
*
|
108
|
-
*
|
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
|
-
*
|
115
|
-
*
|
116
|
-
*
|
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
|
-
|
144
|
-
portions of the Software.
|
161
|
+
## Original
|
145
162
|
|
146
|
-
|
147
|
-
|
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)
|
data/examples/example.rb
ADDED
@@ -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
|
data/lib/columnist/row.rb
CHANGED
@@ -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
|
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
|
data/lib/columnist/table.rb
CHANGED
@@ -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
|
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.
|
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:
|
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
|