formatador 0.2.1 → 0.2.2

This diff has not been reviewed by any users.
Sign up to get free protection for your applications and to get access to all the features.
@@ -27,6 +27,33 @@ You use tags, similar to html, to set formatting options:
27
27
  * display_compact_table: Same as display_table, execpt that split lines are not drawn by default in the body of the table. If you need a split line, put a :split constant in the body array.
28
28
  * redisplay_progressbar: takes the current and total values as its first two arguments and redisplays a progressbar (until current = total and then it display_lines). An optional third argument represents the start time and will add an elapsed time counter.
29
29
 
30
+ === Table examples
31
+
32
+ table_data = [{:name => "Joe", :food => "Burger"}, {:name => "Bill", :food => "French fries"}]
33
+ Formatador.display_table(table_data)
34
+
35
+ +------+--------------+
36
+ | name | food |
37
+ +------+--------------+
38
+ | Joe | Burger |
39
+ +------+--------------+
40
+ | Bill | French fries |
41
+ +------+--------------+
42
+
43
+ table_data = [
44
+ {:name => "Joe", :meal => {:main_dish => "Burger", :drink => "water"},
45
+ {:name => "Bill", :meal => {:main_dish => "Chicken", drink => "soda"}
46
+ ]
47
+ Formatador.display_table(table_data, [:name, :"meal.drink"])
48
+
49
+ +------+------------+
50
+ | name | meal.drink |
51
+ +------+------------+
52
+ | Joe | water |
53
+ +------+------------+
54
+ | Bill | soda |
55
+ +------+------------+
56
+
30
57
  == Indentation
31
58
 
32
59
  By initializing a formatador object you can keep track of indentation:
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'formatador'
16
- s.version = '0.2.1'
17
- s.date = '2011-09-01'
16
+ s.version = '0.2.2'
17
+ s.date = '2012-05-16'
18
18
  s.rubyforge_project = 'formatador'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'formatador', 'progressbar')
3
3
 
4
4
  class Formatador
5
5
 
6
- VERSION = '0.2.1'
6
+ VERSION = '0.2.2'
7
7
 
8
8
  STYLES = {
9
9
  :"\/" => "0",
@@ -4,9 +4,9 @@ class Formatador
4
4
  options = { :color => 'white', :width => 50, :new_line => true }.merge!(options)
5
5
  data = progressbar(current, total, options)
6
6
  if current < total
7
- redisplay(data)
7
+ redisplay(data, options[:width])
8
8
  else
9
- redisplay("#{data}")
9
+ redisplay("#{data}", options[:width])
10
10
  if options[:new_line]
11
11
  new_line
12
12
  end
@@ -10,54 +10,61 @@ class Formatador
10
10
  def display_compact_table(hashes, keys = nil, &block)
11
11
  headers = keys || []
12
12
  widths = {}
13
+
14
+ # Calculate Widths
13
15
  if hashes.empty? && keys
14
- for key in keys
16
+ keys.each do |key|
15
17
  widths[key] = key.to_s.length
16
18
  end
17
19
  else
18
- for hash in hashes
20
+ hashes.each do |hash|
19
21
  next unless hash.respond_to?(:keys)
20
22
 
21
- for key in hash.keys
22
- unless keys
23
+ (headers + hash.keys).each do |key|
24
+ if !keys
23
25
  headers << key
24
26
  end
25
- widths[key] = [ length(key), widths[key] || 0, hash[key] && length(hash[key]) || 0].max
27
+ widths[key] = [ length(key), widths[key] || 0, length(calculate_datum(key, hash)) || 0].max
26
28
  end
27
29
  headers = headers.uniq
28
30
  end
29
31
  end
30
32
 
33
+ # Determine order of headers
31
34
  if block_given?
32
35
  headers = headers.sort(&block)
33
36
  elsif !keys
34
37
  headers = headers.sort {|x,y| x.to_s <=> y.to_s}
35
38
  end
36
39
 
40
+ # Display separator row
37
41
  split = "+"
38
42
  if headers.empty?
39
43
  split << '--+'
40
44
  else
41
- for header in headers
45
+ headers.each do |header|
42
46
  widths[header] ||= length(header)
43
47
  split << ('-' * (widths[header] + 2)) << '+'
44
48
  end
45
49
  end
46
-
47
50
  display_line(split)
51
+
52
+ # Display data row
48
53
  columns = []
49
- for header in headers
54
+ headers.each do |header|
50
55
  columns << "[bold]#{header}[/]#{' ' * (widths[header] - header.to_s.length)}"
51
56
  end
52
57
  display_line("| #{columns.join(' | ')} |")
53
58
  display_line(split)
54
59
 
55
- for hash in hashes
60
+ hashes.each do |hash|
56
61
  if hash.respond_to? :keys
57
62
  columns = []
58
- for header in headers
59
- datum = hash[header] || ''
60
- columns << "#{datum}#{' ' * (widths[header] - length(datum))}"
63
+ headers.each do |header|
64
+ datum = calculate_datum(header, hash)
65
+ width = widths[header] - length(datum)
66
+ width = width < 0 ? 0 : width
67
+ columns << "#{datum}#{' ' * width}"
61
68
  end
62
69
  display_line("| #{columns.join(' | ')} |")
63
70
  else
@@ -75,4 +82,17 @@ class Formatador
75
82
  def length(value)
76
83
  value.to_s.gsub(PARSE_REGEX, '').length
77
84
  end
85
+
86
+ def calculate_datum(header, hash)
87
+ if (splits = header.to_s.split('.')).length > 1
88
+ datum = nil
89
+ splits.each do |split|
90
+ d = (datum||hash)
91
+ datum = d[split] || d[split.to_sym] || ''
92
+ end
93
+ else
94
+ datum = hash[header] || ''
95
+ end
96
+ datum
97
+ end
78
98
  end
@@ -1,4 +1,4 @@
1
- Shindo.tests("Formatador") do
1
+ Shindo.tests("Formatador: basics") do
2
2
 
3
3
  tests("#display_line(Formatador)").returns(" Formatador\n") do
4
4
  capture_stdout do
@@ -10,6 +10,7 @@ output = <<-OUTPUT
10
10
  one
11
11
  two
12
12
  OUTPUT
13
+ output = Formatador.parse(output)
13
14
 
14
15
  tests("#display_lines(['one', 'two']").returns(output) do
15
16
  capture_stdout do
@@ -1,14 +1,15 @@
1
- Shindo.tests("Formatador") do
1
+ Shindo.tests("Formatador: tables") do
2
2
 
3
3
  output = <<-OUTPUT
4
4
  +---+
5
- | \e[1ma\e[0m |
5
+ | [bold]a[/] |
6
6
  +---+
7
7
  | 1 |
8
8
  +---+
9
9
  | 2 |
10
10
  +---+
11
11
  OUTPUT
12
+ output = Formatador.parse(output)
12
13
 
13
14
  tests("#display_table([{:a => 1}, {:a => 2}])").returns(output) do
14
15
  capture_stdout do
@@ -18,10 +19,11 @@ OUTPUT
18
19
 
19
20
  output = <<-OUTPUT
20
21
  +--------+
21
- | \e[1mheader\e[0m |
22
+ | [bold]header[/] |
22
23
  +--------+
23
24
  +--------+
24
25
  OUTPUT
26
+ output = Formatador.parse(output)
25
27
 
26
28
  tests("#display_table([], [:header])").returns(output) do
27
29
  capture_stdout do
@@ -31,11 +33,12 @@ OUTPUT
31
33
 
32
34
  output = <<-OUTPUT
33
35
  +--------+
34
- | \e[1mheader\e[0m |
36
+ | [bold]header[/] |
35
37
  +--------+
36
38
  | |
37
39
  +--------+
38
40
  OUTPUT
41
+ output = Formatador.parse(output)
39
42
 
40
43
  tests("#display_table([{:a => 1}], [:header])").returns(output) do
41
44
  capture_stdout do
@@ -43,4 +46,36 @@ OUTPUT
43
46
  end
44
47
  end
45
48
 
46
- end
49
+
50
+
51
+ output = <<-OUTPUT
52
+ +---+------------+
53
+ | [bold]a[/] | [bold]nested.key[/] |
54
+ +---+------------+
55
+ | 1 | value |
56
+ +---+------------+
57
+ OUTPUT
58
+ output = Formatador.parse(output)
59
+
60
+ tests("#display_table([{:a => 1, :nested => {:key => 'value'}}], [:header, :'nested.key'])").returns(output) do
61
+ capture_stdout do
62
+ Formatador.display_table([{:a => 1, :nested => {:key => 'value'}}], [:a, :'nested.key'])
63
+ end
64
+ end
65
+
66
+ output = <<-OUTPUT
67
+ +---+-----------------+
68
+ | [bold]a[/] | [bold]nested[/] |
69
+ +---+-----------------+
70
+ | 1 | {:key=>"value"} |
71
+ +---+-----------------+
72
+ OUTPUT
73
+ output = Formatador.parse(output)
74
+
75
+ tests("#display_table([{:a => 1, :nested => {:key => 'value'}}])").returns(output) do
76
+ capture_stdout do
77
+ Formatador.display_table([{:a => 1, :nested => {:key => 'value'}}])
78
+ end
79
+ end
80
+
81
+ end
@@ -3,6 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'formatador'
4
4
  require 'rubygems'
5
5
  require 'shindo'
6
+ require 'stringio'
6
7
 
7
8
  def capture_stdout
8
9
  old_stdout = $stdout
metadata CHANGED
@@ -1,59 +1,45 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: formatador
3
- version: !ruby/object:Gem::Version
4
- hash: 21
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - geemus (Wesley Beary)
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-09-01 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rake
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70155363939860 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: shindo
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70155363939860
25
+ - !ruby/object:Gem::Dependency
26
+ name: shindo
27
+ requirement: &70155363961280 !ruby/object:Gem::Requirement
38
28
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
46
33
  type: :development
47
- version_requirements: *id002
34
+ prerelease: false
35
+ version_requirements: *70155363961280
48
36
  description: STDOUT text formatting
49
37
  email: geemus@gmail.com
50
38
  executables: []
51
-
52
39
  extensions: []
53
-
54
- extra_rdoc_files:
40
+ extra_rdoc_files:
55
41
  - README.rdoc
56
- files:
42
+ files:
57
43
  - Gemfile
58
44
  - README.rdoc
59
45
  - Rakefile
@@ -66,36 +52,30 @@ files:
66
52
  - tests/tests_helper.rb
67
53
  homepage: http://github.com/geemus/NAME
68
54
  licenses: []
69
-
70
55
  post_install_message:
71
- rdoc_options:
56
+ rdoc_options:
72
57
  - --charset=UTF-8
73
- require_paths:
58
+ require_paths:
74
59
  - lib
75
- required_ruby_version: !ruby/object:Gem::Requirement
60
+ required_ruby_version: !ruby/object:Gem::Requirement
76
61
  none: false
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- hash: 3
81
- segments:
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
82
67
  - 0
83
- version: "0"
84
- required_rubygems_version: !ruby/object:Gem::Requirement
68
+ hash: -794223940542750498
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
70
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
- version: "0"
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
93
75
  requirements: []
94
-
95
76
  rubyforge_project: formatador
96
- rubygems_version: 1.8.5
77
+ rubygems_version: 1.8.15
97
78
  signing_key:
98
79
  specification_version: 2
99
80
  summary: Ruby STDOUT text formatting
100
81
  test_files: []
101
-