tabulo 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ac51b57ff0b2dc5ce13ea28612183fe2691ed88
4
+ data.tar.gz: f004eedf9ec5fa112a289aba3f8411805d88a9fb
5
+ SHA512:
6
+ metadata.gz: 5aa026d70495b3a06a6d364ef2f58bfdb007911f0f43f770dd7fc66bdc6b7c55321234cfe938d1869661d6f5e5526b1b0eea4bf0c31348ccab4a901151c7347a
7
+ data.tar.gz: e05b95a3ee28396e71b4771ddaf2f3803715935c391cfce31e1488c8dbdbb3d5c9e5fc641c7fb8a61ffb24ea241979a637f285b24cabf6c49d54fbd24842b8c1
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ **/.*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.5
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tabulo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Matthew Harvey
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,114 @@
1
+ # Tabulo
2
+
3
+ ## Overview
4
+
5
+ Tabulo generates ASCII tables for displaying in a terminal or as preformatted text.
6
+
7
+ A `Tabulo::Table` can be printed:
8
+
9
+ ```
10
+ > puts table
11
+ +----------+----------+
12
+ | N | Doubled |
13
+ +----------+----------+
14
+ | 1 | 2 |
15
+ | 2 | 4 |
16
+ | 50000000 | 10000000 |
17
+ ```
18
+
19
+ But it is also `Enumerable`, so you can process it one row at a time:
20
+
21
+ ```ruby
22
+ table.each do |row|
23
+ puts row
24
+ # do some other thing that you want to do for each row
25
+ end
26
+ ```
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'tabulo'
34
+ ```
35
+
36
+ And then execute:
37
+
38
+ $ bundle
39
+
40
+ Or install it yourself as:
41
+
42
+ $ gem install tabulo
43
+
44
+ ## Usage
45
+
46
+ Require the gem:
47
+
48
+ ```ruby
49
+ require 'tabulo'
50
+ ```
51
+
52
+ Instantiate a `Tabulo::Table` by passing it an underlying `Enumerable` and then telling it
53
+ the columns you want to generate.
54
+
55
+ A simple case involves initializing columns from symbols corresponding to methods on members of the
56
+ `Enumerable`. In this case the symbol also provides the header for each column:
57
+
58
+ ```ruby
59
+ table = Tabulo::Table.new([1, 2, 5]) do |t|
60
+ t.add_column(:itself)
61
+ t.add_column(:even?)
62
+ t.add_column(:odd?)
63
+ end
64
+
65
+ # > puts table
66
+ # +----------+----------+----------+
67
+ # | itself | even? | odd? |
68
+ # +----------+----------+----------+
69
+ # | 1 | false | true |
70
+ # | 2 | true | false |
71
+ # | 5 | false | true |
72
+ ```
73
+
74
+ Columns can also be initialized using blocks or procs that are called on each object. In this case
75
+ the first argument provides the column header:
76
+
77
+ ```ruby
78
+ table = Tabulo::Table.new([1, 2, 5]) do |t|
79
+ t.add_column("N", &:itself)
80
+ t.add_column("Doubled") { |n| n * 2 }
81
+ end
82
+
83
+ # > puts table
84
+ # +----------+----------+
85
+ # | N | Doubled |
86
+ # +----------+----------+
87
+ # | 1 | 2 |
88
+ # | 2 | 4 |
89
+ # | 5 | 10 |
90
+ ```
91
+
92
+ TODO: Finish this...
93
+
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
98
+ the tests. You can also run `bin/console` for an interactive prompt that will allow you to
99
+ experiment.
100
+
101
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new
102
+ version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
103
+ will create a git tag for the version, push git commits and tags, and push the `.gem` file to
104
+ [rubygems.org](https://rubygems.org).
105
+
106
+ ## Contributing
107
+
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matt-harvey/tabulo.
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT
113
+ License](http://opensource.org/licenses/MIT).
114
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tabulo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/tabulo.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "tabulo/version"
2
+ require "tabulo/table"
3
+ require "tabulo/row"
4
+ require "tabulo/column"
5
+
6
+ # TODO Tidy the code.
7
+ # TODO Handle newline in output.
8
+ # TODO Allow the option of a horizontal rule between rows.
@@ -0,0 +1,66 @@
1
+ module Tabulo
2
+
3
+ class Column
4
+
5
+ attr_reader :label, :truncate, :width
6
+
7
+ def initialize(options)
8
+ @label, @header = options[:label], options[:header]
9
+ @truncate = options[:truncate]
10
+ @align_header, @align_body = options[:align_header], options[:align_body]
11
+ @extractor, @formatter = options[:extractor], options[:formatter]
12
+ @width = options[:width]
13
+ @horizontal_rule_character = options[:horizontal_rule_character]
14
+ end
15
+
16
+ def header_cell
17
+ align_cell_content(@header, @align_header)
18
+ end
19
+
20
+ def horizontal_rule
21
+ @horizontal_rule_character * @width
22
+ end
23
+
24
+ def body_cell(source)
25
+ cell_datum = body_cell_value(source)
26
+ formatted_cell_content = @formatter.call(cell_datum)
27
+ real_alignment = @align_body || infer_alignment(cell_datum)
28
+ align_cell_content(formatted_cell_content, real_alignment)
29
+ end
30
+
31
+ def body_cell_value(source)
32
+ @extractor.call(source)
33
+ end
34
+
35
+ private
36
+
37
+ def align_cell_content(content, real_alignment)
38
+ padding = [@width - content.length, 0].max
39
+ left_padding, right_padding =
40
+ case real_alignment
41
+ when :center
42
+ half_padding = padding / 2
43
+ [padding - half_padding, half_padding]
44
+ when :left
45
+ [0, padding]
46
+ when :right
47
+ [padding, 0]
48
+ else
49
+ raise "Unrecognized alignment: #{real_alignment}"
50
+ end
51
+
52
+ "#{' ' * left_padding}#{content}#{' ' * right_padding}"
53
+ end
54
+
55
+ def infer_alignment(cell_datum)
56
+ case cell_datum
57
+ when Numeric
58
+ :right
59
+ when TrueClass, FalseClass
60
+ :center
61
+ else
62
+ :left
63
+ end
64
+ end
65
+ end
66
+ end
data/lib/tabulo/row.rb ADDED
@@ -0,0 +1,26 @@
1
+ module Tabulo
2
+
3
+ class Row
4
+ include Enumerable
5
+
6
+ def initialize(table, source, options = { with_header: true })
7
+ @table = table
8
+ @source = source
9
+ @with_header = options[:with_header]
10
+ end
11
+
12
+ def each
13
+ @table.columns.each do |column|
14
+ yield column.body_cell_value(@source)
15
+ end
16
+ end
17
+
18
+ def to_s
19
+ @table.formatted_body_row(@source, with_header: @with_header)
20
+ end
21
+
22
+ def to_h
23
+ @table.columns.map(&:label).zip(to_a).to_h
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,123 @@
1
+ module Tabulo
2
+
3
+ class Table
4
+ include Enumerable
5
+
6
+ attr_reader :columns
7
+
8
+ def initialize(sources, options = { })
9
+ opts = {
10
+ header_frequency: :start,
11
+
12
+ # nil to wrap to no max, 1 to wrap to 1 row then truncate, etc..
13
+ wrap_header_cells_to: nil,
14
+ wrap_cells_to: nil
15
+
16
+ }.merge(options)
17
+
18
+ @header_frequency = opts[:header_frequency]
19
+ @wrap_header_cells_to = opts[:wrap_header_cells_to]
20
+ @wrap_cells_to = opts[:wrap_cells_to]
21
+ @sources = sources
22
+ @joiner = "|"
23
+ @corner_character = "+"
24
+ @horizontal_rule_character = "-"
25
+ @truncation_indicator = "~"
26
+ @padding_character = " "
27
+ @columns = []
28
+ @default_column_width = 8
29
+ yield self if block_given?
30
+ end
31
+
32
+ def add_column(label, options = {}, &extractor)
33
+ @columns << Column.new({
34
+ label: label.to_sym,
35
+ header: label.to_s,
36
+ truncate: true,
37
+ align_header: :center,
38
+ align_body: nil,
39
+ horizontal_rule_character: @horizontal_rule_character,
40
+ width: @default_column_width,
41
+ extractor: extractor || (label.respond_to?(:to_proc) ? label.to_proc : proc { nil }),
42
+ formatter: :to_s.to_proc
43
+
44
+ }.merge(options))
45
+ end
46
+
47
+ def to_s
48
+ join_lines(map(&:to_s))
49
+ end
50
+
51
+ def each
52
+ @sources.each_with_index do |source, index|
53
+ include_header =
54
+ case @header_frequency
55
+ when :start
56
+ index == 0
57
+ when Fixnum
58
+ index % @header_frequency == 0
59
+ else
60
+ @header_frequency
61
+ end
62
+ yield body_row(source, with_header: include_header)
63
+ end
64
+ end
65
+
66
+ def header_row
67
+ format_row(true, &:header_cell)
68
+ end
69
+
70
+ def horizontal_rule
71
+ format_row(false, @horizontal_rule_character, @corner_character, &:horizontal_rule)
72
+ end
73
+
74
+ def formatted_body_row(source, options = { with_header: false })
75
+ inner = format_row { |column| column.body_cell(source) }
76
+ if options[:with_header]
77
+ join_lines([horizontal_rule, header_row, horizontal_rule, inner])
78
+ else
79
+ inner
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def body_row(source, options = { with_header: false })
86
+ Row.new(self, source, options)
87
+ end
88
+
89
+ def format_row(header = false, padder = @padding_character, joiner = @joiner)
90
+ cell_stacks = @columns.map do |column|
91
+ raw = yield column
92
+ wrap = (header ? @wrap_header_cells_to : @wrap_cells_to)
93
+ truncate = (column.truncate && wrap)
94
+ column_width = column.width
95
+ cell_body_length = (truncate ? column_width * wrap : raw.length)
96
+ truncated = (cell_body_length < raw.length)
97
+ cell_body = raw[0...cell_body_length]
98
+ num_subcells = (cell_body_length.to_f / column_width).ceil
99
+ (0...num_subcells).map do |i|
100
+ s = cell_body.slice(i * column_width, column_width)
101
+ right_padder = ((truncated && i == num_subcells - 1) ? @truncation_indicator : padder)
102
+ "#{padder}#{s}#{padder * (column_width - s.length)}#{right_padder}"
103
+ end
104
+ end
105
+
106
+ subrows = (0...cell_stacks.map(&:size).max).map do |subrow_index|
107
+ cell_stacks.map.with_index do |cell_stack, column_index|
108
+ if subrow_index < cell_stack.size
109
+ cell_stack[subrow_index]
110
+ else
111
+ "#{padder}#{' ' * @columns[column_index].width}#{padder}"
112
+ end
113
+ end
114
+ end
115
+
116
+ join_lines(subrows.map { |subrow| "#{joiner}#{subrow.join(joiner)}#{joiner}" })
117
+ end
118
+
119
+ def join_lines(lines)
120
+ lines.join($/) # join strings with cross-platform newline
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,3 @@
1
+ module Tabulo
2
+ VERSION = "0.1.0"
3
+ end
data/tabulo.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tabulo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tabulo"
8
+ spec.version = Tabulo::VERSION
9
+ spec.authors = ["Matthew Harvey"]
10
+ spec.email = ["software@matthewharvey.net"]
11
+
12
+ spec.summary = "Enumerable ASCII table"
13
+ spec.description = "Enumerable ASCII table"
14
+ spec.homepage = "https://github.com/matt-harvey/tabulo"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabulo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Harvey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Enumerable ASCII table
56
+ email:
57
+ - software@matthewharvey.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/tabulo.rb
72
+ - lib/tabulo/column.rb
73
+ - lib/tabulo/row.rb
74
+ - lib/tabulo/table.rb
75
+ - lib/tabulo/version.rb
76
+ - tabulo.gemspec
77
+ homepage: https://github.com/matt-harvey/tabulo
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Enumerable ASCII table
101
+ test_files: []