hirb 0.3.6 → 0.4.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.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.4.0
2
+ * Add unicode table helper thanks to janlelis
3
+ * Make pager compatible with full width characters
4
+
1
5
  == 0.3.6
2
6
  * Tweak ripl support
3
7
  * Allow override of :hirb_number thanks to asanghi.
data/README.rdoc CHANGED
@@ -16,6 +16,10 @@ Install the gem with:
16
16
 
17
17
  gem install hirb
18
18
 
19
+ For people using full-width unicode characters, install {hirb-unicode}[https://github.com/miaout17/hirb-unicode]:
20
+
21
+ gem install hirb-unicode
22
+
19
23
  == View Tutorials
20
24
 
21
25
  * To create and configure views, see Hirb::View or {here if on the web}[http://tagaholic.me/hirb/doc/classes/Hirb/View.html].
@@ -34,8 +38,8 @@ fields from an array of arrays. See
34
38
  == Rails Example
35
39
 
36
40
  Let's load and enable the view framework:
37
- $ script/console
38
- Loading local environment (Rails 2.3.5)
41
+ $ rails console
42
+ Loading local environment (Rails 3.0.3)
39
43
  >> require 'hirb'
40
44
  => true
41
45
  >> Hirb.enable
@@ -102,6 +106,15 @@ tables/views for any output object:
102
106
  >> extend Hirb::Console
103
107
  => main
104
108
 
109
+ # Create a unicode table
110
+ >> table [[:a, :b, :c]], :unicode => true
111
+ ┌───┬───┬───┐
112
+ │ 0 │ 1 │ 2 │
113
+ ├───┼───┼───┤
114
+ │ a ╎ b ╎ c │
115
+ └───┴───┴───┘
116
+ 1 row in set
117
+
105
118
  # Create a table of Dates comparing them with different formats.
106
119
  >> table [Date.today, Date.today.next_month], :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
107
120
  +------------+--------+-----------+-------+--------------------------+
@@ -120,7 +133,7 @@ If these console methods weren't convenient enough, try:
120
133
 
121
134
  # Imports view() to all objects.
122
135
  >> require 'hirb/import_object'
123
- =>true
136
+ => true
124
137
  # Yields same table as above examples.
125
138
  >> [Date.today, Date.today.next_month].view :class=>:object_table,
126
139
  :fields=>[:to_s, :ld, :ajd, :amjd, :asctime]
@@ -135,7 +148,7 @@ Although views by default are printed to STDOUT, they can be easily modified to
135
148
 
136
149
  # Doesn't write to file because Symbol doesn't have a view and thus defaults to irb's echo mode.
137
150
  >> :blah
138
- =>:blah
151
+ => :blah
139
152
 
140
153
  # Go back to printing Hirb views to STDOUT.
141
154
  >> Hirb::View.reset_render_method
@@ -166,6 +179,7 @@ Table code from http://gist.github.com/72234 and {my console app's needs}[http:/
166
179
 
167
180
  == Credits
168
181
  * Chrononaut for vertical table helper.
182
+ * janlelis for unicode table helper.
169
183
  * crafterm, spastorino, xaviershay, bogdan, asanghi and joshua for patches.
170
184
 
171
185
  == Bugs/Issues
@@ -4,7 +4,7 @@ complete(:methods=>%w{Hirb::View.enable Hirb.enable}) {
4
4
  complete(:methods=>%w{Hirb::Helpers::Table.render table}) {
5
5
  %w{fields headers max_fields max_width resize number change_fields}+
6
6
  %w{filters header_filter filter_any filter_classes vertical all_fields}+
7
- %w{description escape_special_chars table_class hide_empty}
7
+ %w{description escape_special_chars table_class hide_empty unicode}
8
8
  }
9
9
  complete(:method=>"Hirb::Helpers::Tree.render") {
10
10
  %w{type validate indent limit description multi_line_nodes value_method children_method}
data/lib/hirb/helpers.rb CHANGED
@@ -12,6 +12,7 @@ module Hirb
12
12
  end
13
13
  end
14
14
 
15
- %w{table object_table auto_table tree parent_child_tree vertical_table}.each do |e|
15
+ %w{table object_table auto_table tree parent_child_tree vertical_table
16
+ unicode_table}.each do |e|
16
17
  require "hirb/helpers/#{e}"
17
- end
18
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'hirb/helpers/table/filters'
2
3
  require 'hirb/helpers/table/resizer'
3
4
 
@@ -62,6 +63,14 @@ module Hirb
62
63
  MIN_FIELD_LENGTH = 3
63
64
  class TooManyFieldsForWidthError < StandardError; end
64
65
 
66
+ CHARS = {
67
+ :top => {:left => '+', :center => '+', :right => '+', :horizontal => '-',
68
+ :vertical => {:outside => '|', :inside => '|'} },
69
+ :middle => {:left => '+', :center => '+', :right => '+', :horizontal => '-'},
70
+ :bottom => {:left => '+', :center => '+', :right => '+', :horizontal => '-',
71
+ :vertical => {:outside => '|', :inside => '|'} }
72
+ }
73
+
65
74
  class << self
66
75
 
67
76
  # Main method which returns a formatted table.
@@ -86,6 +95,7 @@ module Hirb
86
95
  # Default Hirb::Helpers::Table.filter_any().
87
96
  # [*:filter_classes*] Hash which maps classes to filters. Default is Hirb::Helpers::Table.filter_classes().
88
97
  # [*:vertical*] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false.
98
+ # [*:unicode*] When set to true, renders a unicode table using Hirb::Helpers::UnicodeTable. Default is false.
89
99
  # [*:all_fields*] When set to true, renders fields in all rows. Valid only in rows that are hashes. Default is false.
90
100
  # [*:description*] When set to true, renders row count description at bottom. Default is true.
91
101
  # [*:escape_special_chars*] When set to true, escapes special characters \n,\t,\r so they don't disrupt tables. Default is false for
@@ -99,9 +109,10 @@ module Hirb
99
109
  # Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}
100
110
  def render(rows, options={})
101
111
  options[:vertical] ? Helpers::VerticalTable.render(rows, options) :
112
+ options[:unicode] ? Helpers::UnicodeTable.render(rows, options) :
102
113
  new(rows, options).render
103
114
  rescue TooManyFieldsForWidthError
104
- $stderr.puts "", "** Error: Too many fields for the current width. Configure your width " +
115
+ $stderr.puts "", "** Hirb Warning: Too many fields for the current width. Configure your width " +
105
116
  "and/or fields to avoid this error. Defaulting to a vertical table. **"
106
117
  Helpers::VerticalTable.render(rows, options)
107
118
  end
@@ -117,6 +128,10 @@ module Hirb
117
128
  end
118
129
  self.filter_classes = { Array=>:comma_join, Hash=>:inspect }
119
130
 
131
+ def chars
132
+ self.class.const_get(:CHARS)
133
+ end
134
+
120
135
  #:stopdoc:
121
136
  attr_accessor :width, :max_fields, :field_lengths, :fields
122
137
  def initialize(rows, options={})
@@ -198,22 +213,26 @@ module Hirb
198
213
  end
199
214
 
200
215
  def render_header
201
- @headers ? render_table_header : [render_border]
216
+ @headers ? render_table_header : [render_border(:top)]
202
217
  end
203
218
 
204
219
  def render_footer
205
- [render_border]
220
+ [render_border(:bottom)]
206
221
  end
207
222
 
208
223
  def render_table_header
209
- title_row = '| ' + @fields.map {|f|
210
- format_cell(@headers[f], @field_lengths[f])
211
- }.join(' | ') + ' |'
212
- [render_border, title_row, render_border]
224
+ title_row = chars[:top][:vertical][:outside] + ' ' +
225
+ @fields.map {|f| format_cell(@headers[f], @field_lengths[f]) }.
226
+ join(' ' + chars[:top][:vertical][:inside] +' ') +
227
+ ' ' + chars[:top][:vertical][:outside]
228
+ [render_border(:top), title_row, render_border(:middle)]
213
229
  end
214
230
 
215
- def render_border
216
- '+-' + @fields.map {|f| '-' * @field_lengths[f] }.join('-+-') + '-+'
231
+ def render_border(which)
232
+ chars[which][:left] + chars[which][:horizontal] +
233
+ @fields.map {|f| chars[which][:horizontal] * @field_lengths[f] }.
234
+ join(chars[which][:horizontal] + chars[which][:center] + chars[which][:horizontal]) +
235
+ chars[which][:horizontal] + chars[which][:right]
217
236
  end
218
237
 
219
238
  def format_cell(value, cell_width)
@@ -226,9 +245,9 @@ module Hirb
226
245
 
227
246
  def render_rows
228
247
  @rows.map do |row|
229
- row = '| ' + @fields.map {|f|
248
+ row = chars[:bottom][:vertical][:outside] + ' ' + @fields.map {|f|
230
249
  format_cell(row[f], @field_lengths[f])
231
- }.join(' | ') + ' |'
250
+ }.join(' ' +chars[:bottom][:vertical][:inside] + ' ') + ' ' + chars[:bottom][:vertical][:outside]
232
251
  end
233
252
  end
234
253
 
@@ -0,0 +1,15 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Hirb::Helpers::UnicodeTable < Hirb::Helpers::Table
3
+ CHARS = {
4
+ :top => {:left => '┌', :center => '┬', :right => '┐', :horizontal => '─',
5
+ :vertical => {:outside => '│', :inside => '│'} },
6
+ :middle => {:left => '├', :center => '┼', :right => '┤', :horizontal => '─'},
7
+ :bottom => {:left => '└', :center => '┴', :right => '┘', :horizontal => '─',
8
+ :vertical => {:outside => '│', :inside => '╎'} }
9
+ }
10
+
11
+ # Renders a unicode table
12
+ def self.render(rows, options={})
13
+ new(rows, options).render
14
+ end
15
+ end
data/lib/hirb/pager.rb CHANGED
@@ -73,7 +73,7 @@ module Hirb
73
73
  effective_height = @height - 2 # takes into account pager prompt
74
74
  if inspect_mode
75
75
  sliced_output = String.slice(output, 0, @width * effective_height)
76
- output.replace String.slice(output, @width * effective_height, String.size(output))
76
+ output.replace String.slice(output, char_count(sliced_output), String.size(output))
77
77
  sliced_output
78
78
  else
79
79
  # could use output.scan(/[^\n]*\n?/) instead of split
@@ -88,6 +88,16 @@ module Hirb
88
88
  inspect_mode ? (String.size(string_to_page) > @height * @width) : (string_to_page.count("\n") > @height)
89
89
  end
90
90
 
91
+ if String.method_defined? :chars
92
+ def char_count(string) #:nodoc:
93
+ string.chars.count
94
+ end
95
+ else
96
+ def char_count(string) #:nodoc:
97
+ String.size(string)
98
+ end
99
+ end
100
+
91
101
  def resize(width, height) #:nodoc:
92
102
  @width, @height = View.determine_terminal_size(width, height)
93
103
  end
data/lib/hirb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hirb
2
- VERSION = '0.3.6'
2
+ VERSION = '0.4.0'
3
3
  end
data/test/table_test.rb CHANGED
@@ -70,7 +70,7 @@ describe "Table" do
70
70
  it "with too many fields defaults to vertical table" do
71
71
  rows = [Array.new(25, "A"* 10)]
72
72
  Helpers::VerticalTable.expects(:render).with(rows, anything)
73
- capture_stderr { table(rows)}.should =~ /Error/
73
+ capture_stderr { table(rows)}.should =~ /Warning:/
74
74
  end
75
75
 
76
76
  it "with no rows renders" do
@@ -439,6 +439,19 @@ describe "Table" do
439
439
  table([{:a=>'', :b=>2}, {:a=>3, :b=>nil}], :hide_empty=>true, :vertical=>true).should == expected_table
440
440
  end
441
441
 
442
+ it "unicode option renders" do
443
+ expected_table = <<-TABLE.unindent
444
+ ┌───┬───┐
445
+ │ a │ b │
446
+ ├───┼───┤
447
+ │ 1 ╎ 2 │
448
+ │ 3 ╎ 4 │
449
+ └───┴───┘
450
+ 2 rows in set
451
+ TABLE
452
+ table([{:a=>1, :b=>2}, {:a=>3, :b=>4}], :unicode => true).should == expected_table
453
+ end
454
+
442
455
  it "all_fields option renders all fields" do
443
456
  expected_table = <<-TABLE.unindent
444
457
  +---+---+---+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hirb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 6
10
- version: 0.3.6
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gabriel Horner
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-08 00:00:00 -05:00
18
+ date: 2011-02-22 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -97,6 +97,7 @@ files:
97
97
  - lib/hirb/helpers/table/resizer.rb
98
98
  - lib/hirb/helpers/table.rb
99
99
  - lib/hirb/helpers/tree.rb
100
+ - lib/hirb/helpers/unicode_table.rb
100
101
  - lib/hirb/helpers/vertical_table.rb
101
102
  - lib/hirb/helpers.rb
102
103
  - lib/hirb/import_object.rb