sergeysazonov-simple_console_table 0.1.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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sergey Sazonov.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ = SimpleConsoleTable - a library capable of printing nicely formatted tables to stdout
2
+ Copyright (C) 2009 Sergey Sazonov
3
+
4
+ == Example
5
+ === Basic example
6
+
7
+ require 'rubygems'
8
+ require 'simple_console_table'
9
+
10
+ table = SimpleConsoleTable.new
11
+
12
+ table << ['one', 'two', 'three']
13
+ table << ['four', 'five', 'six', 'seven']
14
+
15
+ table.as_string
16
+
17
+ # +--------+--------+---------+---------+
18
+ # | one | two | three | |
19
+ # +--------+--------+---------+---------+
20
+ # | four | five | six | seven |
21
+ # +--------+--------+---------+---------+
22
+
23
+ Use print to write the table directly to the console. Use as_string to obtain the result as a string.
24
+
25
+ === Using objects other than strings
26
+
27
+ SimpleConsoleTable invokes to_s method on every object thus converting everything to strings.
28
+
29
+ class Dummy
30
+ def initialize(dummy_id); @dummy_id = dummy_id; end
31
+ def to_s; "Dummy #{@dummy_id}"; end
32
+ end
33
+
34
+ require 'rubygems'
35
+ require 'simple_console_table'
36
+
37
+ table = SimpleConsoleTable.new
38
+
39
+ table << ['one', 2, 'three']
40
+ table << [true, 'five', -6.89, Dummy.new(345)]
41
+
42
+ table.as_string
43
+
44
+ # +--------+--------+---------+-------------+
45
+ # | one | 2 | three | |
46
+ # +--------+--------+---------+-------------+
47
+ # | true | five | -6.89 | Dummy 345 |
48
+ # +--------+--------+---------+-------------+
49
+
50
+ === Passing nested arrays
51
+
52
+ SimpleConsoleTable invokes flatten method on every array passed to <<
53
+
54
+ require 'rubygems'
55
+ require 'simple_console_table'
56
+
57
+ table = SimpleConsoleTable.new
58
+
59
+ table << [1, 2, [3, 4], [5]]
60
+ table << [[[[[[6, 7, 8]]], [[[[9, 10]]]]]]]
61
+
62
+ table.as_string
63
+
64
+ # +-----+-----+-----+-----+------+
65
+ # | 1 | 2 | 3 | 4 | 5 |
66
+ # +-----+-----+-----+-----+------+
67
+ # | 6 | 7 | 8 | 9 | 10 |
68
+ # +-----+-----+-----+-----+------+
@@ -0,0 +1,140 @@
1
+ # = SimpleConsoleTable - a library capable of printing nicely formatted tables to stdout
2
+ # Copyright (C) 2009 Sergey Sazonov
3
+ #
4
+ # == Example
5
+ # === Basic example
6
+ #
7
+ # require 'rubygems'
8
+ # require 'simple_console_table'
9
+ #
10
+ # table = SimpleConsoleTable.new
11
+ #
12
+ # table << ['one', 'two', 'three']
13
+ # table << ['four', 'five', 'six', 'seven']
14
+ #
15
+ # table.as_string
16
+ #
17
+ # # +--------+--------+---------+---------+
18
+ # # | one | two | three | |
19
+ # # +--------+--------+---------+---------+
20
+ # # | four | five | six | seven |
21
+ # # +--------+--------+---------+---------+
22
+ #
23
+ # Use print to write the table directly to the console. Use as_string to obtain the result as a string.
24
+ #
25
+ # === Using objects other than strings
26
+ #
27
+ # SimpleConsoleTable invokes to_s method on every object thus converting everything to strings.
28
+ #
29
+ # class Dummy
30
+ # def initialize(dummy_id); @dummy_id = dummy_id; end
31
+ # def to_s; "Dummy #{@dummy_id}"; end
32
+ # end
33
+ #
34
+ # require 'rubygems'
35
+ # require 'simple_console_table'
36
+ #
37
+ # table = SimpleConsoleTable.new
38
+ #
39
+ # table << ['one', 2, 'three']
40
+ # table << [true, 'five', -6.89, Dummy.new(345)]
41
+ #
42
+ # table.as_string
43
+ #
44
+ # # +--------+--------+---------+-------------+
45
+ # # | one | 2 | three | |
46
+ # # +--------+--------+---------+-------------+
47
+ # # | true | five | -6.89 | Dummy 345 |
48
+ # # +--------+--------+---------+-------------+
49
+ #
50
+ # === Passing nested arrays
51
+ #
52
+ # SimpleConsoleTable invokes flatten method on every array passed to <<
53
+ #
54
+ # require 'rubygems'
55
+ # require 'simple_console_table'
56
+ #
57
+ # table = SimpleConsoleTable.new
58
+ #
59
+ # table << [1, 2, [3, 4], [5]]
60
+ # table << [[[[[[6, 7, 8]]], [[[[9, 10]]]]]]]
61
+ #
62
+ # table.as_string
63
+ #
64
+ # # +-----+-----+-----+-----+------+
65
+ # # | 1 | 2 | 3 | 4 | 5 |
66
+ # # +-----+-----+-----+-----+------+
67
+ # # | 6 | 7 | 8 | 9 | 10 |
68
+ # # +-----+-----+-----+-----+------+
69
+
70
+ class SimpleConsoleTable
71
+
72
+ DEFAULT_FORMATTER = lambda do |text_to_format, column_width|
73
+ sprintf(" %#{column_width}.#{column_width}s ", text_to_format)
74
+ end
75
+
76
+ def initialize
77
+ @rows = []
78
+ @formatter = DEFAULT_FORMATTER
79
+ end
80
+
81
+ # Adds row.flatten.collect { |cell_content| cell_content.to_s } to the table
82
+ def <<(row)
83
+ @rows << row.flatten.collect { |cell_content| cell_content.to_s }
84
+ end
85
+
86
+ # Returns an empty string if the table is empty (that is, no rows have been added).
87
+ # Otherwise, returns the formatted table as a string.
88
+ def as_string
89
+ return '' if @rows.empty?
90
+
91
+ column_widths = calculate_column_widths
92
+ formatted_rows = format_table(column_widths)
93
+ formatted_column_widths = formatted_rows.first.collect { |cell_content| cell_content.length }
94
+ row_separator = construct_row_separator(formatted_column_widths)
95
+
96
+ output = formatted_rows.collect do |row|
97
+ ['', row, ''].flatten.join('|')
98
+ end
99
+
100
+ separated_output = ([row_separator] * (output.length + 1)).zip(output).flatten
101
+
102
+ separated_output.join("\n")
103
+ end
104
+
105
+ # A shortcut for puts(as_string)
106
+ def print
107
+ puts(as_string)
108
+ end
109
+
110
+ private
111
+
112
+ def calculate_column_widths
113
+ column_widths = []
114
+
115
+ @rows.each do |row|
116
+ row.each_with_index do |cell_content, index|
117
+ column_widths[index] ||= 0
118
+ column_widths[index] = [column_widths[index], cell_content.length].max
119
+ end
120
+ end
121
+
122
+ column_widths
123
+ end
124
+
125
+ def format_table(column_widths)
126
+ @rows.collect do |row|
127
+ (0...column_widths.length).collect do |column_index|
128
+ @formatter.call(row[column_index] || '', column_widths[column_index])
129
+ end
130
+ end
131
+ end
132
+
133
+ def construct_row_separator(column_widths)
134
+ separator = column_widths.collect do |column_width|
135
+ '-' * column_width
136
+ end.join('+')
137
+
138
+ "+#{separator}+"
139
+ end
140
+ end
@@ -0,0 +1,73 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe SimpleConsoleTable do
4
+
5
+ before(:each) do
6
+ @table = SimpleConsoleTable.new
7
+ end
8
+
9
+ it 'should return an empty string if the table is empty' do
10
+ @table.as_string.should be_empty
11
+ end
12
+
13
+ it 'should output a nicely formatted table' do
14
+ @table << ['one', 'two', 'three']
15
+ @table << ['four', 'five', 'six']
16
+
17
+ @table.as_string.should == <<-EOF
18
+ +--------+--------+---------+
19
+ | one | two | three |
20
+ +--------+--------+---------+
21
+ | four | five | six |
22
+ +--------+--------+---------+
23
+ EOF
24
+ end
25
+
26
+ it 'should allow adding rows of different lengths' do
27
+ @table << ['one', 'two', 'three']
28
+ @table << ['four', 'five', 'six', 'seven']
29
+
30
+ @table.as_string.should == <<-EOF
31
+ +--------+--------+---------+---------+
32
+ | one | two | three | |
33
+ +--------+--------+---------+---------+
34
+ | four | five | six | seven |
35
+ +--------+--------+---------+---------+
36
+ EOF
37
+ end
38
+
39
+ it 'should use to_s method for values other than strings' do
40
+ class Dummy
41
+ def initialize(dummy_id); @dummy_id = dummy_id; end
42
+ def to_s; "Dummy #{@dummy_id}"; end
43
+ end
44
+
45
+ @table << ['one', 2, 'three']
46
+ @table << [true, 'five', -6.89, Dummy.new(345)]
47
+
48
+ @table.as_string.should == <<-EOF
49
+ +--------+--------+---------+-------------+
50
+ | one | 2 | three | |
51
+ +--------+--------+---------+-------------+
52
+ | true | five | -6.89 | Dummy 345 |
53
+ +--------+--------+---------+-------------+
54
+ EOF
55
+ end
56
+
57
+ it 'should flatten nested arrays' do
58
+ @table << [1, 2, [3, 4], [5]]
59
+ @table << [[[[[[6, 7, 8]]], [[[[9, 10]]]]]]]
60
+
61
+ @table.as_string.should == <<-EOF
62
+ +-----+-----+-----+-----+------+
63
+ | 1 | 2 | 3 | 4 | 5 |
64
+ +-----+-----+-----+-----+------+
65
+ | 6 | 7 | 8 | 9 | 10 |
66
+ +-----+-----+-----+-----+------+
67
+ EOF
68
+ end
69
+
70
+ it 'should have print method for printing the table directly to the standard output' do
71
+ @table.should respond_to(:print)
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sergeysazonov-simple_console_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Sazonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A library capable of printing nicely formatted tables to the standard output
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - lib/simple_console_table.rb
27
+ - LICENSE
28
+ - README.rdoc
29
+ has_rdoc: false
30
+ homepage:
31
+ licenses:
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A library capable of printing nicely formatted tables to the standard output
56
+ test_files:
57
+ - spec/simple_console_table_spec.rb