rtables 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 476b203d969073a14520ac1ea98765e688b211f5
4
+ data.tar.gz: 0b6436a04b12f9ca02d6f36653fec415cf19a917
5
+ SHA512:
6
+ metadata.gz: fc21c375f9d192b9e50bc8dd2703298b65ba5e431df8544b6816911cf7d8a0bc520da5e0518422bf5c3a46bb2a8c5af5a17f04ab6add3ad04ca2fc0cb920e38e
7
+ data.tar.gz: 3ff2f50aed25777364d30e22238428ead2e851f117206fd52b2d9af29a4ea46469088a149d443f8c3c30aaed8c71a6be98330458cda431196233dbdc85feb39b
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jos 'Zarthus' Ahrens
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Ruby Tables
2
+
3
+ RTables (Ruby Tables) is a small library that helps you generate table output in various predefined formats.
4
+
5
+ ## Installation
6
+
7
+ To install RTables, you can either `git clone` the GitHub repository, or simply run `gem install rtables`
8
+
9
+ ## Example Code
10
+
11
+ ```ruby
12
+ require 'rtables'
13
+
14
+ # Initialize the table you wish to use.
15
+ table = RTables::Table::PlainTable.new
16
+
17
+ # Add the table headers to the table. This will structure our table.
18
+ table.add_column('Example')
19
+ table.add_column('Second Column')
20
+
21
+ # Start adding rows to the table.
22
+ # For each column you have added, you need to pass another parameter.
23
+ table.add_row('Hello', 'World!')
24
+ table.add_row('Another day', 'Another row!')
25
+ table.add_row('It\'s a beautiful day to be a table', 'Indeed.')
26
+
27
+ # The final step, we either .render or .to_s our table.
28
+ # .render returns a line-separated array,
29
+ # .to_s calls render.join("\n").
30
+ puts table.to_s
31
+ ```
32
+
33
+ ## Picking the right table format
34
+
35
+ Tables are nice, but each table has their positive and negative things.
36
+
37
+ The [documentation](docs/tables) explains the cons and pros of each table.
38
+
39
+ ## Support
40
+
41
+ There are two primary means of support.
42
+
43
+ - Leave an [issue](https://github.com/zarthus/rtables/issues/new) on the [GitHub Repository](https://github.com/zarthus/rtables)
44
+ - Alternatively, you may ask your question through IRC on [EsperNet in #zarthus](https://webchat.esper.net?channels=zarthus)
45
+
46
+ To allow us to help you, we suggest you seek out [the documentation](docs) and [contributing](CONTRIBUTING.md) files first.
47
+
48
+ ## License
49
+
50
+ RTables is licensed under the [MIT license](LICENSE)
@@ -0,0 +1,61 @@
1
+ # Making your own table
2
+
3
+ Making your own table is very easy.
4
+
5
+ Your table needs to meet the following two conditions:
6
+
7
+ - Extend `RTables::TableBuilder`
8
+ - Have a `render` method that returns an array of lines.
9
+ - May call `raise_if_empty` in `render` to generate a `TableFormatError` when there is no content.
10
+
11
+ Tables can make use of the following data included from `RTables::TableBuilder`:
12
+
13
+ - `@columns` and `@rows` integer values with the number of columns and rows.
14
+ - `@table_header` - an array of all columns.
15
+ - `@table_content` - a list of arrays with table content.
16
+
17
+ Refer to any of the existing tables for guidance on how you could use this to your benefit.
18
+
19
+ ## What it should roughly look like
20
+
21
+ ```ruby
22
+ require 'rtables/tablebuilder'
23
+
24
+ class MyTable < RTables::TableBuilder
25
+ def render
26
+ raise_if_empty
27
+
28
+ lines = []
29
+ i = 0
30
+
31
+ @table_content.each do |content|
32
+ line = []
33
+ @table_header.each do |header|
34
+ line << "#{header}: #{content[i]}"
35
+ i += 1
36
+ end
37
+ lines << line.join(', ')
38
+ i = 0
39
+ end
40
+
41
+ lines
42
+ end
43
+ end
44
+
45
+ # Elsewhere, where you need the table.
46
+ table = MyTable.new
47
+
48
+ table.add_column('First Name')
49
+ table.add_column('Last Name')
50
+
51
+ table.add_row('Max', 'Caulfield')
52
+ table.add_row('Chloe', 'Price')
53
+
54
+ puts table.to_s
55
+ ```
56
+
57
+ Output:
58
+ ```
59
+ First Name: Max, Last Name: Caulfield
60
+ First Name: Chloe, Last Name: Price
61
+ ```
@@ -0,0 +1,42 @@
1
+ # Small Monospace Table
2
+
3
+ Code:
4
+
5
+ ```ruby
6
+ require 'rtables/table/monotable'
7
+ table = RTables::Table::MonoTable.new
8
+
9
+ table.add_column('Example')
10
+ table.add_column('Field')
11
+ table.add_row('First', 'This is an example text')
12
+ table.add_row('Small', 'Field')
13
+ table.add_row('Very Large Field', 'With a large amount of text on it.')
14
+
15
+ table.render
16
+ ```
17
+
18
+ Looks like:
19
+
20
+ ```
21
+ +---------+------------------------------------+
22
+ | Example | First |
23
+ | Field | This is an example text |
24
+ +---------+------------------------------------+
25
+ | Example | Small |
26
+ | Field | Field |
27
+ +---------+------------------------------------+
28
+ | Example | Very Large Field |
29
+ | Field | With a large amount of text on it. |
30
+ +---------+------------------------------------+
31
+ ```
32
+
33
+ ## Positives
34
+
35
+ - Easy to read
36
+ - Perfect for key => value display
37
+
38
+ ## Negatives
39
+
40
+ - Requires a Monospace font to render properly.
41
+ - If field names are too long the table will look strange.
42
+ - Table::MonoTable solves this by making use of [`BigMonoTable`](BigMonoTable.md) dynamically when the content is too big.
File without changes
File without changes
File without changes
@@ -0,0 +1,83 @@
1
+ # Building a table
2
+
3
+ A table exists of two components. Columns, and rows.
4
+
5
+ In your source code, you will define the columns before adding any rows.
6
+
7
+ Given the following example:
8
+ ```ruby
9
+ require 'rtables'
10
+
11
+ # Initialize the table you wish to use.
12
+ table = RTables::Table::TableName.new
13
+
14
+ # Add the table headers to the table. This will structure our table.
15
+ table.add_column('Example')
16
+ table.add_column('Second Column')
17
+
18
+ # Start adding rows to the table.
19
+ # For each column you have added, you need to pass another parameter.
20
+ table.add_row('Hello', 'World!')
21
+ table.add_row('Another day', 'Another row!')
22
+ table.add_row('It\'s a beautiful day to be a table', 'Indeed.')
23
+
24
+ # The final step, we either .render or .to_s our table.
25
+ # .render returns a line-separated array,
26
+ # .to_s calls render.join("\n").
27
+ puts table.to_s
28
+ ```
29
+
30
+ The following output may be generated:
31
+
32
+ ```
33
+ # PlainTable - the most boring table, but also the one that is the most consistent
34
+
35
+ Column: Row1 Value1, Column2: Row1 Value2, ...
36
+ Column: Row2 Value1, Column2: Row2 Value2, ...
37
+ Column: Row3 Value1, Column2: Row3 Value2, ...
38
+ ```
39
+
40
+ ```
41
+ # SimpleTable - slightly less boring table, keeping the same level of consistency
42
+
43
+ Column: Row1 Value1
44
+ Column2: Row1 Value2
45
+ ----
46
+ Column: Row2 Value1
47
+ Column2: Row2 Value2
48
+ ----
49
+ Column: Row3 Value1
50
+ Column2: Row3 Value2
51
+
52
+ ```
53
+
54
+ ```
55
+ # MonoTable - prettier, but requires a monospace font to function.
56
+
57
+ ```
58
+
59
+ ```
60
+ # UnicodeMonoTable - the prettiest, but requires a monospace font to function.
61
+ # Makes use of non-ascii characters, so may cause rendering issues on some clients.
62
+
63
+ ```
64
+
65
+ ## Making your own table
66
+ Your table needs to meet the following two conditions:
67
+
68
+ - Extend `RTables::TableBuilder`
69
+ - Have a `render` method that returns an array of lines.
70
+ - May call `raise_if_empty` in `render` to generate a `TableFormatError` when there is no content.
71
+
72
+ ### Base code
73
+
74
+ ```ruby
75
+ require 'rtables/tablebuilder'
76
+
77
+ class MyTable < RTables::TableBuilder
78
+ def render
79
+ raise_if_empty
80
+
81
+ # Logic that returns an array of lines ..
82
+ end
83
+ end
@@ -0,0 +1,63 @@
1
+ module RTables
2
+ class TableBuilder
3
+ def initialize
4
+ @table_header = []
5
+ @table_content = []
6
+
7
+ @columns = 0
8
+ @rows = 0
9
+ end
10
+
11
+ attr_reader :table_header, :table_content, :columns, :rows
12
+
13
+ def add_column(name)
14
+ return false if column_exist?(name)
15
+ fail TableFormatError, 'Cannot add more columns after rows have been added.' if @rows != 0
16
+
17
+ @table_header.push(name)
18
+ @columns += 1
19
+ true
20
+ end
21
+
22
+ def add_row(*args)
23
+ if args.count != @columns
24
+ fail TableFormatError, "Number of arguments passed does not equal number of columns. [#{args.count} != #{@columns}]"
25
+ end
26
+
27
+ @table_content.push(args)
28
+ @rows += 1
29
+ true
30
+ end
31
+
32
+ def column_exist?(name)
33
+ @table_header.include?(name)
34
+ end
35
+
36
+ def raise_if_empty
37
+ fail TableFormatError, 'Table has no content to display.' if empty?
38
+ end
39
+
40
+ def empty?
41
+ @columns == 0 && @rows == 0
42
+ end
43
+
44
+ def render
45
+ fail TableFormatError, 'This table does not generate any output.'
46
+ end
47
+
48
+ def to_s
49
+ raise_if_empty
50
+
51
+ lines = render
52
+
53
+ lines.join("\n")
54
+ end
55
+
56
+ def inspect
57
+ "<Table columns=#{@columns} rows=#{@rows}>"
58
+ end
59
+ end
60
+
61
+ class TableFormatError < StandardError
62
+ end
63
+ end
@@ -0,0 +1,68 @@
1
+ module RTables
2
+ module Table
3
+ class MonoTable < TableBuilder
4
+ def render
5
+ corner = '+'
6
+ line_horizontal = '-'
7
+ line_vertical = '|'
8
+
9
+ hlen = max_header_length
10
+ clen = max_content_length
11
+
12
+ row_sep = "#{corner}#{line_horizontal * (hlen + 2)}#{corner}#{line_horizontal * (clen + 2)}#{corner}"
13
+ line_fmt = "#{line_vertical} %{header} #{line_vertical} %{content} #{line_vertical}"
14
+ lines = []
15
+ i = 0
16
+
17
+ lines << row_sep
18
+ @table_content.each do |contents|
19
+ contents.each do |content|
20
+ lines << line_fmt % { header: pad_header(@table_header[i], hlen), content: pad_content(content, clen) }
21
+ i += 1
22
+ end
23
+
24
+ i = 0
25
+ lines << row_sep
26
+ end
27
+
28
+ lines
29
+ end
30
+
31
+ def pad_header(header, hlen)
32
+ spacing = hlen - header.length
33
+
34
+ header + (' ' * spacing)
35
+ end
36
+
37
+ def pad_content(content, clen)
38
+ spacing = clen - content.length
39
+
40
+ content + (' ' * spacing)
41
+ end
42
+
43
+ def max_content_length
44
+ max_len = 0
45
+
46
+ @table_content.each do |contents|
47
+ contents.each do |content|
48
+ len = content.length
49
+ max_len = len if max_len < len
50
+ end
51
+ end
52
+
53
+ max_len
54
+ end
55
+
56
+ def max_header_length
57
+ max_len = 0
58
+
59
+ @table_header.each do |head|
60
+ len = head.length
61
+ max_len = len if max_len < len
62
+ end
63
+
64
+ max_len
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,23 @@
1
+ module RTables
2
+ module Table
3
+ class PlainTable < TableBuilder
4
+ def render
5
+ raise_if_empty
6
+
7
+ lines = []
8
+ i = 0
9
+
10
+ @table_content.each do |content|
11
+ @table_header.each do |header|
12
+ lines << "#{header}: #{content[i]}"
13
+ i += 1
14
+ end
15
+ lines << '----'
16
+ i = 0
17
+ end
18
+
19
+ lines
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module RTables
2
+ module Table
3
+ class SimpleTable < TableBuilder
4
+ def render
5
+ raise_if_empty
6
+
7
+ lines = []
8
+ i = 0
9
+
10
+ @table_content.each do |content|
11
+ line = []
12
+ @table_header.each do |header|
13
+ line << "#{header}: #{content[i]}"
14
+ i += 1
15
+ end
16
+ lines << line.join(', ')
17
+ i = 0
18
+ end
19
+
20
+ lines
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,85 @@
1
+ module RTables
2
+ module Table
3
+ class UnicodeMonoTable < TableBuilder
4
+ CORNER_POS_LTOP = '┌'
5
+ CORNER_POS_RTOP = '┐'
6
+ CORNER_POS_MTOP = '┬'
7
+
8
+ CORNER_POS_LMID = '├'
9
+ CORNER_POS_RMID = '┤'
10
+ CORNER_POS_MMID = '┼'
11
+
12
+ CORNER_POS_LBOT = '└'
13
+ CORNER_POS_RBOT = '┘'
14
+ CORNER_POS_MBOT = '┴'
15
+
16
+ def render
17
+ line_horizontal = '─'
18
+ line_vertical = '│'
19
+
20
+ hlen = max_header_length
21
+ clen = max_content_length
22
+
23
+ row_sep_base = "#{line_horizontal * (hlen + 2)}%{tchar}#{line_horizontal * (clen + 2)}"
24
+ row_sep_top = "#{CORNER_POS_LTOP}#{row_sep_base % { tchar: CORNER_POS_MTOP }}#{CORNER_POS_RTOP}"
25
+ row_sep_mid = "#{CORNER_POS_LMID}#{row_sep_base % { tchar: CORNER_POS_MMID }}#{CORNER_POS_RMID}"
26
+ row_sep_bot = "#{CORNER_POS_LBOT}#{row_sep_base % { tchar: CORNER_POS_MBOT }}#{CORNER_POS_RBOT}"
27
+
28
+ line_fmt = "#{line_vertical} %{header} #{line_vertical} %{content} #{line_vertical}"
29
+ lines = []
30
+ i = 0
31
+
32
+ lines << row_sep_top
33
+ @table_content.each do |contents|
34
+ contents.each do |content|
35
+ lines << line_fmt % { header: pad_header(@table_header[i], hlen), content: pad_content(content, clen) }
36
+ i += 1
37
+ end
38
+
39
+ i = 0
40
+ lines << row_sep_mid
41
+ end
42
+ lines.pop
43
+ lines << row_sep_bot
44
+
45
+ lines
46
+ end
47
+
48
+ def pad_header(header, hlen)
49
+ spacing = hlen - header.length
50
+
51
+ header + (' ' * spacing)
52
+ end
53
+
54
+ def pad_content(content, clen)
55
+ spacing = clen - content.length
56
+
57
+ content + (' ' * spacing)
58
+ end
59
+
60
+ def max_content_length
61
+ max_len = 0
62
+
63
+ @table_content.each do |contents|
64
+ contents.each do |content|
65
+ len = content.length
66
+ max_len = len if max_len < len
67
+ end
68
+ end
69
+
70
+ max_len
71
+ end
72
+
73
+ def max_header_length
74
+ max_len = 0
75
+
76
+ @table_header.each do |head|
77
+ len = head.length
78
+ max_len = len if max_len < len
79
+ end
80
+
81
+ max_len
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module RTables
2
+ VERSION = '1.0.0'.freeze
3
+ end
data/lib/rtables.rb ADDED
@@ -0,0 +1,9 @@
1
+ dir = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
3
+
4
+ require 'rtables/tablebuilder'
5
+
6
+ require 'rtables/tables/plaintable'
7
+ require 'rtables/tables/simpletable'
8
+ require 'rtables/tables/monotable'
9
+ require 'rtables/tables/unicodemonotable'
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtables
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jos Ahrens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: RTables allows you to generate your own table output in the least amount
14
+ of code.
15
+ email: gems@zarth.us
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - docs/making_your_own_table.md
23
+ - docs/tables/MonoTable.md
24
+ - docs/tables/PlainTable.md
25
+ - docs/tables/SimpleTable.md
26
+ - docs/tables/UnicodeMonoTable.md
27
+ - docs/tables_in_2_minutes.md
28
+ - lib/rtables.rb
29
+ - lib/rtables/tablebuilder.rb
30
+ - lib/rtables/tables/monotable.rb
31
+ - lib/rtables/tables/plaintable.rb
32
+ - lib/rtables/tables/simpletable.rb
33
+ - lib/rtables/tables/unicodemonotable.rb
34
+ - lib/rtables/version.rb
35
+ homepage: https://github.com/zarthus/rtables
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.4.5
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: 'Ruby Tables: Generate tables fast and easy'
59
+ test_files: []