hirb 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ == 0.2.3
2
+ * Added UTF-8 support for Ruby 1.8.x
3
+ * Added :all_fields option to Table helper.
4
+
1
5
  == 0.2.2
2
6
  * Added a friendlier default (a vertical table) to incorrectly configured tables.
3
7
  * Added vertical table helper thanks to chrononaut.
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 2
4
+ :patch: 3
@@ -1,6 +1,11 @@
1
1
  current_dir = File.dirname(__FILE__)
2
2
  $:.unshift(current_dir) unless $:.include?(current_dir) || $:.include?(File.expand_path(current_dir))
3
+
4
+ # Needed by Hirb::String to handle multibyte characters
5
+ $KCODE = 'u' if RUBY_VERSION < '1.9'
6
+
3
7
  require 'hirb/util'
8
+ require 'hirb/string'
4
9
  require 'hirb/hash_struct'
5
10
  require 'hirb/helpers'
6
11
  require 'hirb/view'
@@ -24,7 +24,8 @@
24
24
  # By default, the fields/columns are the keys of the first hash.
25
25
  #--
26
26
  # derived from http://gist.github.com/72234
27
- class Hirb::Helpers::Table
27
+ module Hirb
28
+ class Helpers::Table
28
29
  BORDER_LENGTH = 3 # " | " and "-+-" are the borders
29
30
  class TooManyFieldsForWidthError < StandardError; end
30
31
 
@@ -44,6 +45,7 @@ class Hirb::Helpers::Table
44
45
  # [:filters] A hash of fields and the filters that each row in the field must run through. The filter converts the cell's value by applying
45
46
  # a given proc or an array containing a method and optional arguments to it.
46
47
  # [:vertical] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false.
48
+ # [:all_fields] When set to true, renders fields in all rows. Valid only in rows that are hashes. Default is false.
47
49
  # Examples:
48
50
  # Hirb::Helpers::Table.render [[1,2], [2,3]]
49
51
  # Hirb::Helpers::Table.render [[1,2], [2,3]], :field_lengths=>{0=>10}
@@ -51,11 +53,11 @@ class Hirb::Helpers::Table
51
53
  # Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"}
52
54
  # Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}
53
55
  def render(rows, options={})
54
- options.delete(:vertical) ? Hirb::Helpers::VerticalTable.render(rows, options) : new(rows, options).render
56
+ options.delete(:vertical) ? Helpers::VerticalTable.render(rows, options) : new(rows, options).render
55
57
  rescue TooManyFieldsForWidthError
56
58
  $stderr.puts "", "** Error: Too many fields for the current width. Configure your width " +
57
59
  "and/or fields to avoid this error. Defaulting to a vertical table. **"
58
- Hirb::Helpers::VerticalTable.render(rows, options)
60
+ Helpers::VerticalTable.render(rows, options)
59
61
  end
60
62
  end
61
63
 
@@ -63,8 +65,7 @@ class Hirb::Helpers::Table
63
65
  def initialize(rows, options={})
64
66
  @options = options
65
67
  @options[:filters] ||= {}
66
- @fields = @options[:fields] ? @options[:fields].dup : ((rows[0].is_a?(Hash)) ? rows[0].keys.sort {|a,b| a.to_s <=> b.to_s} :
67
- rows[0].is_a?(Array) ? (0..rows[0].length - 1).to_a : [])
68
+ @fields = set_fields(rows)
68
69
  @rows = setup_rows(rows)
69
70
  @headers = @fields.inject({}) {|h,e| h[e] = e.to_s; h}
70
71
  if @options.has_key?(:headers)
@@ -76,7 +77,20 @@ class Hirb::Helpers::Table
76
77
  @fields.unshift :hirb_number
77
78
  end
78
79
  end
79
-
80
+
81
+ def set_fields(rows)
82
+ if @options[:fields]
83
+ @options[:fields].dup
84
+ else
85
+ if rows[0].is_a?(Hash)
86
+ keys = @options[:all_fields] ? rows.map {|e| e.keys}.flatten.uniq : rows[0].keys
87
+ keys.sort {|a,b| a.to_s <=> b.to_s}
88
+ else
89
+ rows[0].is_a?(Array) ? (0..rows[0].length - 1).to_a : []
90
+ end
91
+ end
92
+ end
93
+
80
94
  def setup_rows(rows)
81
95
  rows ||= []
82
96
  rows = [rows] unless rows.is_a?(Array)
@@ -123,13 +137,13 @@ class Hirb::Helpers::Table
123
137
  end
124
138
 
125
139
  def format_cell(value, cell_width)
126
- text = value.length > cell_width ?
140
+ text = String.size(value) > cell_width ?
127
141
  (
128
- (cell_width < 5) ? value.slice(0,cell_width) : value.slice(0, cell_width - 3) + '...'
142
+ (cell_width < 5) ? String.slice(value, 0, cell_width) : String.slice(value, 0, cell_width - 3) + '...'
129
143
  ) : value
130
- sprintf("%-#{cell_width}s", text)
144
+ String.ljust(text, cell_width)
131
145
  end
132
-
146
+
133
147
  def render_rows
134
148
  @rows.map do |row|
135
149
  row = '| ' + @fields.map {|f|
@@ -148,7 +162,7 @@ class Hirb::Helpers::Table
148
162
  if @options[:field_lengths]
149
163
  @field_lengths.merge!(@options[:field_lengths])
150
164
  else
151
- table_max_width = @options.has_key?(:max_width) ? @options[:max_width] : Hirb::View.width
165
+ table_max_width = @options.has_key?(:max_width) ? @options[:max_width] : View.width
152
166
  restrict_field_lengths(@field_lengths, table_max_width) if table_max_width
153
167
  end
154
168
  end
@@ -200,10 +214,10 @@ class Hirb::Helpers::Table
200
214
 
201
215
  # find max length for each field; start with the headers
202
216
  def default_field_lengths
203
- field_lengths = @headers ? @headers.inject({}) {|h,(k,v)| h[k] = v.length; h} : {}
217
+ field_lengths = @headers ? @headers.inject({}) {|h,(k,v)| h[k] = String.size(v); h} : {}
204
218
  @rows.each do |row|
205
219
  @fields.each do |field|
206
- len = row[field].length
220
+ len = String.size(row[field])
207
221
  field_lengths[field] = len if len > field_lengths[field].to_i
208
222
  end
209
223
  end
@@ -239,3 +253,4 @@ class Hirb::Helpers::Table
239
253
  end
240
254
  #:startdoc:
241
255
  end
256
+ end
@@ -16,12 +16,12 @@ class Hirb::Helpers::VerticalTable < Hirb::Helpers::Table
16
16
 
17
17
  def render_rows
18
18
  i = 0
19
- longest_header = @headers.values.sort_by {|e| e.length}.last.length
19
+ longest_header = Hirb::String.size @headers.values.sort_by {|e| Hirb::String.size(e) }.last
20
20
  stars = "*" * [(longest_header + (longest_header / 2)), 3].max
21
21
  @rows.map do |row|
22
22
  row = "#{stars} #{i+1}. row #{stars}\n" +
23
23
  @fields.map {|f|
24
- "#{@headers[f].rjust(longest_header)}: #{row[f]}"
24
+ "#{Hirb::String.rjust(@headers[f], longest_header)}: #{row[f]}"
25
25
  }.join("\n")
26
26
  i+= 1
27
27
  row
@@ -71,8 +71,8 @@ module Hirb
71
71
  def slice!(output, inspect_mode=false) #:nodoc:
72
72
  effective_height = @height - 2 # takes into account pager prompt
73
73
  if inspect_mode
74
- sliced_output = output.slice(0, @width * effective_height)
75
- output.replace output.slice(@width * effective_height..-1)
74
+ sliced_output = String.slice(output, 0, @width * effective_height)
75
+ output.replace String.slice(output, @width * effective_height, String.size(output))
76
76
  sliced_output
77
77
  else
78
78
  # could use output.scan(/[^\n]*\n?/) instead of split
@@ -84,11 +84,11 @@ module Hirb
84
84
 
85
85
  # Determines if string should be paged based on configured width and height.
86
86
  def activated_by?(string_to_page, inspect_mode=false)
87
- inspect_mode ? (string_to_page.size > @height * @width) : (string_to_page.count("\n") > @height)
87
+ inspect_mode ? (String.size(string_to_page) > @height * @width) : (string_to_page.count("\n") > @height)
88
88
  end
89
89
 
90
90
  def resize(width, height) #:nodoc:
91
- @width, @height = Hirb::View.determine_terminal_size(width, height)
91
+ @width, @height = View.determine_terminal_size(width, height)
92
92
  end
93
93
  end
94
94
  end
@@ -0,0 +1,43 @@
1
+ module Hirb
2
+ # Provides string helpers to deal with UTF-8 and ruby 1.8.x
3
+ module String
4
+ extend self
5
+ # :stopdoc:
6
+ if RUBY_VERSION < '1.9'
7
+ def size(string)
8
+ string.scan(/./).length
9
+ end
10
+
11
+ def ljust(string, desired_length)
12
+ leftover = desired_length - size(string)
13
+ leftover > 0 ? string + " " * leftover : string
14
+ end
15
+
16
+ def rjust(string, desired_length)
17
+ leftover = desired_length - size(string)
18
+ leftover > 0 ? " " * leftover + string : string
19
+ end
20
+
21
+ def slice(string, start, finish)
22
+ string.scan(/./).slice(start, finish).join('')
23
+ end
24
+ else
25
+ def size(string)
26
+ string.length
27
+ end
28
+
29
+ def ljust(string, desired_length)
30
+ string.ljust(desired_length)
31
+ end
32
+
33
+ def rjust(string, desired_length)
34
+ string.rjust(desired_length)
35
+ end
36
+
37
+ def slice(*args)
38
+ string.slice(*args)
39
+ end
40
+ end
41
+ #:startdoc:
42
+ end
43
+ end
@@ -12,17 +12,17 @@ class FormatterTest < Test::Unit::TestCase
12
12
  test "klass_config merges ancestor options" do
13
13
  set_formatter "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output, :ancestor=>true}, "Kernel"=>{:method=>:default_output}
14
14
  expected_result = {:method=>:object_output, :args=>[1, 2], :ancestor=>true}
15
- @formatter.klass_config(String).should == expected_result
15
+ @formatter.klass_config(::String).should == expected_result
16
16
  end
17
17
 
18
18
  test "klass_config doesn't merge ancestor options" do
19
19
  set_formatter "String"=>{:args=>[1,2]}, "Object"=>{:method=>:object_output}, "Kernel"=>{:method=>:default_output}
20
20
  expected_result = {:args=>[1, 2]}
21
- @formatter.klass_config(String).should == expected_result
21
+ @formatter.klass_config(::String).should == expected_result
22
22
  end
23
23
 
24
24
  test "klass_config returns hash when nothing found" do
25
- set_formatter.klass_config(String).should == {}
25
+ set_formatter.klass_config(::String).should == {}
26
26
  end
27
27
 
28
28
  test "reload detects new Hirb::Views" do
@@ -65,6 +65,21 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
65
65
  test "with no rows renders" do
66
66
  table([]).should == "0 rows in set"
67
67
  end
68
+
69
+ test "renders utf8" do
70
+ expected_table = <<-TABLE.unindent
71
+ +--------------------+
72
+ | name |
73
+ +--------------------+
74
+ | アイウエオカキ |
75
+ | クケコサシスセソタチツテ |
76
+ | Tata l'asticote |
77
+ | toto létoile PAOLI |
78
+ +--------------------+
79
+ 4 rows in set
80
+ TABLE
81
+ table([{:name=>"アイウエオカキ"}, {:name=>"クケコサシスセソタチツテ"}, {:name=>"Tata l'asticote"}, {:name=>"toto létoile PAOLI"}]).should == expected_table
82
+ end
68
83
  end
69
84
 
70
85
  context "table with" do
@@ -263,6 +278,19 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
263
278
  TABLE
264
279
  table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :vertical=>true).should == expected_table
265
280
  end
281
+
282
+ test "all_fields option renders all fields" do
283
+ expected_table = <<-TABLE.unindent
284
+ +---+---+---+
285
+ | a | b | c |
286
+ +---+---+---+
287
+ | 1 | 2 | |
288
+ | 3 | | 4 |
289
+ +---+---+---+
290
+ 2 rows in set
291
+ TABLE
292
+ table([{:a=>1, :b=>2}, {:a=>3, :c=>4}], :all_fields=>true).should == expected_table
293
+ end
266
294
  end
267
295
 
268
296
  test "restrict_field_lengths ensures columns total doesn't exceed max width" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hirb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Horner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-30 00:00:00 -04:00
12
+ date: 2009-07-08 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -43,6 +43,7 @@ files:
43
43
  - lib/hirb/import_object.rb
44
44
  - lib/hirb/menu.rb
45
45
  - lib/hirb/pager.rb
46
+ - lib/hirb/string.rb
46
47
  - lib/hirb/util.rb
47
48
  - lib/hirb/view.rb
48
49
  - lib/hirb/views/activerecord_base.rb