tabulo 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/README.md +71 -3
- data/VERSION +1 -1
- data/lib/tabulo/row.rb +6 -4
- data/lib/tabulo/table.rb +127 -92
- data/lib/tabulo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 295d0e4161611edd684ef1f65143767b418af24f0e630e83bb6554cf25636e44
|
4
|
+
data.tar.gz: e4a0e0fcc8a2efdedb251d413f65f2627b7706136af55026728d6c906f0bc311
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cc23cce74adbe70451dc387007baa32339ac8d0942d55594e8516ac871880c4ef8176a69db5da7dad2dcfa122d83ae838cb9129e3df8212f3a1ae4ab337c92a
|
7
|
+
data.tar.gz: dfa4e051e1f45a41e3611b8c22422ee9f043671d4d7f432318fab6465c6b888c5a3e67302a3950e241d73dc17e99d3b26e2d2738b3eacc162f5a4e07b628acaa
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
### v2.2.0
|
4
|
+
|
5
|
+
* New `column_formatter` option on `Tabulo::Table` initializer, enabling the table's default column
|
6
|
+
formatter to be customized.
|
7
|
+
* New `row_divider_frequency` option on `Tabulo::Table` initializer, to add a horizontal dividing line
|
8
|
+
after every N rows.
|
9
|
+
|
3
10
|
### v2.1.1
|
4
11
|
|
5
12
|
* Fix issue where blank lines appear in table when certain border types (e.g. `:classic`) are
|
6
|
-
used with a non-nil
|
13
|
+
used with a non-nil `border_styler`.
|
14
|
+
* Minor documentation fix
|
7
15
|
|
8
16
|
### v2.1.0
|
9
17
|
|
data/README.md
CHANGED
@@ -72,6 +72,7 @@ Tabulo has also been ported to Crystal (with some modifications): see [Tablo](ht
|
|
72
72
|
* [Accessing the underlying enumerable](#accessing-sources)
|
73
73
|
* [Transposing rows and columns](#transposition)
|
74
74
|
* [Border configuration](#borders)
|
75
|
+
* [Row dividers](#dividers)
|
75
76
|
* [Comparison with other libraries](#motivation)
|
76
77
|
* [Contributing](#contributing)
|
77
78
|
* [License](#license)
|
@@ -425,6 +426,31 @@ Note the numbers in the "Reciprocal" column in this example are still right-alig
|
|
425
426
|
the callable passed to `formatter` returns a String. Default cell alignment is determined by the type
|
426
427
|
of the underlying cell value, not the way it is formatted. This is usually the desired result.
|
427
428
|
|
429
|
+
If you want to set the default formatter for all columns of the table to something other than
|
430
|
+
`#to_s`, use the `formatter` option when initializing the table:
|
431
|
+
|
432
|
+
```ruby
|
433
|
+
table = Tabulo::Table.new(1..3, formatter: -> (n) { "%.2f" % n }) do |t|
|
434
|
+
t.add_column("N", &:itself)
|
435
|
+
t.add_column("Reciprocal") { |n| 1.0 / n }
|
436
|
+
t.add_column("Half") { |n| n / 2.0 }
|
437
|
+
end
|
438
|
+
```
|
439
|
+
|
440
|
+
```
|
441
|
+
> puts table
|
442
|
+
+--------------+--------------+--------------+
|
443
|
+
| N | Reciprocal | Half |
|
444
|
+
+--------------+--------------+--------------+
|
445
|
+
| 1.00 | 1.00 | 0.50 |
|
446
|
+
| 2.00 | 0.50 | 1.00 |
|
447
|
+
| 3.00 | 0.33 | 1.50 |
|
448
|
+
+--------------+--------------+--------------+
|
449
|
+
```
|
450
|
+
|
451
|
+
Formatters set for individual columns on calling `#add_column` always override the default formatter for
|
452
|
+
the table.
|
453
|
+
|
428
454
|
<a name="colours-and-styling"></a>
|
429
455
|
### Colours and styling
|
430
456
|
|
@@ -651,7 +677,7 @@ a new table in which the rows and columns are swapped:
|
|
651
677
|
By default, a header row is added to the new table, showing the string value of the element
|
652
678
|
represented in that column. This can be configured, however, along with other aspects of
|
653
679
|
`transpose`'s behaviour. For details, see the
|
654
|
-
[documentation](https://www.rubydoc.info/gems/tabulo/2.
|
680
|
+
[documentation](https://www.rubydoc.info/gems/tabulo/2.2.0/Tabulo/Table#transpose-instance_method).
|
655
681
|
|
656
682
|
<a name="borders"></a>
|
657
683
|
### Configuring borders
|
@@ -745,6 +771,48 @@ but without a bottom border:
|
|
745
771
|
| 3 | false | true |
|
746
772
|
```
|
747
773
|
|
774
|
+
Note that, by default, none of the border options includes lines drawn _between_ rows in the body of the table.
|
775
|
+
These are configured via a separate option: see [below](#dividers).
|
776
|
+
|
777
|
+
<a name="dividers"></a>
|
778
|
+
### Row dividers
|
779
|
+
|
780
|
+
To add lines between rows in the table body, use the `row_divider_frequency` option when initializing
|
781
|
+
the table. The default value for this option is `nil`, meaning there are no dividing lines between
|
782
|
+
rows. But if this option passed is a positive integer N, then a dividing line is inserted before
|
783
|
+
every Nth row. For example:
|
784
|
+
|
785
|
+
```
|
786
|
+
> puts Tabulo::Table.new(1..6, :itself, :even?, :odd?, row_divider_frequency: 2)
|
787
|
+
+--------------+--------------+--------------+
|
788
|
+
| itself | even? | odd? |
|
789
|
+
+--------------+--------------+--------------+
|
790
|
+
| 1 | false | true |
|
791
|
+
| 2 | true | false |
|
792
|
+
+--------------+--------------+--------------+
|
793
|
+
| 3 | false | true |
|
794
|
+
| 4 | true | false |
|
795
|
+
+--------------+--------------+--------------+
|
796
|
+
| 5 | false | true |
|
797
|
+
| 6 | true | false |
|
798
|
+
+--------------+--------------+--------------+
|
799
|
+
```
|
800
|
+
|
801
|
+
If you want a line before every row, pass `1` to `row_divider_frequency`. For example:
|
802
|
+
|
803
|
+
```
|
804
|
+
> puts Tabulo::Table.new(1..3, :itself, :even?, :odd?, border: :modern, row_divider_frequency: 1)
|
805
|
+
┌──────────────┬──────────────┬──────────────┐
|
806
|
+
│ itself │ even? │ odd? │
|
807
|
+
├──────────────┼──────────────┼──────────────┤
|
808
|
+
│ 1 │ false │ true │
|
809
|
+
├──────────────┼──────────────┼──────────────┤
|
810
|
+
│ 2 │ true │ false │
|
811
|
+
├──────────────┼──────────────┼──────────────┤
|
812
|
+
│ 3 │ false │ true │
|
813
|
+
└──────────────┴──────────────┴──────────────┘
|
814
|
+
```
|
815
|
+
|
748
816
|
<a name="motivation"></a>
|
749
817
|
## Comparison with other libraries
|
750
818
|
|
@@ -847,14 +915,14 @@ The gem is available as open source under the terms of the [MIT
|
|
847
915
|
License](http://opensource.org/licenses/MIT).
|
848
916
|
|
849
917
|
[Gem Version]: https://rubygems.org/gems/tabulo
|
850
|
-
[Documentation]: http://www.rubydoc.info/gems/tabulo/2.
|
918
|
+
[Documentation]: http://www.rubydoc.info/gems/tabulo/2.2.0
|
851
919
|
[Build Status]: https://travis-ci.org/matt-harvey/tabulo
|
852
920
|
[Coverage Status]: https://coveralls.io/r/matt-harvey/tabulo
|
853
921
|
[Code Climate]: https://codeclimate.com/github/matt-harvey/tabulo
|
854
922
|
[Awesome Ruby]: https://github.com/markets/awesome-ruby#cli-utilities
|
855
923
|
|
856
924
|
[GV img]: https://img.shields.io/gem/v/tabulo.svg
|
857
|
-
[DC img]: https://img.shields.io/badge/documentation-v2.
|
925
|
+
[DC img]: https://img.shields.io/badge/documentation-v2.2.0-blue.svg
|
858
926
|
[BS img]: https://img.shields.io/travis/matt-harvey/tabulo.svg
|
859
927
|
[CS img]: https://img.shields.io/coveralls/matt-harvey/tabulo.svg
|
860
928
|
[CC img]: https://codeclimate.com/github/matt-harvey/tabulo/badges/gpa.svg
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2.0
|
data/lib/tabulo/row.rb
CHANGED
@@ -7,10 +7,11 @@ module Tabulo
|
|
7
7
|
attr_reader :source
|
8
8
|
|
9
9
|
# @!visibility private
|
10
|
-
def initialize(table, source, header: :top)
|
10
|
+
def initialize(table, source, header: :top, divider: false)
|
11
11
|
@table = table
|
12
12
|
@source = source
|
13
13
|
@header = header
|
14
|
+
@divider = divider
|
14
15
|
end
|
15
16
|
|
16
17
|
# Calls the given block once for each {Cell} in the {Row}, passing that {Cell} as parameter.
|
@@ -28,11 +29,12 @@ module Tabulo
|
|
28
29
|
end
|
29
30
|
|
30
31
|
# @return a String being an "ASCII" graphical representation of the {Row}, including
|
31
|
-
# any column headers that appear just above it in the {Table} (depending on where
|
32
|
-
# in the {Table} and how the {Table} was configured with respect to header frequency
|
32
|
+
# any column headers or row divider that appear just above it in the {Table} (depending on where
|
33
|
+
# this Row is in the {Table}, and how the {Table} was configured with respect to header frequency
|
34
|
+
# and divider frequency).
|
33
35
|
def to_s
|
34
36
|
if @table.column_registry.any?
|
35
|
-
@table.formatted_body_row(@source, header: @header)
|
37
|
+
@table.formatted_body_row(@source, header: @header, divider: @divider)
|
36
38
|
else
|
37
39
|
""
|
38
40
|
end
|
data/lib/tabulo/table.rb
CHANGED
@@ -35,40 +35,14 @@ module Tabulo
|
|
35
35
|
# be unique. Each element of the Array will be used to create a column whose content is
|
36
36
|
# created by calling the corresponding method on each element of sources. Note
|
37
37
|
# the {#add_column} method is a much more flexible way to set up columns on the table.
|
38
|
-
# @param [Integer, nil] column_width The default column width for columns in this
|
39
|
-
# table, not excluding padding. If <tt>nil</tt>, then {DEFAULT_COLUMN_WIDTH} will be used.
|
40
|
-
# @param [:start, nil, Integer] header_frequency Controls the display of column headers.
|
41
|
-
# If passed <tt>:start</tt>, headers will be shown at the top of the table only. If passed <tt>nil</tt>,
|
42
|
-
# headers will not be shown. If passed an Integer N (> 0), headers will be shown at the top of the table,
|
43
|
-
# then repeated every N rows.
|
44
|
-
# @param [nil, Integer] wrap_header_cells_to Controls wrapping behaviour for header
|
45
|
-
# cells if the content thereof is longer than the column's fixed width. If passed <tt>nil</tt> (default),
|
46
|
-
# content will be wrapped for as many rows as required to accommodate it. If passed an Integer N (> 0),
|
47
|
-
# content will be wrapped up to N rows and then truncated thereafter.
|
48
|
-
# @param [nil, Integer] wrap_body_cells_to Controls wrapping behaviour for table cells (excluding
|
49
|
-
# headers), if their content is longer than the column's fixed width. If passed <tt>nil</tt>, content will
|
50
|
-
# be wrapped for as many rows as required to accommodate it. If passed an Integer N (> 0), content will be
|
51
|
-
# wrapped up to N rows and then truncated thereafter.
|
52
|
-
# headers), if their content is longer than the column's fixed width. If passed <tt>nil</tt>, content will
|
53
|
-
# be wrapped for as many rows as required to accommodate it. If passed an Integer N (> 0), content will be
|
54
|
-
# wrapped up to N rows and then truncated thereafter.
|
55
|
-
# @param [nil, String] truncation_indicator Determines the character used to indicate that a
|
56
|
-
# cell's content has been truncated. If omitted or passed <tt>nil</tt>,
|
57
|
-
# defaults to {DEFAULT_TRUNCATION_INDICATOR}. If passed something other than <tt>nil</tt> or
|
58
|
-
# a single-character String, raises {InvalidTruncationIndicatorError}.
|
59
|
-
# @param [nil, Integer, Array] column_padding (1) Determines the amount of blank space with which to pad
|
60
|
-
# either side of each column. If passed an Integer, then the given amount of padding is
|
61
|
-
# applied to each side of each column. If passed a two-element Array, then the first element of the
|
62
|
-
# Array indicates the amount of padding to apply to the left of each column, and the second
|
63
|
-
# element indicates the amount to apply to the right.
|
64
|
-
# @param [:left, :right, :center] align_header (:center) Determines the alignment of header text
|
65
|
-
# for columns in this Table. Can be overridden for individual columns using the
|
66
|
-
# <tt>align_header</tt> option passed to {#add_column}
|
67
38
|
# @param [:left, :right, :center, :auto] align_body (:auto) Determines the alignment of body cell
|
68
39
|
# (i.e. non-header) content within columns in this Table. Can be overridden for individual columns
|
69
40
|
# using the <tt>align_body</tt> option passed to {#add_column}. If passed <tt>:auto</tt>,
|
70
41
|
# alignment is determined by cell content, with numbers aligned right, booleans
|
71
42
|
# center-aligned, and other values left-aligned.
|
43
|
+
# @param [:left, :right, :center] align_header (:center) Determines the alignment of header text
|
44
|
+
# for columns in this Table. Can be overridden for individual columns using the
|
45
|
+
# <tt>align_header</tt> option passed to {#add_column}
|
72
46
|
# @param [:ascii, :markdown, :modern, :blank, nil] border (nil) Determines the characters used
|
73
47
|
# for the Table border, including both the characters around the outside of table, and the lines drawn
|
74
48
|
# within the table to separate columns from each other and the header row from the Table body.
|
@@ -93,22 +67,67 @@ module Tabulo
|
|
93
67
|
# {border_styler} is not taken into consideration by the internal table rendering calculations
|
94
68
|
# Thus it can be used to apply ANSI escape codes to border characters, to colour the borders
|
95
69
|
# for example, without breaking the table formatting.
|
70
|
+
# @param [nil, Integer, Array] column_padding (1) Determines the amount of blank space with which to pad
|
71
|
+
# either side of each column. If passed an Integer, then the given amount of padding is
|
72
|
+
# applied to each side of each column. If passed a two-element Array, then the first element of the
|
73
|
+
# Array indicates the amount of padding to apply to the left of each column, and the second
|
74
|
+
# element indicates the amount to apply to the right.
|
75
|
+
# @param [Integer, nil] column_width The default column width for columns in this
|
76
|
+
# table, not excluding padding. If <tt>nil</tt>, then {DEFAULT_COLUMN_WIDTH} will be used.
|
77
|
+
# @param [nil, #to_proc] formatter (:to_s.to_proc) The default value for the `formatter` option
|
78
|
+
# when columns are added to this table via {#add_column}.
|
79
|
+
# @param [:start, nil, Integer] header_frequency (:start) Controls the display of column headers.
|
80
|
+
# If passed <tt>:start</tt>, headers will be shown at the top of the table only. If passed <tt>nil</tt>,
|
81
|
+
# headers will not be shown. If passed an Integer N (> 0), headers will be shown at the top of the table,
|
82
|
+
# then repeated every N rows.
|
83
|
+
# @param [nil, Integer] row_divider_frequency (nil) Controls the display of horizontal row dividers within
|
84
|
+
# the table body. If passed <tt>nil</tt>, dividers will not be shown. If passed an Integer N (> 0),
|
85
|
+
# dividers will be shown after every N rows. The characters used to form the dividers are
|
86
|
+
# determined by the `border` option, and are the same as those used to form the bottom edge of the
|
87
|
+
# header row.
|
88
|
+
# @param [nil, String] truncation_indicator Determines the character used to indicate that a
|
89
|
+
# cell's content has been truncated. If omitted or passed <tt>nil</tt>,
|
90
|
+
# defaults to {DEFAULT_TRUNCATION_INDICATOR}. If passed something other than <tt>nil</tt> or
|
91
|
+
# a single-character String, raises {InvalidTruncationIndicatorError}.
|
92
|
+
# @param [nil, Integer] wrap_body_cells_to Controls wrapping behaviour for table cells (excluding
|
93
|
+
# headers), if their content is longer than the column's fixed width. If passed <tt>nil</tt>, content will
|
94
|
+
# be wrapped for as many rows as required to accommodate it. If passed an Integer N (> 0), content will be
|
95
|
+
# wrapped up to N rows and then truncated thereafter.
|
96
|
+
# headers), if their content is longer than the column's fixed width. If passed <tt>nil</tt>, content will
|
97
|
+
# be wrapped for as many rows as required to accommodate it. If passed an Integer N (> 0), content will be
|
98
|
+
# wrapped up to N rows and then truncated thereafter.
|
99
|
+
# @param [nil, Integer] wrap_header_cells_to Controls wrapping behaviour for header
|
100
|
+
# cells if the content thereof is longer than the column's fixed width. If passed <tt>nil</tt> (default),
|
101
|
+
# content will be wrapped for as many rows as required to accommodate it. If passed an Integer N (> 0),
|
102
|
+
# content will be wrapped up to N rows and then truncated thereafter.
|
96
103
|
# @return [Table] a new {Table}
|
97
104
|
# @raise [InvalidColumnLabelError] if non-unique Symbols are provided to columns.
|
98
105
|
# @raise [InvalidBorderError] if invalid option passed to `border` parameter.
|
99
|
-
def initialize(
|
100
|
-
|
101
|
-
|
106
|
+
def initialize(
|
107
|
+
sources,
|
108
|
+
*columns,
|
109
|
+
align_body: :auto,
|
110
|
+
align_header: :center,
|
111
|
+
border: nil,
|
112
|
+
border_styler: nil,
|
113
|
+
column_padding: nil,
|
114
|
+
column_width: nil,
|
115
|
+
formatter: :to_s.to_proc,
|
116
|
+
header_frequency: :start,
|
117
|
+
row_divider_frequency: nil,
|
118
|
+
truncation_indicator: nil,
|
119
|
+
wrap_body_cells_to: nil,
|
120
|
+
wrap_header_cells_to: nil)
|
102
121
|
|
103
122
|
@sources = sources
|
104
|
-
@header_frequency = header_frequency
|
105
|
-
@wrap_header_cells_to = wrap_header_cells_to
|
106
|
-
@wrap_body_cells_to = wrap_body_cells_to
|
107
|
-
@column_width = (column_width || DEFAULT_COLUMN_WIDTH)
|
108
|
-
@align_header = align_header
|
109
|
-
@align_body = align_body
|
110
123
|
|
124
|
+
@align_body = align_body
|
125
|
+
@align_header = align_header
|
126
|
+
@border = (border || DEFAULT_BORDER)
|
127
|
+
@border_styler = border_styler
|
128
|
+
@border_instance = Border.from(@border, @border_styler)
|
111
129
|
@column_padding = (column_padding || DEFAULT_COLUMN_PADDING)
|
130
|
+
|
112
131
|
@left_column_padding, @right_column_padding =
|
113
132
|
case @column_padding
|
114
133
|
when Array
|
@@ -117,12 +136,14 @@ module Tabulo
|
|
117
136
|
[@column_padding, @column_padding]
|
118
137
|
end
|
119
138
|
|
120
|
-
@
|
121
|
-
@
|
122
|
-
@
|
123
|
-
|
139
|
+
@column_width = (column_width || DEFAULT_COLUMN_WIDTH)
|
140
|
+
@formatter = formatter
|
141
|
+
@header_frequency = header_frequency
|
142
|
+
@row_divider_frequency = row_divider_frequency
|
124
143
|
@truncation_indicator = validate_character(truncation_indicator,
|
125
144
|
DEFAULT_TRUNCATION_INDICATOR, InvalidTruncationIndicatorError, "truncation indicator")
|
145
|
+
@wrap_body_cells_to = wrap_body_cells_to
|
146
|
+
@wrap_header_cells_to = wrap_header_cells_to
|
126
147
|
|
127
148
|
@column_registry = { }
|
128
149
|
columns.each { |item| add_column(item) }
|
@@ -138,13 +159,6 @@ module Tabulo
|
|
138
159
|
# a method to be called on each item in the table sources to provide the content
|
139
160
|
# for this column. If a String is passed as the label, then it will be converted to
|
140
161
|
# a Symbol for the purpose of serving as this label.
|
141
|
-
# @param [nil, #to_s] header (nil) Text to be displayed in the column header. If passed nil,
|
142
|
-
# the column's label will also be used as its header text.
|
143
|
-
# @param [:left, :center, :right, nil] align_header (nil) Specifies how the header text
|
144
|
-
# should be aligned. If <tt>nil</tt> is passed, then the alignment is determined
|
145
|
-
# by the Table-level setting passed to the <tt>align_header</tt> (which itself defaults
|
146
|
-
# to <tt>:center</tt>). Otherwise, this option determines the alignment of the header
|
147
|
-
# content for this column.
|
148
162
|
# @param [:left, :center, :right, :auto, nil] align_body (nil) Specifies how the cell body contents
|
149
163
|
# should be aligned. If <tt>nil</tt> is passed, then the alignment is determined
|
150
164
|
# by the Table-level setting passed to the <tt>align_body</tt> option on Table initialization
|
@@ -152,15 +166,33 @@ module Tabulo
|
|
152
166
|
# this column. If <tt>:auto</tt> is passed, the alignment is determined by the type of the cell
|
153
167
|
# value, with numbers aligned right, booleans center-aligned, and other values left-aligned.
|
154
168
|
# Note header text alignment is configured separately using the :align_header param.
|
155
|
-
# @param [
|
156
|
-
#
|
157
|
-
#
|
158
|
-
#
|
169
|
+
# @param [:left, :center, :right, nil] align_header (nil) Specifies how the header text
|
170
|
+
# should be aligned. If <tt>nil</tt> is passed, then the alignment is determined
|
171
|
+
# by the Table-level setting passed to the <tt>align_header</tt> (which itself defaults
|
172
|
+
# to <tt>:center</tt>). Otherwise, this option determines the alignment of the header
|
173
|
+
# content for this column.
|
174
|
+
# @param [#to_proc] formatter (nil) A lambda or other callable object that
|
159
175
|
# will be passed the calculated value of each cell to determine how it should be displayed. This
|
160
176
|
# is distinct from the extractor (see below). For example, if the extractor for this column
|
161
177
|
# generates a Date, then the formatter might format that Date in a particular way.
|
162
|
-
# If no formatter is provided, then
|
163
|
-
# the
|
178
|
+
# If no formatter is provided, then the callable that was passed to the `formatter` option
|
179
|
+
# of the table itself on its creation (see {#initialize}) (which itself defaults to
|
180
|
+
# `:to_s.to_proc`), will be used as the formatter for the column.
|
181
|
+
# @param [nil, #to_s] header (nil) Text to be displayed in the column header. If passed nil,
|
182
|
+
# the column's label will also be used as its header text.
|
183
|
+
# @param [nil, #to_proc] header_styler (nil) A lambda or other callable object taking
|
184
|
+
# a single parameter, representing a single line of within the header content for
|
185
|
+
# this column. For example, if the header cell content is wrapped over three lines, then
|
186
|
+
# the {header_styler} will be called once for each line. If passed <tt>nil</tt>, then
|
187
|
+
# no additional styling will be applied to the header cell content. If passed a callable,
|
188
|
+
# then that callable will be called for each line of content within the header cell, and the
|
189
|
+
# resulting string rendered in place of that line. The extra width of the string returned by the
|
190
|
+
# {header_styler} is not taken into consideration by the internal table and
|
191
|
+
# cell width calculations involved in rendering the table. Thus it can be used to apply
|
192
|
+
# ANSI escape codes to header cell content, to colour the cell content for example, without
|
193
|
+
# breaking the table formatting.
|
194
|
+
# Note that if the header content is truncated, then any {header_styler} will be applied to the
|
195
|
+
# truncation indicator character as well as to the truncated content.
|
164
196
|
# @param [nil, #to_proc] styler (nil) A lambda or other callable object that will be passed
|
165
197
|
# two arguments: the calculated value of the cell (prior to the {formatter} being applied);
|
166
198
|
# and a string representing a single formatted line within the cell. For example, if the
|
@@ -176,19 +208,9 @@ module Tabulo
|
|
176
208
|
# breaking the table formatting.
|
177
209
|
# Note that if the content of a cell is truncated, then the whatever styling is applied by the
|
178
210
|
# {styler} to the cell content will also be applied to the truncation indicator character.
|
179
|
-
# @param [
|
180
|
-
#
|
181
|
-
#
|
182
|
-
# the {header_styler} will be called once for each line. If passed <tt>nil</tt>, then
|
183
|
-
# no additional styling will be applied to the header cell content. If passed a callable,
|
184
|
-
# then that callable will be called for each line of content within the header cell, and the
|
185
|
-
# resulting string rendered in place of that line. The extra width of the string returned by the
|
186
|
-
# {header_styler} is not taken into consideration by the internal table and
|
187
|
-
# cell width calculations involved in rendering the table. Thus it can be used to apply
|
188
|
-
# ANSI escape codes to header cell content, to colour the cell content for example, without
|
189
|
-
# breaking the table formatting.
|
190
|
-
# Note that if the header content is truncated, then any {header_styler} will be applied to the
|
191
|
-
# truncation indicator character as well as to the truncated content.
|
211
|
+
# @param [Integer] width (nil) Specifies the width of the column, excluding padding. If
|
212
|
+
# nil, then the column will take the width provided by the `column_width` param
|
213
|
+
# with which the Table was initialized.
|
192
214
|
# @param [#to_proc] extractor A block or other callable
|
193
215
|
# that will be passed each of the Table sources to determine the value in each cell of this
|
194
216
|
# column. If this is not provided, then the column label will be treated as a method to be
|
@@ -196,8 +218,16 @@ module Tabulo
|
|
196
218
|
# @raise [InvalidColumnLabelError] if label has already been used for another column in this
|
197
219
|
# Table. (This is case-sensitive, but is insensitive to whether a String or Symbol is passed
|
198
220
|
# to the label parameter.)
|
199
|
-
def add_column(
|
200
|
-
|
221
|
+
def add_column(
|
222
|
+
label,
|
223
|
+
align_body: nil,
|
224
|
+
align_header: nil,
|
225
|
+
formatter: nil,
|
226
|
+
header: nil,
|
227
|
+
header_styler: nil,
|
228
|
+
styler: nil,
|
229
|
+
width: nil,
|
230
|
+
&extractor)
|
201
231
|
|
202
232
|
column_label =
|
203
233
|
case label
|
@@ -213,16 +243,16 @@ module Tabulo
|
|
213
243
|
|
214
244
|
@column_registry[column_label] =
|
215
245
|
Column.new(
|
216
|
-
header: (header || label).to_s,
|
217
|
-
align_header: align_header || @align_header,
|
218
246
|
align_body: align_body || @align_body,
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
247
|
+
align_header: align_header || @align_header,
|
248
|
+
extractor: extractor || label.to_proc,
|
249
|
+
formatter: formatter || @formatter,
|
250
|
+
header: (header || label).to_s,
|
223
251
|
header_styler: header_styler,
|
224
|
-
truncation_indicator: @truncation_indicator,
|
225
252
|
padding_character: PADDING_CHARACTER,
|
253
|
+
styler: styler,
|
254
|
+
truncation_indicator: @truncation_indicator,
|
255
|
+
width: width || @column_width,
|
226
256
|
)
|
227
257
|
end
|
228
258
|
|
@@ -262,7 +292,10 @@ module Tabulo
|
|
262
292
|
else
|
263
293
|
@header_frequency
|
264
294
|
end
|
265
|
-
|
295
|
+
|
296
|
+
show_divider = @row_divider_frequency && (index != 0) && (index % @row_divider_frequency == 0)
|
297
|
+
|
298
|
+
yield Row.new(self, source, header: header, divider: show_divider)
|
266
299
|
end
|
267
300
|
end
|
268
301
|
|
@@ -360,11 +393,12 @@ module Tabulo
|
|
360
393
|
#
|
361
394
|
# @param [Hash] opts Options for configuring the new, transposed {Table}.
|
362
395
|
# The following options are the same as the keyword params for the {#initialize} method for
|
363
|
-
# {Table}: <tt>column_width</tt>, <tt>column_padding</tt>, <tt>
|
364
|
-
# <tt>
|
365
|
-
# <tt>
|
366
|
-
#
|
367
|
-
#
|
396
|
+
# {Table}: <tt>column_width</tt>, <tt>column_padding</tt>, <tt>formatter</tt>,
|
397
|
+
# <tt>header_frequency</tt>, <tt>row_divider_frequency</tt>, <tt>wrap_header_cells_to</tt>,
|
398
|
+
# <tt>wrap_body_cells_to</tt>, <tt>border</tt>, <tt>border_styler</tt>, <tt>truncation_indicator</tt>,
|
399
|
+
# <tt>align_header</tt>, <tt>align_body</tt>.
|
400
|
+
# These are applied in the same way as documented for {#initialize}, when
|
401
|
+
# creating the new, transposed Table. Any options not specified explicitly in the call to {#transpose}
|
368
402
|
# will inherit their values from the original {Table} (with the exception of settings
|
369
403
|
# for the left-most column, containing the field names, which are determined as described
|
370
404
|
# below). In addition, the following options also apply to {#transpose}:
|
@@ -386,9 +420,9 @@ module Tabulo
|
|
386
420
|
# @return [Table] a new {Table}
|
387
421
|
# @raise [InvalidBorderError] if invalid argument passed to `border` parameter.
|
388
422
|
def transpose(opts = {})
|
389
|
-
default_opts = [:column_width, :column_padding, :
|
390
|
-
:
|
391
|
-
:border_styler].map do |sym|
|
423
|
+
default_opts = [:column_width, :column_padding, :formatter, :header_frequency,
|
424
|
+
:row_divider_frequency, :wrap_header_cells_to, :wrap_body_cells_to, :truncation_indicator, :align_header,
|
425
|
+
:align_body, :border, :border_styler].map do |sym|
|
392
426
|
[sym, instance_variable_get("@#{sym}")]
|
393
427
|
end.to_h
|
394
428
|
|
@@ -419,9 +453,10 @@ module Tabulo
|
|
419
453
|
end
|
420
454
|
|
421
455
|
# @!visibility private
|
422
|
-
def formatted_body_row(source, header:
|
456
|
+
def formatted_body_row(source, header:, divider:)
|
423
457
|
cells = column_registry.map { |_, column| column.body_cell(source) }
|
424
458
|
inner = format_row(cells, @wrap_body_cells_to)
|
459
|
+
|
425
460
|
if header
|
426
461
|
join_lines([
|
427
462
|
horizontal_rule(header == :top ? :top : :middle),
|
@@ -429,6 +464,11 @@ module Tabulo
|
|
429
464
|
horizontal_rule(:middle),
|
430
465
|
inner,
|
431
466
|
].reject(&:empty?))
|
467
|
+
elsif divider
|
468
|
+
join_lines([
|
469
|
+
horizontal_rule(:middle),
|
470
|
+
inner,
|
471
|
+
].reject(&:empty?))
|
432
472
|
else
|
433
473
|
inner
|
434
474
|
end
|
@@ -464,11 +504,6 @@ module Tabulo
|
|
464
504
|
end
|
465
505
|
end
|
466
506
|
|
467
|
-
# @!visibility private
|
468
|
-
def body_row(source, header: nil)
|
469
|
-
Row.new(self, source, header: header)
|
470
|
-
end
|
471
|
-
|
472
507
|
# @!visibility private
|
473
508
|
#
|
474
509
|
# Formats a single header row or body row as a String.
|
data/lib/tabulo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tabulo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Harvey
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-screen
|