formatador 0.0.10 → 0.0.11

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.
data/Rakefile CHANGED
@@ -10,10 +10,9 @@ begin
10
10
  gem.email = "wbeary@engineyard.com"
11
11
  gem.homepage = "http://github.com/geemus/formatador"
12
12
  gem.authors = ["Wesley Beary"]
13
- gem.add_development_dependency "shindo", ">= 0"
13
+ # gem.add_development_dependency "shindo", ">= 0"
14
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
15
  end
16
- Jeweler::GemcutterTasks.new
17
16
  rescue LoadError
18
17
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
18
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
data/formatador.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{formatador}
8
- s.version = "0.0.10"
8
+ s.version = "0.0.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Wesley Beary"]
12
- s.date = %q{2010-03-04}
12
+ s.date = %q{2010-04-03}
13
13
  s.description = %q{STDOUT text formatting}
14
14
  s.email = %q{wbeary@engineyard.com}
15
15
  s.extra_rdoc_files = [
@@ -17,14 +17,16 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.files = [
19
19
  ".document",
20
- ".gitignore",
21
- "README.rdoc",
22
- "Rakefile",
23
- "VERSION",
24
- "formatador.gemspec",
25
- "lib/formatador.rb",
26
- "tests/formatador_tests.rb",
27
- "tests/tests_helper.rb"
20
+ ".gitignore",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "formatador.gemspec",
25
+ "lib/formatador.rb",
26
+ "lib/formatador/progressbar.rb",
27
+ "lib/formatador/table.rb",
28
+ "tests/formatador_tests.rb",
29
+ "tests/tests_helper.rb"
28
30
  ]
29
31
  s.homepage = %q{http://github.com/geemus/formatador}
30
32
  s.rdoc_options = ["--charset=UTF-8"]
@@ -37,12 +39,9 @@ Gem::Specification.new do |s|
37
39
  s.specification_version = 3
38
40
 
39
41
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
- s.add_development_dependency(%q<shindo>, [">= 0"])
41
42
  else
42
- s.add_dependency(%q<shindo>, [">= 0"])
43
43
  end
44
44
  else
45
- s.add_dependency(%q<shindo>, [">= 0"])
46
45
  end
47
46
  end
48
47
 
data/lib/formatador.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require File.join(File.dirname(__FILE__), 'formatador', 'table')
2
+ require File.join(File.dirname(__FILE__), 'formatador', 'progressbar')
3
+
1
4
  class Formatador
2
5
 
3
6
  STYLES = {
@@ -67,54 +70,6 @@ class Formatador
67
70
  nil
68
71
  end
69
72
 
70
- def display_table(hashes, keys = nil, &block)
71
- headers = keys || []
72
- widths = {}
73
- for hash in hashes
74
- for key in hash.keys
75
- unless keys
76
- headers << key
77
- end
78
- widths[key] = [key.to_s.length, widths[key] || 0, hash[key] && hash[key].to_s.length || 0].max
79
- end
80
- headers = headers.uniq
81
- end
82
-
83
- unless block_given?
84
- headers = headers.sort {|x,y| x.to_s <=> y.to_s}
85
- else
86
- headers = headers.sort(&block)
87
- end
88
-
89
- split = "+"
90
- if headers.empty?
91
- split << '--+'
92
- else
93
- for header in headers
94
- split << ('-' * (widths[header] + 2)) << '+'
95
- end
96
- end
97
-
98
- display_line(split)
99
- columns = []
100
- for header in headers
101
- columns << "#{header}#{' ' * (widths[header] - header.to_s.length)}"
102
- end
103
- display_line("| #{columns.join(' | ')} |")
104
- display_line(split)
105
-
106
- for hash in hashes
107
- columns = []
108
- for header in headers
109
- datum = hash[header] || ''
110
- columns << "#{datum}#{' ' * (widths[header] - datum.to_s.length)}"
111
- end
112
- display_line("| #{columns.join(' | ')} |")
113
- display_line(split)
114
- end
115
- nil
116
- end
117
-
118
73
  def format(string)
119
74
  if STDOUT.tty?
120
75
  string.gsub(FORMAT_REGEX) { "\e[#{STYLES[$1.to_sym]}m" }.gsub(INDENT_REGEX) { indentation }
@@ -139,7 +94,7 @@ class Formatador
139
94
  nil
140
95
  end
141
96
 
142
- %w{display display_line display_table format redisplay}.each do |method|
97
+ %w{display display_line display_table format redisplay redisplay_progressbar}.each do |method|
143
98
  eval <<-DEF
144
99
  def self.#{method}(*args, &block)
145
100
  new.#{method}(*args, &block)
@@ -0,0 +1,40 @@
1
+ class Formatador
2
+
3
+ def redisplay_progressbar(current, total, options = {})
4
+ options = { :color => 'white', :width => 50 }.merge!(options)
5
+ data = progressbar(current, total, options)
6
+ if current < total
7
+ redisplay(data)
8
+ else
9
+ redisplay("#{data}\n")
10
+ @progressbar_started_at = nil
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def progressbar(current, total, options)
17
+ color = options[:color]
18
+ started_at = options[:started_at]
19
+ width = options[:width]
20
+
21
+ output = []
22
+
23
+ padding = ' ' * (total.to_s.size - current.to_s.size)
24
+ output << "[#{color}]#{padding}#{current}/#{total}[/]"
25
+
26
+ percent = current.to_f / total.to_f
27
+ output << "[_white_]|[/][#{color}][_#{color}_]#{'*' * (percent * width).ceil}[/]#{' ' * (width - (percent * width).ceil)}[_white_]|[/]"
28
+
29
+ if started_at
30
+ elapsed = Time.now - started_at
31
+ minutes = (elapsed / 60).round.to_s
32
+ seconds = (elapsed % 60).round.to_s
33
+ output << "#{minutes}:#{'0' if seconds.size < 2}#{seconds}"
34
+ end
35
+
36
+ output << ''
37
+ output.join(' ')
38
+ end
39
+
40
+ end
@@ -0,0 +1,51 @@
1
+ class Formatador
2
+
3
+ def display_table(hashes, keys = nil, &block)
4
+ headers = keys || []
5
+ widths = {}
6
+ for hash in hashes
7
+ for key in hash.keys
8
+ unless keys
9
+ headers << key
10
+ end
11
+ widths[key] = [key.to_s.length, widths[key] || 0, hash[key] && hash[key].to_s.length || 0].max
12
+ end
13
+ headers = headers.uniq
14
+ end
15
+
16
+ if block_given?
17
+ headers = headers.sort(&block)
18
+ elsif !keys
19
+ headers = headers.sort {|x,y| x.to_s <=> y.to_s}
20
+ end
21
+
22
+ split = "+"
23
+ if headers.empty?
24
+ split << '--+'
25
+ else
26
+ for header in headers
27
+ split << ('-' * (widths[header] + 2)) << '+'
28
+ end
29
+ end
30
+
31
+ display_line(split)
32
+ columns = []
33
+ for header in headers
34
+ columns << "#{header}#{' ' * (widths[header] - header.to_s.length)}"
35
+ end
36
+ display_line("| #{columns.join(' | ')} |")
37
+ display_line(split)
38
+
39
+ for hash in hashes
40
+ columns = []
41
+ for header in headers
42
+ datum = hash[header] || ''
43
+ columns << "#{datum}#{' ' * (widths[header] - datum.to_s.length)}"
44
+ end
45
+ display_line("| #{columns.join(' | ')} |")
46
+ display_line(split)
47
+ end
48
+ nil
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 10
9
- version: 0.0.10
8
+ - 11
9
+ version: 0.0.11
10
10
  platform: ruby
11
11
  authors:
12
12
  - Wesley Beary
@@ -14,21 +14,10 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-04 00:00:00 -08:00
17
+ date: 2010-04-03 00:00:00 -07:00
18
18
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: shindo
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
30
- type: :development
31
- version_requirements: *id001
19
+ dependencies: []
20
+
32
21
  description: STDOUT text formatting
33
22
  email: wbeary@engineyard.com
34
23
  executables: []
@@ -45,6 +34,8 @@ files:
45
34
  - VERSION
46
35
  - formatador.gemspec
47
36
  - lib/formatador.rb
37
+ - lib/formatador/progressbar.rb
38
+ - lib/formatador/table.rb
48
39
  - tests/formatador_tests.rb
49
40
  - tests/tests_helper.rb
50
41
  has_rdoc: true