cldwalker-hirb 0.1.0 → 0.1.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.
- data/CHANGELOG.rdoc +5 -0
- data/README.rdoc +20 -9
- data/Rakefile +1 -1
- data/VERSION.yml +1 -1
- data/lib/hirb/helpers/active_record_table.rb +1 -1
- data/lib/hirb/helpers/table.rb +14 -8
- data/lib/hirb/view.rb +2 -1
- data/test/table_test.rb +33 -12
- metadata +7 -6
data/CHANGELOG.rdoc
ADDED
data/README.rdoc
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
== Description
|
2
2
|
|
3
3
|
Hirb currently provides a mini view framework for console applications, designed with irb in mind.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
Given the output of a console application, it renders a view if there is one configured, based on
|
5
|
+
the output's class. The framework encourages reusing views by letting you package them in classes
|
6
|
+
and associate them with any number of output classes. Hirb comes with table views (see
|
7
|
+
Hirb::Helpers::Table) which work out of the box with any output class, especially Rails' model
|
8
|
+
classes.
|
9
9
|
|
10
10
|
== Install
|
11
11
|
|
@@ -157,7 +157,7 @@ it will be automatically loaded with no configuration. Something to think about
|
|
157
157
|
sharing views with others.
|
158
158
|
|
159
159
|
# Create yaml view class
|
160
|
-
irb>> class Hirb::Views::Hash; def self.render(output); output.to_yaml; end ;end
|
160
|
+
irb>> class Hirb::Views::Hash; def self.render(output, options={}); output.to_yaml; end ;end
|
161
161
|
=>nil
|
162
162
|
# Just reload since no configuration is necessary
|
163
163
|
irb>>Hirb::View.reload_config
|
@@ -169,7 +169,7 @@ often want view classes that can be reused with multiple outputs. For this case,
|
|
169
169
|
use the Hirb::Helpers namespace.
|
170
170
|
|
171
171
|
# Create yaml view class
|
172
|
-
irb>> class Hirb::Helpers::Yaml; def self.render(output); output.to_yaml; end ;end
|
172
|
+
irb>> class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
|
173
173
|
=>nil
|
174
174
|
|
175
175
|
# Configure view and reload it
|
@@ -185,7 +185,7 @@ them at startup by passing Hirb::View.enable a block:
|
|
185
185
|
# In .irbrc
|
186
186
|
require 'hirb'
|
187
187
|
# View class needs to come before enable()
|
188
|
-
class Hirb::Helpers::Yaml; def self.render(output); output.to_yaml; end ;end
|
188
|
+
class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
|
189
189
|
Hirb::View.enable {|conf| conf.output = {"Hash"=>{:class=>"Hirb::Helpers::Yaml"}} }
|
190
190
|
|
191
191
|
Or by creating a config file at config/hirb.yml or ~/.hirb.yml:
|
@@ -199,15 +199,26 @@ Or by creating a config file at config/hirb.yml or ~/.hirb.yml:
|
|
199
199
|
# In .irbrc
|
200
200
|
require 'hirb'
|
201
201
|
# View class needs to come before enable()
|
202
|
-
class Hirb::Helpers::Yaml; def self.render(output); output.to_yaml; end ;end
|
202
|
+
class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
|
203
203
|
Hirb::View.enable
|
204
204
|
|
205
|
+
== Contributing Views
|
206
|
+
If you have views of your own you'd like to share, fork Hirb and put your views under
|
207
|
+
the Hirb::Helpers namespace and the view files under lib/hirb/helpers/.
|
208
|
+
|
205
209
|
== Limitations
|
206
210
|
Although Hirb preserves Wirble colorizing irb's default echo mode, it doesn't colorize its own views.
|
207
211
|
This is mainly because colorizing caused table classes to render incorrectly. If you can get tables
|
208
212
|
and colors to work nicely, please fork. To colorize your Hirb output:
|
209
213
|
Hirb::View.render_method = lambda {|output| puts Wirble::Colorize.colorize(output) }
|
210
214
|
|
215
|
+
== Motivation
|
216
|
+
Table code from http://gist.github.com/72234 and {my console
|
217
|
+
app}[http://github.com/cldwalker/tag-tree].
|
218
|
+
|
219
|
+
== Links
|
220
|
+
* http://tagaholic.me/2009/03/13/hirb-irb-on-the-good-stuff.html
|
221
|
+
|
211
222
|
== Todo
|
212
223
|
* Create tree views.
|
213
224
|
* Possibly add non-view irb goodies ie command manager.
|
data/Rakefile
CHANGED
@@ -25,7 +25,7 @@ begin
|
|
25
25
|
s.authors = ["Gabriel Horner"]
|
26
26
|
s.has_rdoc = true
|
27
27
|
s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
|
28
|
-
s.files = FileList["
|
28
|
+
s.files = FileList["[A-Z]*", "{bin,lib,test}/**/*"]
|
29
29
|
end
|
30
30
|
|
31
31
|
rescue LoadError
|
data/VERSION.yml
CHANGED
data/lib/hirb/helpers/table.rb
CHANGED
@@ -26,6 +26,7 @@
|
|
26
26
|
# derived from http://gist.github.com/72234
|
27
27
|
class Hirb::Helpers::Table
|
28
28
|
DEFAULT_MAX_WIDTH = 150
|
29
|
+
class TooManyFieldsForWidthError < StandardError; end
|
29
30
|
class << self
|
30
31
|
attr_accessor :max_width
|
31
32
|
|
@@ -38,6 +39,7 @@ class Hirb::Helpers::Table
|
|
38
39
|
# length than it's truncated and has a ... appended to it. Fields that aren't specified here have no maximum allowed
|
39
40
|
# length.
|
40
41
|
# [:max_width] The maximum allowed width of all fields put together. This option is enforced except when the field_lengths option is set.
|
42
|
+
# This doesn't count field borders as part of the total.
|
41
43
|
# Examples:
|
42
44
|
# Hirb::Helpers::Table.render [[1,2], [2,3]]
|
43
45
|
# Hirb::Helpers::Table.render [[1,2], [2,3]], :field_lengths=>{0=>10}
|
@@ -99,7 +101,7 @@ class Hirb::Helpers::Table
|
|
99
101
|
def format_cell(value, cell_width)
|
100
102
|
text = value.length > cell_width ?
|
101
103
|
(
|
102
|
-
(cell_width <
|
104
|
+
(cell_width < 5) ? value.slice(0,cell_width) : value.slice(0, cell_width - 3) + '...'
|
103
105
|
) : value
|
104
106
|
sprintf("%-#{cell_width}s", text)
|
105
107
|
end
|
@@ -122,21 +124,25 @@ class Hirb::Helpers::Table
|
|
122
124
|
if @options[:field_lengths]
|
123
125
|
@field_lengths.merge!(@options[:field_lengths])
|
124
126
|
else
|
125
|
-
|
126
|
-
|
127
|
+
table_max_width = Hirb::Helpers::Table.max_width || DEFAULT_MAX_WIDTH
|
128
|
+
table_max_width = @options[:max_width] if @options.has_key?(:max_width)
|
129
|
+
restrict_field_lengths(@field_lengths, table_max_width)
|
127
130
|
end
|
128
131
|
end
|
129
132
|
|
130
133
|
# Simple algorithm which given a max width, allows smaller fields to be displayed while
|
131
|
-
# restricting longer fields at
|
134
|
+
# restricting longer fields at an average_long_field_length.
|
132
135
|
def restrict_field_lengths(field_lengths, max_width)
|
133
136
|
total_length = field_lengths.values.inject {|t,n| t += n}
|
134
|
-
if total_length > max_width
|
137
|
+
if max_width && total_length > max_width
|
138
|
+
min_field_length = 3
|
139
|
+
raise TooManyFieldsForWidthError if @fields.size > max_width.to_f / min_field_length
|
135
140
|
average_field_length = total_length / @fields.size.to_f
|
136
|
-
long_lengths
|
137
|
-
|
141
|
+
long_lengths = field_lengths.values.select {|e| e > average_field_length}
|
142
|
+
total_long_field_length = (long_lengths.inject {|t,n| t += n}) * max_width/total_length
|
143
|
+
average_long_field_length = total_long_field_length / long_lengths.size
|
138
144
|
field_lengths.each {|f,length|
|
139
|
-
field_lengths[f] =
|
145
|
+
field_lengths[f] = average_long_field_length if length > average_long_field_length
|
140
146
|
}
|
141
147
|
end
|
142
148
|
end
|
data/lib/hirb/view.rb
CHANGED
@@ -41,7 +41,8 @@ module Hirb
|
|
41
41
|
# and returns true. Returns false if no formatter found.
|
42
42
|
# ==== Options:
|
43
43
|
# [:method] Specifies a global (Kernel) method to do the formatting.
|
44
|
-
# [:class] Specifies a class to do the formatting, using its render() class method.
|
44
|
+
# [:class] Specifies a class to do the formatting, using its render() class method. The render() method's arguments are the output and
|
45
|
+
# an options hash.
|
45
46
|
# [:options] Options to pass the formatter method or class.
|
46
47
|
def render_output(output, options={}, &block)
|
47
48
|
if block && block.arity > 0
|
data/test/table_test.rb
CHANGED
@@ -55,6 +55,10 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
55
55
|
table([[1,2], [3,4]]).should == expected_table
|
56
56
|
end
|
57
57
|
|
58
|
+
test "with too many fields raises error" do
|
59
|
+
assert_raises(Hirb::Helpers::Table::TooManyFieldsForWidthError) { table([Array.new(70, 'AAA')]) }
|
60
|
+
end
|
61
|
+
|
58
62
|
test "with no rows renders" do
|
59
63
|
table([]).should == "0 rows in set"
|
60
64
|
end
|
@@ -137,27 +141,39 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
137
141
|
|
138
142
|
test "max_width option renders" do
|
139
143
|
expected_table = <<-TABLE.unindent
|
140
|
-
|
141
|
-
| a
|
142
|
-
|
143
|
-
|
|
144
|
-
|
144
|
+
+--------------------------+---+------------+
|
145
|
+
| a | b | c |
|
146
|
+
+--------------------------+---+------------+
|
147
|
+
| AAAAAAAAAAAAAAAAAAAAA... | 2 | CCCCCCCCCC |
|
148
|
+
+--------------------------+---+------------+
|
145
149
|
1 row in set
|
146
150
|
TABLE
|
147
151
|
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>30).should == expected_table
|
148
152
|
end
|
149
|
-
|
153
|
+
|
154
|
+
test "max_width option nil renders full table" do
|
155
|
+
expected_table = <<-TABLE.unindent
|
156
|
+
+----------------------------------------------------+---+------------+
|
157
|
+
| a | b | c |
|
158
|
+
+----------------------------------------------------+---+------------+
|
159
|
+
| AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | 2 | CCCCCCCCCC |
|
160
|
+
+----------------------------------------------------+---+------------+
|
161
|
+
1 row in set
|
162
|
+
TABLE
|
163
|
+
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}], :max_width=>nil).should == expected_table
|
164
|
+
end
|
165
|
+
|
150
166
|
test "global max_width renders" do
|
151
167
|
expected_table = <<-TABLE.unindent
|
152
|
-
|
153
|
-
| a
|
154
|
-
|
155
|
-
|
|
156
|
-
|
168
|
+
+--------------------------+---+------------+
|
169
|
+
| a | b | c |
|
170
|
+
+--------------------------+---+------------+
|
171
|
+
| AAAAAAAAAAAAAAAAAAAAA... | 2 | CCCCCCCCCC |
|
172
|
+
+--------------------------+---+------------+
|
157
173
|
1 row in set
|
158
174
|
TABLE
|
159
175
|
Hirb::Helpers::Table.max_width = 30
|
160
|
-
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}]).should == expected_table
|
176
|
+
table([{:a=> "A" * 50, :b=>2, :c=>"C"*10}]).should == expected_table
|
161
177
|
Hirb::Helpers::Table.max_width = Hirb::Helpers::Table::DEFAULT_MAX_WIDTH
|
162
178
|
end
|
163
179
|
|
@@ -239,4 +255,9 @@ class Hirb::Helpers::TableTest < Test::Unit::TestCase
|
|
239
255
|
Hirb::Helpers::ActiveRecordTable.render(@pets).should == expected_table
|
240
256
|
end
|
241
257
|
end
|
258
|
+
|
259
|
+
test "restrict_field_lengths handles many fields" do
|
260
|
+
@table = Hirb::Helpers::Table.new([{:field1=>'f1', :field2=>'f2', :field3=>'f3'}])
|
261
|
+
@table.restrict_field_lengths({:field1=>10, :field2=>15, :field3=>100}, 10)
|
262
|
+
end
|
242
263
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cldwalker-hirb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Horner
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-15 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: A mini view framework for irb that's easy to use, even while under its influence.
|
16
|
+
description: A mini view framework for console/irb that's easy to use, even while under its influence.
|
17
17
|
email: gabriel.horner@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -23,10 +23,11 @@ extra_rdoc_files:
|
|
23
23
|
- README.rdoc
|
24
24
|
- LICENSE.txt
|
25
25
|
files:
|
26
|
+
- CHANGELOG.rdoc
|
27
|
+
- LICENSE.txt
|
26
28
|
- Rakefile
|
27
|
-
- VERSION.yml
|
28
29
|
- README.rdoc
|
29
|
-
-
|
30
|
+
- VERSION.yml
|
30
31
|
- lib/hirb
|
31
32
|
- lib/hirb/console.rb
|
32
33
|
- lib/hirb/hash_struct.rb
|
@@ -74,6 +75,6 @@ rubyforge_project:
|
|
74
75
|
rubygems_version: 1.2.0
|
75
76
|
signing_key:
|
76
77
|
specification_version: 2
|
77
|
-
summary: A mini view framework for irb that's easy to use, even while under its influence.
|
78
|
+
summary: A mini view framework for console/irb that's easy to use, even while under its influence.
|
78
79
|
test_files: []
|
79
80
|
|