hsume2-hirb 0.6.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemspec +21 -0
- data/CHANGELOG.rdoc +144 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +194 -0
- data/Rakefile +35 -0
- data/lib/bond/completions/hirb.rb +15 -0
- data/lib/hirb/console.rb +43 -0
- data/lib/hirb/dynamic_view.rb +113 -0
- data/lib/hirb/formatter.rb +126 -0
- data/lib/hirb/helpers/auto_table.rb +24 -0
- data/lib/hirb/helpers/object_table.rb +14 -0
- data/lib/hirb/helpers/parent_child_tree.rb +24 -0
- data/lib/hirb/helpers/tab_table.rb +24 -0
- data/lib/hirb/helpers/table/filters.rb +10 -0
- data/lib/hirb/helpers/table/resizer.rb +82 -0
- data/lib/hirb/helpers/table.rb +349 -0
- data/lib/hirb/helpers/tree.rb +181 -0
- data/lib/hirb/helpers/unicode_table.rb +15 -0
- data/lib/hirb/helpers/vertical_table.rb +37 -0
- data/lib/hirb/helpers.rb +18 -0
- data/lib/hirb/import_object.rb +10 -0
- data/lib/hirb/menu.rb +238 -0
- data/lib/hirb/pager.rb +105 -0
- data/lib/hirb/string.rb +44 -0
- data/lib/hirb/util.rb +96 -0
- data/lib/hirb/version.rb +3 -0
- data/lib/hirb/view.rb +270 -0
- data/lib/hirb/views/couch_db.rb +11 -0
- data/lib/hirb/views/misc_db.rb +15 -0
- data/lib/hirb/views/mongo_db.rb +14 -0
- data/lib/hirb/views/orm.rb +11 -0
- data/lib/hirb/views/rails.rb +19 -0
- data/lib/hirb/views.rb +8 -0
- data/lib/hirb.rb +82 -0
- data/lib/ripl/hirb.rb +15 -0
- data/test/auto_table_test.rb +30 -0
- data/test/console_test.rb +27 -0
- data/test/deps.rip +4 -0
- data/test/dynamic_view_test.rb +94 -0
- data/test/formatter_test.rb +176 -0
- data/test/hirb_test.rb +39 -0
- data/test/import_test.rb +9 -0
- data/test/menu_test.rb +255 -0
- data/test/object_table_test.rb +79 -0
- data/test/pager_test.rb +162 -0
- data/test/resizer_test.rb +62 -0
- data/test/table_test.rb +630 -0
- data/test/test_helper.rb +61 -0
- data/test/tree_test.rb +184 -0
- data/test/util_test.rb +59 -0
- data/test/view_test.rb +165 -0
- data/test/views_test.rb +13 -0
- metadata +184 -0
data/lib/hirb/util.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module Hirb
|
2
|
+
# Group of handy utility functions used throughout Hirb.
|
3
|
+
module Util
|
4
|
+
extend self
|
5
|
+
# Returns a constant like Module#const_get no matter what namespace it's nested in.
|
6
|
+
# Returns nil if the constant is not found.
|
7
|
+
def any_const_get(name)
|
8
|
+
return name if name.is_a?(Module)
|
9
|
+
begin
|
10
|
+
klass = Object
|
11
|
+
name.split('::').each {|e|
|
12
|
+
klass = klass.const_get(e)
|
13
|
+
}
|
14
|
+
klass
|
15
|
+
rescue
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Recursively merge hash1 with hash2.
|
21
|
+
def recursive_hash_merge(hash1, hash2)
|
22
|
+
hash1.merge(hash2) {|k,o,n| (o.is_a?(Hash)) ? recursive_hash_merge(o,n) : n}
|
23
|
+
end
|
24
|
+
|
25
|
+
# From Rails ActiveSupport, converting undescored lowercase to camel uppercase.
|
26
|
+
def camelize(string)
|
27
|
+
string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
28
|
+
end
|
29
|
+
|
30
|
+
# Used by Hirb::Menu to select items from an array. Array counting starts at 1. Ranges of numbers are specified with a '-' or '..'.
|
31
|
+
# Multiple ranges can be comma delimited. Anything that isn't a valid number is ignored. All elements can be returned with a '*'.
|
32
|
+
# Examples:
|
33
|
+
# 1-3,5-6 -> [1,2,3,5,6]
|
34
|
+
# * -> all elements in array
|
35
|
+
# '' -> []
|
36
|
+
def choose_from_array(array, input, options={})
|
37
|
+
options = {:splitter=>","}.merge(options)
|
38
|
+
return array if input.strip == '*'
|
39
|
+
result = []
|
40
|
+
input.split(options[:splitter]).each do |e|
|
41
|
+
if e =~ /-|\.\./
|
42
|
+
min,max = e.split(/-|\.\./)
|
43
|
+
slice_min = min.to_i - 1
|
44
|
+
result.push(*array.slice(slice_min, max.to_i - min.to_i + 1))
|
45
|
+
elsif e =~ /\s*(\d+)\s*/
|
46
|
+
index = $1.to_i - 1
|
47
|
+
next if index < 0
|
48
|
+
result.push(array[index]) if array[index]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
result
|
52
|
+
end
|
53
|
+
|
54
|
+
# Determines if a shell command exists by searching for it in ENV['PATH'].
|
55
|
+
def command_exists?(command)
|
56
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns [width, height] of terminal when detected, nil if not detected.
|
60
|
+
# Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()
|
61
|
+
def detect_terminal_size
|
62
|
+
if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
|
63
|
+
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
|
64
|
+
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
|
65
|
+
[`tput cols`.to_i, `tput lines`.to_i]
|
66
|
+
elsif STDIN.tty? && command_exists?('stty')
|
67
|
+
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
|
68
|
+
else
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
rescue
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
|
75
|
+
# Captures STDOUT of anything run in its block and returns it as string.
|
76
|
+
def capture_stdout(&block)
|
77
|
+
original_stdout = $stdout
|
78
|
+
$stdout = fake = StringIO.new
|
79
|
+
begin
|
80
|
+
yield
|
81
|
+
ensure
|
82
|
+
$stdout = original_stdout
|
83
|
+
end
|
84
|
+
fake.string
|
85
|
+
end
|
86
|
+
|
87
|
+
# From Rubygems, determine a user's home.
|
88
|
+
def find_home
|
89
|
+
['HOME', 'USERPROFILE'].each {|e| return ENV[e] if ENV[e] }
|
90
|
+
return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
|
91
|
+
File.expand_path("~")
|
92
|
+
rescue
|
93
|
+
File::ALT_SEPARATOR ? "C:/" : "/"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/hirb/version.rb
ADDED
data/lib/hirb/view.rb
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
module Hirb
|
2
|
+
# This class is responsible for managing all view-related functionality.
|
3
|
+
#
|
4
|
+
# == Create a View
|
5
|
+
# Let's create a simple view for Hash objects:
|
6
|
+
# $ irb -rubygems
|
7
|
+
# >> require 'hirb'
|
8
|
+
# =>true
|
9
|
+
# >> Hirb.enable
|
10
|
+
# =>nil
|
11
|
+
# >> require 'yaml'
|
12
|
+
# =>true
|
13
|
+
#
|
14
|
+
# # A view method is the smallest view
|
15
|
+
# >> def yaml(output); output.to_yaml; end
|
16
|
+
# => nil
|
17
|
+
# # Add the view
|
18
|
+
# >> Hirb.add_view Hash, :method=>:yaml
|
19
|
+
# => true
|
20
|
+
#
|
21
|
+
# # Hashes now appear as yaml
|
22
|
+
# >> {:a=>1, :b=>{:c=>3}}
|
23
|
+
# ---
|
24
|
+
# :a : 1
|
25
|
+
# :b :
|
26
|
+
# :c : 3
|
27
|
+
# => true
|
28
|
+
#
|
29
|
+
# Another way of creating a view is a Helper class:
|
30
|
+
#
|
31
|
+
# # Create yaml view class
|
32
|
+
# >> class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
|
33
|
+
# =>nil
|
34
|
+
# # Add the view
|
35
|
+
# >> Hirb.add_view Hash, :class=>Hirb::Helpers::Yaml
|
36
|
+
# =>true
|
37
|
+
#
|
38
|
+
# # Hashes appear as yaml like above ...
|
39
|
+
#
|
40
|
+
# == Configure a View
|
41
|
+
# To configure the above Helper class as a view, either pass Hirb.enable a hash:
|
42
|
+
# # In .irbrc
|
43
|
+
# require 'hirb'
|
44
|
+
# # View class needs to come before enable()
|
45
|
+
# class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
|
46
|
+
# Hirb.enable :output=>{"Hash"=>{:class=>"Hirb::Helpers::Yaml"}}
|
47
|
+
#
|
48
|
+
# Or create a config file at config/hirb.yml or ~/.hirb.yml:
|
49
|
+
# # The config file for the yaml example would look like:
|
50
|
+
# # ---
|
51
|
+
# # :output :
|
52
|
+
# # Hash :
|
53
|
+
# # :class : Hirb::Helpers::Yaml
|
54
|
+
#
|
55
|
+
# # In .irbrc
|
56
|
+
# require 'hirb'
|
57
|
+
# # View class needs to come before enable()
|
58
|
+
# class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
|
59
|
+
# Hirb.enable
|
60
|
+
#
|
61
|
+
# For more about configuring Hirb, see the Config Files section in Hirb.
|
62
|
+
module View
|
63
|
+
DEFAULT_WIDTH = 120
|
64
|
+
DEFAULT_HEIGHT = 40
|
65
|
+
class<<self
|
66
|
+
attr_accessor :render_method
|
67
|
+
attr_reader :config
|
68
|
+
|
69
|
+
# This activates view functionality i.e. the formatter, pager and size detection. If irb exists, it overrides irb's output
|
70
|
+
# method with Hirb::View.view_output. When called multiple times, new configs are merged into the existing config.
|
71
|
+
# If using Wirble, you should call this after it. The view configuration can be specified in a hash via a config file,
|
72
|
+
# or as options to this method. In addition to the config keys mentioned in Hirb, options also take the following keys:
|
73
|
+
# ==== Options:
|
74
|
+
# * config_file: Name of config file(s) that are merged into existing config
|
75
|
+
# Examples:
|
76
|
+
# Hirb.enable
|
77
|
+
# Hirb.enable :formatter=>false
|
78
|
+
def enable(options={}, &block)
|
79
|
+
Array(options.delete(:config_file)).each {|e|
|
80
|
+
@new_config_file = true
|
81
|
+
Hirb.config_files << e
|
82
|
+
}
|
83
|
+
enable_output_method unless @output_method
|
84
|
+
merge_or_load_config options
|
85
|
+
resize(config[:width], config[:height])
|
86
|
+
@enabled = true
|
87
|
+
end
|
88
|
+
|
89
|
+
# Indicates if Hirb::View is enabled.
|
90
|
+
def enabled?
|
91
|
+
@enabled || false
|
92
|
+
end
|
93
|
+
|
94
|
+
# Disable's Hirb's output and revert's irb's output method if irb exists.
|
95
|
+
def disable
|
96
|
+
@enabled = false
|
97
|
+
disable_output_method if @output_method
|
98
|
+
false
|
99
|
+
end
|
100
|
+
|
101
|
+
# Toggles pager on or off. The pager only works while Hirb::View is enabled.
|
102
|
+
def toggle_pager
|
103
|
+
config[:pager] = !config[:pager]
|
104
|
+
end
|
105
|
+
|
106
|
+
# Toggles formatter on or off.
|
107
|
+
def toggle_formatter
|
108
|
+
config[:formatter] = !config[:formatter]
|
109
|
+
end
|
110
|
+
|
111
|
+
# Resizes the console width and height for use with the table and pager i.e. after having resized the console window. *nix users
|
112
|
+
# should only have to call this method. Non-*nix users should call this method with explicit width and height. If you don't know
|
113
|
+
# your width and height, in irb play with "a"* width to find width and puts "a\n" * height to find height.
|
114
|
+
def resize(width=nil, height=nil)
|
115
|
+
config[:width], config[:height] = determine_terminal_size(width, height)
|
116
|
+
pager.resize(config[:width], config[:height])
|
117
|
+
end
|
118
|
+
|
119
|
+
# This is the main method of this class. When view is enabled, this method searches for a formatter it can use for the output and if
|
120
|
+
# successful renders it using render_method(). The options this method takes are helper config hashes as described in
|
121
|
+
# Hirb::Formatter.format_output(). Returns true if successful and false if no formatting is done or if not enabled.
|
122
|
+
def view_output(output, options={})
|
123
|
+
enabled? && config[:formatter] && render_output(output, options)
|
124
|
+
rescue Exception=>e
|
125
|
+
if config[:ignore_errors]
|
126
|
+
$stderr.puts "Hirb Error: #{e.message}"
|
127
|
+
false
|
128
|
+
else
|
129
|
+
index = (obj = e.backtrace.find {|f| f =~ /^\(eval\)/}) ? e.backtrace.index(obj) : e.backtrace.length
|
130
|
+
$stderr.puts "Hirb Error: #{e.message}", e.backtrace.slice(0,index).map {|e| " " + e }
|
131
|
+
true
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Captures STDOUT and renders it using render_method(). The main use case is to conditionally page captured stdout.
|
136
|
+
def capture_and_render(&block)
|
137
|
+
render_method.call Util.capture_stdout(&block)
|
138
|
+
end
|
139
|
+
|
140
|
+
# A lambda or proc which handles the final formatted object.
|
141
|
+
# Although this pages/puts the object by default, it could be set to do other things
|
142
|
+
# i.e. write the formatted object to a file.
|
143
|
+
def render_method
|
144
|
+
@render_method ||= default_render_method
|
145
|
+
end
|
146
|
+
|
147
|
+
# Resets render_method back to its default.
|
148
|
+
def reset_render_method
|
149
|
+
@render_method = default_render_method
|
150
|
+
end
|
151
|
+
|
152
|
+
# Current console width
|
153
|
+
def width
|
154
|
+
config && config[:width] ? config[:width] : DEFAULT_WIDTH
|
155
|
+
end
|
156
|
+
|
157
|
+
# Current console height
|
158
|
+
def height
|
159
|
+
config && config[:height] ? config[:height] : DEFAULT_HEIGHT
|
160
|
+
end
|
161
|
+
|
162
|
+
# Current formatter config, storing a hash of all static views
|
163
|
+
def formatter_config
|
164
|
+
formatter.config
|
165
|
+
end
|
166
|
+
|
167
|
+
# Adds a view when View is enabled. See Formatter.add_view for more details.
|
168
|
+
def add(klass, view_config)
|
169
|
+
if enabled?
|
170
|
+
formatter.add_view(klass, view_config)
|
171
|
+
else
|
172
|
+
puts "View must be enabled to add a view"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
#:stopdoc:
|
177
|
+
def enable_output_method
|
178
|
+
if defined?(Ripl) && Ripl.respond_to?(:started?) && Ripl.started?
|
179
|
+
@output_method = true
|
180
|
+
require 'ripl/hirb' unless defined? Ripl::Hirb
|
181
|
+
elsif defined? IRB::Irb
|
182
|
+
@output_method = true
|
183
|
+
::IRB::Irb.class_eval do
|
184
|
+
alias_method :non_hirb_view_output, :output_value
|
185
|
+
def output_value #:nodoc:
|
186
|
+
Hirb::View.view_or_page_output(@context.last_value) || non_hirb_view_output
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def disable_output_method
|
193
|
+
if defined?(IRB::Irb) && !defined? Ripl
|
194
|
+
::IRB::Irb.send :alias_method, :output_value, :non_hirb_view_output
|
195
|
+
end
|
196
|
+
@output_method = nil
|
197
|
+
end
|
198
|
+
|
199
|
+
def view_or_page_output(str)
|
200
|
+
view_output(str) || page_output(str.inspect, true)
|
201
|
+
end
|
202
|
+
|
203
|
+
def render_output(output, options={})
|
204
|
+
if (formatted_output = formatter.format_output(output, options))
|
205
|
+
render_method.call(formatted_output)
|
206
|
+
true
|
207
|
+
else
|
208
|
+
false
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def determine_terminal_size(width, height)
|
213
|
+
detected = (width.nil? || height.nil?) ? Util.detect_terminal_size || [] : []
|
214
|
+
[width || detected[0] || DEFAULT_WIDTH , height || detected[1] || DEFAULT_HEIGHT]
|
215
|
+
end
|
216
|
+
|
217
|
+
def page_output(output, inspect_mode=false)
|
218
|
+
if enabled? && config[:pager] && pager.activated_by?(output, inspect_mode)
|
219
|
+
pager.page(output, inspect_mode)
|
220
|
+
true
|
221
|
+
else
|
222
|
+
false
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def pager
|
227
|
+
@pager ||= Pager.new(config[:width], config[:height], :pager_command=>config[:pager_command])
|
228
|
+
end
|
229
|
+
|
230
|
+
def pager=(value); @pager = value; end
|
231
|
+
|
232
|
+
def formatter(reload=false)
|
233
|
+
@formatter = reload || @formatter.nil? ? Formatter.new(config[:output]) : @formatter
|
234
|
+
end
|
235
|
+
|
236
|
+
def formatter=(value); @formatter = value; end
|
237
|
+
|
238
|
+
def merge_or_load_config(additional_config={})
|
239
|
+
if @config && (@new_config_file || !additional_config.empty?)
|
240
|
+
Hirb.config = nil
|
241
|
+
load_config Util.recursive_hash_merge(@config, additional_config)
|
242
|
+
@new_config_file = false
|
243
|
+
elsif !@enabled
|
244
|
+
load_config(additional_config)
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def load_config(additional_config={})
|
249
|
+
@config = Util.recursive_hash_merge default_config, additional_config
|
250
|
+
formatter(true)
|
251
|
+
true
|
252
|
+
end
|
253
|
+
|
254
|
+
def config_loaded?; !!@config; end
|
255
|
+
|
256
|
+
def config
|
257
|
+
@config
|
258
|
+
end
|
259
|
+
|
260
|
+
def default_render_method
|
261
|
+
lambda {|output| page_output(output) || puts(output) }
|
262
|
+
end
|
263
|
+
|
264
|
+
def default_config
|
265
|
+
Util.recursive_hash_merge({:pager=>true, :formatter=>true}, Hirb.config || {})
|
266
|
+
end
|
267
|
+
#:startdoc:
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Hirb::Views::CouchDb #:nodoc:
|
2
|
+
def default_couch(obj)
|
3
|
+
{:fields=>([:_id] + obj.class.properties.map {|e| e.name }) }
|
4
|
+
end
|
5
|
+
|
6
|
+
alias_method :couch_rest__extended_document_view, :default_couch
|
7
|
+
alias_method :couch_foo__base_view, :default_couch
|
8
|
+
alias_method :couch_potato__persistence_view, :default_couch
|
9
|
+
end
|
10
|
+
|
11
|
+
Hirb::DynamicView.add Hirb::Views::CouchDb, :helper=>:auto_table
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Hirb::Views::MiscDb #:nodoc:
|
2
|
+
def friendly__document_view(obj)
|
3
|
+
{:fields=>obj.class.attributes.keys - [:id]}
|
4
|
+
end
|
5
|
+
|
6
|
+
def ripple__document_view(obj)
|
7
|
+
{:fields=>obj.class.properties.keys}
|
8
|
+
end
|
9
|
+
|
10
|
+
def d_b_i__row_view(obj)
|
11
|
+
{:fields=>obj.column_names, :table_class=>Hirb::Helpers::Table}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Hirb::DynamicView.add Hirb::Views::MiscDb, :helper=>:auto_table
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Hirb::Views::MongoDb #:nodoc:
|
2
|
+
def mongoid__document_view(obj)
|
3
|
+
{:fields=>['_id'] + obj.class.fields.keys}
|
4
|
+
end
|
5
|
+
|
6
|
+
def mongo_mapper__document_view(obj)
|
7
|
+
fields = obj.class.column_names
|
8
|
+
fields.delete('_id') && fields.unshift('_id')
|
9
|
+
{:fields=>fields}
|
10
|
+
end
|
11
|
+
alias_method :mongo_mapper__embedded_document_view, :mongo_mapper__document_view
|
12
|
+
end
|
13
|
+
|
14
|
+
Hirb::DynamicView.add Hirb::Views::MongoDb, :helper=>:auto_table
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Hirb::Views::ORM #:nodoc:
|
2
|
+
def data_mapper__resource_view(obj)
|
3
|
+
{:fields=>obj.class.properties.map {|e| e.name }}
|
4
|
+
end
|
5
|
+
|
6
|
+
def sequel__model_view(obj)
|
7
|
+
{:fields=>obj.class.columns}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Hirb::DynamicView.add Hirb::Views::ORM, :helper=>:auto_table
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Hirb::Views::Rails #:nodoc:
|
2
|
+
def active_record__base_view(obj)
|
3
|
+
{:fields=>get_active_record_fields(obj)}
|
4
|
+
end
|
5
|
+
|
6
|
+
def get_active_record_fields(obj)
|
7
|
+
fields = obj.class.column_names.map {|e| e.to_sym }
|
8
|
+
# if query used select
|
9
|
+
if obj.attributes.keys.sort != obj.class.column_names.sort
|
10
|
+
selected_columns = obj.attributes.keys
|
11
|
+
sorted_columns = obj.class.column_names.dup.delete_if {|e| !selected_columns.include?(e) }
|
12
|
+
sorted_columns += (selected_columns - sorted_columns)
|
13
|
+
fields = sorted_columns.map {|e| e.to_sym}
|
14
|
+
end
|
15
|
+
fields
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Hirb::DynamicView.add Hirb::Views::Rails, :helper=>:auto_table
|
data/lib/hirb/views.rb
ADDED
data/lib/hirb.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Needed by Hirb::String to handle multibyte characters
|
2
|
+
$KCODE = 'u' if RUBY_VERSION < '1.9'
|
3
|
+
|
4
|
+
require 'yaml'
|
5
|
+
require 'hirb/util'
|
6
|
+
require 'hirb/string'
|
7
|
+
require 'hirb/formatter' # must come before helpers/auto_table
|
8
|
+
require 'hirb/dynamic_view'
|
9
|
+
require 'hirb/helpers'
|
10
|
+
require 'hirb/views'
|
11
|
+
require 'hirb/view'
|
12
|
+
require 'hirb/console'
|
13
|
+
require 'hirb/pager'
|
14
|
+
require 'hirb/menu'
|
15
|
+
require 'hirb/version'
|
16
|
+
|
17
|
+
# Most of Hirb's functionality is in Hirb::View.
|
18
|
+
# For a tutorial on configuring and creating views see Hirb::View. For a tutorial on dynamic views see Hirb::DynamicView.
|
19
|
+
#
|
20
|
+
# == Config Files
|
21
|
+
# Hirb can have multiple config files defined by config_files(). These config files
|
22
|
+
# have the following top level keys:
|
23
|
+
# [*:output*] This hash is used by the formatter object. See Hirb::Formatter.config for its format.
|
24
|
+
# [*:width*] Width of the terminal/console. Defaults to Hirb::View::DEFAULT_WIDTH or possibly autodetected when Hirb is enabled.
|
25
|
+
# [*:height*] Height of the terminal/console. Defaults to Hirb::View::DEFAULT_HEIGHT or possibly autodetected when Hirb is enabled.
|
26
|
+
# [*:formatter*] Boolean which determines if the formatter is enabled. Defaults to true.
|
27
|
+
# [*:pager*] Boolean which determines if the pager is enabled. Defaults to true.
|
28
|
+
# [*:pager_command*] Command to be used for paging. Command can have options after it i.e. 'less -r'.
|
29
|
+
# Defaults to common pagers i.e. less and more if detected.
|
30
|
+
# [*:ignore_errors*] Boolean which ignores internal view errors and continues with original view
|
31
|
+
# (i.e. #inspect for irb). Defaults to false.
|
32
|
+
module Hirb
|
33
|
+
class <<self
|
34
|
+
attr_accessor :config_files, :config
|
35
|
+
|
36
|
+
# Enables view functionality. See Hirb::View.enable for details.
|
37
|
+
def enable(options={}, &block)
|
38
|
+
View.enable(options, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Disables view functionality. See Hirb::View.disable for details.
|
42
|
+
def disable
|
43
|
+
View.disable
|
44
|
+
end
|
45
|
+
|
46
|
+
# Adds views. See Hirb::View.add for details.
|
47
|
+
def add_view(view, options, &block)
|
48
|
+
View.add(view, options, &block)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Adds views. See Hirb::DynamicView.add for details.
|
52
|
+
def add_dynamic_view(view, options, &block)
|
53
|
+
DynamicView.add(view, options, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Array of config files which are merged sequentially to produce config.
|
57
|
+
# Defaults to config/hirb.yml and ~/.hirb_yml
|
58
|
+
def config_files
|
59
|
+
@config_files ||= default_config_files
|
60
|
+
end
|
61
|
+
|
62
|
+
#:stopdoc:
|
63
|
+
def default_config_files
|
64
|
+
[File.join(Util.find_home, ".hirb.yml")] +
|
65
|
+
(File.exists?('config/hirb.yml') ? ['config/hirb.yml'] : [])
|
66
|
+
end
|
67
|
+
|
68
|
+
def read_config_file(file=config_file)
|
69
|
+
File.exists?(file) ? YAML::load_file(file) : {}
|
70
|
+
end
|
71
|
+
|
72
|
+
def config(reload=false)
|
73
|
+
if (@config.nil? || reload)
|
74
|
+
@config = config_files.inject({}) {|acc,e|
|
75
|
+
Util.recursive_hash_merge(acc,read_config_file(e))
|
76
|
+
}
|
77
|
+
end
|
78
|
+
@config
|
79
|
+
end
|
80
|
+
#:startdoc:
|
81
|
+
end
|
82
|
+
end
|
data/lib/ripl/hirb.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'hirb'
|
2
|
+
|
3
|
+
module Ripl::Hirb
|
4
|
+
def before_loop
|
5
|
+
super
|
6
|
+
Hirb.enable(Ripl.config[:hirb] || {})
|
7
|
+
end
|
8
|
+
|
9
|
+
def format_result(result)
|
10
|
+
return super if !Hirb::View.enabled?
|
11
|
+
Hirb::View.view_or_page_output(result) || super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Ripl::Shell.include Ripl::Hirb
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
describe "auto table" do
|
4
|
+
it "converts nonarrays to arrays and renders" do
|
5
|
+
require 'set'
|
6
|
+
expected_table = <<-TABLE.unindent
|
7
|
+
+-------+
|
8
|
+
| value |
|
9
|
+
+-------+
|
10
|
+
| 1 |
|
11
|
+
| 2 |
|
12
|
+
| 3 |
|
13
|
+
+-------+
|
14
|
+
3 rows in set
|
15
|
+
TABLE
|
16
|
+
Helpers::AutoTable.render(::Set.new([1,2,3])).should == expected_table
|
17
|
+
end
|
18
|
+
|
19
|
+
it "renders hash" do
|
20
|
+
expected_table = <<-TABLE.unindent
|
21
|
+
+---+-------+
|
22
|
+
| 0 | 1 |
|
23
|
+
+---+-------+
|
24
|
+
| a | 12345 |
|
25
|
+
+---+-------+
|
26
|
+
1 row in set
|
27
|
+
TABLE
|
28
|
+
Helpers::AutoTable.render({:a=>12345}).should == expected_table
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
describe "Console" do
|
4
|
+
it "#table is called without Hirb enabled" do
|
5
|
+
extend Hirb::Console
|
6
|
+
reset_config
|
7
|
+
expected_table = <<-TABLE.unindent
|
8
|
+
+-------+
|
9
|
+
| value |
|
10
|
+
+-------+
|
11
|
+
| 5 |
|
12
|
+
| 3 |
|
13
|
+
+-------+
|
14
|
+
2 rows in set
|
15
|
+
TABLE
|
16
|
+
capture_stdout {
|
17
|
+
table([5,3], :fields=>[:to_s])
|
18
|
+
}.should == expected_table +"\n"
|
19
|
+
end
|
20
|
+
|
21
|
+
it ".render_output sets config if it wasn't before" do
|
22
|
+
reset_config
|
23
|
+
View.expects(:render_output)
|
24
|
+
Console.render_output('blah')
|
25
|
+
View.config.is_a?(Hash).should == true
|
26
|
+
end
|
27
|
+
end
|
data/test/deps.rip
ADDED