tty-table 0.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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +22 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +389 -0
- data/Rakefile +8 -0
- data/benchmarks/speed.rb +36 -0
- data/lib/tty/table/border/ascii.rb +32 -0
- data/lib/tty/table/border/null.rb +37 -0
- data/lib/tty/table/border/row_line.rb +18 -0
- data/lib/tty/table/border/unicode.rb +32 -0
- data/lib/tty/table/border.rb +219 -0
- data/lib/tty/table/border_dsl.rb +251 -0
- data/lib/tty/table/border_options.rb +53 -0
- data/lib/tty/table/column_set.rb +121 -0
- data/lib/tty/table/columns.rb +170 -0
- data/lib/tty/table/empty.rb +26 -0
- data/lib/tty/table/error.rb +39 -0
- data/lib/tty/table/field.rb +139 -0
- data/lib/tty/table/header.rb +163 -0
- data/lib/tty/table/indentation.rb +52 -0
- data/lib/tty/table/operation/alignment_set.rb +103 -0
- data/lib/tty/table/operation/escape.rb +30 -0
- data/lib/tty/table/operation/filter.rb +34 -0
- data/lib/tty/table/operation/padding.rb +95 -0
- data/lib/tty/table/operation/truncation.rb +41 -0
- data/lib/tty/table/operation/wrapped.rb +43 -0
- data/lib/tty/table/operations.rb +69 -0
- data/lib/tty/table/options.rb +30 -0
- data/lib/tty/table/orientation/horizontal.rb +48 -0
- data/lib/tty/table/orientation/vertical.rb +38 -0
- data/lib/tty/table/orientation.rb +57 -0
- data/lib/tty/table/padder.rb +180 -0
- data/lib/tty/table/renderer/ascii.rb +16 -0
- data/lib/tty/table/renderer/basic.rb +294 -0
- data/lib/tty/table/renderer/color.rb +12 -0
- data/lib/tty/table/renderer/unicode.rb +21 -0
- data/lib/tty/table/renderer.rb +101 -0
- data/lib/tty/table/row.rb +248 -0
- data/lib/tty/table/transformation.rb +39 -0
- data/lib/tty/table/validatable.rb +64 -0
- data/lib/tty/table/version.rb +7 -0
- data/lib/tty/table.rb +469 -0
- data/lib/tty-table.rb +48 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/unit/access_spec.rb +86 -0
- data/spec/unit/add_row_spec.rb +28 -0
- data/spec/unit/border/ascii/rendering_spec.rb +90 -0
- data/spec/unit/border/new_spec.rb +27 -0
- data/spec/unit/border/null/rendering_spec.rb +130 -0
- data/spec/unit/border/options/from_spec.rb +38 -0
- data/spec/unit/border/options/new_spec.rb +14 -0
- data/spec/unit/border/unicode/rendering_spec.rb +63 -0
- data/spec/unit/border_options/new_spec.rb +20 -0
- data/spec/unit/border_options/update_spec.rb +18 -0
- data/spec/unit/column_set/extract_widths_spec.rb +15 -0
- data/spec/unit/column_set/total_width_spec.rb +15 -0
- data/spec/unit/column_set/widths_from_spec.rb +51 -0
- data/spec/unit/columns/enforce_spec.rb +67 -0
- data/spec/unit/columns/widths_spec.rb +35 -0
- data/spec/unit/data_spec.rb +14 -0
- data/spec/unit/each_spec.rb +41 -0
- data/spec/unit/each_with_index_spec.rb +57 -0
- data/spec/unit/empty_spec.rb +23 -0
- data/spec/unit/eql_spec.rb +34 -0
- data/spec/unit/field/equality_spec.rb +51 -0
- data/spec/unit/field/length_spec.rb +21 -0
- data/spec/unit/field/lines_spec.rb +21 -0
- data/spec/unit/field/new_spec.rb +29 -0
- data/spec/unit/field/width_spec.rb +23 -0
- data/spec/unit/filter_spec.rb +23 -0
- data/spec/unit/header/call_spec.rb +30 -0
- data/spec/unit/header/color_spec.rb +19 -0
- data/spec/unit/header/equality_spec.rb +51 -0
- data/spec/unit/header/height_spec.rb +27 -0
- data/spec/unit/header/new_spec.rb +25 -0
- data/spec/unit/header/set_spec.rb +20 -0
- data/spec/unit/header/to_ary_spec.rb +14 -0
- data/spec/unit/header_spec.rb +13 -0
- data/spec/unit/indentation/insert_indent_spec.rb +27 -0
- data/spec/unit/initialize_spec.rb +88 -0
- data/spec/unit/operation/alignment_set/call_spec.rb +39 -0
- data/spec/unit/operation/alignment_set/each_spec.rb +17 -0
- data/spec/unit/operation/alignment_set/new_spec.rb +27 -0
- data/spec/unit/operation/alignment_set/to_ary_spec.rb +14 -0
- data/spec/unit/operation/escape/call_spec.rb +16 -0
- data/spec/unit/operation/filter/call_spec.rb +17 -0
- data/spec/unit/operation/truncation/call_spec.rb +32 -0
- data/spec/unit/operation/wrapped/call_spec.rb +33 -0
- data/spec/unit/operations/new_spec.rb +30 -0
- data/spec/unit/options/access_spec.rb +14 -0
- data/spec/unit/options_spec.rb +25 -0
- data/spec/unit/orientation_spec.rb +145 -0
- data/spec/unit/padder/parse_spec.rb +45 -0
- data/spec/unit/padder/to_s_spec.rb +14 -0
- data/spec/unit/padding_spec.rb +120 -0
- data/spec/unit/properties_spec.rb +25 -0
- data/spec/unit/render_spec.rb +63 -0
- data/spec/unit/render_with_spec.rb +106 -0
- data/spec/unit/renderer/ascii/indentation_spec.rb +41 -0
- data/spec/unit/renderer/ascii/padding_spec.rb +61 -0
- data/spec/unit/renderer/ascii/render_spec.rb +68 -0
- data/spec/unit/renderer/ascii/resizing_spec.rb +114 -0
- data/spec/unit/renderer/ascii/separator_spec.rb +28 -0
- data/spec/unit/renderer/basic/alignment_spec.rb +88 -0
- data/spec/unit/renderer/basic/coloring_spec.rb +46 -0
- data/spec/unit/renderer/basic/extract_column_widths_spec.rb +28 -0
- data/spec/unit/renderer/basic/filter_spec.rb +53 -0
- data/spec/unit/renderer/basic/indentation_spec.rb +48 -0
- data/spec/unit/renderer/basic/multiline_content_spec.rb +135 -0
- data/spec/unit/renderer/basic/new_spec.rb +26 -0
- data/spec/unit/renderer/basic/options_spec.rb +52 -0
- data/spec/unit/renderer/basic/padding_spec.rb +52 -0
- data/spec/unit/renderer/basic/render_spec.rb +57 -0
- data/spec/unit/renderer/basic/resizing_spec.rb +96 -0
- data/spec/unit/renderer/basic/separator_spec.rb +43 -0
- data/spec/unit/renderer/basic/truncation_spec.rb +35 -0
- data/spec/unit/renderer/basic/wrapping_spec.rb +40 -0
- data/spec/unit/renderer/border_spec.rb +104 -0
- data/spec/unit/renderer/render_spec.rb +36 -0
- data/spec/unit/renderer/select_spec.rb +22 -0
- data/spec/unit/renderer/style_spec.rb +72 -0
- data/spec/unit/renderer/unicode/indentation_spec.rb +41 -0
- data/spec/unit/renderer/unicode/padding_spec.rb +61 -0
- data/spec/unit/renderer/unicode/render_spec.rb +68 -0
- data/spec/unit/renderer/unicode/separator_spec.rb +26 -0
- data/spec/unit/renderer_spec.rb +19 -0
- data/spec/unit/rotate_spec.rb +86 -0
- data/spec/unit/row/access_spec.rb +25 -0
- data/spec/unit/row/call_spec.rb +45 -0
- data/spec/unit/row/data_spec.rb +26 -0
- data/spec/unit/row/each_spec.rb +31 -0
- data/spec/unit/row/equality_spec.rb +73 -0
- data/spec/unit/row/height_spec.rb +27 -0
- data/spec/unit/row/new_spec.rb +41 -0
- data/spec/unit/row/to_ary_spec.rb +14 -0
- data/spec/unit/to_s_spec.rb +63 -0
- data/spec/unit/transformation/extract_tuples_spec.rb +35 -0
- data/spec/unit/validatable/validate_options_spec.rb +33 -0
- data/spec/unit/validatable_spec.rb +32 -0
- data/tasks/console.rake +10 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/spec.rake +29 -0
- data/tty-table.gemspec +28 -0
- metadata +371 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5fd8b00601ae34976c7045b404d50fabf7729279
|
4
|
+
data.tar.gz: 446d1950b5192161cab664732b9caeeec25bd186
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 821d23c84214d24ba025b652bfecc77c155ffce1af44e9a575d3f8187b3165393e1e2085740570b359a6e4d9d7c4607b784fc1a863090512fd248fa762f55e8c
|
7
|
+
data.tar.gz: e3a93020ff3ab85e8afbca2664c7a6034cb385a2d2fce62e37563ea14c37255e0a5a18ab6ebaad72a1c2d7fd29933614212d43843300ed866959b7a4251beecb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
language: ruby
|
2
|
+
bundler_args: --without yard benchmarks
|
3
|
+
script: "bundle exec rake ci"
|
4
|
+
rvm:
|
5
|
+
- 1.9.3
|
6
|
+
- 2.0.0
|
7
|
+
- 2.1.0
|
8
|
+
- 2.2.0
|
9
|
+
- ruby-head
|
10
|
+
matrix:
|
11
|
+
include:
|
12
|
+
- rvm: jruby-19mode
|
13
|
+
- rvm: jruby-20mode
|
14
|
+
- rvm: jruby-21mode
|
15
|
+
- rvm: jruby-head
|
16
|
+
- rvm: rbx-2
|
17
|
+
allow_failures:
|
18
|
+
- rvm: ruby-head
|
19
|
+
- rvm: jruby-head
|
20
|
+
fast_finish: true
|
21
|
+
branches:
|
22
|
+
only: master
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem 'rake', '~> 10.3.2'
|
7
|
+
gem 'rspec', '~> 3.1.0'
|
8
|
+
gem 'yard', '~> 0.8.7'
|
9
|
+
end
|
10
|
+
|
11
|
+
group :metrics do
|
12
|
+
gem 'coveralls', '~> 0.7.0'
|
13
|
+
gem 'simplecov', '~> 0.8.2'
|
14
|
+
gem 'yardstick', '~> 0.9.9'
|
15
|
+
end
|
16
|
+
|
17
|
+
group :benchmarks do
|
18
|
+
gem 'benchmark_suite', '~> 1.0.0'
|
19
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Piotr Murach
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,389 @@
|
|
1
|
+
# TTY::Table
|
2
|
+
|
3
|
+
[][gem]
|
4
|
+
[][travis]
|
5
|
+
[][codeclimate]
|
6
|
+
[][coverage]
|
7
|
+
|
8
|
+
[gem]: http://badge.fury.io/rb/tty-table
|
9
|
+
[travis]: http://travis-ci.org/peter-murach/tty-table
|
10
|
+
[codeclimate]: https://codeclimate.com/github/peter-murach/tty-table
|
11
|
+
[coverage]: https://coveralls.io/r/peter-murach/tty-table
|
12
|
+
|
13
|
+
> A flexible and intuitive table generator.
|
14
|
+
|
15
|
+
**TTY::Table** provides independent table formatting component for [TTY](https://github.com/peter-murach/tty) toolkit.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'tty-table'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install tty-table
|
32
|
+
|
33
|
+
## Contents
|
34
|
+
|
35
|
+
* [1. Usage](#1-usage)
|
36
|
+
* [2. Interface](#2-interface)
|
37
|
+
* [2.1 Initializaation](#21-initialization)
|
38
|
+
* [2.2 Rendering](#22-rendering)
|
39
|
+
* [2.3 Multiline](#23-multiline)
|
40
|
+
* [2.4 Border](#24-border)
|
41
|
+
* [2.5 Alignment](#25-alignment)
|
42
|
+
* [2.6 Padding](#26-padding)
|
43
|
+
* [2.7 Filter](#27-filter)
|
44
|
+
* [2.8 Width](#28-width)
|
45
|
+
|
46
|
+
## 1. Usage
|
47
|
+
|
48
|
+
First, provide **TTY::Table** with headers and data:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
|
52
|
+
```
|
53
|
+
|
54
|
+
Then simply call `render` or `to_s` on the instance:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
table.render
|
58
|
+
# =>
|
59
|
+
+-------+-------+
|
60
|
+
|header1|header2|
|
61
|
+
+-------+-------+
|
62
|
+
|a1 |a2 |
|
63
|
+
+-------+-------+
|
64
|
+
|b1 |b2 |
|
65
|
+
+-------+-------+
|
66
|
+
```
|
67
|
+
|
68
|
+
## 2. Interface
|
69
|
+
|
70
|
+
### 2.1 Initialization
|
71
|
+
|
72
|
+
To instantiate **TTY::Table** pass 2-dimensional array:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
table = TTY::Table[['a1', 'a2'], ['b1', 'b2']]
|
76
|
+
table = TTY::Table.new [['a1', 'a2'], ['b1', 'b2']]
|
77
|
+
table = TTY::Table.new rows: [['a1', 'a2'], ['b1', 'b2']]
|
78
|
+
table = TTY::Table.new ['h1', 'h2'], [['a1', 'a2'], ['b1', 'b2']]
|
79
|
+
table = TTY::Table.new header: ['h1', 'h2'], rows: [['a1', 'a2'], ['b1', 'b2']]
|
80
|
+
```
|
81
|
+
|
82
|
+
or cross header with rows inside a hash like so
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
|
86
|
+
```
|
87
|
+
|
88
|
+
Table behaves like an Array so `<<`, `each` and familiar methods can be used
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
table << ['a1', 'a2', 'a3']
|
92
|
+
table << ['b1', 'b2', 'b3']
|
93
|
+
table << ['a1', 'a2'] << ['b1', 'b2'] # chain rows assignment
|
94
|
+
|
95
|
+
table.each { |row| ... } # iterate over rows
|
96
|
+
table.each_with_index # iterate over each element with row and column index
|
97
|
+
table[i, j] # return element at row(i) and column(j)
|
98
|
+
table.row(i) { ... } # return array for row(i)
|
99
|
+
table.column(j) { ... } # return array for column(j)
|
100
|
+
table.column(name) # return array for column(name), name of header
|
101
|
+
table.row_size # return row size
|
102
|
+
table.column_size # return column size
|
103
|
+
table.size # return an array of [row_size, column_size]
|
104
|
+
table.border # specify border properties
|
105
|
+
```
|
106
|
+
|
107
|
+
or pass your rows in a block
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
table = TTY::Table.new do |t|
|
111
|
+
t << ['a1', 'a2', 'a3']
|
112
|
+
t << ['b1', 'b2', 'b3']
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
### 2.2 Rendering
|
117
|
+
|
118
|
+
Once you have an instance of `TTY::Table` you can print it out to the stdout like so:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
table.to_s
|
122
|
+
|
123
|
+
a1 a2 a3
|
124
|
+
b1 b2 b3
|
125
|
+
```
|
126
|
+
|
127
|
+
This will use so called `basic` renderer with default options.
|
128
|
+
|
129
|
+
However, you can include other customization options such as
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
border # hash of border properties out of :characters, :style, :separator keys
|
133
|
+
border_class # a type of border to use
|
134
|
+
column_widths # array of maximum columns widths
|
135
|
+
column_aligns # array of cell alignments out of :left, :center and :right, default :left
|
136
|
+
filter # a proc object that is applied to every field in a row
|
137
|
+
indent # indentation applied to rendered table
|
138
|
+
multiline # if true will wrap text at new line or column width,
|
139
|
+
# when false will escape special characters
|
140
|
+
orientation # either :horizontal or :vertical
|
141
|
+
padding # array of integers to set table fields padding
|
142
|
+
renderer # enforce display type out of :basic, :color, :unicode, :ascii
|
143
|
+
resize # if true will expand/shrink table column sizes to match the width,
|
144
|
+
# otherwise if false rotate table vertically
|
145
|
+
width # constrain the table total width, otherwise dynamically
|
146
|
+
# calculated from content and terminal size
|
147
|
+
```
|
148
|
+
|
149
|
+
### 2.3 Multiline
|
150
|
+
|
151
|
+
Renderer options may include `multiline` parameter. The `true` value will cause the table fields wrap at their natural line breaks or in case when the column widths are set the content will wrap.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
table = TTY::Table.new [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
|
155
|
+
table.render :ascii, multiline: true
|
156
|
+
# =>
|
157
|
+
+-------+-+
|
158
|
+
|First |1|
|
159
|
+
|Multi |2|
|
160
|
+
|Line | |
|
161
|
+
|Content| |
|
162
|
+
|Third |3|
|
163
|
+
+-------+-+
|
164
|
+
```
|
165
|
+
|
166
|
+
When the `false` option is specified all the special characters will be escaped and if the column widths are set the content will be truncated like so
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
table = TTY::Table.new [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
|
170
|
+
table.render :ascii, multiline: false
|
171
|
+
# =>
|
172
|
+
+------------------+-+
|
173
|
+
|First |1|
|
174
|
+
|Multiline\nContent|2|
|
175
|
+
|Third |3|
|
176
|
+
+------------------+-+
|
177
|
+
```
|
178
|
+
|
179
|
+
### 2.4 Border
|
180
|
+
|
181
|
+
To print border around data table you need to specify `renderer` type out of `basic`, `ascii`, `unicode`. By default `basic` is used. For instance, to output unicode border:
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
185
|
+
table.render :unicode
|
186
|
+
# =>
|
187
|
+
┌───────┬───────┐
|
188
|
+
│header1│header2│
|
189
|
+
├───────┼───────┤
|
190
|
+
│a1 │a2 │
|
191
|
+
│b1 │b2 │
|
192
|
+
└───────┴───────┘
|
193
|
+
```
|
194
|
+
|
195
|
+
You can also create your own custom border by subclassing `TTY::Table::Border` and implementing the `def_border` method using internal DSL methods like so:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
class MyBorder < TTY::Table::Border
|
199
|
+
def_border do
|
200
|
+
left '$'
|
201
|
+
center '$'
|
202
|
+
right '$'
|
203
|
+
bottom ' '
|
204
|
+
bottom_mid '*'
|
205
|
+
bottom_left '*'
|
206
|
+
bottom_right '*'
|
207
|
+
end
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
Next pass the border class to your table instance `render_with` method
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
215
|
+
table.render_with MyBorder
|
216
|
+
# =>
|
217
|
+
$header1$header2$
|
218
|
+
$a1 $a2 $
|
219
|
+
* * *
|
220
|
+
```
|
221
|
+
|
222
|
+
Finally, if you want to introduce slight modifications to the predefined border types, you can use table `border` helper like so
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
226
|
+
table.render do |renderer|
|
227
|
+
renderer.border do
|
228
|
+
mid '='
|
229
|
+
mid_mid ' '
|
230
|
+
end
|
231
|
+
end
|
232
|
+
# =>
|
233
|
+
header1 header2
|
234
|
+
======= =======
|
235
|
+
a1 a2
|
236
|
+
b1 b2
|
237
|
+
```
|
238
|
+
|
239
|
+
In addition to specifying border characters you can force table to render separator line on each row like:
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']]
|
243
|
+
table.render do |renderer|
|
244
|
+
renderer.border.separator = :each_row
|
245
|
+
end
|
246
|
+
# =>
|
247
|
+
+-------+-------+
|
248
|
+
|header1|header2|
|
249
|
+
+-------+-------+
|
250
|
+
|a1 |a2 |
|
251
|
+
+-------+-------+
|
252
|
+
|b1 |b2 |
|
253
|
+
+-------+-------+
|
254
|
+
```
|
255
|
+
|
256
|
+
Also to change the display color of your border do:
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
table.render do |renderer|
|
260
|
+
renderer.border.style = :red
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
264
|
+
### 2.5 Alignment
|
265
|
+
|
266
|
+
All columns are left aligned by default. You can enforce per column alignment by passing `column_aligns` option like so
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
rows = [['a1', 'a2'], ['b1', 'b2']
|
270
|
+
table = TTY::Table.new rows: rows
|
271
|
+
table.render column_aligns: [:center, :right]
|
272
|
+
```
|
273
|
+
|
274
|
+
To align a single column do
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
table.align_column(1, :right)
|
278
|
+
```
|
279
|
+
|
280
|
+
If you require a more granular alignment you can align individual fields in a row by passing `align` option
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
table = TTY::Table.new do |t|
|
284
|
+
t << ['a1', 'a2', 'a3']
|
285
|
+
t << ['b1', {:value => 'b2', :align => :right}, 'b3']
|
286
|
+
t << ['c1', 'c2', {:value => 'c3', :align => :center}]
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
#### 2.6 Padding
|
291
|
+
|
292
|
+
By default padding is not applied. You can add `padding` to table fields like so
|
293
|
+
|
294
|
+
```ruby
|
295
|
+
header = ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra']
|
296
|
+
rows = [['id', 'int(11)', 'YES', 'nil', 'NULL', '']]
|
297
|
+
table = TTY::Table.new(header, rows)
|
298
|
+
table.render { |renderer| renderer.padding= [0,1,0,1] }
|
299
|
+
# =>
|
300
|
+
+-------+---------+------+-----+---------+-------+
|
301
|
+
| Field | Type | Null | Key | Default | Extra |
|
302
|
+
+-------+---------+------+-----+---------+-------+
|
303
|
+
| id | int(11) | YES | nil | NULL | |
|
304
|
+
+-------+---------+------+-----+---------+-------+
|
305
|
+
```
|
306
|
+
|
307
|
+
or you can set specific padding using `right`, `left`, `top`, `bottom` helpers. However, when adding top or bottom padding a `multiline` option needs to be set to `true` to allow for rows to span multiple lines. For example
|
308
|
+
|
309
|
+
```ruby
|
310
|
+
table.render { |renderer|
|
311
|
+
renderer.multiline = true
|
312
|
+
renderer.padding.top = 1
|
313
|
+
}
|
314
|
+
# =>
|
315
|
+
+-----+-------+----+---+-------+-----+
|
316
|
+
| | | | | | |
|
317
|
+
|Field|Type |Null|Key|Default|Extra|
|
318
|
+
+-----+-------+----+---+-------+-----+
|
319
|
+
| | | | | | |
|
320
|
+
|id |int(11)|YES |nil|NULL | |
|
321
|
+
+-----+-------+----+---+-------+-----+
|
322
|
+
```
|
323
|
+
|
324
|
+
### 2.7 Filter
|
325
|
+
|
326
|
+
You can define filters that will modify individual table fields value before they are rendered. A filter can be a callable such as proc. Here's an example that formats
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
|
330
|
+
table.render do |renderer|
|
331
|
+
renderer.filter = Proc.new do |val, row_index, col_index|
|
332
|
+
if col_index == 1 and !(row_index == 0)
|
333
|
+
val.capitalize
|
334
|
+
else
|
335
|
+
val
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
339
|
+
# =>
|
340
|
+
+-------+-------+
|
341
|
+
|header1|header2|
|
342
|
+
+-------+-------+
|
343
|
+
|a1 |A2 |
|
344
|
+
+-------+-------+
|
345
|
+
|b1 |B2 |
|
346
|
+
+-------+-------+
|
347
|
+
```
|
348
|
+
|
349
|
+
To color even fields red on green background add filter like so
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
table.render do |renderer|
|
353
|
+
renderer.filter = proc do |val, row_index, col_index|
|
354
|
+
col_index % 2 == 1 ? TTY.color.set(val, :red, :on_green) : val
|
355
|
+
end
|
356
|
+
end
|
357
|
+
```
|
358
|
+
|
359
|
+
### 2.8 Width
|
360
|
+
|
361
|
+
To control table's column sizes pass `width`, `resize` options. By default table's natural column widths are calculated from the content. If the total table width does not fit in terminal window then the table is rotated vertically to preserve content.
|
362
|
+
|
363
|
+
The `resize` property will force the table to expand/shrink to match the terminal width or custom `width`. On its own the `width` property will not resize table but only enforce table vertical rotation if content overspills.
|
364
|
+
|
365
|
+
```ruby
|
366
|
+
header = ['h1', 'h2', 'h3']
|
367
|
+
rows = [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']]
|
368
|
+
table = TTY::Table.new header, rows
|
369
|
+
table.render width: 80, resize: true
|
370
|
+
# =>
|
371
|
+
+---------+-------+------------+
|
372
|
+
|h1 |h2 |h3 |
|
373
|
+
+---------+-------+------------+
|
374
|
+
|aaa1 |aa2 |aaaaaaa3 |
|
375
|
+
|b1 |b2 |b3 |
|
376
|
+
+---------+-------+------------+
|
377
|
+
```
|
378
|
+
|
379
|
+
## Contributing
|
380
|
+
|
381
|
+
1. Fork it ( https://github.com/[my-github-username]/tty-table/fork )
|
382
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
383
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
384
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
385
|
+
5. Create a new Pull Request
|
386
|
+
|
387
|
+
## Copyright
|
388
|
+
|
389
|
+
Copyright (c) 2015 Piotr Murach. See LICENSE for further details.
|
data/Rakefile
ADDED
data/benchmarks/speed.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
# Benchmark speed of table operations
|
4
|
+
|
5
|
+
require 'tty-table'
|
6
|
+
require 'benchmark'
|
7
|
+
require 'benchmark/ips'
|
8
|
+
|
9
|
+
header = [:name, :color]
|
10
|
+
rows = (1..100).map { |n| ["row#{n}", "red"] }
|
11
|
+
table = TTY::Table.new(header, rows)
|
12
|
+
table_ascii = TTY::Table.new(header, rows, :renderer => :ascii)
|
13
|
+
table_unicode = TTY::Table.new(header, rows, :renderer => :unicode)
|
14
|
+
table_color = TTY::Table.new(header, rows, :renderer => :ascii, :border => { :style => :red })
|
15
|
+
|
16
|
+
Benchmark.ips do |r|
|
17
|
+
r.report("Ruby #to_s") do
|
18
|
+
rows.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
r.report("TTY #to_s") do
|
22
|
+
table.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
r.report("TTY ASCII #to_s") do
|
26
|
+
table_ascii.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
r.report("TTY Unicode #to_s") do
|
30
|
+
table_unicode.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
r.report("TTY Color #to_s") do
|
34
|
+
table_color.to_s
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
class Border
|
6
|
+
# A class that represents an ascii border.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class ASCII < Border
|
10
|
+
|
11
|
+
def_border do
|
12
|
+
top '-'
|
13
|
+
top_mid '+'
|
14
|
+
top_left '+'
|
15
|
+
top_right '+'
|
16
|
+
bottom '-'
|
17
|
+
bottom_mid '+'
|
18
|
+
bottom_left '+'
|
19
|
+
bottom_right '+'
|
20
|
+
mid '-'
|
21
|
+
mid_mid '+'
|
22
|
+
mid_left '+'
|
23
|
+
mid_right '+'
|
24
|
+
left '|'
|
25
|
+
center '|'
|
26
|
+
right '|'
|
27
|
+
end
|
28
|
+
|
29
|
+
end # ASCII
|
30
|
+
end # Border
|
31
|
+
end # Table
|
32
|
+
end # TTY
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
class Border
|
6
|
+
# A class that represents no border.
|
7
|
+
class Null < Border
|
8
|
+
|
9
|
+
def_border do
|
10
|
+
center SPACE_CHAR
|
11
|
+
end
|
12
|
+
|
13
|
+
# A stub top line
|
14
|
+
#
|
15
|
+
# @api private
|
16
|
+
def top_line
|
17
|
+
border ? super : nil
|
18
|
+
end
|
19
|
+
|
20
|
+
# A stub separator line
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
def separator
|
24
|
+
return [] if border.separator == EACH_ROW
|
25
|
+
border ? super : nil
|
26
|
+
end
|
27
|
+
|
28
|
+
# A stub bottom line
|
29
|
+
#
|
30
|
+
# @api private
|
31
|
+
def bottom_line
|
32
|
+
border ? super : nil
|
33
|
+
end
|
34
|
+
end # Null
|
35
|
+
end # Border
|
36
|
+
end # Table
|
37
|
+
end # TTY
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
class Border
|
6
|
+
# A class for a table row line chars manipulation
|
7
|
+
class RowLine < Struct.new(:left, :center, :right)
|
8
|
+
# Colorize characters with a given style
|
9
|
+
#
|
10
|
+
# @api public
|
11
|
+
def colorize(border, style)
|
12
|
+
colorized_chars = border.set_color(style, right, center, left)
|
13
|
+
self.right, self.center, self.left = colorized_chars
|
14
|
+
end
|
15
|
+
end # RowLine
|
16
|
+
end # Border
|
17
|
+
end # Table
|
18
|
+
end # TTY
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
class Border
|
6
|
+
# A class that represents a unicode border.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Unicode < Border
|
10
|
+
|
11
|
+
def_border do
|
12
|
+
top '─'
|
13
|
+
top_mid '┬'
|
14
|
+
top_left '┌'
|
15
|
+
top_right '┐'
|
16
|
+
bottom '─'
|
17
|
+
bottom_mid '┴'
|
18
|
+
bottom_left '└'
|
19
|
+
bottom_right '┘'
|
20
|
+
mid '─'
|
21
|
+
mid_mid '┼'
|
22
|
+
mid_left '├'
|
23
|
+
mid_right '┤'
|
24
|
+
left '│'
|
25
|
+
center '│'
|
26
|
+
right '│'
|
27
|
+
end
|
28
|
+
|
29
|
+
end # Unicode
|
30
|
+
end # Border
|
31
|
+
end # Table
|
32
|
+
end # TTY
|