tabularize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tabularize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Junegunn Choi
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.md ADDED
@@ -0,0 +1,115 @@
1
+ tabularize
2
+ ==========
3
+
4
+ Formatting tabular data with paddings.
5
+ Inspired by tabular.vim (https://github.com/godlygeek/tabular)
6
+
7
+ Installation
8
+ ------------
9
+
10
+ ```
11
+ gem install tabularize
12
+ ```
13
+
14
+ Basic usage
15
+ -----------
16
+
17
+ ### Formatting CSV data
18
+
19
+ #### Sample data in CSV
20
+
21
+ ```
22
+ Name|Dept|Location|Phone
23
+ John Doe|Finance|Los Angeles, CA 90089|555-1555
24
+ Average Joe|Engineering|Somewhere over the rainbow|N/A
25
+ Hong Gildong|HR|Nowhere|555-5555
26
+ ```
27
+
28
+ #### Tabularize.it
29
+
30
+ ```ruby
31
+ require 'csv'
32
+ require 'awesome_print'
33
+ require 'tabularize'
34
+
35
+ data = CSV.read 'test.csv', :col_sep => '|'
36
+ ap data
37
+ ap Tabularize.it(data).map { |row| row.join ' | ' }
38
+ ```
39
+
40
+ #### Output
41
+
42
+ ```
43
+ [
44
+ [0] [
45
+ [0] "Name",
46
+ [1] "Dept",
47
+ [2] "Location",
48
+ [3] "Phone"
49
+ ],
50
+ [1] [
51
+ [0] "John Doe",
52
+ [1] "Finance",
53
+ [2] "Los Angeles, CA 90089",
54
+ [3] "555-1555"
55
+ ],
56
+ [2] [
57
+ [0] "Average Joe",
58
+ [1] "Engineering",
59
+ [2] "Somewhere over the rainbow",
60
+ [3] "N/A"
61
+ ],
62
+ [3] [
63
+ [0] "Hong Gildong",
64
+ [1] "HR",
65
+ [2] "Nowhere",
66
+ [3] "555-5555"
67
+ ]
68
+ ]
69
+ [
70
+ [0] "Name | Dept | Location | Phone ",
71
+ [1] "John Doe | Finance | Los Angeles, CA 90089 | 555-1555",
72
+ [2] "Average Joe | Engineering | Somewhere over the rainbow | N/A ",
73
+ [3] "Hong Gildong | HR | Nowhere | 555-5555"
74
+ ]
75
+ ```
76
+
77
+ #### Alignments
78
+
79
+ ```ruby
80
+ puts Tabularize.it(data, :align => :right).map { |row| row.join ' | ' }
81
+ puts
82
+ puts Tabularize.it(data, :align => :center).map { |row| row.join ' | ' }
83
+ ```
84
+
85
+ ```
86
+ Name | Dept | Location | Phone
87
+ John Doe | Finance | Los Angeles, CA 90089 | 555-1555
88
+ Average Joe | Engineering | Somewhere over the rainbow | N/A
89
+ Hong Gildong | HR | Nowhere | 555-5555
90
+
91
+ Name | Dept | Location | Phone
92
+ John Doe | Finance | Los Angeles, CA 90089 | 555-1555
93
+ Average Joe | Engineering | Somewhere over the rainbow | N/A
94
+ Hong Gildong | HR | Nowhere | 555-5555
95
+ ```
96
+
97
+ #### Padding with other characters
98
+
99
+ ```ruby
100
+ puts Tabularize.it(data, :pad => '_').map { |row| row.join ' | ' }
101
+ ```
102
+
103
+ ```
104
+ Name________ | Dept_______ | Location__________________ | Phone___
105
+ John Doe____ | Finance____ | Los Angeles, CA 90089_____ | 555-1555
106
+ Average Joe_ | Engineering | Somewhere over the rainbow | N/A_____
107
+ Hong Gildong | HR_________ | Nowhere___________________ | 555-5555
108
+ ```
109
+
110
+ Copyright
111
+ ---------
112
+
113
+ Copyright (c) 2012 Junegunn Choi. See LICENSE.txt for
114
+ further details.
115
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
@@ -0,0 +1,3 @@
1
+ module Tabularize
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tabularize.rb ADDED
@@ -0,0 +1,52 @@
1
+ require "tabularize/version"
2
+
3
+ module Tabularize
4
+ DEFAULT_OPTIONS = {
5
+ :pad => ' ',
6
+ :align => :left
7
+ }
8
+
9
+ # Formats two-dimensional tabular data.
10
+ # One-dimensional data (e.g. Array of Strings) is treated as tabular data
11
+ # of which each row has only one column.
12
+ # @param [Enumerable] table_data
13
+ # @param [Hash] options Formatting options.
14
+ # @return [Array] Two-dimensional Array of formatted cells.
15
+ def self.it table_data, options = {}
16
+ raise ArgumentError.new("Not enumerable") unless
17
+ table_data.respond_to?(:each)
18
+
19
+ options = DEFAULT_OPTIONS.merge(options)
20
+ pad = options[:pad].to_s
21
+ align = options[:align]
22
+ raise ArgumentError.new("Invalid alignment") unless pad.length == 1
23
+ raise ArgumentError.new("Invalid alignment") unless
24
+ [:left, :right, :center].include?(align)
25
+
26
+ rows = []
27
+ max_widths = []
28
+ table_data.each do |row|
29
+ rows << row = [*row].map(&:to_s).map(&:chomp)
30
+
31
+ row.each_with_index do |cell, idx|
32
+ max_widths[idx] = [ cell.length, max_widths[idx] || 0 ].max
33
+ end
34
+ end
35
+
36
+ rows.map { |row|
37
+ idx = -1
38
+ row.map { |str|
39
+ idx += 1
40
+ w = max_widths[idx]
41
+ case align
42
+ when :left
43
+ str.ljust(w, pad)
44
+ when :right
45
+ str.rjust(w, pad)
46
+ when :center
47
+ str.rjust((w - str.length) / 2 + str.length, pad).ljust(w, pad)
48
+ end
49
+ }
50
+ }
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tabularize/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tabularize"
7
+ s.version = Tabularize::VERSION
8
+ s.authors = ["Junegunn Choi"]
9
+ s.email = ["junegunn.c@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Formatting tabular data}
12
+ s.description = %q{Formatting tabular data with paddings and alignments}
13
+
14
+ s.rubyforge_project = "tabularize"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "awesome_print"
23
+ end
data/test/test.csv ADDED
@@ -0,0 +1,4 @@
1
+ Name|Dept|Location|Phone
2
+ John Doe|Finance|Los Angeles, CA 90089|555-1555
3
+ Average Joe|Engineering|Somewhere over the rainbow|N/A
4
+ Hong Gildong|HR|Nowhere|555-5555
@@ -0,0 +1,125 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup(:default, :development)
4
+ require 'test/unit'
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'tabularize'
7
+ require 'awesome_print'
8
+ require 'csv'
9
+
10
+ class TestTabularize < Test::Unit::TestCase
11
+ DATA0 = %w[a aa aaa aaaa aaaaa]
12
+ DATA1 =
13
+ [
14
+ %w[a aa aaa aaaa],
15
+ %w[bbbb bbb bb b]
16
+ ]
17
+ DATA2 =
18
+ [
19
+ %w[a aa aaa aaaa],
20
+ %w[cccccccccccccccccccc],
21
+ %w[ddd dddd d],
22
+ %w[bbbb bbb bb b]
23
+ ]
24
+ RESULT = {
25
+ DATA0 => {
26
+ :left => [
27
+ "a ",
28
+ "aa ",
29
+ "aaa ",
30
+ "aaaa ",
31
+ "aaaaa",
32
+ ],
33
+ :right => [
34
+ " a",
35
+ " aa",
36
+ " aaa",
37
+ " aaaa",
38
+ "aaaaa",
39
+ ],
40
+ :center => [
41
+ " a ",
42
+ " aa ",
43
+ " aaa ",
44
+ "aaaa ",
45
+ "aaaaa",
46
+ ],
47
+ },
48
+ DATA1 => {
49
+ :left =>
50
+ [
51
+ "a |aa |aaa|aaaa",
52
+ "bbbb|bbb|bb |b "
53
+ ],
54
+ :right =>
55
+ [
56
+ " a| aa|aaa|aaaa",
57
+ "bbbb|bbb| bb| b"
58
+ ],
59
+ :center =>
60
+ [
61
+ " a |aa |aaa|aaaa",
62
+ "bbbb|bbb|bb | b "
63
+ ]
64
+ },
65
+ DATA2 => {
66
+ :left =>
67
+ [
68
+ "a |aa |aaa|aaaa",
69
+ "cccccccccccccccccccc",
70
+ "ddd |dddd|d ",
71
+ "bbbb |bbb |bb |b "
72
+ ],
73
+ :right =>
74
+ [
75
+ " a| aa|aaa|aaaa",
76
+ "cccccccccccccccccccc",
77
+ " ddd|dddd| d",
78
+ " bbbb| bbb| bb| b"
79
+ ],
80
+ :center =>
81
+ [
82
+ " a | aa |aaa|aaaa",
83
+ "cccccccccccccccccccc",
84
+ " ddd |dddd| d ",
85
+ " bbbb |bbb |bb | b "
86
+ ]
87
+ }
88
+ }
89
+
90
+ def test_tabularize
91
+ data_lines = DATA0.join($/).lines
92
+ RESULT[data_lines] = RESULT[DATA0]
93
+ [DATA0, data_lines, DATA1, DATA2].each do |data|
94
+ [ '.', '_' ].each do |pad|
95
+ [:left, :right, :center].each do |align|
96
+ result = Tabularize.it(data, :pad => pad, :align => align)
97
+ ap :align => align, :pad => pad, :from => data, :to => result.map { |r| r.join('|') }
98
+ assert_equal RESULT[data][align], result.map { |row| row.join('|').gsub(pad, ' ') }
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ def test_tabularize_csv
105
+ data = CSV.read(File.join(File.dirname(__FILE__), 'test.csv'), :col_sep => '|')
106
+ ap data
107
+ output = Tabularize.it(data).map { |row| row.join ' | ' }
108
+ ap output
109
+ puts
110
+ puts output
111
+
112
+ puts Tabularize.it(data, :align => :right).map { |row| row.join ' | ' }
113
+ puts
114
+ puts Tabularize.it(data, :align => :center).map { |row| row.join ' | ' }
115
+ puts
116
+ puts Tabularize.it(data, :pad => '_').map { |row| row.join ' | ' }
117
+ end
118
+
119
+ def test_invalid_arguments
120
+ assert_raise(ArgumentError) { Tabularize.it(5) }
121
+ assert_raise(ArgumentError) { Tabularize.it("hello") }
122
+ assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :align => :noidea) }
123
+ assert_raise(ArgumentError) { Tabularize.it([1, 2, 3], :pad => 'long') }
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabularize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: awesome_print
16
+ requirement: &2152636520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152636520
25
+ description: Formatting tabular data with paddings and alignments
26
+ email:
27
+ - junegunn.c@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - lib/tabularize.rb
38
+ - lib/tabularize/version.rb
39
+ - tabularize.gemspec
40
+ - test/test.csv
41
+ - test/test_tabularize.rb
42
+ homepage: ''
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project: tabularize
62
+ rubygems_version: 1.8.15
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Formatting tabular data
66
+ test_files:
67
+ - test/test.csv
68
+ - test/test_tabularize.rb
69
+ has_rdoc: