tablify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +20 -0
- data/README.md +61 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/tablify.rb +44 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/tablify_spec.rb +151 -0
- metadata +158 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
tablify
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-1.9.3-p327
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Max Thom Stahl
|
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,61 @@
|
|
1
|
+
# tablify
|
2
|
+
|
3
|
+
Ever want those super pretty old-school UNIX style tables in your program's
|
4
|
+
output? Do you have tables or paragraphs you want to gussy up for a night on
|
5
|
+
the town? Then Tablify is your jam. Use it.
|
6
|
+
|
7
|
+
Make an array of hashes into something truly beautiful:
|
8
|
+
|
9
|
+
ruby> foo = [{foo: 1, bar: 2}, {foo: 11, bar: 22, baz: 42}, {foo: 111, bar: 222}]
|
10
|
+
ruby> puts Tablify(foo)
|
11
|
+
+-----+-----+-----+
|
12
|
+
| foo | bar | baz |
|
13
|
+
+-----+-----+-----+
|
14
|
+
| 1 | 2 | |
|
15
|
+
| 11 | 22 | 42 |
|
16
|
+
| 111 | 222 | |
|
17
|
+
+-----+-----+-----+
|
18
|
+
|
19
|
+
What's that? You didn't want that `baz` column in there after all? Jam this
|
20
|
+
into your input pipe and smoke it:
|
21
|
+
|
22
|
+
ruby> puts Tablify(foo).omit(:baz)
|
23
|
+
+-----+-----+
|
24
|
+
| foo | bar |
|
25
|
+
+-----+-----+
|
26
|
+
| 1 | 2 |
|
27
|
+
| 11 | 22 |
|
28
|
+
| 111 | 222 |
|
29
|
+
+-----+-----+
|
30
|
+
|
31
|
+
or...
|
32
|
+
|
33
|
+
ruby> puts Tablify(foo).include(:foo, :bar)
|
34
|
+
+-----+-----+
|
35
|
+
| foo | bar |
|
36
|
+
+-----+-----+
|
37
|
+
| 1 | 2 |
|
38
|
+
| 11 | 22 |
|
39
|
+
| 111 | 222 |
|
40
|
+
+-----+-----+
|
41
|
+
|
42
|
+
## Contributing to tablify
|
43
|
+
|
44
|
+
* Check out the latest master to make sure the feature hasn't been
|
45
|
+
implemented or the bug hasn't been fixed yet.
|
46
|
+
* Check out the issue tracker to make sure someone already hasn't requested
|
47
|
+
it and/or contributed it.
|
48
|
+
* Fork the project.
|
49
|
+
* Start a feature/bugfix branch.
|
50
|
+
* Commit and push until you are happy with your contribution.
|
51
|
+
* Make sure to add tests for it. This is important so I don't break it in a
|
52
|
+
future version unintentionally.
|
53
|
+
* Please try not to mess with the Rakefile, version, or history. If you want
|
54
|
+
to have your own version, or is otherwise necessary, that is fine, but
|
55
|
+
please isolate to its own commit so I can cherry-pick around it.
|
56
|
+
|
57
|
+
## Copyright
|
58
|
+
|
59
|
+
Copyright (c) 2013 Max Thom Stahl. See LICENSE.txt for
|
60
|
+
further details.
|
61
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "tablify"
|
18
|
+
gem.homepage = "http://github.com/mstahl/tablify"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Gussied up tables for your consoles.}
|
21
|
+
gem.description = %Q{Use Tablify to make prettier tabulated output in the console.}
|
22
|
+
gem.email = "max@villainousindustri.es"
|
23
|
+
gem.authors = ["max thom stahl"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'yard'
|
42
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/tablify.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Tablify
|
2
|
+
def self.table(array, opts = {})
|
3
|
+
headers = array.map(&:keys).flatten.uniq.sort
|
4
|
+
|
5
|
+
if opts[:include]
|
6
|
+
headers = headers & opts[:include]
|
7
|
+
end
|
8
|
+
if opts[:exclude]
|
9
|
+
headers = headers - opts[:exclude]
|
10
|
+
end
|
11
|
+
|
12
|
+
column_widths = headers.map(&:length)
|
13
|
+
|
14
|
+
array.each do |hash|
|
15
|
+
headers.each_with_index do |header, i|
|
16
|
+
l = hash[header].to_s.length
|
17
|
+
if l > column_widths[i]
|
18
|
+
column_widths[i] = l
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
lines = [horizontal_rule(column_widths)]
|
24
|
+
lines << aligned_row(headers, column_widths)
|
25
|
+
lines << horizontal_rule(column_widths)
|
26
|
+
array.each do |row|
|
27
|
+
row_elements = headers.map{|h| row[h]}
|
28
|
+
lines << aligned_row(row_elements, column_widths)
|
29
|
+
end
|
30
|
+
lines << horizontal_rule(column_widths)
|
31
|
+
|
32
|
+
lines.join("\n")
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def self.aligned_row(row, widths)
|
38
|
+
"| " + (widths.map{|width| "%0#{width}s"}.join(" | ") % row) + " |"
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.horizontal_rule(widths)
|
42
|
+
"+" + widths.map{|w| "-" * (w + 2)}.join("+") + "+"
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'tablify'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Tablify do
|
4
|
+
describe '::table' do
|
5
|
+
|
6
|
+
context 'with only an array argument, with no options specified' do
|
7
|
+
it 'should convert an array of hashes by itself into a table with alphabetized key columns' do
|
8
|
+
table = [
|
9
|
+
{
|
10
|
+
a: 1,
|
11
|
+
b: 2,
|
12
|
+
c: 3
|
13
|
+
}, {
|
14
|
+
a: 4,
|
15
|
+
b: 5,
|
16
|
+
c: 6
|
17
|
+
}, {
|
18
|
+
a: 7,
|
19
|
+
b: 8,
|
20
|
+
c: 9
|
21
|
+
}
|
22
|
+
]
|
23
|
+
|
24
|
+
Tablify.table(table).strip.should == %{
|
25
|
+
+---+---+---+
|
26
|
+
| a | b | c |
|
27
|
+
+---+---+---+
|
28
|
+
| 1 | 2 | 3 |
|
29
|
+
| 4 | 5 | 6 |
|
30
|
+
| 7 | 8 | 9 |
|
31
|
+
+---+---+---+
|
32
|
+
}.strip
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should convert an array of hashes when not all hashes contain all keys' do
|
36
|
+
table = [
|
37
|
+
{
|
38
|
+
a: 1,
|
39
|
+
c: 3
|
40
|
+
}, {
|
41
|
+
b: 5,
|
42
|
+
}, {
|
43
|
+
a: 7,
|
44
|
+
c: 9
|
45
|
+
}
|
46
|
+
]
|
47
|
+
|
48
|
+
Tablify.table(table).strip.should == %{
|
49
|
+
+---+---+---+
|
50
|
+
| a | b | c |
|
51
|
+
+---+---+---+
|
52
|
+
| 1 | | 3 |
|
53
|
+
| | 5 | |
|
54
|
+
| 7 | | 9 |
|
55
|
+
+---+---+---+
|
56
|
+
}.strip
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should throw an error if called on a non-enumerable' do
|
60
|
+
pending "Not implemented yet."
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should throw an error if called on an array that contains nil or non-Hash elements' do
|
64
|
+
pending "Not implemented yet."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'with :include specified' do
|
69
|
+
it 'should only include the columns specified with :include' do
|
70
|
+
table = [
|
71
|
+
{
|
72
|
+
a: 1,
|
73
|
+
b: 2,
|
74
|
+
c: 3
|
75
|
+
}, {
|
76
|
+
a: 4,
|
77
|
+
b: 5,
|
78
|
+
c: 6
|
79
|
+
}, {
|
80
|
+
a: 7,
|
81
|
+
b: 8,
|
82
|
+
c: 9
|
83
|
+
}
|
84
|
+
]
|
85
|
+
|
86
|
+
Tablify.table(table, include: [:a, :c]).strip.should == %{
|
87
|
+
+---+---+
|
88
|
+
| a | c |
|
89
|
+
+---+---+
|
90
|
+
| 1 | 3 |
|
91
|
+
| 4 | 6 |
|
92
|
+
| 7 | 9 |
|
93
|
+
+---+---+
|
94
|
+
}.strip
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should convert an array of hashes when not all hashes contain all keys' do
|
98
|
+
pending "Not implemented yet."
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'with :exclude specified' do
|
103
|
+
it 'should only include the columns not specified with :exclude' do
|
104
|
+
table = [
|
105
|
+
{
|
106
|
+
a: 1,
|
107
|
+
b: 2,
|
108
|
+
c: 3
|
109
|
+
}, {
|
110
|
+
a: 4,
|
111
|
+
b: 5,
|
112
|
+
c: 6
|
113
|
+
}, {
|
114
|
+
a: 7,
|
115
|
+
b: 8,
|
116
|
+
c: 9
|
117
|
+
}
|
118
|
+
]
|
119
|
+
|
120
|
+
Tablify.table(table, exclude: [:a]).strip.should == %{
|
121
|
+
+---+---+
|
122
|
+
| b | c |
|
123
|
+
+---+---+
|
124
|
+
| 2 | 3 |
|
125
|
+
| 5 | 6 |
|
126
|
+
| 8 | 9 |
|
127
|
+
+---+---+
|
128
|
+
}.strip
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should convert an array of hashes when not all hashes contain all keys' do
|
132
|
+
pending "Not implemented yet."
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'with :horizontal specified' do
|
137
|
+
it 'should convert an array of hashes, arranging their keys in the first column of a table' do
|
138
|
+
pending "Not implemented yet."
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'should convert an array of hashes when not all hashes contain all keys' do
|
142
|
+
pending "Not implemented yet."
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
describe '::paragraph' do
|
149
|
+
pending "Not implemented yet."
|
150
|
+
end
|
151
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tablify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- max thom stahl
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: yard
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rdoc
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: jeweler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Use Tablify to make prettier tabulated output in the console.
|
111
|
+
email: max@villainousindustri.es
|
112
|
+
executables: []
|
113
|
+
extensions: []
|
114
|
+
extra_rdoc_files:
|
115
|
+
- LICENSE.txt
|
116
|
+
- README.md
|
117
|
+
files:
|
118
|
+
- .document
|
119
|
+
- .rspec
|
120
|
+
- .ruby-gemset
|
121
|
+
- .ruby-version
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- VERSION
|
127
|
+
- lib/tablify.rb
|
128
|
+
- spec/spec_helper.rb
|
129
|
+
- spec/tablify_spec.rb
|
130
|
+
homepage: http://github.com/mstahl/tablify
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
hash: 3072618796010270161
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ! '>='
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 1.8.24
|
155
|
+
signing_key:
|
156
|
+
specification_version: 3
|
157
|
+
summary: Gussied up tables for your consoles.
|
158
|
+
test_files: []
|