tabulo 2.4.0 → 2.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +332 -122
- data/VERSION +1 -1
- data/assets/social_media_preview/table.png +0 -0
- data/lib/tabulo/border.rb +66 -102
- data/lib/tabulo/cell.rb +28 -17
- data/lib/tabulo/cell_data.rb +2 -1
- data/lib/tabulo/column.rb +28 -5
- data/lib/tabulo/table.rb +204 -86
- data/lib/tabulo/util.rb +22 -0
- data/lib/tabulo/version.rb +1 -1
- data/tabulo.gemspec +2 -2
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dde64a0fb26dfd5b5624fc18d4c442989cf81433371267d2ce52dbcac4137433
|
4
|
+
data.tar.gz: b0bc0295c6bb8fa5bbaf855ede1dc44fea722acfd23f2b74e1db68b36396e5df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dddd9de39377071967c86f75e65a175352f33966d52d39e5dd725012a7a5531415870b0e95a7ce546e9bec0af4c15a0fefd2a50eb3585a64a93ed99cae3bd37
|
7
|
+
data.tar.gz: b936c064abfab53617eb82f7977dd2f7b1957115990ea301d211de8f23661667ada82a92fd2dbdc9c34d0d741ce00b1a3dd03a655fe73b855f610cfb663fe298
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,33 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
### v2.6.2
|
4
|
+
|
5
|
+
* Ensure line break character sequences are property formatted in output, regardless
|
6
|
+
of whether they are "\r\n", "\r" or "\n".
|
7
|
+
|
8
|
+
### v2.6.1
|
9
|
+
|
10
|
+
* Update dependency versions
|
11
|
+
* Minor documentation improvements
|
12
|
+
|
13
|
+
### v2.6.0
|
14
|
+
|
15
|
+
* Add an additional, optional parameter to `styler`, `header_styler` and `title_styler`
|
16
|
+
callbacks, which will receive the index (0, 1 or etc.) of the line within the cell
|
17
|
+
being styled.
|
18
|
+
* Allow padding to be configured on a column-by-column basis.
|
19
|
+
* Minor documentation improvements.
|
20
|
+
|
21
|
+
### v2.5.0
|
22
|
+
|
23
|
+
* Add option of table title, together with options for styling and aligning the title
|
24
|
+
|
25
|
+
### v2.4.1
|
26
|
+
|
27
|
+
* Fix warnings under Ruby 2.7
|
28
|
+
* Fix minor error in README
|
29
|
+
* Minor documentation tweaks
|
30
|
+
|
3
31
|
### v2.4.0
|
4
32
|
|
5
33
|
* Add additional, optional `CellData` parameter to `styler` and `formatter` callbacks
|
data/README.md
CHANGED
@@ -7,24 +7,28 @@
|
|
7
7
|
[![Code Climate][CC img]][Code Climate]
|
8
8
|
[![Awesome][AR img]][Awesome Ruby]
|
9
9
|
|
10
|
-
Tabulo is a
|
10
|
+
Tabulo is a Ruby library for generating plain text tables (also known as “terminal tables”
|
11
|
+
or “ASCII tables”). It is both highly configurable and very easy to use.
|
12
|
+
|
13
|
+
<a name="overview"></a>
|
14
|
+
## Overview
|
11
15
|
|
12
16
|
_Quick API:_
|
13
17
|
|
14
18
|
```
|
15
|
-
> puts Tabulo::Table.new(User.all, :id, :first_name, :last_name
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
> puts Tabulo::Table.new(User.all, :id, :first_name, :last_name).pack
|
20
|
+
+----+------------+-----------+
|
21
|
+
| id | first_name | last_name |
|
22
|
+
+----+------------+-----------+
|
23
|
+
| 1 | John | Citizen |
|
24
|
+
| 2 | Jane | Doe |
|
25
|
+
+----+------------+-----------+
|
22
26
|
```
|
23
27
|
|
24
28
|
_Full API:_
|
25
29
|
|
26
30
|
```
|
27
|
-
table = Tabulo::Table.new(User.all
|
31
|
+
table = Tabulo::Table.new(User.all) do |t|
|
28
32
|
t.add_column("ID", &:id)
|
29
33
|
t.add_column("First name", &:first_name)
|
30
34
|
t.add_column("Last name") { |user| user.last_name.upcase }
|
@@ -33,48 +37,47 @@ end
|
|
33
37
|
|
34
38
|
```
|
35
39
|
> puts table.pack
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
+----+------------+-----------+
|
41
|
+
| ID | First name | Last name |
|
42
|
+
+----+------------+-----------+
|
43
|
+
| 1 | John | CITIZEN |
|
44
|
+
| 2 | Jane | DOE |
|
45
|
+
+----+------------+-----------+
|
42
46
|
```
|
43
47
|
|
48
|
+
<a name="features"></a>
|
44
49
|
## Features
|
45
50
|
|
46
|
-
* Presents a [DRY
|
47
|
-
|
48
|
-
with that of the body rows.
|
51
|
+
* Presents a [DRY API](#adding-columns) that is column-based, not row-based, meaning header and body rows are
|
52
|
+
automatically in sync
|
49
53
|
* Lets you set [fixed column widths](#fixed-column-widths), then either [wrap](#overflow-handling)
|
50
|
-
or [truncate](#overflow-handling) the overflow
|
51
|
-
* Alternatively,
|
52
|
-
|
53
|
-
*
|
54
|
-
|
55
|
-
* Tabulate any `Enumerable`: the underlying collection need not be an array
|
56
|
-
*
|
57
|
-
|
58
|
-
|
59
|
-
*
|
60
|
-
|
61
|
-
*
|
62
|
-
*
|
63
|
-
*
|
64
|
-
* Apply [colours](#colours-and-styling) and other styling to table content and borders, without breaking the table.
|
65
|
-
* Easily [transpose](#transposition) the table, so that rows are swapped with columns.
|
54
|
+
or [truncate](#overflow-handling) the overflow
|
55
|
+
* Alternatively, [“pack”](#pack) the table so that columns are auto-sized to their
|
56
|
+
contents, but [without overflowing the terminal](#max-table-width)
|
57
|
+
* Cell alignment is [configurable](#cell-alignment), but has helpful content-based defaults (numbers right, strings
|
58
|
+
left)
|
59
|
+
* Tabulate any `Enumerable`: the underlying collection need not be an array
|
60
|
+
* [Step through](#enumerator) your table a row at a time, printing as you go, without waiting for the
|
61
|
+
underlying collection to load. In other words, have a [streaming interface](#enumerator) for free.
|
62
|
+
* Add an optional [title](#title) to your table
|
63
|
+
* The header row can be [repeated](#repeating-headers) at arbitrary intervals
|
64
|
+
* Newlines within cell content are correctly handled
|
65
|
+
* Multibyte Unicode characters are correctly handled
|
66
|
+
* Apply [colours](#colours-and-styling) and other styling to table content and borders, without breaking the table
|
67
|
+
* Easily [transpose](#transposition) the table, so that rows are swapped with columns
|
66
68
|
* Choose from multiple [border configurations](#borders), including Markdown, “ASCII”, and smoothly
|
67
|
-
joined Unicode border characters
|
69
|
+
joined Unicode border characters
|
68
70
|
|
69
71
|
Tabulo has also been ported to Crystal (with some modifications): see [Tablo](https://github.com/hutou/tablo).
|
70
72
|
|
73
|
+
<a name="contents"></a>
|
71
74
|
## Contents
|
72
75
|
|
76
|
+
* [Overview](#overview)
|
73
77
|
* [Features](#features)
|
74
78
|
* [Table of contents](#table-of-contents)
|
75
79
|
* [Installation](#installation)
|
76
80
|
* [Detailed usage](#detailed-usage)
|
77
|
-
* [Requiring the gem](#requiring-the-gem)
|
78
81
|
* [Creating a table](#table-initialization)
|
79
82
|
* [Adding columns](#adding-columns)
|
80
83
|
* [Quick API](#quick-api)
|
@@ -82,18 +85,21 @@ Tabulo has also been ported to Crystal (with some modifications): see [Tablo](ht
|
|
82
85
|
* [Column labels _vs_ headers](#labels-headers)
|
83
86
|
* [Positioning columns](#column-positioning)
|
84
87
|
* [Removing columns](#removing-columns)
|
88
|
+
* [Adding a title](#title)
|
85
89
|
* [Cell alignment](#cell-alignment)
|
86
90
|
* [Column width, wrapping and truncation](#column-width-wrapping-and-truncation)
|
87
91
|
* [Configuring fixed widths](#configuring-fixed-widths)
|
88
92
|
* [Automating column widths](#automating-column-widths)
|
89
93
|
* [Configuring padding](#configuring-padding)
|
90
94
|
* [Overflow handling](#overflow-handling)
|
95
|
+
* [Manual cell wrapping](#manual-wrapping)
|
91
96
|
* [Formatting cell values](#formatting-cell-values)
|
92
97
|
* [Colours and other styling](#colours-and-styling)
|
93
98
|
* [Styling cell content](#styling-cell-content)
|
94
99
|
* [Styling column headers](#styling-column-headers)
|
100
|
+
* [Styling the table title](#styling-title)
|
95
101
|
* [Setting default styles](#default-styles)
|
96
|
-
* [Styling borders](#
|
102
|
+
* [Styling borders](#styling-borders)
|
97
103
|
* [Repeating headers](#repeating-headers)
|
98
104
|
* [Using a Table Enumerator](#using-a-table-enumerator)
|
99
105
|
* [Accessing cell values](#accessing-cell-values)
|
@@ -106,7 +112,7 @@ Tabulo has also been ported to Crystal (with some modifications): see [Tablo](ht
|
|
106
112
|
* [Contributing](#contributing)
|
107
113
|
* [License](#license)
|
108
114
|
|
109
|
-
## Installation
|
115
|
+
## Installation [↑](#contents)
|
110
116
|
|
111
117
|
Add this line to your application’s Gemfile:
|
112
118
|
|
@@ -122,16 +128,14 @@ Or install it yourself:
|
|
122
128
|
|
123
129
|
$ gem install tabulo
|
124
130
|
|
125
|
-
|
126
|
-
|
127
|
-
### Requiring the gem
|
131
|
+
To use the gem, you need to require it in your source code as follows:
|
128
132
|
|
129
133
|
```ruby
|
130
134
|
require 'tabulo'
|
131
135
|
```
|
132
136
|
|
133
137
|
<a name="table-initialization"></a>
|
134
|
-
### Creating a table
|
138
|
+
### Creating a table [↑](#contents)
|
135
139
|
|
136
140
|
You instantiate a `Tabulo::Table` by passing it an underlying `Enumerable`, being the collection of
|
137
141
|
things that you want to tabulate. Each member of this collection will end up
|
@@ -146,10 +150,10 @@ other_table = Tabulo::Table.new(User.all)
|
|
146
150
|
For the table to be useful, however, it must also contain columns…
|
147
151
|
|
148
152
|
<a name="adding-columns"></a>
|
149
|
-
### Adding columns
|
153
|
+
### Adding columns [↑](#contents)
|
150
154
|
|
151
155
|
<a name="quick-api"></a>
|
152
|
-
#### Quick API
|
156
|
+
#### Quick API [↑](#contents)
|
153
157
|
|
154
158
|
When the columns correspond to methods on members of the underlying enumerable, you can use
|
155
159
|
the “quick API”, by passing a symbol directly to `Tabulo::Table.new` for each column.
|
@@ -171,7 +175,7 @@ table = Tabulo::Table.new([1, 2, 5], :itself, :even?, :odd?)
|
|
171
175
|
```
|
172
176
|
|
173
177
|
<a name="full-api"></a>
|
174
|
-
#### Full API
|
178
|
+
#### Full API [↑](#contents)
|
175
179
|
|
176
180
|
Columns can also be added to the table one-by-one using `add_column`. This “full API” is
|
177
181
|
more verbose, but provides greater configurability:
|
@@ -240,7 +244,7 @@ end
|
|
240
244
|
```
|
241
245
|
|
242
246
|
<a name="labels-headers"></a>
|
243
|
-
#### Column labels _vs_ headers
|
247
|
+
#### Column labels _vs_ headers [↑](#contents)
|
244
248
|
|
245
249
|
The first argument to `add_column` is the called the _label_ for that column. It serves as the
|
246
250
|
column’s unique identifier: only one column may have a given label per table.
|
@@ -254,14 +258,14 @@ table.add_column(:itself2, header: "N", &:itself) # header need not be unique
|
|
254
258
|
```
|
255
259
|
|
256
260
|
<a name="column-positioning"></a>
|
257
|
-
#### Positioning columns
|
261
|
+
#### Positioning columns [↑](#contents)
|
258
262
|
|
259
263
|
By default, each new column is added to the right of all the other columns so far added to the
|
260
264
|
table. However, if you want to insert a new column into some other position, you can use the
|
261
265
|
`before` option, passing the label of the column to the left of which you want the new column to be added:
|
262
266
|
|
263
267
|
```ruby
|
264
|
-
table =
|
268
|
+
table = Tabulo::Table.new([1, 2, 3], :itself, :odd?)
|
265
269
|
table.add_column(:even?, before: :odd?)
|
266
270
|
```
|
267
271
|
|
@@ -277,7 +281,7 @@ table.add_column(:even?, before: :odd?)
|
|
277
281
|
```
|
278
282
|
|
279
283
|
<a name="removing-columns"></a>
|
280
|
-
### Removing columns
|
284
|
+
### Removing columns [↑](#contents)
|
281
285
|
|
282
286
|
There is also a `#remove_column` method, for deleting an existing column from a table. Pass it
|
283
287
|
the label of the column you want to remove:
|
@@ -286,8 +290,34 @@ the label of the column you want to remove:
|
|
286
290
|
table.remove_column(:even?)
|
287
291
|
```
|
288
292
|
|
293
|
+
<a name="title"></a>
|
294
|
+
### Adding a title [↑](#contents)
|
295
|
+
|
296
|
+
You can give your table a title, using the `title` option:
|
297
|
+
|
298
|
+
```ruby
|
299
|
+
table = Tabulo::Table.new([1, 2, 3], :itself, :even?, :odd?, title: "Numbers")
|
300
|
+
```
|
301
|
+
|
302
|
+
```
|
303
|
+
> puts table
|
304
|
+
+--------------------------------------------+
|
305
|
+
| Numbers |
|
306
|
+
+--------------+--------------+--------------+
|
307
|
+
| itself | even? | odd? |
|
308
|
+
+--------------+--------------+--------------+
|
309
|
+
| 1 | false | true |
|
310
|
+
| 2 | true | false |
|
311
|
+
| 3 | false | true |
|
312
|
+
+--------------+--------------+--------------+
|
313
|
+
```
|
314
|
+
|
315
|
+
There is a caveat: Using the `title` option with the `:markdown` [border type](#borders) will cause
|
316
|
+
the rendered table to cease being valid Markdown, as unfortunately almost no markdown engines support
|
317
|
+
adding a captions (i.e. titles) to tables.
|
318
|
+
|
289
319
|
<a name="cell-alignment"></a>
|
290
|
-
### Cell alignment
|
320
|
+
### Cell alignment [↑](#contents)
|
291
321
|
|
292
322
|
By default, column header text is center-aligned, while the content of each body cell is aligned
|
293
323
|
according to its data type. Numbers are right-aligned, text is left-aligned, and booleans (`false`
|
@@ -307,10 +337,17 @@ passing similarly-named options to `add_column`, e.g.:
|
|
307
337
|
table.add_column("Doubled", align_header: :right, align_body: :left) { |n| n * 2 }
|
308
338
|
```
|
309
339
|
|
310
|
-
|
340
|
+
If a table title is present, it is center-aligned by default. This can be changed using the
|
341
|
+
`align_title` option when initializing the table:
|
342
|
+
|
343
|
+
```ruby
|
344
|
+
table = Tabulo::Table.new([1, 2], :itself, :even?, title: "Numbers", align_title: :left)
|
345
|
+
```
|
346
|
+
|
347
|
+
### Column width, wrapping and truncation [↑](#contents)
|
311
348
|
|
312
349
|
<a name="fixed-column-widths"></a>
|
313
|
-
#### Configuring fixed widths
|
350
|
+
#### Configuring fixed widths [↑](#contents)
|
314
351
|
|
315
352
|
By default, column width is fixed at 12 characters, plus 1 character of padding on either side.
|
316
353
|
This can be adjusted on a column-by-column basis using the `width` option of `add_column`:
|
@@ -352,7 +389,7 @@ table = Tabulo::Table.new([1, 2], :itself, :even?, column_width: 6)
|
|
352
389
|
Widths set for individual columns always override the default column width for the table.
|
353
390
|
|
354
391
|
<a name="pack"></a>
|
355
|
-
#### Automating column widths
|
392
|
+
#### Automating column widths [↑](#contents)
|
356
393
|
|
357
394
|
Instead of setting column widths “manually”, you can tell the table to sort out the widths
|
358
395
|
itself, so that each column is just wide enough for its header and contents (plus a character
|
@@ -373,6 +410,26 @@ table.pack
|
|
373
410
|
+-------------------------+------+
|
374
411
|
```
|
375
412
|
|
413
|
+
If the table [title](#title) happens to be too long to for the existing width of the table, `pack`
|
414
|
+
will also arrange for the table to be widened sufficiently to accommodate it without wrapping:
|
415
|
+
|
416
|
+
```ruby
|
417
|
+
table = Tabulo::Table.new(["a", "b"], :itself, :size, title: "Here are some letters of the alphabet")
|
418
|
+
table.pack
|
419
|
+
```
|
420
|
+
|
421
|
+
```
|
422
|
+
> puts table
|
423
|
+
+---------------------------------------+
|
424
|
+
| Here are some letters of the alphabet |
|
425
|
+
+-------------------+-------------------+
|
426
|
+
| itself | size |
|
427
|
+
+-------------------+-------------------+
|
428
|
+
| a | 1 |
|
429
|
+
| b | 1 |
|
430
|
+
+-------------------+-------------------+
|
431
|
+
```
|
432
|
+
|
376
433
|
The `pack` method returns the table itself, so you can “pack-and-print” in one go:
|
377
434
|
|
378
435
|
```ruby
|
@@ -421,7 +478,7 @@ table itself. There are [ways around this](#freezing-a-table), however, if this
|
|
421
478
|
behaviour—see [below](#freezing-a-table).
|
422
479
|
|
423
480
|
<a name="configuring-padding"></a>
|
424
|
-
#### Configuring padding
|
481
|
+
#### Configuring padding [↑](#contents)
|
425
482
|
|
426
483
|
The single character of padding either side of each column is not counted in the column width.
|
427
484
|
The amount of this extra padding can be configured for the table as a whole, using the `column_padding`
|
@@ -435,14 +492,14 @@ table = Tabulo::Table.new([1, 2, 5], :itself, :even?, :odd?, column_padding: 0)
|
|
435
492
|
```
|
436
493
|
|
437
494
|
```
|
438
|
-
> puts table
|
439
|
-
|
440
|
-
|
|
441
|
-
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
495
|
+
> puts table.pack
|
496
|
+
+------+-----+-----+
|
497
|
+
|itself|even?| odd?|
|
498
|
+
+------+-----+-----+
|
499
|
+
| 1|false| true|
|
500
|
+
| 2| true|false|
|
501
|
+
| 5|false| true|
|
502
|
+
+------+-----+-----+
|
446
503
|
```
|
447
504
|
|
448
505
|
Passing an array of _two_ integers to this option configures the left and right padding for each
|
@@ -453,18 +510,42 @@ table = Tabulo::Table.new([1, 2, 5], :itself, :even?, :odd?, column_padding: [0,
|
|
453
510
|
```
|
454
511
|
|
455
512
|
```
|
456
|
-
> puts table
|
457
|
-
|
458
|
-
|
|
459
|
-
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
513
|
+
> puts table.pack
|
514
|
+
+--------+-------+-------+
|
515
|
+
|itself |even? | odd? |
|
516
|
+
+--------+-------+-------+
|
517
|
+
| 1 |false | true |
|
518
|
+
| 2 | true |false |
|
519
|
+
| 5 |false | true |
|
520
|
+
+--------+-------+-------+
|
521
|
+
```
|
522
|
+
|
523
|
+
Note how the padding amount is completely unaffected by the call `pack`.
|
524
|
+
|
525
|
+
Padding can also be configured on a column-by-column basis, using the `padding` option when calling
|
526
|
+
`add_column`:
|
527
|
+
|
528
|
+
```ruby
|
529
|
+
table = Tabulo::Table.new([1, 2, 5], :itself, :even?)
|
530
|
+
table.add_column(:odd?, padding: 3)
|
531
|
+
```
|
532
|
+
|
533
|
+
```
|
534
|
+
> puts table.pack
|
535
|
+
+--------+-------+-----------+
|
536
|
+
| itself | even? | odd? |
|
537
|
+
+--------+-------+-----------+
|
538
|
+
| 1 | false | true |
|
539
|
+
| 2 | true | false |
|
540
|
+
| 5 | false | true |
|
541
|
+
+--------+-------+-----------+
|
464
542
|
```
|
465
543
|
|
544
|
+
This column-level `padding` setting always overrides any table-level `column_padding` setting, for
|
545
|
+
the column in question.
|
546
|
+
|
466
547
|
<a name="overflow-handling"></a>
|
467
|
-
#### Overflow handling
|
548
|
+
#### Overflow handling [↑](#contents)
|
468
549
|
|
469
550
|
By default, if cell contents exceed their column width, they are wrapped for as many rows as
|
470
551
|
required:
|
@@ -515,8 +596,39 @@ table = Tabulo::Table.new(
|
|
515
596
|
The character used to indicate truncation, which defaults to `~`, can be configured using the
|
516
597
|
`truncation_indicator` option passed to `Table.new`.
|
517
598
|
|
599
|
+
<a name="manual-wrapping"></a>
|
600
|
+
#### Manual cell wrapping [↑](#contents)
|
601
|
+
|
602
|
+
You can “manually” wrap the content of a title, header or body cell at a particular
|
603
|
+
point, simply by placing a newline character, at that point:
|
604
|
+
|
605
|
+
```ruby
|
606
|
+
table = Tabulo::Table.new(1..3) do |t|
|
607
|
+
t.add_column("The number\nitself", &:itself)
|
608
|
+
t.add_column("Even?", &:even?)
|
609
|
+
t.add_column("Odd?", &:odd?)
|
610
|
+
end
|
611
|
+
```
|
612
|
+
|
613
|
+
```
|
614
|
+
> puts table
|
615
|
+
+--------------+--------------+--------------+
|
616
|
+
| The number | Even? | Odd? |
|
617
|
+
| itself | | |
|
618
|
+
+--------------+--------------+--------------+
|
619
|
+
| 1 | false | true |
|
620
|
+
| 2 | true | false |
|
621
|
+
| 3 | false | true |
|
622
|
+
+--------------+--------------+--------------+
|
623
|
+
```
|
624
|
+
|
625
|
+
Tabulo will treat any of the character combinations `"\n"`, `"\r\n"` or `"\r"` equally, as a line break,
|
626
|
+
regardless of the platform it’s currently being run on. This ensures things are formatted as
|
627
|
+
expected if, for example, you are examining content that was produced on another platform from
|
628
|
+
the one you’re running Tabulo on.
|
629
|
+
|
518
630
|
<a name="formatting-cell-values"></a>
|
519
|
-
### Formatting cell values
|
631
|
+
### Formatting cell values [↑](#contents)
|
520
632
|
|
521
633
|
While the callable passed to `add_column` determines the underyling, calculated value in each
|
522
634
|
cell of the column, there is a separate concept, of a “formatter”, that determines how
|
@@ -573,17 +685,17 @@ end
|
|
573
685
|
Formatters set for individual columns on calling `#add_column` always override the default formatter for
|
574
686
|
the table.
|
575
687
|
|
576
|
-
The `formatter` callback has an alternative, 2-parameter version. If `formatter` is passed
|
688
|
+
The `formatter` callback also has an alternative, 2-parameter version. If `formatter` is passed
|
577
689
|
a 2-parameter callable, the second parameter will be given a `CellData` instance,
|
578
690
|
containing additional information about the cell that may be useful in determining how to format
|
579
|
-
it—see the [documentation](https://www.rubydoc.info/gems/tabulo/2.
|
691
|
+
it—see the [documentation](https://www.rubydoc.info/gems/tabulo/2.6.2/Tabulo/CellData.html)
|
580
692
|
for details.
|
581
693
|
|
582
694
|
<a name="colours-and-styling"></a>
|
583
|
-
### Colours and other styling
|
695
|
+
### Colours and other styling [↑](#contents)
|
584
696
|
|
585
697
|
<a name="styling-cell-content"></a>
|
586
|
-
#### Styling cell content
|
698
|
+
#### Styling cell content [↑](#contents)
|
587
699
|
|
588
700
|
In most terminals, if you want to print text that is coloured, or has certain other styles such as
|
589
701
|
underlining, you need to use ANSI escape sequences, either directly, or by means of a library such
|
@@ -616,13 +728,13 @@ table.add_column(
|
|
616
728
|
)
|
617
729
|
```
|
618
730
|
|
619
|
-
The `styler` option should be passed a callable that takes either
|
620
|
-
first parameter represents the underlying value of the cell (in this case a boolean indicating whether
|
621
|
-
|
622
|
-
content after any processing by the [formatter](#formatting-cell-values)
|
623
|
-
|
624
|
-
|
625
|
-
[documentation](https://www.rubydoc.info/gems/tabulo/2.
|
731
|
+
The `styler` option should be passed a callable that takes either 2, 3 or 4 parameters.
|
732
|
+
The first parameter represents the underlying value of the cell (in this case a boolean indicating whether the
|
733
|
+
number is even). The second parameter represents the formatted string value of that cell, i.e. the cell
|
734
|
+
content after any processing by the [formatter](#formatting-cell-values). The third and fourth
|
735
|
+
parameters are optional, and contain further information about the cell and its contents that may be useful in
|
736
|
+
determining how to style it. See the
|
737
|
+
[documentation](https://www.rubydoc.info/gems/tabulo/2.6.2/Tabulo/Table#add_column-instance_method) for details.
|
626
738
|
|
627
739
|
If the content of a cell is wrapped over multiple lines, then the `styler` will be called once
|
628
740
|
per line, so that each line of the cell will have the escape sequence applied to it separately
|
@@ -632,7 +744,7 @@ If the content of a cell has been [truncated](#overflow-handling), then whatever
|
|
632
744
|
styling apply to the cell content will also be applied the truncation indicator character.
|
633
745
|
|
634
746
|
<a name="styling-column-headers"></a>
|
635
|
-
#### Styling column headers
|
747
|
+
#### Styling column headers [↑](#contents)
|
636
748
|
|
637
749
|
If you want to apply colours or other styling to the content of a column header, as opposed
|
638
750
|
to cells in the table body, use the `header_styler` option, e.g.:
|
@@ -641,28 +753,26 @@ to cells in the table body, use the `header_styler` option, e.g.:
|
|
641
753
|
table.add_column(:even?, header_styler: -> (s) { "\033[32m#{s}\033[0m" })
|
642
754
|
```
|
643
755
|
|
644
|
-
The `header_styler` option accepts
|
645
|
-
[documentation](https://www.rubydoc.info/gems/tabulo/2.
|
756
|
+
The `header_styler` option accepts a 1-, 2- or 3-parameter callable. See the
|
757
|
+
[documentation](https://www.rubydoc.info/gems/tabulo/2.6.2/Tabulo/Table#add_column-instance_method)
|
646
758
|
for details.
|
647
759
|
|
648
|
-
<a name="
|
649
|
-
####
|
760
|
+
<a name="styling-title"></a>
|
761
|
+
#### Styling the table title [↑](#contents)
|
650
762
|
|
651
|
-
|
652
|
-
|
653
|
-
It is possible to apply styling by default to _all_ columns in a table, however, as the table initializer
|
654
|
-
also accepts both a `header_styler` and a `styler` option. For example, if you want all the header text
|
655
|
-
in the table to be green, you could do:
|
763
|
+
To apply colours or other styling to the table title, if present, use the `title_styler` option
|
764
|
+
when initializing the table. This accepts a single-parameter callable:
|
656
765
|
|
657
766
|
```ruby
|
658
|
-
table = Tabulo::Table.new(1..5, :itself, :even?, :odd?,
|
767
|
+
table = Tabulo::Table.new(1..5, :itself, :even?, :odd?, title: "Numbers", title_styler: -> (s) { "\033[32m#{s}\033[0m" })
|
659
768
|
```
|
660
769
|
|
661
|
-
|
662
|
-
|
770
|
+
The `title_styler` option accepts a 1- or 2-parameter callable. See the
|
771
|
+
[documentation](https://www.rubydoc.info/gems/tabulo/2.6.2/Tabulo/Table#initialize-instance_method)
|
772
|
+
for details.
|
663
773
|
|
664
|
-
<a name="
|
665
|
-
#### Styling borders
|
774
|
+
<a name="styling-borders"></a>
|
775
|
+
#### Styling borders [↑](#contents)
|
666
776
|
|
667
777
|
Styling can also be applied to borders and dividing lines, using the `border_styler` option when
|
668
778
|
initializing the table, e.g.:
|
@@ -671,8 +781,24 @@ initializing the table, e.g.:
|
|
671
781
|
table = Tabulo::Table.new(1..5, :itself, :even?, :odd?, border_styler: -> (s) { "\033[32m#{s}\033[0m" })
|
672
782
|
```
|
673
783
|
|
784
|
+
<a name="default-styles"></a>
|
785
|
+
#### Setting default styles [↑](#contents)
|
786
|
+
|
787
|
+
By default, no styling is applied to the headers or body content of a column unless configured to do
|
788
|
+
so via the `header_styler` or `styler` option when calling `add_column` for that particular column.
|
789
|
+
It is possible to apply styling by default to _all_ columns in a table, however, as the table initializer
|
790
|
+
also accepts both a `header_styler` and a `styler` option. For example, if you want all the header text
|
791
|
+
in the table to be green, you could do:
|
792
|
+
|
793
|
+
```ruby
|
794
|
+
table = Tabulo::Table.new(1..5, :itself, :even?, :odd?, header_styler: -> (s) { "\033[32m#{s}\033[0m" })
|
795
|
+
```
|
796
|
+
|
797
|
+
Now, all columns in the table will automatically have green header text, unless overridden by another
|
798
|
+
header styler being passed to `#add_column`.
|
799
|
+
|
674
800
|
<a name="repeating-headers"></a>
|
675
|
-
### Repeating headers
|
801
|
+
### Repeating headers [↑](#contents)
|
676
802
|
|
677
803
|
By default, headers are only shown once, at the top of the table (`header_frequency: :start`). If
|
678
804
|
`header_frequency` is passed `nil`, headers are not shown at all; or, if passed an `Integer` N,
|
@@ -706,8 +832,10 @@ table = Tabulo::Table.new(1..10, :itself, :even?, header_frequency: 5)
|
|
706
832
|
+--------------+--------------+
|
707
833
|
```
|
708
834
|
|
835
|
+
Note that if the table has a [title](#title), it will not be repeated; only column headers are repeated.
|
836
|
+
|
709
837
|
<a name="enumerator"></a>
|
710
|
-
### Using a Table Enumerator
|
838
|
+
### Using a Table Enumerator [↑](#contents)
|
711
839
|
|
712
840
|
Because it’s an `Enumerable`, a `Tabulo::Table` can also give you an `Enumerator`,
|
713
841
|
which is useful when you want to step through rows one at a time. In a Rails console,
|
@@ -736,7 +864,7 @@ in that case the entire collection must be traversed up front in order for colum
|
|
736
864
|
calculated.)
|
737
865
|
|
738
866
|
<a name="accessing-cell-values"></a>
|
739
|
-
### Accessing cell values
|
867
|
+
### Accessing cell values [↑](#contents)
|
740
868
|
|
741
869
|
Each `Tabulo::Table` is an `Enumerable` of which each element is a `Tabulo::Row`. Each `Tabulo::Row`
|
742
870
|
is itself an `Enumerable`, of `Tabulo::Cell`. The `Tabulo::Cell#value` method will return the
|
@@ -772,7 +900,7 @@ end
|
|
772
900
|
```
|
773
901
|
|
774
902
|
<a name="accessing-sources"></a>
|
775
|
-
### Accessing the underlying enumerable
|
903
|
+
### Accessing the underlying enumerable [↑](#contents)
|
776
904
|
|
777
905
|
The underlying enumerable for a table can be retrieved by calling the `sources` getter:
|
778
906
|
|
@@ -807,7 +935,7 @@ end
|
|
807
935
|
```
|
808
936
|
|
809
937
|
<a name="transposition"></a>
|
810
|
-
### Transposing rows and columns
|
938
|
+
### Transposing rows and columns [↑](#contents)
|
811
939
|
|
812
940
|
By default, Tabulo generates a table in which each row corresponds to a _record_, i.e. an element of
|
813
941
|
the underlying enumerable, and each column to a _field_. However, there are times when one instead
|
@@ -835,10 +963,10 @@ a new table in which the rows and columns are swapped:
|
|
835
963
|
By default, a header row is added to the new table, showing the string value of the element
|
836
964
|
represented in that column. This can be configured, however, along with other aspects of
|
837
965
|
`transpose`’s behaviour. For details, see the
|
838
|
-
[documentation](https://www.rubydoc.info/gems/tabulo/2.
|
966
|
+
[documentation](https://www.rubydoc.info/gems/tabulo/2.6.2/Tabulo/Table#transpose-instance_method).
|
839
967
|
|
840
968
|
<a name="borders"></a>
|
841
|
-
### Configuring borders
|
969
|
+
### Configuring borders [↑](#contents)
|
842
970
|
|
843
971
|
You can configure the kind of border and divider characters that are used when the table is printed.
|
844
972
|
This is done using the `border` option passed to `Table.new`. The options are as follows.
|
@@ -854,6 +982,17 @@ This is done using the `border` option passed to `Table.new`. The options are as
|
|
854
982
|
| 2 | true | false |
|
855
983
|
| 3 | false | true |
|
856
984
|
+--------------+--------------+--------------+
|
985
|
+
|
986
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :ascii, title: "Numbers")
|
987
|
+
+--------------------------------------------+
|
988
|
+
| Numbers |
|
989
|
+
+--------------+--------------+--------------+
|
990
|
+
| itself | even? | odd? |
|
991
|
+
+--------------+--------------+--------------+
|
992
|
+
| 1 | false | true |
|
993
|
+
| 2 | true | false |
|
994
|
+
| 3 | false | true |
|
995
|
+
+--------------+--------------+--------------+
|
857
996
|
```
|
858
997
|
|
859
998
|
`:modern`—uses smoothly joined Unicode characters:
|
@@ -867,8 +1006,23 @@ This is done using the `border` option passed to `Table.new`. The options are as
|
|
867
1006
|
│ 2 │ true │ false │
|
868
1007
|
│ 3 │ false │ true │
|
869
1008
|
└──────────────┴──────────────┴──────────────┘
|
1009
|
+
|
1010
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :modern, title: "Numbers")
|
1011
|
+
┌────────────────────────────────────────────┐
|
1012
|
+
│ Numbers │
|
1013
|
+
├──────────────┬──────────────┬──────────────┤
|
1014
|
+
│ itself │ even? │ odd? │
|
1015
|
+
├──────────────┼──────────────┼──────────────┤
|
1016
|
+
│ 1 │ false │ true │
|
1017
|
+
│ 2 │ true │ false │
|
1018
|
+
│ 3 │ false │ true │
|
1019
|
+
└──────────────┴──────────────┴──────────────┘
|
870
1020
|
```
|
871
1021
|
|
1022
|
+
_Note: The unicode characters used for the `:modern` border may not render properly
|
1023
|
+
when viewing this documentation on some browsers or devices. This doesn’t reflect any brokenness
|
1024
|
+
in `tabulo` itself._
|
1025
|
+
|
872
1026
|
`:markdown`—renders a GitHub flavoured Markdown table:
|
873
1027
|
|
874
1028
|
```
|
@@ -878,8 +1032,20 @@ This is done using the `border` option passed to `Table.new`. The options are as
|
|
878
1032
|
| 1 | false | true |
|
879
1033
|
| 2 | true | false |
|
880
1034
|
| 3 | false | true |
|
1035
|
+
|
1036
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :markdown, title: "Numbers")
|
1037
|
+
| Numbers |
|
1038
|
+
| itself | even? | odd? |
|
1039
|
+
|--------------|--------------|--------------|
|
1040
|
+
| 1 | false | true |
|
1041
|
+
| 2 | true | false |
|
1042
|
+
| 3 | false | true |
|
881
1043
|
```
|
882
1044
|
|
1045
|
+
_However_, note that when a table is rendered using the `:markdown` border type in combination with a
|
1046
|
+
(non-`nil`) `title`, the result will be _invalid Markdown_. This is because Markdown engines do not
|
1047
|
+
generally support adding a caption (i.e. title) element to tables.
|
1048
|
+
|
883
1049
|
`:blank`—no border or divider characters are printed:
|
884
1050
|
|
885
1051
|
```
|
@@ -888,6 +1054,14 @@ This is done using the `border` option passed to `Table.new`. The options are as
|
|
888
1054
|
1 false true
|
889
1055
|
2 true false
|
890
1056
|
3 false true
|
1057
|
+
|
1058
|
+
|
1059
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :blank, title: "Numbers")
|
1060
|
+
Numbers
|
1061
|
+
itself even? odd?
|
1062
|
+
1 false true
|
1063
|
+
2 true false
|
1064
|
+
3 false true
|
891
1065
|
```
|
892
1066
|
|
893
1067
|
`:reduced_ascii`—similar to `:ascii`, but without vertical lines:
|
@@ -901,6 +1075,17 @@ This is done using the `border` option passed to `Table.new`. The options are as
|
|
901
1075
|
2 true false
|
902
1076
|
3 false true
|
903
1077
|
-------------- -------------- --------------
|
1078
|
+
|
1079
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :reduced_modern, title: "Numbers")
|
1080
|
+
--------------------------------------------
|
1081
|
+
Numbers
|
1082
|
+
-------------- -------------- --------------
|
1083
|
+
itself even? odd?
|
1084
|
+
-------------- -------------- --------------
|
1085
|
+
1 false true
|
1086
|
+
2 true false
|
1087
|
+
3 false true
|
1088
|
+
-------------- -------------- --------------
|
904
1089
|
```
|
905
1090
|
|
906
1091
|
`:reduced_modern`—similar to `:modern`, but without vertical lines:
|
@@ -914,8 +1099,23 @@ This is done using the `border` option passed to `Table.new`. The options are as
|
|
914
1099
|
2 true false
|
915
1100
|
3 false true
|
916
1101
|
────────────── ────────────── ──────────────
|
1102
|
+
|
1103
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :reduced_ascii, title: "Numbers")
|
1104
|
+
────────────────────────────────────────────
|
1105
|
+
Numbers
|
1106
|
+
────────────── ────────────── ──────────────
|
1107
|
+
itself even? odd?
|
1108
|
+
────────────── ────────────── ──────────────
|
1109
|
+
1 false true
|
1110
|
+
2 true false
|
1111
|
+
3 false true
|
1112
|
+
────────────── ────────────── ──────────────
|
917
1113
|
```
|
918
1114
|
|
1115
|
+
_Note: The unicode characters used for the `:reduced_modern` border may not render properly
|
1116
|
+
when viewing this documentation on some browsers or devices. This doesn’t reflect any brokenness
|
1117
|
+
in `tabulo` itself._
|
1118
|
+
|
919
1119
|
`:classic`—reproduces the default behaviour in Tabulo v1; this is like the `:ascii` option,
|
920
1120
|
but without a bottom border:
|
921
1121
|
|
@@ -927,13 +1127,23 @@ but without a bottom border:
|
|
927
1127
|
| 1 | false | true |
|
928
1128
|
| 2 | true | false |
|
929
1129
|
| 3 | false | true |
|
1130
|
+
|
1131
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :classic, title: "Numbers")
|
1132
|
+
+--------------------------------------------+
|
1133
|
+
| Numbers |
|
1134
|
+
+--------------+--------------+--------------+
|
1135
|
+
| itself | even? | odd? |
|
1136
|
+
+--------------+--------------+--------------+
|
1137
|
+
| 1 | false | true |
|
1138
|
+
| 2 | true | false |
|
1139
|
+
| 3 | false | true |
|
930
1140
|
```
|
931
1141
|
|
932
1142
|
Note that, by default, none of the border options includes lines drawn _between_ rows in the body of the table.
|
933
1143
|
These are configured via a separate option: see [below](#dividers).
|
934
1144
|
|
935
1145
|
<a name="dividers"></a>
|
936
|
-
### Row dividers
|
1146
|
+
### Row dividers [↑](#contents)
|
937
1147
|
|
938
1148
|
To add lines between rows in the table body, use the `row_divider_frequency` option when initializing
|
939
1149
|
the table. The default value for this option is `nil`, meaning there are no dividing lines between
|
@@ -959,20 +1169,20 @@ every Nth row. For example:
|
|
959
1169
|
If you want a line before every row, pass `1` to `row_divider_frequency`. For example:
|
960
1170
|
|
961
1171
|
```
|
962
|
-
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?,
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
1172
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, row_divider_frequency: 1)
|
1173
|
+
+--------------+--------------+--------------+
|
1174
|
+
| itself | even? | odd? |
|
1175
|
+
+--------------+--------------+--------------+
|
1176
|
+
| 1 | false | true |
|
1177
|
+
+--------------+--------------+--------------+
|
1178
|
+
| 2 | true | false |
|
1179
|
+
+--------------+--------------+--------------+
|
1180
|
+
| 3 | false | true |
|
1181
|
+
+--------------+--------------+--------------+
|
972
1182
|
```
|
973
1183
|
|
974
1184
|
<a name="freezing-a-table"></a>
|
975
|
-
### Using a table as a snapshot rather than as a dynamic view
|
1185
|
+
### Using a table as a snapshot rather than as a dynamic view [↑](#contents)
|
976
1186
|
|
977
1187
|
The nature of a `Tabulo::Table` is that of a dynamic view onto the underlying `sources` enumerable
|
978
1188
|
from which it was initialized (or which was subsequently assigned to its `sources` attribute). That
|
@@ -1042,7 +1252,7 @@ rendered_table = Tabulo::Table.new(1..10, :itself, :even?, :odd?).pack.to_s
|
|
1042
1252
|
```
|
1043
1253
|
|
1044
1254
|
<a name="motivation"></a>
|
1045
|
-
## Comparison with other libraries
|
1255
|
+
## Comparison with other libraries [↑](#contents)
|
1046
1256
|
|
1047
1257
|
There are other libraries for generating plain text tables in Ruby. Popular among these are:
|
1048
1258
|
|
@@ -1127,7 +1337,7 @@ environment seems cumbersome. Moreover, it seems no longer to be maintained. At
|
|
1127
1337
|
its last commit was in March 2015.
|
1128
1338
|
|
1129
1339
|
<a name="contributing"></a>
|
1130
|
-
## Contributing
|
1340
|
+
## Contributing [↑](#contents)
|
1131
1341
|
|
1132
1342
|
Issues and pull requests are welcome on GitHub at https://github.com/matt-harvey/tabulo.
|
1133
1343
|
|
@@ -1138,20 +1348,20 @@ install dependencies.
|
|
1138
1348
|
`bundle exec rake spec` will run the test suite. For a list of other Rake tasks that are available in
|
1139
1349
|
the development environment, run `bundle exec rake -T`.
|
1140
1350
|
|
1141
|
-
## License
|
1351
|
+
## License [↑](#contents)
|
1142
1352
|
|
1143
1353
|
The gem is available as open source under the terms of the [MIT
|
1144
1354
|
License](http://opensource.org/licenses/MIT).
|
1145
1355
|
|
1146
1356
|
[Gem Version]: https://rubygems.org/gems/tabulo
|
1147
|
-
[Documentation]: http://www.rubydoc.info/gems/tabulo/2.
|
1357
|
+
[Documentation]: http://www.rubydoc.info/gems/tabulo/2.6.2
|
1148
1358
|
[Build Status]: https://travis-ci.org/matt-harvey/tabulo
|
1149
1359
|
[Coverage Status]: https://coveralls.io/r/matt-harvey/tabulo
|
1150
1360
|
[Code Climate]: https://codeclimate.com/github/matt-harvey/tabulo
|
1151
1361
|
[Awesome Ruby]: https://github.com/markets/awesome-ruby#cli-utilities
|
1152
1362
|
|
1153
1363
|
[GV img]: https://img.shields.io/gem/v/tabulo.svg
|
1154
|
-
[DC img]: https://img.shields.io/badge/documentation-v2.
|
1364
|
+
[DC img]: https://img.shields.io/badge/documentation-v2.6.2-blue.svg
|
1155
1365
|
[BS img]: https://img.shields.io/travis/matt-harvey/tabulo.svg
|
1156
1366
|
[CS img]: https://img.shields.io/coveralls/matt-harvey/tabulo.svg
|
1157
1367
|
[CC img]: https://codeclimate.com/github/matt-harvey/tabulo/badges/gpa.svg
|