tablegen 0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 57a14ccba33ed27298a35cbe25625d55d837b6e2
4
+ data.tar.gz: 0f0a6c29b6761d162d459d64a919008f9cf6aaaf
5
+ SHA512:
6
+ metadata.gz: 480697a91c71dff89565c3d65a9a813e83c17f457c1e42e18c795cfb14dc395939a0b4e2a642694be0b952870f1847bdbca425b8556cc042558294d1365d5b15
7
+ data.tar.gz: ab342a58c97afd5156a670df6eca9ed71c73213ce8128bd5789fe4b543497d4ab3a917de54188bd82411bcc3cac042763985654c94af39ce3de019093f1058b2
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tablegen.gemspec
4
+ gemspec
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,91 @@
1
+ # Table Generator
2
+
3
+ Plain text table generator for ruby.
4
+
5
+ Main features:
6
+
7
+ - Choose column/header alignment (left, center or right)
8
+ - CJK characters support
9
+ - Embed progress bars (example in the documentation)
10
+ - Insert text in the table
11
+ - Minimum column length
12
+ - Optional size constraint
13
+ - Per column data formatter (data is separated from the layout)
14
+ - Scale down large tables with collapsible columns
15
+ - Scale up small tables with stretchable columns
16
+ - Unlimited border length
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'tablegen'
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install tablegen
31
+
32
+ ## Documentation
33
+
34
+ The documentation is hosted at
35
+ [http://rubydoc.info/gems/tablegen/TableGen](http://rubydoc.info/gems/tablegen/TableGen).
36
+
37
+ ## Basic Usage
38
+
39
+ ```ruby
40
+ require 'tablegen'
41
+
42
+ table = TableGen.new
43
+ table.header 'Browser Name', 'Layout Engine', 'License'
44
+ table.separator
45
+ table.row 'Chromium', 'Blink', 'BSD'
46
+ table.row 'dwb', 'WebKit', 'GNU GPLv3'
47
+ table.row 'Internet Explorer', 'Trident', 'Proprietary'
48
+ table.row 'Mozilla Firefox', 'Gecko', 'MPL'
49
+ puts table
50
+ ```
51
+
52
+ Output:
53
+
54
+ ```text
55
+ Browser Name Layout Engine License
56
+ ===========================================
57
+ Chromium Blink BSD
58
+ dwb WebKit GNU GPLv3
59
+ Internet Explorer Trident Proprietary
60
+ Mozilla Firefox Gecko MPL
61
+ ```
62
+
63
+ Set the size constraint and stretch the first column:
64
+
65
+ ```ruby
66
+ table.width = 50
67
+ table.column 0 do |col|
68
+ col.stretch = true
69
+ end
70
+ puts table
71
+ ```
72
+
73
+ Output:
74
+
75
+ ```text
76
+ Browser Name Layout Engine License
77
+ ==================================================
78
+ Chromium Blink BSD
79
+ dwb WebKit GNU GPLv3
80
+ Internet Explorer Trident Proprietary
81
+ Mozilla Firefox Gecko MPL
82
+ ```
83
+
84
+ ## Contributing
85
+
86
+ 1. [Fork it](https://bitbucket.org/cfi30/tablegen/fork)
87
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
88
+ 3. Test your changes (`rake`)
89
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 5. Push to the branch (`git push origin my-new-feature`)
91
+ 6. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:test]
5
+ Rake::TestTask.new do |t|
6
+ t.test_files = FileList['test/helper.rb', 'test/test_*.rb']
7
+ end
@@ -0,0 +1,368 @@
1
+ require 'tablegen/version'
2
+
3
+ # TableGen is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ class TableGen
8
+ require 'tablegen/column'
9
+
10
+ # Base class for exceptions.
11
+ class Error < RuntimeError; end
12
+
13
+ # Raised when the table cannot respect the width constraint.
14
+ class WidthError < Error; end
15
+
16
+ # @api private
17
+ Header = Struct.new \
18
+ :name
19
+
20
+ # @api private
21
+ Line = Struct.new \
22
+ :type,
23
+ :data
24
+
25
+ # @attribute border [rw] The column separator.
26
+ # May be of any length. Defaults to a space.
27
+ #
28
+ # @return [String]
29
+ attr_accessor :border
30
+ attr_writer :width
31
+
32
+ def initialize
33
+ @border = "\x20"
34
+ @columns = []
35
+ @lines = []
36
+ @collapsed = []
37
+ end
38
+
39
+ # Yields and returns the column at the specified index.
40
+ #
41
+ # @example Change the alignment of the first column
42
+ # table.column 0 do |col|
43
+ # col.alignment = :right
44
+ # end
45
+ #
46
+ # @param index [Fixnum]
47
+ # @yield [Column column] the requested column
48
+ # @return [Column] the requested column
49
+ #
50
+ # @see #columns
51
+ # @see Column
52
+ def column(index)
53
+ unless col = @columns[index]
54
+ if @columns.count < index
55
+ # create columns so the holes are not filled with nil
56
+ (@columns.count..index).each {|i| column i }
57
+ end
58
+
59
+ col = Column.new index
60
+ @columns[index] = col
61
+ end
62
+
63
+ yield col if block_given?
64
+ col
65
+ end
66
+
67
+ # Shorthand to {#column}: Yields specified columns.
68
+ #
69
+ # @example Allow columns 6 and 8 to be collapsed
70
+ # table.columns 6, 8 do |col|
71
+ # col.collapse = true
72
+ # end
73
+ #
74
+ # @param [Array<Fixnum>] indexes
75
+ # @yield [Column column] a column from the index list
76
+ #
77
+ # @see #column
78
+ # @see Column
79
+ def columns(*indexes, &block)
80
+ indexes.each {|index|
81
+ column index, &block
82
+ }
83
+ end
84
+
85
+ # Add a row to the table. The fields are formatted with {Column#format}.
86
+ #
87
+ # @example
88
+ # table.column 2 do |col|
89
+ # col.format = proc {|price|
90
+ # "$%.2f" % price
91
+ # }
92
+ # end
93
+ #
94
+ # # Product Name, Quantity, Price
95
+ # table.row 'Table Generator', 42, 0
96
+ #
97
+ # @param [Array<Object>] fields
98
+ # @raise [ArgumentError] at least one field is required
99
+ #
100
+ # @see #header
101
+ def row(*fields)
102
+ raise ArgumentError, 'wrong number of arguments (0 for 1+)' if fields.empty?
103
+ @lines << Line.new(:row, fields)
104
+ end
105
+
106
+ # Add a header row to the table. The fields are not formatted.
107
+ #
108
+ # @example
109
+ # table.header 'Product Name', 'Quantity', 'Price'
110
+ #
111
+ # @param [Array<String>] fields
112
+ # @raise [ArgumentError] at least one field is required
113
+ #
114
+ # @see #row
115
+ def header(*fields)
116
+ row *fields.map {|name| Header.new name }
117
+ end
118
+
119
+ # Add a separator to the table.
120
+ #
121
+ # @param [String] char the character to repeat
122
+ def separator(char = '=')
123
+ @lines << Line.new(:separator, char)
124
+ end
125
+
126
+ # Add a text line to the table.
127
+ # The text is wrapped automatically to fit into the table.
128
+ #
129
+ # @param line [String]
130
+ def text(line)
131
+ @lines << Line.new(:text, line)
132
+ end
133
+
134
+ # Empty the table. Columns settings are conserved.
135
+ #
136
+ # @see #clear!
137
+ def clear
138
+ @lines.clear
139
+ end
140
+
141
+ # Empty the table AND delete the columns.
142
+ #
143
+ # @see #clear
144
+ def clear!
145
+ clear
146
+ @columns.clear
147
+ end
148
+
149
+ # The maximum width (in characters) of the table.
150
+ # If unspecified (nil), returns the space required to display every row and header.
151
+ #
152
+ # @example Fit the table in the terminal
153
+ # if STDOUT.tty?
154
+ # table.width = STDOUT.winsize[1]
155
+ # end
156
+ #
157
+ # @return [Fixnum]
158
+ #
159
+ # @see #real_width
160
+ def width
161
+ return @width unless @width.nil?
162
+
163
+ width = 0
164
+ rows.each {|row|
165
+ format = format_row row.data
166
+ length = real_length format
167
+ width = [width, length].max
168
+ }
169
+ width
170
+ end
171
+
172
+ # Calculates the exact width (in characters) of the entire table.
173
+ #
174
+ # @return [Fixnum]
175
+ #
176
+ # @see #width
177
+ def real_width
178
+ to_s.each_line.map {|l| real_length l.chomp }.max || 0
179
+ end
180
+
181
+ # The minimum height (in lines) of the table.
182
+ # @note Does not calculate wrapped text lines. If required, use {#real_height} instead.
183
+ #
184
+ # @!attribute [r] height
185
+ # @return [Fixnum]
186
+ #
187
+ # @see #real_height
188
+ def height
189
+ @lines.count
190
+ end
191
+
192
+ # Calculates the exact height (in lines) of the table.
193
+ #
194
+ # @return [Fixnum]
195
+ #
196
+ # @see #height
197
+ def real_height
198
+ to_s.lines.count
199
+ end
200
+
201
+ # Generate the table.
202
+ #
203
+ # begin
204
+ # puts table
205
+ # rescue TableGen::WidthError
206
+ # puts 'Terminal is too small'
207
+ # end
208
+ #
209
+ # @return [String] the table
210
+ # @raise [WidthError] if the table is too large to fit in the {#width} constraint
211
+ # @raise [Error] if something is wrong (eg. invalid column alignment)
212
+ def to_s
213
+ create_columns
214
+
215
+ table = ''
216
+ @collapsed.clear
217
+
218
+ loop do
219
+ table, missing_width = generate_table
220
+ break if missing_width == 0
221
+
222
+ candidates = []
223
+ @columns.each_with_index {|col, index|
224
+ if col.collapse && !@collapsed.include?(index)
225
+ candidates << index
226
+ end
227
+ }
228
+
229
+ if candidates.empty?
230
+ raise WidthError, "insufficient width to generate the table"
231
+ end
232
+
233
+ @collapsed << candidates.min_by {|index|
234
+ (column_width(index, false) - missing_width).abs
235
+ }
236
+ end
237
+
238
+ table
239
+ end
240
+
241
+ private
242
+ def rows
243
+ @lines.select {|l| l.type == :row }
244
+ end
245
+
246
+ def real_length(string)
247
+ doublesize = string.scan(/\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/).count
248
+ string.length + doublesize
249
+ end
250
+
251
+ def format_row(fields)
252
+ out = ''
253
+
254
+ fields.each_with_index {|data, index|
255
+ next if @collapsed.include? index
256
+ col = column index
257
+
258
+ width = column_width index
259
+ field = format_field(col, data, width)
260
+ length = real_length field
261
+
262
+ pad_width = width - length
263
+ padding = col.padding[0] * pad_width
264
+
265
+ alignment = if data.is_a?(Header) && col.header_alignment != :auto
266
+ col.header_alignment
267
+ else
268
+ col.alignment
269
+ end
270
+
271
+ out += @border unless out.empty?
272
+ out += \
273
+ case alignment
274
+ when :left
275
+ field + padding
276
+ when :right
277
+ padding + field
278
+ when :center
279
+ left = pad_width / 2
280
+ right = left + (pad_width % 2)
281
+ padding[0...left] + field + padding[0...right]
282
+ else
283
+ raise Error, "invalid alignment '%s'" % col.alignment
284
+ end
285
+ }
286
+ out
287
+ end
288
+
289
+ def column_width(index, can_stretch = true)
290
+ col = column index
291
+
292
+ stretch_index = @columns.find_index {|c|
293
+ c.stretch && !@collapsed.include?(@columns.index(c))
294
+ }
295
+
296
+ if can_stretch && index == stretch_index && @width
297
+ used_width = 0
298
+ @columns.each_with_index {|dist_col, dist_index|
299
+ next if dist_index == stretch_index || @collapsed.include?(dist_index)
300
+
301
+ dist_width = column_width(dist_index, false)
302
+ next if dist_width.nil?
303
+
304
+ used_width += dist_width
305
+ used_width += real_length @border
306
+ }
307
+
308
+ remaining_width = @width - used_width
309
+ needed_width = column_width index, false
310
+ [remaining_width, needed_width].max
311
+ else
312
+ sizes = []
313
+ rows.each {|row|
314
+ data = row.data[index]
315
+ next unless data
316
+
317
+ length = real_length format_field(col, data, col.min_width)
318
+ sizes << [col.min_width, length].max
319
+ }
320
+ sizes.max
321
+ end
322
+ end
323
+
324
+ def format_field(column, data, width)
325
+ if data.is_a? Header
326
+ data.name
327
+ else
328
+ column.format[data, width]
329
+ end
330
+ end
331
+
332
+ def create_columns
333
+ rows.each {|row|
334
+ # creates all columns up to the specified index
335
+ column row.data.count - 1
336
+ }
337
+ end
338
+
339
+ def generate_table
340
+ table = ''
341
+ missing_width = [0]
342
+
343
+ @lines.each {|line|
344
+ out = case line.type
345
+ when :row
346
+ format_row line.data
347
+ when :separator
348
+ line.data[0] * width
349
+ when :text
350
+ if width > 0
351
+ line.data.scan(/.{1,#{width}}/).join $/
352
+ else
353
+ line.data
354
+ end
355
+ end
356
+ out.rstrip!
357
+
358
+ if line.type == :row
359
+ line_length = real_length out
360
+ missing_width << line_length - @width if @width
361
+ end
362
+
363
+ table += out + $/
364
+ }
365
+
366
+ return table.chomp, missing_width.max
367
+ end
368
+ end