tty-table 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +104 -98
  5. data/lib/tty-table.rb +1 -1
  6. data/lib/tty/table.rb +70 -34
  7. data/lib/tty/table/alignment_set.rb +1 -3
  8. data/lib/tty/table/border.rb +50 -57
  9. data/lib/tty/table/border/ascii.rb +16 -16
  10. data/lib/tty/table/border/null.rb +19 -22
  11. data/lib/tty/table/border/row_line.rb +3 -2
  12. data/lib/tty/table/border/unicode.rb +16 -16
  13. data/lib/tty/table/border_dsl.rb +24 -22
  14. data/lib/tty/table/border_options.rb +32 -27
  15. data/lib/tty/table/column_constraint.rb +12 -9
  16. data/lib/tty/table/columns.rb +6 -6
  17. data/lib/tty/table/field.rb +46 -28
  18. data/lib/tty/table/header.rb +5 -5
  19. data/lib/tty/table/indentation.rb +7 -4
  20. data/lib/tty/table/operation/alignment.rb +1 -1
  21. data/lib/tty/table/operation/escape.rb +1 -2
  22. data/lib/tty/table/operation/padding.rb +1 -1
  23. data/lib/tty/table/operation/truncation.rb +1 -1
  24. data/lib/tty/table/operation/wrapped.rb +1 -1
  25. data/lib/tty/table/options.rb +1 -1
  26. data/lib/tty/table/orientation.rb +4 -4
  27. data/lib/tty/table/renderer.rb +8 -8
  28. data/lib/tty/table/renderer/ascii.rb +2 -2
  29. data/lib/tty/table/renderer/basic.rb +37 -40
  30. data/lib/tty/table/renderer/unicode.rb +1 -1
  31. data/lib/tty/table/row.rb +7 -7
  32. data/lib/tty/table/validatable.rb +23 -7
  33. data/lib/tty/table/version.rb +1 -1
  34. metadata +23 -272
  35. data/Rakefile +0 -10
  36. data/examples/alignment.rb +0 -10
  37. data/examples/basic.rb +0 -6
  38. data/examples/orientation.rb +0 -13
  39. data/examples/padding.rb +0 -15
  40. data/examples/resize.rb +0 -15
  41. data/spec/spec_helper.rb +0 -50
  42. data/spec/unit/access_spec.rb +0 -84
  43. data/spec/unit/add_row_spec.rb +0 -26
  44. data/spec/unit/alignment_set/each_spec.rb +0 -15
  45. data/spec/unit/alignment_set/new_spec.rb +0 -25
  46. data/spec/unit/alignment_set/to_ary_spec.rb +0 -12
  47. data/spec/unit/alignment_spec.rb +0 -69
  48. data/spec/unit/border/ascii/rendering_spec.rb +0 -88
  49. data/spec/unit/border/new_spec.rb +0 -25
  50. data/spec/unit/border/null/rendering_spec.rb +0 -128
  51. data/spec/unit/border/options/from_spec.rb +0 -36
  52. data/spec/unit/border/options/new_spec.rb +0 -12
  53. data/spec/unit/border/unicode/rendering_spec.rb +0 -61
  54. data/spec/unit/border_options/new_spec.rb +0 -18
  55. data/spec/unit/border_options/update_spec.rb +0 -16
  56. data/spec/unit/column_constraint/enforce_spec.rb +0 -68
  57. data/spec/unit/column_constraint/widths_spec.rb +0 -33
  58. data/spec/unit/columns/extract_widths_spec.rb +0 -41
  59. data/spec/unit/columns/total_width_spec.rb +0 -11
  60. data/spec/unit/columns/widths_from_spec.rb +0 -49
  61. data/spec/unit/data_spec.rb +0 -10
  62. data/spec/unit/each_spec.rb +0 -24
  63. data/spec/unit/each_with_index_spec.rb +0 -49
  64. data/spec/unit/empty_spec.rb +0 -21
  65. data/spec/unit/eql_spec.rb +0 -32
  66. data/spec/unit/field/equality_spec.rb +0 -49
  67. data/spec/unit/field/length_spec.rb +0 -33
  68. data/spec/unit/field/lines_spec.rb +0 -19
  69. data/spec/unit/field/new_spec.rb +0 -27
  70. data/spec/unit/field/width_spec.rb +0 -21
  71. data/spec/unit/filter_spec.rb +0 -20
  72. data/spec/unit/header/call_spec.rb +0 -28
  73. data/spec/unit/header/color_spec.rb +0 -17
  74. data/spec/unit/header/equality_spec.rb +0 -49
  75. data/spec/unit/header/height_spec.rb +0 -25
  76. data/spec/unit/header/new_spec.rb +0 -13
  77. data/spec/unit/header/set_spec.rb +0 -18
  78. data/spec/unit/header/to_ary_spec.rb +0 -12
  79. data/spec/unit/header_spec.rb +0 -11
  80. data/spec/unit/indentation/indent_spec.rb +0 -15
  81. data/spec/unit/new_spec.rb +0 -71
  82. data/spec/unit/operation/alignment/call_spec.rb +0 -37
  83. data/spec/unit/operation/escape/call_spec.rb +0 -13
  84. data/spec/unit/operation/filter/call_spec.rb +0 -14
  85. data/spec/unit/operation/truncation/call_spec.rb +0 -28
  86. data/spec/unit/operation/wrapped/call_spec.rb +0 -38
  87. data/spec/unit/operations/new_spec.rb +0 -28
  88. data/spec/unit/options/access_spec.rb +0 -12
  89. data/spec/unit/options_spec.rb +0 -23
  90. data/spec/unit/orientation_spec.rb +0 -143
  91. data/spec/unit/padding_spec.rb +0 -115
  92. data/spec/unit/properties_spec.rb +0 -23
  93. data/spec/unit/render_repeat_spec.rb +0 -39
  94. data/spec/unit/render_spec.rb +0 -61
  95. data/spec/unit/render_with_spec.rb +0 -104
  96. data/spec/unit/renderer/ascii/coloring_spec.rb +0 -83
  97. data/spec/unit/renderer/ascii/indentation_spec.rb +0 -39
  98. data/spec/unit/renderer/ascii/multiline_spec.rb +0 -99
  99. data/spec/unit/renderer/ascii/padding_spec.rb +0 -115
  100. data/spec/unit/renderer/ascii/render_spec.rb +0 -66
  101. data/spec/unit/renderer/ascii/resizing_spec.rb +0 -112
  102. data/spec/unit/renderer/ascii/separator_spec.rb +0 -38
  103. data/spec/unit/renderer/basic/alignment_spec.rb +0 -86
  104. data/spec/unit/renderer/basic/coloring_spec.rb +0 -59
  105. data/spec/unit/renderer/basic/extract_column_widths_spec.rb +0 -26
  106. data/spec/unit/renderer/basic/filter_spec.rb +0 -51
  107. data/spec/unit/renderer/basic/indentation_spec.rb +0 -46
  108. data/spec/unit/renderer/basic/multiline_spec.rb +0 -72
  109. data/spec/unit/renderer/basic/new_spec.rb +0 -24
  110. data/spec/unit/renderer/basic/options_spec.rb +0 -50
  111. data/spec/unit/renderer/basic/padding_spec.rb +0 -74
  112. data/spec/unit/renderer/basic/render_spec.rb +0 -55
  113. data/spec/unit/renderer/basic/resizing_spec.rb +0 -94
  114. data/spec/unit/renderer/basic/separator_spec.rb +0 -41
  115. data/spec/unit/renderer/basic/single_row_separator_spec.rb +0 -80
  116. data/spec/unit/renderer/basic/truncation_spec.rb +0 -33
  117. data/spec/unit/renderer/basic/wrapping_spec.rb +0 -38
  118. data/spec/unit/renderer/border_spec.rb +0 -102
  119. data/spec/unit/renderer/render_spec.rb +0 -34
  120. data/spec/unit/renderer/select_spec.rb +0 -20
  121. data/spec/unit/renderer/unicode/coloring_spec.rb +0 -68
  122. data/spec/unit/renderer/unicode/indentation_spec.rb +0 -39
  123. data/spec/unit/renderer/unicode/padding_spec.rb +0 -59
  124. data/spec/unit/renderer/unicode/render_spec.rb +0 -66
  125. data/spec/unit/renderer/unicode/separator_spec.rb +0 -36
  126. data/spec/unit/renderer_spec.rb +0 -17
  127. data/spec/unit/rotate_spec.rb +0 -84
  128. data/spec/unit/row/access_spec.rb +0 -23
  129. data/spec/unit/row/call_spec.rb +0 -43
  130. data/spec/unit/row/data_spec.rb +0 -24
  131. data/spec/unit/row/each_spec.rb +0 -29
  132. data/spec/unit/row/equality_spec.rb +0 -71
  133. data/spec/unit/row/height_spec.rb +0 -25
  134. data/spec/unit/row/new_spec.rb +0 -39
  135. data/spec/unit/row/to_ary_spec.rb +0 -12
  136. data/spec/unit/to_s_spec.rb +0 -56
  137. data/spec/unit/transformation/extract_tuples_spec.rb +0 -33
  138. data/spec/unit/utf_spec.rb +0 -31
  139. data/spec/unit/validatable/validate_options_spec.rb +0 -31
  140. data/spec/unit/validatable_spec.rb +0 -30
  141. data/tasks/console.rake +0 -11
  142. data/tasks/coverage.rake +0 -11
  143. data/tasks/spec.rake +0 -29
  144. data/tty-table.gemspec +0 -41
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e62b5af0c89b3acf1aa73598053bc5b5f364d9f346fc40e20129d2a639c3919
4
- data.tar.gz: 812b92b6eccf2743f121bfd85c069f833e0e3d80507526a8c1cabfacb776df15
3
+ metadata.gz: d1de8ba17e3ea8c4b575292757c2d9908abbbeec7511fda9692b4718bb423fbd
4
+ data.tar.gz: 5208f9766c7309b44f97b54282b6cd9bb7d5bf8966fdd8db337bd653e65696af
5
5
  SHA512:
6
- metadata.gz: bdc6608e3633b35eaed9481e545732c6d55f9ec43823ca111b08b177edb03046e1710221d36e7e4b6df8980d8555ebfecfdfc07535786199ae04f37e18423228
7
- data.tar.gz: 1a77ffd2f0ef11427c5bb4769edb6ec41f691f0226f6f216c3988abd19519ce5ab84685de4b90d7a2f590023b3fedde2a075a4b80f7992b74a6160be909280a6
6
+ metadata.gz: 7547f09416d4e89f7292c6505554c3bbde7f6b83531acbad831a8e0ba96012c2e806611f9a0162e97610e3ed85c98d67b3b96f936e279546145d44ae74cbdbf2
7
+ data.tar.gz: 3a7daadb0f83bd72dc99c3992b1b9f82684714b20e9bcb55b16bb8a3eee87cd3e2c88bd175eff5af5d7dc5f270cc82e386979fd90cd4d3ec06e6ac6af73c14fd
@@ -1,5 +1,17 @@
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
+
3
15
  ## [v0.11.0] - 2019-08-01
4
16
 
5
17
  ### Add
@@ -103,6 +115,7 @@
103
115
 
104
116
  * Initial implementation and release
105
117
 
118
+ [v0.12.0]: https://github.com/piotrmurach/tty-table/compare/v0.11.0...v0.12.0
106
119
  [v0.11.0]: https://github.com/piotrmurach/tty-table/compare/v0.10.0...v0.11.0
107
120
  [v0.10.0]: https://github.com/piotrmurach/tty-table/compare/v0.9.0...v0.10.0
108
121
  [v0.9.0]: https://github.com/piotrmurach/tty-table/compare/v0.8.0...v0.9.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,5 +1,5 @@
1
1
  <div align="center">
2
- <a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/blob/master/images/tty.png" alt="tty logo" /></a>
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
3
  </div>
4
4
 
5
5
  # TTY::Table [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
@@ -36,7 +36,7 @@
36
36
  Add this line to your application's Gemfile:
37
37
 
38
38
  ```ruby
39
- gem 'tty-table'
39
+ gem "tty-table"
40
40
  ```
41
41
 
42
42
  And then execute:
@@ -58,9 +58,9 @@ Or install it yourself as:
58
58
  * [3. Rendering](#3-rendering)
59
59
  * [3.1 Render](#31-render)
60
60
  * [3.2 Renderer](#32-renderer)
61
- * [3.1.1 Basic renderer](#311-basic)
62
- * [3.1.2 ASCII renderer](#312-ascii-renderer)
63
- * [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)
64
64
  * [3.3 Options](#33-options)
65
65
  * [3.4 Alignment](#34-alignment)
66
66
  * [3.5 Border](#35-border)
@@ -76,16 +76,16 @@ Or install it yourself as:
76
76
 
77
77
  ## 1. Usage
78
78
 
79
- First, provide **TTY::Table** with headers and data:
79
+ First, provide **TTY::Table** with data, for example, two headers and two rows:
80
80
 
81
81
  ```ruby
82
- table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
82
+ table = TTY::Table.new(["header1","header2"], [["a1", "a2"], ["b1", "b2"]])
83
83
  ```
84
84
 
85
- 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:
86
86
 
87
87
  ```ruby
88
- table.render(:ascii)
88
+ puts table.render(:ascii)
89
89
  # =>
90
90
  # +-------+-------+
91
91
  # |header1|header2|
@@ -103,39 +103,39 @@ table.render(:ascii)
103
103
  **TTY::Table** can be created in variety of ways. The easiest way is to pass 2-dimensional array:
104
104
 
105
105
  ```ruby
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']]
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"]])
109
109
  ```
110
110
 
111
- Alternatively you can specify rows one by one inside block:
111
+ Alternatively you can specify the rows one by one inside a block:
112
112
 
113
113
  ```ruby
114
114
  table = TTY::Table.new do |t|
115
- t << ['a1', 'a2']
116
- t << ['b1', 'b2']
115
+ t << ["a1", "a2"]
116
+ t << ["b1", "b2"]
117
117
  end
118
118
  ```
119
119
 
120
- You can add rows of data after initialization:
120
+ You can add rows of data after initialization with `<<` operator:
121
121
 
122
122
  ```ruby
123
123
  table = TTY::Table.new
124
- table << ['a1','a2']
125
- table << ['b1','b2']
124
+ table << ["a1","a2"]
125
+ table << ["b1","b2"]
126
126
  ```
127
127
 
128
- In addition to rows you can specify table header:
128
+ In addition to rows, you can specify table header:
129
129
 
130
130
  ```ruby
131
- table = TTY::Table.new ['h1', 'h2'], [['a1', 'a2'], ['b1', 'b2']]
132
- 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"]])
133
133
  ```
134
134
 
135
- or cross header with rows inside a hash like so
135
+ You can also mix header with rows inside a hash like so:
136
136
 
137
137
  ```ruby
138
- table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
138
+ table = TTY::Table.new([{"h1" => ["a1", "a2"], "h2" => ["b1", "b2"]}])
139
139
  ```
140
140
 
141
141
  ### 2.2 Iteration
@@ -143,9 +143,9 @@ table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
143
143
  Table behaves like an Array so `<<`, `each` and familiar methods can be used:
144
144
 
145
145
  ```ruby
146
- table << ['a1', 'a2', 'a3']
147
- table << ['b1', 'b2', 'b3']
148
- 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
149
149
  ```
150
150
 
151
151
  In order to iterate over table rows including headers do:
@@ -160,30 +160,30 @@ table.each_with_index { |row, index| ... } # iterate over rows with an index
160
160
  In order to reference the row at `index` do:
161
161
 
162
162
  ```ruby
163
- table = TTY::Table.new [['a1','a2'], ['b1','b2']]
164
- table[0] # => ['a1','a2']
165
- 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"]
166
166
  table.row(i) { |row| ... } # return array for row(i)
167
167
  ```
168
168
 
169
169
  Negative indices count backwards from the end of table data (`-1` is the last element):
170
170
 
171
171
  ```ruby
172
- table[-1] # => ['b1','b2']
172
+ table[-1] # => ["b1","b2"]
173
173
  ```
174
174
 
175
175
  To reference element at given row(i) and column(j) do:
176
176
 
177
177
  ```ruby
178
178
  table[i, j] # return element at row(i) and column(j)
179
- table[0,0] # => 'a1'
179
+ table[0,0] # => "a1"
180
180
  ```
181
181
 
182
182
  To specifically reference column(j) do:
183
183
 
184
184
  ```ruby
185
185
  table.column(j) { ... } # return array for column(j)
186
- table.column(0) # => ['a1','b1']
186
+ table.column(0) # => ["a1","b1"]
187
187
  table.column(name) # return array for column(name), name of header
188
188
  ```
189
189
 
@@ -210,7 +210,7 @@ table.size # return an array of [row_size, column_size]
210
210
  Given a table:
211
211
 
212
212
  ```ruby
213
- table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
213
+ table = TTY::Table.new(["header1","header2"], [["a1", "a2"], ["b1", "b2"]])
214
214
  ```
215
215
 
216
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:
@@ -246,7 +246,7 @@ end
246
246
  Given a table of data:
247
247
 
248
248
  ```ruby
249
- table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
249
+ table = TTY::Table.new ["header1","header2"], [["a1", "a2"], ["b1", "b2"]]
250
250
  ```
251
251
 
252
252
  You can create a special renderer for it:
@@ -347,25 +347,16 @@ table.render(:unicode)
347
347
 
348
348
  Rendering of **TTY-Table** includes numerous customization options:
349
349
 
350
- ```ruby
351
- alignments # array of cell alignments out of :left, :center and :right,
352
- # default :left
353
- border # hash of border options - :characters, :style and :separator
354
- border_class # a type of border to use such as TTY::Table::Border::Null,
355
- # TTY::Table::Border::ASCII, TTY::Table::Border::Unicode
356
- column_widths # array of maximum column widths
357
- filter # a proc object that is applied to every field in a row
358
- indent # indentation applied to rendered table, by default 0
359
- multiline # if true will wrap text at new line or column width,
360
- # when false will escape special characters
361
- padding # array of integers to set table fields padding,
362
- # by default [0,0,0,0]
363
- resize # if true will expand/shrink table column sizes to match
364
- # the terminal width, otherwise if false will rotate
365
- # table vertically. By default set to false
366
- width # constrain the table total width, by default dynamically
367
- # calculated based on content and terminal size
368
- ```
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.
369
360
 
370
361
  The `render` method can accept as a second argument the above options either as hash value:
371
362
 
@@ -373,11 +364,11 @@ The `render` method can accept as a second argument the above options either as
373
364
  table.render(:basic, alignments: [:left, :center])
374
365
  ```
375
366
 
376
- or inside a block:
367
+ Or inside a block as a property:
377
368
 
378
369
  ```ruby
379
370
  table.render(:basic) do |renderer|
380
- renderer.alignments= [:left, :center]
371
+ renderer.alignments = [:left, :center]
381
372
  end
382
373
  ```
383
374
 
@@ -385,10 +376,10 @@ end
385
376
 
386
377
  By default all columns are `:left` aligned.
387
378
 
388
- 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:
389
380
 
390
381
  ```ruby
391
- table.render :ascii, alignments: [:center, :right]
382
+ table.render(:ascii, alignments: [:center, :right])
392
383
  # =>
393
384
  # +-------+-------+
394
385
  # |header1|header2|
@@ -398,10 +389,10 @@ table.render :ascii, alignments: [:center, :right]
398
389
  # +-------+-------+
399
390
  ```
400
391
 
401
- Alternatively you can align all columns with `alignment` option:
392
+ Alternatively you can align all columns with `:alignment` option:
402
393
 
403
394
  ```ruby
404
- table.render :ascii, alignment: [:center]
395
+ table.render(:ascii, alignment: [:center])
405
396
  # =>
406
397
  # +-------+-------+
407
398
  # |header1|header2|
@@ -414,9 +405,9 @@ table.render :ascii, alignment: [:center]
414
405
  If you require a more granular alignment you can align individual fields in a row by passing `:alignment` option like so:
415
406
 
416
407
  ```ruby
417
- table = TTY::Table.new header: ['header1', 'header2']
418
- table << [{value: 'a1', alignment: :right}, 'a2']
419
- 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}]
420
411
  ```
421
412
 
422
413
  and then simply render:
@@ -437,7 +428,7 @@ table.render(:ascii)
437
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:
438
429
 
439
430
  ```ruby
440
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']]
431
+ table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]]
441
432
  table.render :unicode
442
433
  # =>
443
434
  # ┌───────┬───────┐
@@ -480,11 +471,11 @@ The following are available border parts:
480
471
  Using the above border parts you can create your own border with the `border` helper:
481
472
 
482
473
  ```ruby
483
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
474
+ table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]
484
475
  table.render do |renderer|
485
476
  renderer.border do
486
- mid '='
487
- mid_mid ' '
477
+ mid "="
478
+ mid_mid " "
488
479
  end
489
480
  end
490
481
  # =>
@@ -501,13 +492,13 @@ You can also create your own custom border by subclassing `TTY::Table::Border` a
501
492
  ```ruby
502
493
  class MyBorder < TTY::Table::Border
503
494
  def_border do
504
- left '$'
505
- center '$'
506
- right '$'
507
- bottom ' '
508
- bottom_mid '*'
509
- bottom_left '*'
510
- bottom_right '*'
495
+ left "$"
496
+ center "$"
497
+ right "$"
498
+ bottom " "
499
+ bottom_mid "*"
500
+ bottom_left "*"
501
+ bottom_right "*"
511
502
  end
512
503
  end
513
504
  ```
@@ -515,7 +506,7 @@ end
515
506
  Next pass the border class to your table instance `render_with` method
516
507
 
517
508
  ```ruby
518
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
509
+ table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]
519
510
  table.render_with MyBorder
520
511
  # =>
521
512
  # $header1$header2$
@@ -528,7 +519,7 @@ table.render_with MyBorder
528
519
  In addition to specifying border characters you can force the table to render a separator line on each row like:
529
520
 
530
521
  ```ruby
531
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']]
522
+ table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]]
532
523
  table.render do |renderer|
533
524
  renderer.border.separator = :each_row
534
525
  end
@@ -545,7 +536,7 @@ end
545
536
  If you want more control you can provide an array of rows *after* which a separator will be added:
546
537
 
547
538
  ```ruby
548
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2']]
539
+ table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"], ["c1", "c2"]]
549
540
  table.render do |renderer|
550
541
  renderer.border.separator = [0, 2]
551
542
  end
@@ -566,8 +557,8 @@ will not be automatically added.
566
557
  You can also give the separator option a proc to control where the separators are:
567
558
 
568
559
  ```ruby
569
- table = TTY::Table.new ['header1', 'header2'],
570
- [['a1', 'a2'], ['b1', 'b2'], ['c1', 'c2'], ['d1', 'd2']]
560
+ table = TTY::Table.new ["header1", "header2"],
561
+ [["a1", "a2"], ["b1", "b2"], ["c1", "c2"], ["d1", "d2"]]
571
562
  table.render do |renderer|
572
563
  renderer.border.separator = ->(row) { row == 0 || (row+1) % 2 == 0} # separate every two rows
573
564
  end
@@ -586,9 +577,9 @@ end
586
577
  Finally you can also position a separator using the `:separator` key word in place of a row:
587
578
 
588
579
  ```ruby
589
- table = TTY::Table.new ['header1', 'header2'],
590
- [:separator, ['a1', 'a2'], ['b1', 'b2']]
591
- table << :separator << ['c1', 'c2'] # you can push separators on too!
580
+ table = TTY::Table.new ["header1", "header2"],
581
+ [:separator, ["a1", "a2"], ["b1", "b2"]]
582
+ table << :separator << ["c1", "c2"] # you can push separators on too!
592
583
  table.render
593
584
  # =>
594
585
  # +-------+-------+
@@ -615,12 +606,14 @@ All [supported colors](https://github.com/piotrmurach/pastel#3-supported-colors)
615
606
 
616
607
  ### 3.6 Filter
617
608
 
618
- 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:
619
612
 
620
613
  ```ruby
621
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']]
614
+ table = TTY::Table.new(["header1", "header2"], [["a1", "a2"], ["b1", "b2"]])
622
615
  table.render do |renderer|
623
- renderer.filter = Proc.new do |val, row_index, col_index|
616
+ renderer.filter = ->(val, row_index, col_index) do
624
617
  if col_index == 1 and !(row_index == 0)
625
618
  val.capitalize
626
619
  else
@@ -638,13 +631,13 @@ end
638
631
  # +-------+-------+
639
632
  ```
640
633
 
641
- 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:
642
635
 
643
636
  ```ruby
644
637
  pastel = Pastel.new
645
638
 
646
639
  table.render do |renderer|
647
- renderer.filter = proc do |val, row_index, col_index|
640
+ renderer.filter = ->(val, row_index, col_index) do
648
641
  col_index % 2 == 1 ? pastel.red.on_green(val) : val
649
642
  end
650
643
  end
@@ -652,11 +645,11 @@ end
652
645
 
653
646
  ### 3.7 Multiline
654
647
 
655
- 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).
656
649
 
657
650
  ```ruby
658
- table = TTY::Table.new [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
659
- table.render :ascii, multiline: true
651
+ table = TTY::Table.new([["First", "1"], ["Multi\nLine\nContent", "2"], ["Third", "3"]])
652
+ table.render(:ascii, multiline: true)
660
653
  # =>
661
654
  # +-------+-+
662
655
  # |First |1|
@@ -670,7 +663,7 @@ table.render :ascii, multiline: true
670
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.
671
664
 
672
665
  ```ruby
673
- table = TTY::Table.new [["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
666
+ table = TTY::Table.new [["First", "1"], ["Multiline\nContent", "2"], ["Third", "3"]]
674
667
  table.render :ascii, multiline: false
675
668
  # =>
676
669
  # +------------------+-+
@@ -712,15 +705,15 @@ table.render(:ascii, padding: [1,2,1,2])
712
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:
713
706
 
714
707
  ```ruby
715
- table = TTY::Table.new header: ['head1', 'head2']
708
+ table = TTY::Table.new(header: ["head1", "head2"])
716
709
  table << ["Multi\nLine", "Text\nthat\nwraps"]
717
- table << ["Some\nother\ntext", 'Simple']
710
+ table << ["Some\nother\ntext", "Simple"]
718
711
  ```
719
712
 
720
- would render as:
713
+ This would render as:
721
714
 
722
715
  ```ruby
723
- table.render :ascii, multiline: true, padding: [1,2,1,2]
716
+ table.render(:ascii, multiline: true, padding: [1,2,1,2])
724
717
  # =>
725
718
  # +---------+----------+
726
719
  # | | |
@@ -742,17 +735,30 @@ table.render :ascii, multiline: true, padding: [1,2,1,2]
742
735
 
743
736
  ### 3.9 Resize
744
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
+
745
744
  ### 3.10 Width
746
745
 
747
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.
748
747
 
749
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.
750
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
+
751
760
  ```ruby
752
- header = ['h1', 'h2', 'h3']
753
- rows = [['aaa1', 'aa2', 'aaaaaaa3'], ['b1', 'b2', 'b3']]
754
- table = TTY::Table.new header, rows
755
- table.render width: 80, resize: true
761
+ table.render(width: 80, resize: true)
756
762
  # =>
757
763
  # +---------+-------+------------+
758
764
  # |h1 |h2 |h3 |