console-text-formatter 0.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,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .project
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in console-text-formatter.gemspec
4
+ gemspec
@@ -0,0 +1,67 @@
1
+ # ConsoleTextFormatter
2
+
3
+ Simple console text formatter for ruby, with ability to print data hash to console
4
+
5
+ ## Why?
6
+
7
+ I didn't find other alternatives for handy print out data to console in table format.
8
+
9
+ So, now it's pretty easy :)
10
+
11
+ ## Install:
12
+
13
+ ``` sh
14
+ $ gem install console-text-formatter
15
+ ```
16
+
17
+ ## Usage Example:
18
+
19
+ ``` rb
20
+ require 'console-text-formatter'
21
+
22
+ # example of data
23
+ data = [
24
+ {
25
+ 'id' => 1,
26
+ 'name' => 'First record'
27
+ },
28
+ {
29
+ 'id' => 2,
30
+ 'name' => 'Seeeeeeeeeecond record'
31
+ }
32
+ ]
33
+
34
+ # format and print data
35
+ ConsoleTextFormatter.format(data, %w(id name))
36
+ ```
37
+
38
+ It will prints:
39
+
40
+ ``` sh
41
+ =============================
42
+ id | name
43
+ -----------------------------
44
+ 1 | First record
45
+ 2 | Seeeeeeeeeecond record
46
+ =============================
47
+ ```
48
+
49
+ ## Author
50
+
51
+ cjfinc, you can follow me on twitter [@cjf_inc](http://twitter.com/cjf_inc) or take a look at github page [cjfinc@github](https://github.com/cjfinc)
52
+
53
+ ## Copyright
54
+
55
+ Copyright (C) 2011 Evgenij Novosilskij - [@cjf_inc](http://twitter.com/cjf_inc)
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
58
+ associated documentation files (the “Software”), to deal in the Software without restriction, including without
59
+ limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
60
+ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
65
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM,
66
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
67
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'console-text-formatter/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "console-text-formatter"
7
+ s.version = ConsoleTextFormatter::VERSION
8
+ s.authors = ["cjfinc"]
9
+ s.email = ["cjf.inc@gmail.com"]
10
+ s.homepage = "https://github.com/cjfinc/console-text-formatter"
11
+ s.summary = %q{Simple console text formatter for ruby}
12
+ s.description = %q{Simple console text formatter for ruby, with ability to print data hash to console}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = %w(lib)
16
+ end
@@ -0,0 +1,27 @@
1
+ require 'console-text-formatter'
2
+
3
+ # example of data
4
+ data = [
5
+ {
6
+ 'id' => 1,
7
+ 'name' => 'First record'
8
+ },
9
+ {
10
+ 'id' => 2,
11
+ 'name' => 'Seeeeeeeeeecond record'
12
+ }
13
+ ]
14
+
15
+ # format and print data
16
+ ConsoleTextFormatter.format(data, %w(id name))
17
+
18
+ =begin
19
+
20
+ =============================
21
+ id | name
22
+ -----------------------------
23
+ 1 | First record
24
+ 2 | Seeeeeeeeeecond record
25
+ =============================
26
+
27
+ =end
@@ -0,0 +1,57 @@
1
+ require 'console-text-formatter/version'
2
+
3
+ module ConsoleTextFormatter
4
+ extend self
5
+
6
+ def self.format(recs, fields)
7
+ fields_width = Array.new(fields.length)
8
+
9
+ fields.each_index{|i| fields_width[i] = fields[i].to_s.length }
10
+
11
+ recs.each do |rec|
12
+ fields.each_index do |i|
13
+ if (fields_width[i] < rec[fields[i]].to_s.length)
14
+ fields_width[i] = rec[fields[i]].to_s.length
15
+ end
16
+ end
17
+ end
18
+ fields_width.each_index{|i| fields_width[i] += 2 }
19
+
20
+ rec_width = fields_width.length - 1
21
+ fields_width.each{|x| rec_width+=x }
22
+
23
+ print_data(recs, rec_width, fields, fields_width)
24
+ end
25
+
26
+ # print formatter
27
+ def self.pad(str, len)
28
+ return "".ljust(len) if str.nil?
29
+ #str = str.slice(0, len) # truncate long strings
30
+ " "+str.ljust(len-1) # pad with whitespace
31
+ end
32
+
33
+ def self.build_row(items, pads)
34
+ output = ''
35
+ items.each_with_index do |item, i|
36
+ output << pad(item, pads[i]) << "|"
37
+ end
38
+ output.chop!
39
+ output << "\n"
40
+ end
41
+
42
+ def self.print_data (recs, width, fields, field_width)
43
+ output = ''
44
+ output = "".ljust(width,"=") << "\n"
45
+ output << build_row(fields, field_width)
46
+ output << "".ljust(width,"-") << "\n"
47
+
48
+ # table data
49
+ recs.each do |rec|
50
+ output << build_row(fields.map { |r| rec[r].to_s }, field_width)
51
+ end
52
+
53
+ output << "".ljust(width,"=") << "\n"
54
+
55
+ print output
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module ConsoleTextFormatter
2
+ VERSION = "0.0.1" unless defined?(ConsoleTextFormatter::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: console-text-formatter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - cjfinc
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-09-17 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Simple console text formatter for ruby, with ability to print data hash to console
22
+ email:
23
+ - cjf.inc@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.md
34
+ - console-text-formatter.gemspec
35
+ - examples/simple.rb
36
+ - lib/console-text-formatter.rb
37
+ - lib/console-text-formatter/version.rb
38
+ homepage: https://github.com/cjfinc/console-text-formatter
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.10
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Simple console text formatter for ruby
71
+ test_files: []
72
+