tty-table 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/README.md +458 -142
  4. data/lib/tty-table.rb +6 -6
  5. data/lib/tty/table.rb +34 -34
  6. data/lib/tty/table/alignment_set.rb +73 -0
  7. data/lib/tty/table/border.rb +54 -36
  8. data/lib/tty/table/border/null.rb +4 -4
  9. data/lib/tty/table/{columns.rb → column_constraint.rb} +30 -32
  10. data/lib/tty/table/column_set.rb +18 -17
  11. data/lib/tty/table/field.rb +50 -25
  12. data/lib/tty/table/header.rb +6 -2
  13. data/lib/tty/table/indentation.rb +7 -12
  14. data/lib/tty/table/operation/alignment.rb +59 -0
  15. data/lib/tty/table/operation/escape.rb +1 -1
  16. data/lib/tty/table/operation/filter.rb +1 -1
  17. data/lib/tty/table/operation/padding.rb +12 -61
  18. data/lib/tty/table/operation/truncation.rb +2 -2
  19. data/lib/tty/table/operation/wrapped.rb +2 -5
  20. data/lib/tty/table/operations.rb +35 -17
  21. data/lib/tty/table/orientation/vertical.rb +4 -4
  22. data/lib/tty/table/renderer.rb +1 -7
  23. data/lib/tty/table/renderer/basic.rb +69 -63
  24. data/lib/tty/table/version.rb +1 -1
  25. data/spec/spec_helper.rb +3 -4
  26. data/spec/unit/access_spec.rb +8 -8
  27. data/spec/unit/{operation/alignment_set → alignment_set}/each_spec.rb +1 -1
  28. data/spec/unit/{operation/alignment_set → alignment_set}/new_spec.rb +4 -4
  29. data/spec/unit/{operation/alignment_set → alignment_set}/to_ary_spec.rb +1 -1
  30. data/spec/unit/alignment_spec.rb +71 -0
  31. data/spec/unit/border/ascii/rendering_spec.rb +12 -12
  32. data/spec/unit/border/new_spec.rb +2 -2
  33. data/spec/unit/border/null/rendering_spec.rb +2 -2
  34. data/spec/unit/border/unicode/rendering_spec.rb +10 -10
  35. data/spec/unit/{columns → column_constraint}/enforce_spec.rb +15 -12
  36. data/spec/unit/{columns → column_constraint}/widths_spec.rb +6 -6
  37. data/spec/unit/column_set/extract_widths_spec.rb +39 -6
  38. data/spec/unit/data_spec.rb +4 -6
  39. data/spec/unit/each_spec.rb +8 -23
  40. data/spec/unit/each_with_index_spec.rb +27 -33
  41. data/spec/unit/field/length_spec.rb +23 -9
  42. data/spec/unit/field/width_spec.rb +1 -1
  43. data/spec/unit/filter_spec.rb +7 -8
  44. data/spec/unit/header/new_spec.rb +6 -15
  45. data/spec/unit/indentation/indent_spec.rb +21 -0
  46. data/spec/unit/new_spec.rb +73 -0
  47. data/spec/unit/operation/{alignment_set → alignment}/call_spec.rb +1 -1
  48. data/spec/unit/operation/escape/call_spec.rb +2 -3
  49. data/spec/unit/operation/filter/call_spec.rb +2 -3
  50. data/spec/unit/operation/truncation/call_spec.rb +6 -8
  51. data/spec/unit/operation/wrapped/call_spec.rb +15 -8
  52. data/spec/unit/operations/new_spec.rb +1 -1
  53. data/spec/unit/orientation_spec.rb +6 -6
  54. data/spec/unit/padding_spec.rb +29 -32
  55. data/spec/unit/properties_spec.rb +4 -4
  56. data/spec/unit/render_repeat_spec.rb +42 -0
  57. data/spec/unit/render_spec.rb +1 -1
  58. data/spec/unit/render_with_spec.rb +3 -3
  59. data/spec/unit/renderer/ascii/coloring_spec.rb +70 -0
  60. data/spec/unit/renderer/ascii/multiline_spec.rb +101 -0
  61. data/spec/unit/renderer/ascii/padding_spec.rb +37 -10
  62. data/spec/unit/renderer/ascii/render_spec.rb +4 -4
  63. data/spec/unit/renderer/ascii/resizing_spec.rb +22 -22
  64. data/spec/unit/renderer/ascii/separator_spec.rb +1 -1
  65. data/spec/unit/renderer/basic/alignment_spec.rb +20 -20
  66. data/spec/unit/renderer/basic/coloring_spec.rb +43 -28
  67. data/spec/unit/renderer/basic/filter_spec.rb +3 -3
  68. data/spec/unit/renderer/basic/multiline_spec.rb +74 -0
  69. data/spec/unit/renderer/basic/options_spec.rb +9 -9
  70. data/spec/unit/renderer/basic/padding_spec.rb +26 -2
  71. data/spec/unit/renderer/basic/render_spec.rb +4 -4
  72. data/spec/unit/renderer/basic/resizing_spec.rb +18 -18
  73. data/spec/unit/renderer/basic/separator_spec.rb +1 -1
  74. data/spec/unit/renderer/basic/truncation_spec.rb +6 -6
  75. data/spec/unit/renderer/basic/wrapping_spec.rb +3 -3
  76. data/spec/unit/renderer/border_spec.rb +4 -4
  77. data/spec/unit/renderer/unicode/coloring_spec.rb +70 -0
  78. data/spec/unit/renderer/unicode/indentation_spec.rb +1 -1
  79. data/spec/unit/renderer/unicode/padding_spec.rb +26 -26
  80. data/spec/unit/renderer/unicode/render_spec.rb +4 -4
  81. data/spec/unit/renderer/unicode/separator_spec.rb +1 -1
  82. data/spec/unit/to_s_spec.rb +4 -11
  83. data/spec/unit/utf_spec.rb +33 -0
  84. data/tty-table.gemspec +2 -1
  85. metadata +52 -32
  86. data/lib/tty/table/operation/alignment_set.rb +0 -103
  87. data/lib/tty/table/padder.rb +0 -180
  88. data/lib/tty/table/renderer/color.rb +0 -12
  89. data/spec/unit/indentation/insert_indent_spec.rb +0 -27
  90. data/spec/unit/initialize_spec.rb +0 -88
  91. data/spec/unit/padder/parse_spec.rb +0 -45
  92. data/spec/unit/padder/to_s_spec.rb +0 -14
  93. data/spec/unit/renderer/basic/multiline_content_spec.rb +0 -135
  94. data/spec/unit/renderer/style_spec.rb +0 -72
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fd8b00601ae34976c7045b404d50fabf7729279
4
- data.tar.gz: 446d1950b5192161cab664732b9caeeec25bd186
3
+ metadata.gz: 4c9b59ba29023d88b80d1337c882ed5a8a21994e
4
+ data.tar.gz: 15ead8cc776eaa5a61920719a1b2252a04505827
5
5
  SHA512:
6
- metadata.gz: 821d23c84214d24ba025b652bfecc77c155ffce1af44e9a575d3f8187b3165393e1e2085740570b359a6e4d9d7c4607b784fc1a863090512fd248fa762f55e8c
7
- data.tar.gz: e3a93020ff3ab85e8afbca2664c7a6034cb385a2d2fce62e37563ea14c37255e0a5a18ab6ebaad72a1c2d7fd29933614212d43843300ed866959b7a4251beecb
6
+ metadata.gz: 90fa0140b4962bacd8aa1fc51fdb33e4390113ad915b3b9c4789e237073507210276c0ea4f3df6ead55c2f5b820aaa737deab5ea70e88366d196f6718141f243
7
+ data.tar.gz: 87f0906fffa4b503fc4d02bffcb4c0eb909730b76cff707a9aeaca9374f6c1118a359c03d364dac28db6645b37ed2fcb3f9340d91f8162c0ebb67bcce1e2ac05
data/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ 0.2.0 (Mar 30, 2015)
2
+
3
+ * Add UTF-8 support for operations
4
+ * Add AlignmentSet for alignments storage
5
+ * Add tests for multilne column widths
6
+
7
+ * Remove padding from wrapped operation to fully rely on Verse.wrap
8
+ * Remove color renderer
9
+ * Remove adjust_padding from Columns
10
+
11
+ * Change Table each_with_index to iterate over rows
12
+ * Change Alignment operation to use AlignmentSet
13
+ * Change Columns to directly depend on table data
14
+ * Change Indentation to stop relying on renderer
15
+ * Change Border to accept padding as argument
16
+ * Change and extract padding operation
17
+ * Change Columns to ColumnConstraint and refactor enforce
18
+
19
+ * Fix table rendering for UTF-8 content
20
+ * Fix alignment to allow for individual field alignment
21
+ * Fix bug with padding operation
22
+ * Fix table border and content coloring
23
+ * Fix bug with table rerendering to allow for multiple renders
24
+ * Fix bug with ANSI codes in table content
data/README.md CHANGED
@@ -14,6 +14,14 @@
14
14
 
15
15
  **TTY::Table** provides independent table formatting component for [TTY](https://github.com/peter-murach/tty) toolkit.
16
16
 
17
+ ## Features
18
+
19
+ * Table behaves like an array with familiar API [see](#2-interface)
20
+ * Create table once and render using custom view renderers [see](#3-rendering)
21
+ * Rendering provides many display options [see](#33-options)
22
+ * Easy custom border creation [see](#352-custom)
23
+ * Supports multibyte character encodings
24
+
17
25
  ## Installation
18
26
 
19
27
  Add this line to your application's Gemfile:
@@ -34,14 +42,28 @@ Or install it yourself as:
34
42
 
35
43
  * [1. Usage](#1-usage)
36
44
  * [2. Interface](#2-interface)
37
- * [2.1 Initializaation](#21-initialization)
38
- * [2.2 Rendering](#22-rendering)
39
- * [2.3 Multiline](#23-multiline)
40
- * [2.4 Border](#24-border)
41
- * [2.5 Alignment](#25-alignment)
42
- * [2.6 Padding](#26-padding)
43
- * [2.7 Filter](#27-filter)
44
- * [2.8 Width](#28-width)
45
+ * [2.1 Initialization](#21-initialization)
46
+ * [2.2 Iteration](#22-iteration)
47
+ * [2.3 Access](#23-access)
48
+ * [2.4 Size](#24-size)
49
+ * [3. Rendering](#3-rendering)
50
+ * [3.1 Render](#31-render)
51
+ * [3.2 Renderer](#32-renderer)
52
+ * [3.1.1 Basic renderer](#311-basic)
53
+ * [3.1.2 ASCII renderer](#312-ascii-renderer)
54
+ * [3.1.3 Unicode renderer](#313-unicode-renderer)
55
+ * [3.3 Options](#33-options)
56
+ * [3.4 Alignment](#34-alignment)
57
+ * [3.5 Border](#35-border)
58
+ * [3.5.1 Parts](#351-parts)
59
+ * [3.5.2 Custom](#352-custom)
60
+ * [3.5.3 Separator](#353-separator)
61
+ * [3.5.4 Style](#354-style)
62
+ * [3.6 Filter](#36-filter)
63
+ * [3.7 Multiline](#37-multiline)
64
+ * [3.8 Padding](#38-padding)
65
+ * [3.9 Resize](#39-resize)
66
+ * [3.10 Width](#310-width)
45
67
 
46
68
  ## 1. Usage
47
69
 
@@ -51,10 +73,10 @@ First, provide **TTY::Table** with headers and data:
51
73
  table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
52
74
  ```
53
75
 
54
- Then simply call `render` or `to_s` on the instance:
76
+ Then simply call `render` on the instance with with border type as first argument:
55
77
 
56
78
  ```ruby
57
- table.render
79
+ table.render(:ascii)
58
80
  # =>
59
81
  +-------+-------+
60
82
  |header1|header2|
@@ -69,12 +91,34 @@ table.render
69
91
 
70
92
  ### 2.1 Initialization
71
93
 
72
- To instantiate **TTY::Table** pass 2-dimensional array:
94
+ **TTY::Table** can be created in variety of ways. The easiest way is to pass 2-dimensional array:
73
95
 
74
96
  ```ruby
75
97
  table = TTY::Table[['a1', 'a2'], ['b1', 'b2']]
76
98
  table = TTY::Table.new [['a1', 'a2'], ['b1', 'b2']]
77
99
  table = TTY::Table.new rows: [['a1', 'a2'], ['b1', 'b2']]
100
+ ```
101
+
102
+ Alternatively you can specify rows one by one inside block:
103
+
104
+ ```ruby
105
+ table = TTY::Table.new do |t|
106
+ t << ['a1', 'a2']
107
+ t << ['b1', 'b2']
108
+ end
109
+ ```
110
+
111
+ You can add rows of data after initialization:
112
+
113
+ ```ruby
114
+ table = TTY::Table.new
115
+ table << ['a1','a2']
116
+ table << ['b1','b2']
117
+ ```
118
+
119
+ In addition to rows you can specify table header:
120
+
121
+ ```ruby
78
122
  table = TTY::Table.new ['h1', 'h2'], [['a1', 'a2'], ['b1', 'b2']]
79
123
  table = TTY::Table.new header: ['h1', 'h2'], rows: [['a1', 'a2'], ['b1', 'b2']]
80
124
  ```
@@ -85,98 +129,301 @@ or cross header with rows inside a hash like so
85
129
  table = TTY::Table.new [{'h1' => ['a1', 'a2'], 'h2' => ['b1', 'b2']}]
86
130
  ```
87
131
 
88
- Table behaves like an Array so `<<`, `each` and familiar methods can be used
132
+ ### 2.2 Iteration
133
+
134
+ Table behaves like an Array so `<<`, `each` and familiar methods can be used:
89
135
 
90
136
  ```ruby
91
137
  table << ['a1', 'a2', 'a3']
92
138
  table << ['b1', 'b2', 'b3']
93
139
  table << ['a1', 'a2'] << ['b1', 'b2'] # chain rows assignment
140
+ ```
141
+
142
+ In order to iterate over table rows including headers do:
143
+
144
+ ```ruby
145
+ table.each { |row| ... } # iterate over rows
146
+ table.each_with_index { |row, index| ... } # iterate over rows with an index
147
+ ```
148
+
149
+ ### 2.3 Access
150
+
151
+ In order to referene the row at `index` do:
152
+
153
+ ```ruby
154
+ table = TTY::Table.new [['a1','a2'], ['b1','b2']]
155
+ table[0] # => ['a1','a2']
156
+ table.row(0) # => ['a1','a2']
157
+ table.row(i) { |row| ... } # return array for row(i)
158
+ ```
159
+
160
+ Negative indices count backwards from the end of table data (`-1` is the last element):
161
+
162
+ ```ruby
163
+ table[-1] # => ['b1','b2']
164
+ ```
165
+
166
+ To reference element at given row(i) and column(j) do:
167
+
168
+ ```ruby
169
+ table[i, j] # return element at row(i) and column(j)
170
+ table[0,0] # => 'a1'
171
+ ```
172
+
173
+ To specifically reference column(j) do:
94
174
 
95
- table.each { |row| ... } # iterate over rows
96
- table.each_with_index # iterate over each element with row and column index
97
- table[i, j] # return element at row(i) and column(j)
98
- table.row(i) { ... } # return array for row(i)
175
+ ```ruby
99
176
  table.column(j) { ... } # return array for column(j)
177
+ table.column(0) # => ['a1','b1']
100
178
  table.column(name) # return array for column(name), name of header
101
- table.row_size # return row size
102
- table.column_size # return column size
103
- table.size # return an array of [row_size, column_size]
104
- table.border # specify border properties
105
179
  ```
106
180
 
107
- or pass your rows in a block
181
+ An `IndexError` is raised for indexes outside of data range.
182
+
183
+ ### 2.4 Size
184
+
185
+ In order to query the number of rows, columns or size do:
186
+
187
+ ```ruby
188
+ table.rows_size # return row size
189
+ table.columns_size # return column size
190
+ table.size # return an array of [row_size, column_size]
191
+ ```
192
+
193
+ ### 2.5 Orientation
194
+
195
+ ## 3 Rendering
196
+
197
+ **TTY-Table** rendering process means you can create tabular data once and then create different renderers to match your needs for formatting the data.
198
+
199
+ ### 3.1 Render
200
+
201
+ Given a table:
202
+
203
+ ```ruby
204
+ table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
205
+ ```
206
+
207
+ 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:
208
+
209
+ ```ruby
210
+ table.render(:basic)
211
+ # =>
212
+ header1 header2
213
+ a1 a2
214
+ b1 b2
215
+ ```
216
+
217
+ This will use so called `:basic` renderer with default options. The other renderers are `:ascii` and `:unicode`.
218
+
219
+ The `render` method can accept as a second argument the [rendering options](#33-options) either as hash value:
220
+
221
+ ```ruby
222
+ table.render(:basic, alignments: [:left, :center])
223
+ ```
224
+
225
+ or inside a block:
108
226
 
109
227
  ```ruby
110
- table = TTY::Table.new do |t|
111
- t << ['a1', 'a2', 'a3']
112
- t << ['b1', 'b2', 'b3']
228
+ table.render(:basic) do |renderer|
229
+ renderer.alignments= [:left, :center]
113
230
  end
114
231
  ```
115
232
 
116
- ### 2.2 Rendering
233
+ ### 3.2 Renderer
234
+
235
+ **TTY::Table** has a definition of `TTY::Table::Renderer` which allows you to provide different view for your tabular data. It comes with few initial renderers built in such as `TTY::Table::Renderer::Basic`, `TTY::Table::Renderer::ASCII` and `TTY::Table::Renderer:Unicode`.
117
236
 
118
- Once you have an instance of `TTY::Table` you can print it out to the stdout like so:
237
+ Given a table of data:
119
238
 
120
239
  ```ruby
121
- table.to_s
240
+ table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
241
+ ```
122
242
 
123
- a1 a2 a3
124
- b1 b2 b3
243
+ You can create a special renderer for it:
244
+
245
+ ```ruby
246
+ multi_renderer = TTY::Table::Renderer::Basic.new(table, multiline: true)
125
247
  ```
126
248
 
127
- This will use so called `basic` renderer with default options.
249
+ and then call `render`
128
250
 
129
- However, you can include other customization options such as
251
+ ```ruby
252
+ multi_renderer.render
253
+ ```
254
+
255
+ This way, you create tabular data once and then create different renderers to match your needs for formatting the data.
256
+
257
+ #### 3.2.1 Basic Renderer
258
+
259
+ The basic render allows for formatting table with whitespace without any border:
260
+
261
+ ```ruby
262
+ renderer = TTY::Table::Renderer::Basic.new(table)
263
+ ```
264
+
265
+ ```ruby
266
+ renderer.render
267
+ # =>
268
+ header1 header2
269
+ a1 a2
270
+ b1 b2
271
+ ```
272
+
273
+ This is the same as calling `render` directly on table:
274
+
275
+ ```ruby
276
+ table.render
277
+ ```
278
+
279
+ #### 3.2.2 ASCII Renderer
280
+
281
+ The ascii renderer allows for formatting table with ASCII type border.
282
+
283
+ Create an instance of ASCII renderer:
284
+
285
+ ```ruby
286
+ renderer = TTY::Table::Renderer::ASCII.new(table)
287
+ ```
288
+
289
+ and then call `render` to get the formatted data:
290
+
291
+ ```ruby
292
+ renderer.render
293
+ # =>
294
+ +-------+-------+
295
+ |header1|header2|
296
+ +-------+-------+
297
+ |a1 |a2 |
298
+ |b1 |b2 |
299
+ +-------+-------+
300
+ ```
301
+
302
+ This is the same as calling `render` directly on table instance with `:ascii` as the first argument:
130
303
 
131
304
  ```ruby
132
- border # hash of border properties out of :characters, :style, :separator keys
133
- border_class # a type of border to use
134
- column_widths # array of maximum columns widths
135
- column_aligns # array of cell alignments out of :left, :center and :right, default :left
305
+ table.render(:ascii)
306
+ ```
307
+
308
+ #### 3.2.3 Unicode Renderer
309
+
310
+ The uniocde renderer allows for formatting table with Unicode type border.
311
+
312
+ Create an instance of Unicode renderer:
313
+
314
+ ```ruby
315
+ renderer = TTY::Table::Renderer::Unicode.new(table)
316
+ ```
317
+
318
+ and then call `render` to get the formatted data:
319
+
320
+ ```ruby
321
+ renderer.render
322
+ # =>
323
+ ┌───────┬───────┐
324
+ │header1│header2│
325
+ ├───────┼───────┤
326
+ │a1 │a2 │
327
+ │b1 │b2 │
328
+ └───────┴───────┘
329
+ ```
330
+
331
+ This is the same as calling `render` directly on table instance with `:unicode` as the first argument:
332
+
333
+ ```ruby
334
+ table.render(:unicode)
335
+ ```
336
+
337
+ ### 3.3 Options
338
+
339
+ Rendering of **TTY-Table** includes numerous customization options:
340
+
341
+ ```ruby
342
+ alignments # array of cell alignments out of :left, :center and :right,
343
+ # default :left
344
+ border # hash of border options - :characters, :style and :separator
345
+ border_class # a type of border to use such as TTY::Table::Border::Null,
346
+ # TTY::Table::Border::ASCII, TTY::Table::Border::Unicode
347
+ column_widths # array of maximum column widths
136
348
  filter # a proc object that is applied to every field in a row
137
- indent # indentation applied to rendered table
349
+ indent # indentation applied to rendered table, by default 0
138
350
  multiline # if true will wrap text at new line or column width,
139
351
  # when false will escape special characters
140
- orientation # either :horizontal or :vertical
141
- padding # array of integers to set table fields padding
142
- renderer # enforce display type out of :basic, :color, :unicode, :ascii
143
- resize # if true will expand/shrink table column sizes to match the width,
144
- # otherwise if false rotate table vertically
145
- width # constrain the table total width, otherwise dynamically
146
- # calculated from content and terminal size
352
+ padding # array of integers to set table fields padding,
353
+ # by default [0,0,0,0]
354
+ resize # if true will expand/shrink table column sizes to match
355
+ # the terminal width, otherwise if false will rotate
356
+ # table vertically. By default set to false
357
+ width # constrain the table total width, by default dynamically
358
+ # calculated based on content and terminal size
147
359
  ```
148
360
 
149
- ### 2.3 Multiline
361
+ The `render` method can accept as a second argument the above options either as hash value:
362
+
363
+ ```ruby
364
+ table.render(:basic, alignments: [:left, :center])
365
+ ```
150
366
 
151
- Renderer options may include `multiline` parameter. The `true` value will cause the table fields wrap at their natural line breaks or in case when the column widths are set the content will wrap.
367
+ or inside a block:
152
368
 
153
369
  ```ruby
154
- table = TTY::Table.new [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
155
- table.render :ascii, multiline: true
370
+ table.render(:basic) do |renderer|
371
+ renderer.alignments= [:left, :center]
372
+ end
373
+ ```
374
+
375
+ ### 3.4 Alignment
376
+
377
+ By default all columns are `:left` aligned.
378
+
379
+ You can align each column individuall by passing `alignments` option to table renderer:
380
+
381
+ ```ruby
382
+ table.render :ascii, alignments: [:center, :right]
156
383
  # =>
157
- +-------+-+
158
- |First |1|
159
- |Multi |2|
160
- |Line | |
161
- |Content| |
162
- |Third |3|
163
- +-------+-+
384
+ +-------+-------+
385
+ |header1|header2|
386
+ +-------+-------+
387
+ | a1 | a2|
388
+ | b1 | b2|
389
+ +-------+-------+
164
390
  ```
165
391
 
166
- When the `false` option is specified all the special characters will be escaped and if the column widths are set the content will be truncated like so
392
+ Alternatively you can align all columns with `alignment` option:
167
393
 
168
394
  ```ruby
169
- table = TTY::Table.new [ ["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
170
- table.render :ascii, multiline: false
395
+ table.render :ascii, alignment: [:center]
171
396
  # =>
172
- +------------------+-+
173
- |First |1|
174
- |Multiline\nContent|2|
175
- |Third |3|
176
- +------------------+-+
397
+ +-------+-------+
398
+ |header1|header2|
399
+ +-------+-------+
400
+ | a1 | a2 |
401
+ | b1 | b2 |
402
+ +-------+-------+
403
+ ```
404
+
405
+ If you require a more granular alignment you can align individual fields in a row by passing `:alignment` option like so:
406
+
407
+ ```ruby
408
+ table = TTY::Table.new header: ['header1', 'header2']
409
+ table << [{value: 'a1', alignment: :right}, 'a2']
410
+ table << ['b1', {value: 'b2', alignment: :center}]
411
+ ```
412
+
413
+ and then simply render:
414
+
415
+ ```ruby
416
+ table.render(:ascii)
417
+ # =>
418
+ +-------+-------+
419
+ |header1|header2|
420
+ +-------+-------+
421
+ | a1|a2 |
422
+ |b1 | b2 |
423
+ +-------+-------+
177
424
  ```
178
425
 
179
- ### 2.4 Border
426
+ ### 3.5 Border
180
427
 
181
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:
182
429
 
@@ -192,6 +439,54 @@ table.render :unicode
192
439
  └───────┴───────┘
193
440
  ```
194
441
 
442
+ or by creating unicode renderer:
443
+
444
+ ```ruby
445
+ renderer = TTY::Table::Renderer::Unicode.new(table)
446
+ renderer.render
447
+ ```
448
+
449
+ #### 3.5.1 Parts
450
+
451
+ The following are available border parts:
452
+
453
+ | Part | ASCII | Unicode |
454
+ | ------------- |:-----:|:-------:|
455
+ | top | `-` | `─` |
456
+ | top_mid | `+` | `┬` |
457
+ | top_left | `+` | `┌` |
458
+ | top_right | `+` | `┐` |
459
+ | bottom | `-` | `─` |
460
+ | bottom_mid | `+` | `┴` |
461
+ | bottom_left | `+` | `└` |
462
+ | bottom_right | `+` | `┘` |
463
+ | mid | `-` | `─` |
464
+ | mid_mid | `+` | `┼` |
465
+ | mid_left | `+` | `├` |
466
+ | mid_right | `+` | `┤` |
467
+ | left | `|` | `│` |
468
+ | center | `|` | `│` |
469
+ | right | `|` | `│` |
470
+
471
+ Using the above border parts you can create your own border with the `border` helper:
472
+
473
+ ```ruby
474
+ table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
475
+ table.render do |renderer|
476
+ renderer.border do
477
+ mid '='
478
+ mid_mid ' '
479
+ end
480
+ end
481
+ # =>
482
+ header1 header2
483
+ ======= =======
484
+ a1 a2
485
+ b1 b2
486
+ ```
487
+
488
+ #### 3.5.2 Custom
489
+
195
490
  You can also create your own custom border by subclassing `TTY::Table::Border` and implementing the `def_border` method using internal DSL methods like so:
196
491
 
197
492
  ```ruby
@@ -219,22 +514,7 @@ table.render_with MyBorder
219
514
  * * *
220
515
  ```
221
516
 
222
- Finally, if you want to introduce slight modifications to the predefined border types, you can use table `border` helper like so
223
-
224
- ```ruby
225
- table = TTY::Table.new ['header1', 'header2'], [['a1', 'a2'], ['b1', 'b2']
226
- table.render do |renderer|
227
- renderer.border do
228
- mid '='
229
- mid_mid ' '
230
- end
231
- end
232
- # =>
233
- header1 header2
234
- ======= =======
235
- a1 a2
236
- b1 b2
237
- ```
517
+ #### 3.5.3 Separator
238
518
 
239
519
  In addition to specifying border characters you can force table to render separator line on each row like:
240
520
 
@@ -253,75 +533,19 @@ end
253
533
  +-------+-------+
254
534
  ```
255
535
 
256
- Also to change the display color of your border do:
536
+ #### 3.5.4 Style
257
537
 
258
- ```ruby
259
- table.render do |renderer|
260
- renderer.border.style = :red
261
- end
262
- ```
263
-
264
- ### 2.5 Alignment
265
-
266
- All columns are left aligned by default. You can enforce per column alignment by passing `column_aligns` option like so
267
-
268
- ```ruby
269
- rows = [['a1', 'a2'], ['b1', 'b2']
270
- table = TTY::Table.new rows: rows
271
- table.render column_aligns: [:center, :right]
272
- ```
273
-
274
- To align a single column do
275
-
276
- ```ruby
277
- table.align_column(1, :right)
278
- ```
279
-
280
- If you require a more granular alignment you can align individual fields in a row by passing `align` option
538
+ If you want to change the display color of your border do:
281
539
 
282
540
  ```ruby
283
- table = TTY::Table.new do |t|
284
- t << ['a1', 'a2', 'a3']
285
- t << ['b1', {:value => 'b2', :align => :right}, 'b3']
286
- t << ['c1', 'c2', {:value => 'c3', :align => :center}]
541
+ table.render do |renderer|
542
+ renderer.border.style = :green
287
543
  end
288
544
  ```
289
545
 
290
- #### 2.6 Padding
291
-
292
- By default padding is not applied. You can add `padding` to table fields like so
293
-
294
- ```ruby
295
- header = ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra']
296
- rows = [['id', 'int(11)', 'YES', 'nil', 'NULL', '']]
297
- table = TTY::Table.new(header, rows)
298
- table.render { |renderer| renderer.padding= [0,1,0,1] }
299
- # =>
300
- +-------+---------+------+-----+---------+-------+
301
- | Field | Type | Null | Key | Default | Extra |
302
- +-------+---------+------+-----+---------+-------+
303
- | id | int(11) | YES | nil | NULL | |
304
- +-------+---------+------+-----+---------+-------+
305
- ```
306
-
307
- or you can set specific padding using `right`, `left`, `top`, `bottom` helpers. However, when adding top or bottom padding a `multiline` option needs to be set to `true` to allow for rows to span multiple lines. For example
546
+ All [supported colors](https://github.com/peter-murach/pastel#3-supported-colors) are provided by the **Pastel** dependency.
308
547
 
309
- ```ruby
310
- table.render { |renderer|
311
- renderer.multiline = true
312
- renderer.padding.top = 1
313
- }
314
- # =>
315
- +-----+-------+----+---+-------+-----+
316
- | | | | | | |
317
- |Field|Type |Null|Key|Default|Extra|
318
- +-----+-------+----+---+-------+-----+
319
- | | | | | | |
320
- |id |int(11)|YES |nil|NULL | |
321
- +-----+-------+----+---+-------+-----+
322
- ```
323
-
324
- ### 2.7 Filter
548
+ ### 3.6 Filter
325
549
 
326
550
  You can define filters that will modify individual table fields value before they are rendered. A filter can be a callable such as proc. Here's an example that formats
327
551
 
@@ -356,7 +580,99 @@ table.render do |renderer|
356
580
  end
357
581
  ```
358
582
 
359
- ### 2.8 Width
583
+ ### 3.7 Multiline
584
+
585
+ 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).
586
+
587
+ ```ruby
588
+ table = TTY::Table.new [ ["First", '1'], ["Multi\nLine\nContent", '2'], ["Third", '3']]
589
+ table.render :ascii, multiline: true
590
+ # =>
591
+ +-------+-+
592
+ |First |1|
593
+ |Multi |2|
594
+ |Line | |
595
+ |Content| |
596
+ |Third |3|
597
+ +-------+-+
598
+ ```
599
+
600
+ 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.
601
+
602
+ ```ruby
603
+ table = TTY::Table.new [["First", '1'], ["Multiline\nContent", '2'], ["Third", '3']]
604
+ table.render :ascii, multiline: false
605
+ # =>
606
+ +------------------+-+
607
+ |First |1|
608
+ |Multiline\nContent|2|
609
+ |Third |3|
610
+ +------------------+-+
611
+ ```
612
+
613
+ ### 3.8 Padding
614
+
615
+ Renderer also accepts `padding` option which accepts array with arguments similar to CSS padding.
616
+
617
+ ```ruby
618
+ [2,2,2,2] # => pad left and right with 2 characters, add 2 lines above and below
619
+ [1,2] # => pad left and right with 2 characters, add 1 line above and below
620
+ 1 # => pad left and right with 1 character, and 1 lines above and below
621
+ ```
622
+
623
+ Therefore, to apply padding to the example table do:
624
+
625
+ ```ruby
626
+ table.render(:ascii, padding: [1,2,1,2])
627
+ # =>
628
+ +---------+---------+
629
+ | | |
630
+ | header1 | header2 |
631
+ | | |
632
+ +---------+---------+
633
+ | | |
634
+ | a1 | a2 |
635
+ | | |
636
+ | | |
637
+ | b1 | b2 |
638
+ | | |
639
+ +---------+---------+
640
+ ```
641
+
642
+ 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:
643
+
644
+ ```ruby
645
+ table = TTY::Table.new header: ['head1', 'head2']
646
+ table << ["Multi\nLine", "Text\nthat\nwraps"]
647
+ table << ["Some\nother\ntext", 'Simple']
648
+ ```
649
+
650
+ would render as:
651
+
652
+ ```ruby
653
+ table.render :ascii, multiline: true, padding: [1,2,1,2]
654
+ # =>
655
+ +---------+----------+
656
+ | | |
657
+ | h1 | head2 |
658
+ | | |
659
+ +---------+----------+
660
+ | | |
661
+ | Multi | Text |
662
+ | Line | that |
663
+ | | wraps |
664
+ | | |
665
+ | | |
666
+ | Some | Simple |
667
+ | other | |
668
+ | text | |
669
+ | | |
670
+ +---------+----------+
671
+ ```
672
+
673
+ ### 3.9 Resize
674
+
675
+ ### 3.10 Width
360
676
 
361
677
  To control table's column sizes pass `width`, `resize` options. By default table's natural column widths are calculated from the content. If the total table width does not fit in terminal window then the table is rotated vertically to preserve content.
362
678