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.
Files changed (151) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +57 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +165 -94
  5. data/lib/tty-table.rb +1 -5
  6. data/lib/tty/table.rb +102 -42
  7. data/lib/tty/table/alignment_set.rb +3 -6
  8. data/lib/tty/table/border.rb +57 -68
  9. data/lib/tty/table/border/ascii.rb +17 -17
  10. data/lib/tty/table/border/null.rb +20 -22
  11. data/lib/tty/table/border/row_line.rb +4 -3
  12. data/lib/tty/table/border/unicode.rb +17 -17
  13. data/lib/tty/table/border_dsl.rb +28 -25
  14. data/lib/tty/table/border_options.rb +50 -25
  15. data/lib/tty/table/column_constraint.rb +29 -17
  16. data/lib/tty/table/{column_set.rb → columns.rb} +44 -69
  17. data/lib/tty/table/empty.rb +2 -0
  18. data/lib/tty/table/error.rb +1 -1
  19. data/lib/tty/table/field.rb +43 -35
  20. data/lib/tty/table/header.rb +7 -5
  21. data/lib/tty/table/indentation.rb +16 -25
  22. data/lib/tty/table/operation/alignment.rb +10 -8
  23. data/lib/tty/table/operation/escape.rb +2 -3
  24. data/lib/tty/table/operation/filter.rb +1 -1
  25. data/lib/tty/table/operation/padding.rb +4 -12
  26. data/lib/tty/table/operation/truncation.rb +4 -4
  27. data/lib/tty/table/operation/wrapped.rb +4 -4
  28. data/lib/tty/table/operations.rb +4 -15
  29. data/lib/tty/table/options.rb +2 -4
  30. data/lib/tty/table/orientation.rb +5 -5
  31. data/lib/tty/table/orientation/horizontal.rb +1 -1
  32. data/lib/tty/table/orientation/vertical.rb +1 -1
  33. data/lib/tty/table/renderer.rb +40 -35
  34. data/lib/tty/table/renderer/ascii.rb +4 -4
  35. data/lib/tty/table/renderer/basic.rb +85 -88
  36. data/lib/tty/table/renderer/unicode.rb +3 -3
  37. data/lib/tty/table/row.rb +10 -8
  38. data/lib/tty/table/transformation.rb +2 -2
  39. data/lib/tty/table/validatable.rb +25 -7
  40. data/lib/tty/table/version.rb +3 -3
  41. metadata +33 -283
  42. data/.gitignore +0 -14
  43. data/.rspec +0 -3
  44. data/.travis.yml +0 -24
  45. data/CODE_OF_CONDUCT.md +0 -49
  46. data/Gemfile +0 -18
  47. data/Rakefile +0 -8
  48. data/benchmarks/speed.rb +0 -41
  49. data/spec/spec_helper.rb +0 -50
  50. data/spec/unit/access_spec.rb +0 -86
  51. data/spec/unit/add_row_spec.rb +0 -28
  52. data/spec/unit/alignment_set/each_spec.rb +0 -17
  53. data/spec/unit/alignment_set/new_spec.rb +0 -27
  54. data/spec/unit/alignment_set/to_ary_spec.rb +0 -14
  55. data/spec/unit/alignment_spec.rb +0 -71
  56. data/spec/unit/border/ascii/rendering_spec.rb +0 -90
  57. data/spec/unit/border/new_spec.rb +0 -27
  58. data/spec/unit/border/null/rendering_spec.rb +0 -130
  59. data/spec/unit/border/options/from_spec.rb +0 -38
  60. data/spec/unit/border/options/new_spec.rb +0 -14
  61. data/spec/unit/border/unicode/rendering_spec.rb +0 -63
  62. data/spec/unit/border_options/new_spec.rb +0 -20
  63. data/spec/unit/border_options/update_spec.rb +0 -18
  64. data/spec/unit/column_constraint/enforce_spec.rb +0 -70
  65. data/spec/unit/column_constraint/widths_spec.rb +0 -35
  66. data/spec/unit/column_set/extract_widths_spec.rb +0 -48
  67. data/spec/unit/column_set/total_width_spec.rb +0 -15
  68. data/spec/unit/column_set/widths_from_spec.rb +0 -51
  69. data/spec/unit/data_spec.rb +0 -12
  70. data/spec/unit/each_spec.rb +0 -26
  71. data/spec/unit/each_with_index_spec.rb +0 -51
  72. data/spec/unit/empty_spec.rb +0 -23
  73. data/spec/unit/eql_spec.rb +0 -34
  74. data/spec/unit/field/equality_spec.rb +0 -51
  75. data/spec/unit/field/length_spec.rb +0 -35
  76. data/spec/unit/field/lines_spec.rb +0 -21
  77. data/spec/unit/field/new_spec.rb +0 -29
  78. data/spec/unit/field/width_spec.rb +0 -23
  79. data/spec/unit/filter_spec.rb +0 -22
  80. data/spec/unit/header/call_spec.rb +0 -30
  81. data/spec/unit/header/color_spec.rb +0 -19
  82. data/spec/unit/header/equality_spec.rb +0 -51
  83. data/spec/unit/header/height_spec.rb +0 -27
  84. data/spec/unit/header/new_spec.rb +0 -16
  85. data/spec/unit/header/set_spec.rb +0 -20
  86. data/spec/unit/header/to_ary_spec.rb +0 -14
  87. data/spec/unit/header_spec.rb +0 -13
  88. data/spec/unit/indentation/indent_spec.rb +0 -21
  89. data/spec/unit/new_spec.rb +0 -73
  90. data/spec/unit/operation/alignment/call_spec.rb +0 -39
  91. data/spec/unit/operation/escape/call_spec.rb +0 -15
  92. data/spec/unit/operation/filter/call_spec.rb +0 -16
  93. data/spec/unit/operation/truncation/call_spec.rb +0 -30
  94. data/spec/unit/operation/wrapped/call_spec.rb +0 -40
  95. data/spec/unit/operations/new_spec.rb +0 -30
  96. data/spec/unit/options/access_spec.rb +0 -14
  97. data/spec/unit/options_spec.rb +0 -25
  98. data/spec/unit/orientation_spec.rb +0 -145
  99. data/spec/unit/padding_spec.rb +0 -117
  100. data/spec/unit/properties_spec.rb +0 -25
  101. data/spec/unit/render_repeat_spec.rb +0 -42
  102. data/spec/unit/render_spec.rb +0 -63
  103. data/spec/unit/render_with_spec.rb +0 -106
  104. data/spec/unit/renderer/ascii/coloring_spec.rb +0 -70
  105. data/spec/unit/renderer/ascii/indentation_spec.rb +0 -41
  106. data/spec/unit/renderer/ascii/multiline_spec.rb +0 -101
  107. data/spec/unit/renderer/ascii/padding_spec.rb +0 -88
  108. data/spec/unit/renderer/ascii/render_spec.rb +0 -68
  109. data/spec/unit/renderer/ascii/resizing_spec.rb +0 -114
  110. data/spec/unit/renderer/ascii/separator_spec.rb +0 -28
  111. data/spec/unit/renderer/basic/alignment_spec.rb +0 -88
  112. data/spec/unit/renderer/basic/coloring_spec.rb +0 -61
  113. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -28
  114. data/spec/unit/renderer/basic/filter_spec.rb +0 -53
  115. data/spec/unit/renderer/basic/indentation_spec.rb +0 -48
  116. data/spec/unit/renderer/basic/multiline_spec.rb +0 -74
  117. data/spec/unit/renderer/basic/new_spec.rb +0 -26
  118. data/spec/unit/renderer/basic/options_spec.rb +0 -52
  119. data/spec/unit/renderer/basic/padding_spec.rb +0 -76
  120. data/spec/unit/renderer/basic/render_spec.rb +0 -57
  121. data/spec/unit/renderer/basic/resizing_spec.rb +0 -96
  122. data/spec/unit/renderer/basic/separator_spec.rb +0 -43
  123. data/spec/unit/renderer/basic/truncation_spec.rb +0 -35
  124. data/spec/unit/renderer/basic/wrapping_spec.rb +0 -40
  125. data/spec/unit/renderer/border_spec.rb +0 -104
  126. data/spec/unit/renderer/render_spec.rb +0 -36
  127. data/spec/unit/renderer/select_spec.rb +0 -22
  128. data/spec/unit/renderer/unicode/coloring_spec.rb +0 -70
  129. data/spec/unit/renderer/unicode/indentation_spec.rb +0 -41
  130. data/spec/unit/renderer/unicode/padding_spec.rb +0 -61
  131. data/spec/unit/renderer/unicode/render_spec.rb +0 -68
  132. data/spec/unit/renderer/unicode/separator_spec.rb +0 -26
  133. data/spec/unit/renderer_spec.rb +0 -19
  134. data/spec/unit/rotate_spec.rb +0 -86
  135. data/spec/unit/row/access_spec.rb +0 -25
  136. data/spec/unit/row/call_spec.rb +0 -45
  137. data/spec/unit/row/data_spec.rb +0 -26
  138. data/spec/unit/row/each_spec.rb +0 -31
  139. data/spec/unit/row/equality_spec.rb +0 -73
  140. data/spec/unit/row/height_spec.rb +0 -27
  141. data/spec/unit/row/new_spec.rb +0 -41
  142. data/spec/unit/row/to_ary_spec.rb +0 -14
  143. data/spec/unit/to_s_spec.rb +0 -56
  144. data/spec/unit/transformation/extract_tuples_spec.rb +0 -35
  145. data/spec/unit/utf_spec.rb +0 -33
  146. data/spec/unit/validatable/validate_options_spec.rb +0 -33
  147. data/spec/unit/validatable_spec.rb +0 -32
  148. data/tasks/console.rake +0 -10
  149. data/tasks/coverage.rake +0 -11
  150. data/tasks/spec.rake +0 -29
  151. data/tty-table.gemspec +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2842eafedd508702de25386dd7bce4bd8904f930
4
- data.tar.gz: d4969e9ea05c9bdaea3d98abe20d5326e0e0ea93
2
+ SHA256:
3
+ metadata.gz: d1de8ba17e3ea8c4b575292757c2d9908abbbeec7511fda9692b4718bb423fbd
4
+ data.tar.gz: 5208f9766c7309b44f97b54282b6cd9bb7d5bf8966fdd8db337bd653e65696af
5
5
  SHA512:
6
- metadata.gz: 9849f80f8267563b4f884c2888dfa18b6099e9209480b68e792a758179e11f616cc3adc4d95543b09279cd8fd6ad3f26c3ea1a0bf5afebdf9f8f01e4d1571be2
7
- data.tar.gz: f8108b28bbff284e92e128db0d2217bd1f0fb3ea73c0d83ea14689d425133f796f46f774ccadd75818fddef62e01ccaabbe3e57964db3b7f0be58313abeb3927
6
+ metadata.gz: 7547f09416d4e89f7292c6505554c3bbde7f6b83531acbad831a8e0ba96012c2e806611f9a0162e97610e3ed85c98d67b3b96f936e279546145d44ae74cbdbf2
7
+ data.tar.gz: 3a7daadb0f83bd72dc99c3992b1b9f82684714b20e9bcb55b16bb8a3eee87cd3e2c88bd175eff5af5d7dc5f270cc82e386979fd90cd4d3ec06e6ac6af73c14fd
@@ -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
@@ -1,4 +1,4 @@
1
- Copyright (c) 2015 Piotr Murach
1
+ Copyright (c) 2015 Piotr Murach (piotrmurach.com)
2
2
 
3
3
  MIT License
4
4
 
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 'tty-table'
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.1.1 Basic renderer](#311-basic)
56
- * [3.1.2 ASCII renderer](#312-ascii-renderer)
57
- * [3.1.3 Unicode renderer](#313-unicode-renderer)
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 data:
79
+ First, provide **TTY::Table** with data, for example, two headers and two rows:
74
80
 
75
81
  ```ruby
76
- table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
82
+ table = TTY::Table.new(["header1","header2"], [["a1", "a2"], ["b1", "b2"]])
77
83
  ```
78
84
 
79
- Then simply call `render` on the instance with with border type as first argument:
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[['a1', 'a2'], ['b1', 'b2']]
101
- table = TTY::Table.new [['a1', 'a2'], ['b1', 'b2']]
102
- table = TTY::Table.new rows: [['a1', 'a2'], ['b1', 'b2']]
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 << ['a1', 'a2']
110
- t << ['b1', 'b2']
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 << ['a1','a2']
119
- table << ['b1','b2']
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 ['h1', 'h2'], [['a1', 'a2'], ['b1', 'b2']]
126
- table = TTY::Table.new header: ['h1', 'h2'], rows: [['a1', 'a2'], ['b1', 'b2']]
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
- or cross header with rows inside a hash like so
135
+ You can also mix header with rows inside a hash like so:
130
136
 
131
137
  ```ruby
132
- table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
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 << ['a1', 'a2', 'a3']
141
- table << ['b1', 'b2', 'b3']
142
- table << ['a1', 'a2'] << ['b1', 'b2'] # chain rows assignment
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 referene the row at `index` do:
160
+ In order to reference the row at `index` do:
155
161
 
156
162
  ```ruby
157
- table = TTY::Table.new [['a1','a2'], ['b1','b2']]
158
- table[0] # => ['a1','a2']
159
- table.row(0) # => ['a1','a2']
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] # => ['b1','b2']
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] # => 'a1'
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) # => ['a1','b1']
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 ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
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 ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
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
- ```ruby
345
- alignments # array of cell alignments out of :left, :center and :right,
346
- # default :left
347
- border # hash of border options - :characters, :style and :separator
348
- border_class # a type of border to use such as TTY::Table::Border::Null,
349
- # TTY::Table::Border::ASCII, TTY::Table::Border::Unicode
350
- column_widths # array of maximum column widths
351
- filter # a proc object that is applied to every field in a row
352
- indent # indentation applied to rendered table, by default 0
353
- multiline # if true will wrap text at new line or column width,
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
- or inside a block:
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 individuall by passing `alignments` option to table renderer:
379
+ You can align each column individually by passing `:alignments` option to table renderer:
383
380
 
384
381
  ```ruby
385
- table.render :ascii, alignments: [:center, :right]
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 `alignment` option:
392
+ Alternatively you can align all columns with `:alignment` option:
396
393
 
397
394
  ```ruby
398
- table.render :ascii, alignment: [:center]
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 header: ['header1', 'header2']
412
- table << [{value: 'a1', alignment: :right}, 'a2']
413
- table << ['b1', {value: 'b2', alignment: :center}]
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 ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
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 ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
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 ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
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 ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']]
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 fields value before they are rendered. A filter can be a callable such as proc. Here's an example that formats
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 ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
614
+ table = TTY::Table.new(["header1", "header2"], [["a1", "a2"], ["b1", "b2"]])
557
615
  table.render do |renderer|
558
- renderer.filter = Proc.new do |val, row_index, col_index|
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 = proc do |val, row_index, col_index|
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 `multiline` parameter. When set to `true`, table fields will wrap at their natural line breaks or the column widths(if provided).
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 [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
594
- table.render :ascii, multiline: true
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", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
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 header: ['head1', 'head2']
708
+ table = TTY::Table.new(header: ["head1", "head2"])
651
709
  table << ["Multi\nLine", "Text\nthat\nwraps"]
652
- table << ["Some\nother\ntext", 'Simple']
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 :ascii, multiline: true, padding: [1,2,1,2]
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
- header = ['h1', 'h2', 'h3']
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-2017 Piotr Murach. See LICENSE for further details.
783
+ Copyright (c) 2015 Piotr Murach. See LICENSE for further details.