New in Table-formatter v0.1.6

Neat fit

The border automatically resizes to fit the columns e.g.

require 'table-formatter'

labels = %w(Name Age Address Code)
a = [['Bob', '20', '10, High Street','A342'],
     ['Jane', '23', '12/3, Lawn Market Court', 'B34F'],
     ['Bruce', '32', '63, Cotswalds Way', 'F34AD'],
     ['Michael', '49', '1, Hollwood Way', 'E234D'],
     ['Stephanie', '34', '2, Hampton Court','A234']]

tfo = TableFormatter.new
tfo.source = a
tfo.labels = labels
puts tfo.display

output:

--------------------------------------------------------
| Name       | Age | Address                  | Code   |
--------------------------------------------------------
| Bob        | 20  | 10, High Street          | A342   |
| Jane       | 23  | 12/3, Lawn Market Court  | B34F   |
| Bruce      | 32  | 63, Cotswalds Way        | F34AD  |
| Michael    | 49  | 1, Hollwood Way          | E234D  |
| Stephanie  | 34  | 2, Hampton Court         | A234   |
--------------------------------------------------------

Wrapping of the last column

e.g.

require 'table-formatter'
labels = %w(Name Age Address Code)
a = [['Bob', '20', '10, High Street','A342 iuy y iu yy123456789 123456789 1234'],
     ['Jane', '23', '12/3, Lawn Market Court', 'B34F'],
     ['Bruce', '32', '63, Cotswalds Way', 'F34AD'],
     ['Michael', '49', '1, Hollwood Way', 'E234D'],
     ['Stephanie', '34', '2, Hampton Court','A234']]

tfo = TableFormatter.new
tfo.source = a
tfo.labels = labels
puts tfo.display

output:

-----------------------------------------------------------------------------
| Name       | Age | Address                  | Code                        |
-----------------------------------------------------------------------------
| Bob        | 20  | 10, High Street          | A342 iuy y iu yy123456789   |
|            |     |                          | 123456789 1234              |
| Jane       | 23  | 12/3, Lawn Market Court  | B34F                        |
| Bruce      | 32  | 63, Cotswalds Way        | F34AD                       |
| Michael    | 49  | 1, Hollwood Way          | E234D                       |
| Stephanie  | 34  | 2, Hampton Court         | A234                        |
-----------------------------------------------------------------------------

Fixed width

The table can be set to a fixed width e.g.

puts tfo.display 70

output:

---------------------------------------------------------------------------------------
| Name       | Age | Address                  | Code                                  |
---------------------------------------------------------------------------------------
| Bob        | 20  | 10, High Street          | A342 iuy y iu yy123456789 123456789   |
|            |     |                          | 1234                                  |
| Jane       | 23  | 12/3, Lawn Market Court  | B34F                                  |
| Bruce      | 32  | 63, Cotswalds Way        | F34AD                                 |
| Michael    | 49  | 1, Hollwood Way          | E234D                                 |
| Stephanie  | 34  | 2, Hampton Court         | A234                                  |
---------------------------------------------------------------------------------------

Column alignment

:left, right: or :center:

require 'table-formatter'
labels = ['Name:' ,'Age', ':Address:', 'Code']
a = [['Bob', '20', '10, High Street','A342'],
     ['Jane', '23', '12/3, Lawn Market Court', 'B34F'],
     ['Bruce', '32', '63, Cotswalds Way', 'F34AD'],
     ['Michael', '49', '1, Hollwood Way', 'E234D'],
     ['Stephanie', '34', '2, Hampton Court','A234']]

tfo = TableFormatter.new
tfo.source = a
tfo.labels = labels
puts tfo.display

output:

--------------------------------------------------------
|        Name| Age |          Address         | Code   |
--------------------------------------------------------
|         Bob| 20  |      10, High Street     | A342   |
|        Jane| 23  |  12/3, Lawn Market Court | B34F   |
|       Bruce| 32  |     63, Cotswalds Way    | F34AD  |
|     Michael| 49  |      1, Hollwood Way     | E234D  |
|   Stephanie| 34  |     2, Hampton Court     | A234   |
--------------------------------------------------------

Resources

Tags:
Source:
1551hrs.txt
Published:
25-09-2013 15:51