tty-table 0.7.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +57 -0
- data/LICENSE.txt +1 -1
- data/README.md +165 -94
- data/lib/tty-table.rb +1 -5
- data/lib/tty/table.rb +102 -42
- data/lib/tty/table/alignment_set.rb +3 -6
- data/lib/tty/table/border.rb +57 -68
- data/lib/tty/table/border/ascii.rb +17 -17
- data/lib/tty/table/border/null.rb +20 -22
- data/lib/tty/table/border/row_line.rb +4 -3
- data/lib/tty/table/border/unicode.rb +17 -17
- data/lib/tty/table/border_dsl.rb +28 -25
- data/lib/tty/table/border_options.rb +50 -25
- data/lib/tty/table/column_constraint.rb +29 -17
- data/lib/tty/table/{column_set.rb → columns.rb} +44 -69
- data/lib/tty/table/empty.rb +2 -0
- data/lib/tty/table/error.rb +1 -1
- data/lib/tty/table/field.rb +43 -35
- data/lib/tty/table/header.rb +7 -5
- data/lib/tty/table/indentation.rb +16 -25
- data/lib/tty/table/operation/alignment.rb +10 -8
- data/lib/tty/table/operation/escape.rb +2 -3
- data/lib/tty/table/operation/filter.rb +1 -1
- data/lib/tty/table/operation/padding.rb +4 -12
- data/lib/tty/table/operation/truncation.rb +4 -4
- data/lib/tty/table/operation/wrapped.rb +4 -4
- data/lib/tty/table/operations.rb +4 -15
- data/lib/tty/table/options.rb +2 -4
- data/lib/tty/table/orientation.rb +5 -5
- data/lib/tty/table/orientation/horizontal.rb +1 -1
- data/lib/tty/table/orientation/vertical.rb +1 -1
- data/lib/tty/table/renderer.rb +40 -35
- data/lib/tty/table/renderer/ascii.rb +4 -4
- data/lib/tty/table/renderer/basic.rb +85 -88
- data/lib/tty/table/renderer/unicode.rb +3 -3
- data/lib/tty/table/row.rb +10 -8
- data/lib/tty/table/transformation.rb +2 -2
- data/lib/tty/table/validatable.rb +25 -7
- data/lib/tty/table/version.rb +3 -3
- metadata +33 -283
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.travis.yml +0 -24
- data/CODE_OF_CONDUCT.md +0 -49
- data/Gemfile +0 -18
- data/Rakefile +0 -8
- data/benchmarks/speed.rb +0 -41
- data/spec/spec_helper.rb +0 -50
- data/spec/unit/access_spec.rb +0 -86
- data/spec/unit/add_row_spec.rb +0 -28
- data/spec/unit/alignment_set/each_spec.rb +0 -17
- data/spec/unit/alignment_set/new_spec.rb +0 -27
- data/spec/unit/alignment_set/to_ary_spec.rb +0 -14
- data/spec/unit/alignment_spec.rb +0 -71
- data/spec/unit/border/ascii/rendering_spec.rb +0 -90
- data/spec/unit/border/new_spec.rb +0 -27
- data/spec/unit/border/null/rendering_spec.rb +0 -130
- data/spec/unit/border/options/from_spec.rb +0 -38
- data/spec/unit/border/options/new_spec.rb +0 -14
- data/spec/unit/border/unicode/rendering_spec.rb +0 -63
- data/spec/unit/border_options/new_spec.rb +0 -20
- data/spec/unit/border_options/update_spec.rb +0 -18
- data/spec/unit/column_constraint/enforce_spec.rb +0 -70
- data/spec/unit/column_constraint/widths_spec.rb +0 -35
- data/spec/unit/column_set/extract_widths_spec.rb +0 -48
- data/spec/unit/column_set/total_width_spec.rb +0 -15
- data/spec/unit/column_set/widths_from_spec.rb +0 -51
- data/spec/unit/data_spec.rb +0 -12
- data/spec/unit/each_spec.rb +0 -26
- data/spec/unit/each_with_index_spec.rb +0 -51
- data/spec/unit/empty_spec.rb +0 -23
- data/spec/unit/eql_spec.rb +0 -34
- data/spec/unit/field/equality_spec.rb +0 -51
- data/spec/unit/field/length_spec.rb +0 -35
- data/spec/unit/field/lines_spec.rb +0 -21
- data/spec/unit/field/new_spec.rb +0 -29
- data/spec/unit/field/width_spec.rb +0 -23
- data/spec/unit/filter_spec.rb +0 -22
- data/spec/unit/header/call_spec.rb +0 -30
- data/spec/unit/header/color_spec.rb +0 -19
- data/spec/unit/header/equality_spec.rb +0 -51
- data/spec/unit/header/height_spec.rb +0 -27
- data/spec/unit/header/new_spec.rb +0 -16
- data/spec/unit/header/set_spec.rb +0 -20
- data/spec/unit/header/to_ary_spec.rb +0 -14
- data/spec/unit/header_spec.rb +0 -13
- data/spec/unit/indentation/indent_spec.rb +0 -21
- data/spec/unit/new_spec.rb +0 -73
- data/spec/unit/operation/alignment/call_spec.rb +0 -39
- data/spec/unit/operation/escape/call_spec.rb +0 -15
- data/spec/unit/operation/filter/call_spec.rb +0 -16
- data/spec/unit/operation/truncation/call_spec.rb +0 -30
- data/spec/unit/operation/wrapped/call_spec.rb +0 -40
- data/spec/unit/operations/new_spec.rb +0 -30
- data/spec/unit/options/access_spec.rb +0 -14
- data/spec/unit/options_spec.rb +0 -25
- data/spec/unit/orientation_spec.rb +0 -145
- data/spec/unit/padding_spec.rb +0 -117
- data/spec/unit/properties_spec.rb +0 -25
- data/spec/unit/render_repeat_spec.rb +0 -42
- data/spec/unit/render_spec.rb +0 -63
- data/spec/unit/render_with_spec.rb +0 -106
- data/spec/unit/renderer/ascii/coloring_spec.rb +0 -70
- data/spec/unit/renderer/ascii/indentation_spec.rb +0 -41
- data/spec/unit/renderer/ascii/multiline_spec.rb +0 -101
- data/spec/unit/renderer/ascii/padding_spec.rb +0 -88
- data/spec/unit/renderer/ascii/render_spec.rb +0 -68
- data/spec/unit/renderer/ascii/resizing_spec.rb +0 -114
- data/spec/unit/renderer/ascii/separator_spec.rb +0 -28
- data/spec/unit/renderer/basic/alignment_spec.rb +0 -88
- data/spec/unit/renderer/basic/coloring_spec.rb +0 -61
- data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -28
- data/spec/unit/renderer/basic/filter_spec.rb +0 -53
- data/spec/unit/renderer/basic/indentation_spec.rb +0 -48
- data/spec/unit/renderer/basic/multiline_spec.rb +0 -74
- data/spec/unit/renderer/basic/new_spec.rb +0 -26
- data/spec/unit/renderer/basic/options_spec.rb +0 -52
- data/spec/unit/renderer/basic/padding_spec.rb +0 -76
- data/spec/unit/renderer/basic/render_spec.rb +0 -57
- data/spec/unit/renderer/basic/resizing_spec.rb +0 -96
- data/spec/unit/renderer/basic/separator_spec.rb +0 -43
- data/spec/unit/renderer/basic/truncation_spec.rb +0 -35
- data/spec/unit/renderer/basic/wrapping_spec.rb +0 -40
- data/spec/unit/renderer/border_spec.rb +0 -104
- data/spec/unit/renderer/render_spec.rb +0 -36
- data/spec/unit/renderer/select_spec.rb +0 -22
- data/spec/unit/renderer/unicode/coloring_spec.rb +0 -70
- data/spec/unit/renderer/unicode/indentation_spec.rb +0 -41
- data/spec/unit/renderer/unicode/padding_spec.rb +0 -61
- data/spec/unit/renderer/unicode/render_spec.rb +0 -68
- data/spec/unit/renderer/unicode/separator_spec.rb +0 -26
- data/spec/unit/renderer_spec.rb +0 -19
- data/spec/unit/rotate_spec.rb +0 -86
- data/spec/unit/row/access_spec.rb +0 -25
- data/spec/unit/row/call_spec.rb +0 -45
- data/spec/unit/row/data_spec.rb +0 -26
- data/spec/unit/row/each_spec.rb +0 -31
- data/spec/unit/row/equality_spec.rb +0 -73
- data/spec/unit/row/height_spec.rb +0 -27
- data/spec/unit/row/new_spec.rb +0 -41
- data/spec/unit/row/to_ary_spec.rb +0 -14
- data/spec/unit/to_s_spec.rb +0 -56
- data/spec/unit/transformation/extract_tuples_spec.rb +0 -35
- data/spec/unit/utf_spec.rb +0 -33
- data/spec/unit/validatable/validate_options_spec.rb +0 -33
- data/spec/unit/validatable_spec.rb +0 -32
- data/tasks/console.rake +0 -10
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-table.gemspec +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d1de8ba17e3ea8c4b575292757c2d9908abbbeec7511fda9692b4718bb423fbd
|
4
|
+
data.tar.gz: 5208f9766c7309b44f97b54282b6cd9bb7d5bf8966fdd8db337bd653e65696af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7547f09416d4e89f7292c6505554c3bbde7f6b83531acbad831a8e0ba96012c2e806611f9a0162e97610e3ed85c98d67b3b96f936e279546145d44ae74cbdbf2
|
7
|
+
data.tar.gz: 3a7daadb0f83bd72dc99c3992b1b9f82684714b20e9bcb55b16bb8a3eee87cd3e2c88bd175eff5af5d7dc5f270cc82e386979fd90cd4d3ec06e6ac6af73c14fd
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,57 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.12.0] - 2020-09-20
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Optimize adding rows by Tim Craft(@timcraft)
|
7
|
+
* Remove the equatable and necromancer dependencies
|
8
|
+
* Update the pastel, tty-screen and strings dependencies
|
9
|
+
* Change Border to use BorderDSL exclusively for handling options
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
* Fix resize option raising an error for small terminal sizes by Katelyn Schiesser(@slowbro)
|
13
|
+
* Fix Table::Indentation to check for map method support by @deemytch
|
14
|
+
|
15
|
+
## [v0.11.0] - 2019-08-01
|
16
|
+
|
17
|
+
### Add
|
18
|
+
* Add ability to specify border separator as an Array, Proc value by Mitch VanDuyn(@catmando)
|
19
|
+
|
20
|
+
### Changed
|
21
|
+
* Change Indentation#indent to stop mutating input
|
22
|
+
* Change gemspec to load files directly
|
23
|
+
* Change to freeze strings
|
24
|
+
* Change to relax constraints on tty-screen & equatable
|
25
|
+
* Change to remove upper boundary on bundler dev dependency
|
26
|
+
|
27
|
+
## [v0.10.0] - 2018-02-18
|
28
|
+
|
29
|
+
### Changed
|
30
|
+
* Change to limit Ruby version to 2.0
|
31
|
+
* Change to use strings instead of verse
|
32
|
+
* Change Field to rely on Strings#display_width
|
33
|
+
* Change Operations to stop accepting table at initialization
|
34
|
+
* Change Operations#run_operations to #apply_to
|
35
|
+
* Change Border to remove padding configuration
|
36
|
+
* Change ColumnSet class to Columns stateless module
|
37
|
+
* Change Indentation class to stateless module
|
38
|
+
|
39
|
+
### Fixed
|
40
|
+
* Fix :resize option to honour :padding values
|
41
|
+
|
42
|
+
## [v0.9.0] - 2017-11-04
|
43
|
+
|
44
|
+
### Changed
|
45
|
+
* Change ColumnSet class to Columns
|
46
|
+
* Change gemset to require Ruby >= 2.0.0
|
47
|
+
* Change to update tty-screen dependency
|
48
|
+
|
49
|
+
## [v0.8.0] - 2017-02-27
|
50
|
+
|
51
|
+
### Changed
|
52
|
+
* Change necromancer dependency to fix Ruby 2.4.0
|
53
|
+
* Change to use relative paths to load files
|
54
|
+
|
3
55
|
## [v0.7.0] - 2017-01-15
|
4
56
|
|
5
57
|
### Changed
|
@@ -63,6 +115,11 @@
|
|
63
115
|
|
64
116
|
* Initial implementation and release
|
65
117
|
|
118
|
+
[v0.12.0]: https://github.com/piotrmurach/tty-table/compare/v0.11.0...v0.12.0
|
119
|
+
[v0.11.0]: https://github.com/piotrmurach/tty-table/compare/v0.10.0...v0.11.0
|
120
|
+
[v0.10.0]: https://github.com/piotrmurach/tty-table/compare/v0.9.0...v0.10.0
|
121
|
+
[v0.9.0]: https://github.com/piotrmurach/tty-table/compare/v0.8.0...v0.9.0
|
122
|
+
[v0.8.0]: https://github.com/piotrmurach/tty-table/compare/v0.7.0...v0.8.0
|
66
123
|
[v0.7.0]: https://github.com/piotrmurach/tty-table/compare/v0.6.0...v0.7.0
|
67
124
|
[v0.6.0]: https://github.com/piotrmurach/tty-table/compare/v0.5.0...v0.6.0
|
68
125
|
[v0.5.0]: https://github.com/piotrmurach/tty-table/compare/v0.4.0...v0.5.0
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<a href="https://ttytoolkit.org" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/blob/master/images/tty.png" alt="TTY Toolkit logo" /></a>
|
3
|
+
</div>
|
4
|
+
|
1
5
|
# TTY::Table [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
6
|
|
3
7
|
[![Gem Version](https://badge.fury.io/rb/tty-table.svg)][gem]
|
4
8
|
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-table.svg?branch=master)][travis]
|
9
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/8fxrbawbyomqd979?svg=true)][appveyor]
|
5
10
|
[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-table/badges/gpa.svg)][codeclimate]
|
6
11
|
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-table/badge.svg?branch=master)][coverage]
|
7
12
|
[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-table.svg?branch=master)][inchpages]
|
@@ -9,6 +14,7 @@
|
|
9
14
|
[gitter]: https://gitter.im/piotrmurach/tty
|
10
15
|
[gem]: http://badge.fury.io/rb/tty-table
|
11
16
|
[travis]: http://travis-ci.org/piotrmurach/tty-table
|
17
|
+
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-table
|
12
18
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-table
|
13
19
|
[coverage]: https://coveralls.io/github/piotrmurach/tty-table
|
14
20
|
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-table
|
@@ -30,7 +36,7 @@
|
|
30
36
|
Add this line to your application's Gemfile:
|
31
37
|
|
32
38
|
```ruby
|
33
|
-
gem
|
39
|
+
gem "tty-table"
|
34
40
|
```
|
35
41
|
|
36
42
|
And then execute:
|
@@ -52,9 +58,9 @@ Or install it yourself as:
|
|
52
58
|
* [3. Rendering](#3-rendering)
|
53
59
|
* [3.1 Render](#31-render)
|
54
60
|
* [3.2 Renderer](#32-renderer)
|
55
|
-
* [3.
|
56
|
-
* [3.
|
57
|
-
* [3.
|
61
|
+
* [3.2.1 Basic renderer](#321-basic-renderer)
|
62
|
+
* [3.2.2 ASCII renderer](#322-ascii-renderer)
|
63
|
+
* [3.2.3 Unicode renderer](#323-unicode-renderer)
|
58
64
|
* [3.3 Options](#33-options)
|
59
65
|
* [3.4 Alignment](#34-alignment)
|
60
66
|
* [3.5 Border](#35-border)
|
@@ -70,16 +76,16 @@ Or install it yourself as:
|
|
70
76
|
|
71
77
|
## 1. Usage
|
72
78
|
|
73
|
-
First, provide **TTY::Table** with headers and
|
79
|
+
First, provide **TTY::Table** with data, for example, two headers and two rows:
|
74
80
|
|
75
81
|
```ruby
|
76
|
-
table = TTY::Table.new
|
82
|
+
table = TTY::Table.new(["header1","header2"], [["a1", "a2"], ["b1", "b2"]])
|
77
83
|
```
|
78
84
|
|
79
|
-
Then
|
85
|
+
Then to print to the console, call the `render` method with border type as a first argument:
|
80
86
|
|
81
87
|
```ruby
|
82
|
-
table.render(:ascii)
|
88
|
+
puts table.render(:ascii)
|
83
89
|
# =>
|
84
90
|
# +-------+-------+
|
85
91
|
# |header1|header2|
|
@@ -97,39 +103,39 @@ table.render(:ascii)
|
|
97
103
|
**TTY::Table** can be created in variety of ways. The easiest way is to pass 2-dimensional array:
|
98
104
|
|
99
105
|
```ruby
|
100
|
-
table = TTY::Table[[
|
101
|
-
table = TTY::Table.new
|
102
|
-
table = TTY::Table.new
|
106
|
+
table = TTY::Table[["a1", "a2"], ["b1", "b2"]]
|
107
|
+
table = TTY::Table.new([["a1", "a2"], ["b1", "b2"]])
|
108
|
+
table = TTY::Table.new(rows: [["a1", "a2"], ["b1", "b2"]])
|
103
109
|
```
|
104
110
|
|
105
|
-
Alternatively you can specify rows one by one inside block:
|
111
|
+
Alternatively you can specify the rows one by one inside a block:
|
106
112
|
|
107
113
|
```ruby
|
108
114
|
table = TTY::Table.new do |t|
|
109
|
-
t << [
|
110
|
-
t << [
|
115
|
+
t << ["a1", "a2"]
|
116
|
+
t << ["b1", "b2"]
|
111
117
|
end
|
112
118
|
```
|
113
119
|
|
114
|
-
You can add rows of data after initialization:
|
120
|
+
You can add rows of data after initialization with `<<` operator:
|
115
121
|
|
116
122
|
```ruby
|
117
123
|
table = TTY::Table.new
|
118
|
-
table << [
|
119
|
-
table << [
|
124
|
+
table << ["a1","a2"]
|
125
|
+
table << ["b1","b2"]
|
120
126
|
```
|
121
127
|
|
122
|
-
In addition to rows you can specify table header:
|
128
|
+
In addition to rows, you can specify table header:
|
123
129
|
|
124
130
|
```ruby
|
125
|
-
table = TTY::Table.new
|
126
|
-
table = TTY::Table.new
|
131
|
+
table = TTY::Table.new(["h1", "h2"], [["a1", "a2"], ["b1", "b2"]])
|
132
|
+
table = TTY::Table.new(header: ["h1", "h2"], rows: [["a1", "a2"], ["b1", "b2"]])
|
127
133
|
```
|
128
134
|
|
129
|
-
|
135
|
+
You can also mix header with rows inside a hash like so:
|
130
136
|
|
131
137
|
```ruby
|
132
|
-
table = TTY::Table.new
|
138
|
+
table = TTY::Table.new([{"h1" => ["a1", "a2"], "h2" => ["b1", "b2"]}])
|
133
139
|
```
|
134
140
|
|
135
141
|
### 2.2 Iteration
|
@@ -137,9 +143,9 @@ table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
|
|
137
143
|
Table behaves like an Array so `<<`, `each` and familiar methods can be used:
|
138
144
|
|
139
145
|
```ruby
|
140
|
-
table << [
|
141
|
-
table << [
|
142
|
-
table << [
|
146
|
+
table << ["a1", "a2", "a3"]
|
147
|
+
table << ["b1", "b2", "b3"]
|
148
|
+
table << ["a1", "a2"] << ["b1", "b2"] # chain rows assignment
|
143
149
|
```
|
144
150
|
|
145
151
|
In order to iterate over table rows including headers do:
|
@@ -151,33 +157,33 @@ table.each_with_index { |row, index| ... } # iterate over rows with an index
|
|
151
157
|
|
152
158
|
### 2.3 Access
|
153
159
|
|
154
|
-
In order to
|
160
|
+
In order to reference the row at `index` do:
|
155
161
|
|
156
162
|
```ruby
|
157
|
-
table = TTY::Table.new [[
|
158
|
-
table[0] # => [
|
159
|
-
table.row(0) # => [
|
163
|
+
table = TTY::Table.new [["a1","a2"], ["b1","b2"]]
|
164
|
+
table[0] # => ["a1","a2"]
|
165
|
+
table.row(0) # => ["a1","a2"]
|
160
166
|
table.row(i) { |row| ... } # return array for row(i)
|
161
167
|
```
|
162
168
|
|
163
169
|
Negative indices count backwards from the end of table data (`-1` is the last element):
|
164
170
|
|
165
171
|
```ruby
|
166
|
-
table[-1] # => [
|
172
|
+
table[-1] # => ["b1","b2"]
|
167
173
|
```
|
168
174
|
|
169
175
|
To reference element at given row(i) and column(j) do:
|
170
176
|
|
171
177
|
```ruby
|
172
178
|
table[i, j] # return element at row(i) and column(j)
|
173
|
-
table[0,0] # =>
|
179
|
+
table[0,0] # => "a1"
|
174
180
|
```
|
175
181
|
|
176
182
|
To specifically reference column(j) do:
|
177
183
|
|
178
184
|
```ruby
|
179
185
|
table.column(j) { ... } # return array for column(j)
|
180
|
-
table.column(0) # => [
|
186
|
+
table.column(0) # => ["a1","b1"]
|
181
187
|
table.column(name) # return array for column(name), name of header
|
182
188
|
```
|
183
189
|
|
@@ -204,7 +210,7 @@ table.size # return an array of [row_size, column_size]
|
|
204
210
|
Given a table:
|
205
211
|
|
206
212
|
```ruby
|
207
|
-
table = TTY::Table.new
|
213
|
+
table = TTY::Table.new(["header1","header2"], [["a1", "a2"], ["b1", "b2"]])
|
208
214
|
```
|
209
215
|
|
210
216
|
Once you have an instance of `TTY::Table` you can decorate the content using the `render` method. In order to display a basic whitespace delimited view do:
|
@@ -240,7 +246,7 @@ end
|
|
240
246
|
Given a table of data:
|
241
247
|
|
242
248
|
```ruby
|
243
|
-
table = TTY::Table.new [
|
249
|
+
table = TTY::Table.new ["header1","header2"], [["a1", "a2"], ["b1", "b2"]]
|
244
250
|
```
|
245
251
|
|
246
252
|
You can create a special renderer for it:
|
@@ -341,25 +347,16 @@ table.render(:unicode)
|
|
341
347
|
|
342
348
|
Rendering of **TTY-Table** includes numerous customization options:
|
343
349
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
# when false will escape special characters
|
355
|
-
padding # array of integers to set table fields padding,
|
356
|
-
# by default [0,0,0,0]
|
357
|
-
resize # if true will expand/shrink table column sizes to match
|
358
|
-
# the terminal width, otherwise if false will rotate
|
359
|
-
# table vertically. By default set to false
|
360
|
-
width # constrain the table total width, by default dynamically
|
361
|
-
# calculated based on content and terminal size
|
362
|
-
```
|
350
|
+
* `:alignments` - array of cell alignments out of `:left`, `:center` and `:righit`. Defaults to `:left`.
|
351
|
+
* `:border` - hash of border options out of `:characters`, `:style` and `:separator`
|
352
|
+
* `:border_class` - a type of border to use such as `TTY::Table::Border::Null`, `TTY::Table::Border::ASCII` and `TTY::Table::Border::Unicode`
|
353
|
+
* `:column_widths` - array of maximum column widths
|
354
|
+
* `:filter` - a `proc` object that is applied to every field in a row
|
355
|
+
* `:indent` - indentation applied to rendered table, by default 0
|
356
|
+
* `:multiline` - when `true` will wrap text at new line or column width, when `false` will escape special characters
|
357
|
+
* `:padding` - array of integers to set table fields padding. Defaults to `[0,0,0,0]`.
|
358
|
+
* `:resize` - when `true` will expand/shrink table column sizes to match the terminal width, otherwise when `false` will rotate table vertically. Defaults to `false`.
|
359
|
+
* `:width` - constrains the table total width. Defaults to value automatically calculated based on the content and terminal size.
|
363
360
|
|
364
361
|
The `render` method can accept as a second argument the above options either as hash value:
|
365
362
|
|
@@ -367,11 +364,11 @@ The `render` method can accept as a second argument the above options either as
|
|
367
364
|
table.render(:basic, alignments: [:left, :center])
|
368
365
|
```
|
369
366
|
|
370
|
-
|
367
|
+
Or inside a block as a property:
|
371
368
|
|
372
369
|
```ruby
|
373
370
|
table.render(:basic) do |renderer|
|
374
|
-
renderer.alignments= [:left, :center]
|
371
|
+
renderer.alignments = [:left, :center]
|
375
372
|
end
|
376
373
|
```
|
377
374
|
|
@@ -379,10 +376,10 @@ end
|
|
379
376
|
|
380
377
|
By default all columns are `:left` aligned.
|
381
378
|
|
382
|
-
You can align each column
|
379
|
+
You can align each column individually by passing `:alignments` option to table renderer:
|
383
380
|
|
384
381
|
```ruby
|
385
|
-
table.render
|
382
|
+
table.render(:ascii, alignments: [:center, :right])
|
386
383
|
# =>
|
387
384
|
# +-------+-------+
|
388
385
|
# |header1|header2|
|
@@ -392,10 +389,10 @@ table.render :ascii, alignments: [:center, :right]
|
|
392
389
|
# +-------+-------+
|
393
390
|
```
|
394
391
|
|
395
|
-
Alternatively you can align all columns with
|
392
|
+
Alternatively you can align all columns with `:alignment` option:
|
396
393
|
|
397
394
|
```ruby
|
398
|
-
table.render
|
395
|
+
table.render(:ascii, alignment: [:center])
|
399
396
|
# =>
|
400
397
|
# +-------+-------+
|
401
398
|
# |header1|header2|
|
@@ -408,9 +405,9 @@ table.render :ascii, alignment: [:center]
|
|
408
405
|
If you require a more granular alignment you can align individual fields in a row by passing `:alignment` option like so:
|
409
406
|
|
410
407
|
```ruby
|
411
|
-
table = TTY::Table.new
|
412
|
-
table << [{value:
|
413
|
-
table << [
|
408
|
+
table = TTY::Table.new(header: ["header1", "header2"])
|
409
|
+
table << [{value: "a1", alignment: :right}, "a2"]
|
410
|
+
table << ["b1", {value: "b2", alignment: :center}]
|
414
411
|
```
|
415
412
|
|
416
413
|
and then simply render:
|
@@ -431,7 +428,7 @@ table.render(:ascii)
|
|
431
428
|
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:
|
432
429
|
|
433
430
|
```ruby
|
434
|
-
table = TTY::Table.new [
|
431
|
+
table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]]
|
435
432
|
table.render :unicode
|
436
433
|
# =>
|
437
434
|
# ┌───────┬───────┐
|
@@ -474,11 +471,11 @@ The following are available border parts:
|
|
474
471
|
Using the above border parts you can create your own border with the `border` helper:
|
475
472
|
|
476
473
|
```ruby
|
477
|
-
table = TTY::Table.new [
|
474
|
+
table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]
|
478
475
|
table.render do |renderer|
|
479
476
|
renderer.border do
|
480
|
-
mid
|
481
|
-
mid_mid
|
477
|
+
mid "="
|
478
|
+
mid_mid " "
|
482
479
|
end
|
483
480
|
end
|
484
481
|
# =>
|
@@ -495,13 +492,13 @@ You can also create your own custom border by subclassing `TTY::Table::Border` a
|
|
495
492
|
```ruby
|
496
493
|
class MyBorder < TTY::Table::Border
|
497
494
|
def_border do
|
498
|
-
left
|
499
|
-
center
|
500
|
-
right
|
501
|
-
bottom
|
502
|
-
bottom_mid
|
503
|
-
bottom_left
|
504
|
-
bottom_right
|
495
|
+
left "$"
|
496
|
+
center "$"
|
497
|
+
right "$"
|
498
|
+
bottom " "
|
499
|
+
bottom_mid "*"
|
500
|
+
bottom_left "*"
|
501
|
+
bottom_right "*"
|
505
502
|
end
|
506
503
|
end
|
507
504
|
```
|
@@ -509,7 +506,7 @@ end
|
|
509
506
|
Next pass the border class to your table instance `render_with` method
|
510
507
|
|
511
508
|
```ruby
|
512
|
-
table = TTY::Table.new [
|
509
|
+
table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]
|
513
510
|
table.render_with MyBorder
|
514
511
|
# =>
|
515
512
|
# $header1$header2$
|
@@ -519,10 +516,10 @@ table.render_with MyBorder
|
|
519
516
|
|
520
517
|
#### 3.5.3 Separator
|
521
518
|
|
522
|
-
In addition to specifying border characters you can force table to render separator line on each row like:
|
519
|
+
In addition to specifying border characters you can force the table to render a separator line on each row like:
|
523
520
|
|
524
521
|
```ruby
|
525
|
-
table = TTY::Table.new [
|
522
|
+
table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]]
|
526
523
|
table.render do |renderer|
|
527
524
|
renderer.border.separator = :each_row
|
528
525
|
end
|
@@ -536,6 +533,65 @@ end
|
|
536
533
|
# +-------+-------+
|
537
534
|
```
|
538
535
|
|
536
|
+
If you want more control you can provide an array of rows *after* which a separator will be added:
|
537
|
+
|
538
|
+
```ruby
|
539
|
+
table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"], ["c1", "c2"]]
|
540
|
+
table.render do |renderer|
|
541
|
+
renderer.border.separator = [0, 2]
|
542
|
+
end
|
543
|
+
# =>
|
544
|
+
# +-------+-------+
|
545
|
+
# |header1|header2|
|
546
|
+
# +-------+-------+
|
547
|
+
# |a1 |a2 |
|
548
|
+
# |b1 |b2 |
|
549
|
+
# +-------+-------+
|
550
|
+
# |c1 |c2 |
|
551
|
+
# +-------+-------+
|
552
|
+
```
|
553
|
+
|
554
|
+
**Note:** if you supply a detailed list of rows to separate, then the separator between the header and the rows
|
555
|
+
will not be automatically added.
|
556
|
+
|
557
|
+
You can also give the separator option a proc to control where the separators are:
|
558
|
+
|
559
|
+
```ruby
|
560
|
+
table = TTY::Table.new ["header1", "header2"],
|
561
|
+
[["a1", "a2"], ["b1", "b2"], ["c1", "c2"], ["d1", "d2"]]
|
562
|
+
table.render do |renderer|
|
563
|
+
renderer.border.separator = ->(row) { row == 0 || (row+1) % 2 == 0} # separate every two rows
|
564
|
+
end
|
565
|
+
# =>
|
566
|
+
# +-------+-------+
|
567
|
+
# |header1|header2|
|
568
|
+
# +-------+-------+
|
569
|
+
# |a1 |a2 |
|
570
|
+
# |b1 |b2 |
|
571
|
+
# +-------+-------+
|
572
|
+
# |c1 |c2 |
|
573
|
+
# |d1 |d2 |
|
574
|
+
# +-------+-------+
|
575
|
+
```
|
576
|
+
|
577
|
+
Finally you can also position a separator using the `:separator` key word in place of a row:
|
578
|
+
|
579
|
+
```ruby
|
580
|
+
table = TTY::Table.new ["header1", "header2"],
|
581
|
+
[:separator, ["a1", "a2"], ["b1", "b2"]]
|
582
|
+
table << :separator << ["c1", "c2"] # you can push separators on too!
|
583
|
+
table.render
|
584
|
+
# =>
|
585
|
+
# +-------+-------+
|
586
|
+
# |header1|header2|
|
587
|
+
# +-------+-------+
|
588
|
+
# |a1 |a2 |
|
589
|
+
# |b1 |b2 |
|
590
|
+
# +-------+-------+
|
591
|
+
# |c1 |c2 |
|
592
|
+
# +-------+-------+
|
593
|
+
```
|
594
|
+
|
539
595
|
#### 3.5.4 Style
|
540
596
|
|
541
597
|
If you want to change the display color of your border do:
|
@@ -550,12 +606,14 @@ All [supported colors](https://github.com/piotrmurach/pastel#3-supported-colors)
|
|
550
606
|
|
551
607
|
### 3.6 Filter
|
552
608
|
|
553
|
-
You can define filters that will modify individual table
|
609
|
+
You can define filters that will modify individual table field value before it is rendered. A filter can be a callable such as proc.
|
610
|
+
|
611
|
+
Here's an example that formats capitalizes each field in second column skipping the header:
|
554
612
|
|
555
613
|
```ruby
|
556
|
-
table = TTY::Table.new
|
614
|
+
table = TTY::Table.new(["header1", "header2"], [["a1", "a2"], ["b1", "b2"]])
|
557
615
|
table.render do |renderer|
|
558
|
-
renderer.filter =
|
616
|
+
renderer.filter = ->(val, row_index, col_index) do
|
559
617
|
if col_index == 1 and !(row_index == 0)
|
560
618
|
val.capitalize
|
561
619
|
else
|
@@ -573,13 +631,13 @@ end
|
|
573
631
|
# +-------+-------+
|
574
632
|
```
|
575
633
|
|
576
|
-
To color even fields red on green background add filter like so
|
634
|
+
To color even fields red on green background add filter like so:
|
577
635
|
|
578
636
|
```ruby
|
579
637
|
pastel = Pastel.new
|
580
638
|
|
581
639
|
table.render do |renderer|
|
582
|
-
renderer.filter =
|
640
|
+
renderer.filter = ->(val, row_index, col_index) do
|
583
641
|
col_index % 2 == 1 ? pastel.red.on_green(val) : val
|
584
642
|
end
|
585
643
|
end
|
@@ -587,11 +645,11 @@ end
|
|
587
645
|
|
588
646
|
### 3.7 Multiline
|
589
647
|
|
590
|
-
Renderer options may include
|
648
|
+
Renderer options may include `:multiline` parameter. When set to `true`, table fields will wrap at their natural line breaks or the column widths(if provided).
|
591
649
|
|
592
650
|
```ruby
|
593
|
-
table = TTY::Table.new
|
594
|
-
table.render
|
651
|
+
table = TTY::Table.new([["First", "1"], ["Multi\nLine\nContent", "2"], ["Third", "3"]])
|
652
|
+
table.render(:ascii, multiline: true)
|
595
653
|
# =>
|
596
654
|
# +-------+-+
|
597
655
|
# |First |1|
|
@@ -605,7 +663,7 @@ table.render :ascii, multiline: true
|
|
605
663
|
When `multiline` is set to `false`, all line break characters will be escaped. In cases when the column widths are set, the content will be truncated.
|
606
664
|
|
607
665
|
```ruby
|
608
|
-
table = TTY::Table.new [["First",
|
666
|
+
table = TTY::Table.new [["First", "1"], ["Multiline\nContent", "2"], ["Third", "3"]]
|
609
667
|
table.render :ascii, multiline: false
|
610
668
|
# =>
|
611
669
|
# +------------------+-+
|
@@ -647,15 +705,15 @@ table.render(:ascii, padding: [1,2,1,2])
|
|
647
705
|
However, when adding top or bottom padding to content with line breaks, the `multiline` option needs to be set to `true` to allow for rows to span multiple lines. For example:
|
648
706
|
|
649
707
|
```ruby
|
650
|
-
table = TTY::Table.new
|
708
|
+
table = TTY::Table.new(header: ["head1", "head2"])
|
651
709
|
table << ["Multi\nLine", "Text\nthat\nwraps"]
|
652
|
-
table << ["Some\nother\ntext",
|
710
|
+
table << ["Some\nother\ntext", "Simple"]
|
653
711
|
```
|
654
712
|
|
655
|
-
would render as:
|
713
|
+
This would render as:
|
656
714
|
|
657
715
|
```ruby
|
658
|
-
table.render
|
716
|
+
table.render(:ascii, multiline: true, padding: [1,2,1,2])
|
659
717
|
# =>
|
660
718
|
# +---------+----------+
|
661
719
|
# | | |
|
@@ -677,17 +735,30 @@ table.render :ascii, multiline: true, padding: [1,2,1,2]
|
|
677
735
|
|
678
736
|
### 3.9 Resize
|
679
737
|
|
738
|
+
You can force table to resize to the terminal full width using the `:resize` option:
|
739
|
+
|
740
|
+
```ruby
|
741
|
+
table.render(:ascii, resize: true)
|
742
|
+
```
|
743
|
+
|
680
744
|
### 3.10 Width
|
681
745
|
|
682
746
|
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.
|
683
747
|
|
684
748
|
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.
|
685
749
|
|
750
|
+
For example, given the following table:
|
751
|
+
|
752
|
+
```ruby
|
753
|
+
header = ["h1", "h2", "h3"]
|
754
|
+
rows = [["aaa1", "aa2", "aaaaaaa3"], ["b1", "b2", "b3"]]
|
755
|
+
table = TTY::Table.new(header, rows)
|
756
|
+
```
|
757
|
+
|
758
|
+
The result of rending to `80` columns width will produce:
|
759
|
+
|
686
760
|
```ruby
|
687
|
-
|
688
|
-
rows = [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']]
|
689
|
-
table = TTY::Table.new header, rows
|
690
|
-
table.render width: 80, resize: true
|
761
|
+
table.render(width: 80, resize: true)
|
691
762
|
# =>
|
692
763
|
# +---------+-------+------------+
|
693
764
|
# |h1 |h2 |h3 |
|
@@ -709,4 +780,4 @@ This project is intended to be a safe, welcoming space for collaboration, and co
|
|
709
780
|
|
710
781
|
## Copyright
|
711
782
|
|
712
|
-
Copyright (c) 2015
|
783
|
+
Copyright (c) 2015 Piotr Murach. See LICENSE for further details.
|