Module: Hirb::View
- Defined in:
- lib/hirb/view.rb
Overview
This class is responsible for managing all view-related functionality. Its functionality is determined by setting up a configuration file as explained in Hirb and/or passed configuration directly to Hirb.enable. Most of the functionality in this class is dormant until enabled.
Constant Summary collapse
- DEFAULT_WIDTH =
120
- DEFAULT_HEIGHT =
40
Class Attribute Summary collapse
-
.config ⇒ Object
readonly
Returns the value of attribute config.
-
.render_method ⇒ Object
A lambda or proc which handles the final formatted object.
Class Method Summary collapse
- .alias_output_method(output_method) ⇒ Object
-
.capture_and_render(&block) ⇒ Object
Captures STDOUT and renders it using render_method().
- .config_loaded? ⇒ Boolean
- .default_config ⇒ Object
- .default_render_method ⇒ Object
- .determine_terminal_size(width, height) ⇒ Object
-
.disable ⇒ Object
Disable’s Hirb’s output and revert’s irb’s output method if irb exists.
-
.enable(options = {}, &block) ⇒ Object
This activates view functionality i.e.
-
.enabled? ⇒ Boolean
Indicates if Hirb::View is enabled.
-
.format_class(klass, helper_config) ⇒ Object
Sets the helper config for the given output class.
- .formatter(reload = false) ⇒ Object
- .formatter=(value) ⇒ Object
-
.formatter_config ⇒ Object
Current formatter config.
-
.height ⇒ Object
Current console height.
- .load_config(additional_config = {}) ⇒ Object
- .page_output(output, inspect_mode = false) ⇒ Object
- .pager ⇒ Object
- .pager=(value) ⇒ Object
- .render_output(output, options = {}) ⇒ Object
-
.reset_render_method ⇒ Object
Resets render_method back to its default.
-
.resize(width = nil, height = nil) ⇒ Object
Resizes the console width and height for use with the table and pager i.e.
-
.toggle_formatter ⇒ Object
Toggles formatter on or off.
-
.toggle_pager ⇒ Object
Toggles pager on or off.
-
.unalias_output_method(output_method) ⇒ Object
:stopdoc:.
-
.view_output(output, options = {}) ⇒ Object
This is the main method of this class.
-
.width ⇒ Object
Current console width.
Class Attribute Details
.config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/hirb/view.rb', line 9 def config @config end |
.render_method ⇒ Object
A lambda or proc which handles the final formatted object. Although this pages/puts the object by default, it could be set to do other things i.e. write the formatted object to a file.
81 82 83 |
# File 'lib/hirb/view.rb', line 81 def render_method @render_method end |
Class Method Details
.alias_output_method(output_method) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/hirb/view.rb', line 120 def alias_output_method(output_method) klass, klass_method = output_method.split(".") eval %[ ::#{klass}.class_eval do alias_method :non_hirb_view_output, :#{klass_method} if '#{klass}' == "IRB::Irb" def #{klass_method} #:nodoc: Hirb::View.view_output(@context.last_value) || Hirb::View.page_output(@context.last_value.inspect, true) || non_hirb_view_output end else def #{klass_method}(output_string) #:nodoc: Hirb::View.view_output(output_string) || Hirb::View.page_output(output_string.inspect, true) || non_hirb_view_output(output_string) end end end ] end |
.capture_and_render(&block) ⇒ Object
Captures STDOUT and renders it using render_method(). The main use case is to conditionally page captured stdout.
74 75 76 |
# File 'lib/hirb/view.rb', line 74 def capture_and_render(&block) render_method.call Util.capture_stdout(&block) end |
.config_loaded? ⇒ Boolean
181 |
# File 'lib/hirb/view.rb', line 181 def config_loaded?; !!@config; end |
.default_config ⇒ Object
191 192 193 |
# File 'lib/hirb/view.rb', line 191 def default_config Util.recursive_hash_merge({:pager=>true, :formatter=>true}, Hirb.config || {}) end |
.default_render_method ⇒ Object
187 188 189 |
# File 'lib/hirb/view.rb', line 187 def default_render_method lambda {|output| page_output(output) || puts(output) } end |
.determine_terminal_size(width, height) ⇒ Object
149 150 151 152 |
# File 'lib/hirb/view.rb', line 149 def determine_terminal_size(width, height) detected = (width.nil? || height.nil?) ? Util.detect_terminal_size || [] : [] [width || detected[0] || DEFAULT_WIDTH , height || detected[1] || DEFAULT_HEIGHT] end |
.disable ⇒ Object
Disable’s Hirb’s output and revert’s irb’s output method if irb exists.
42 43 44 45 46 |
# File 'lib/hirb/view.rb', line 42 def disable @enabled = false unalias_output_method(@output_method) if @output_method false end |
.enable(options = {}, &block) ⇒ Object
This activates view functionality i.e. the formatter, pager and size detection. If irb exists, it overrides irb’s output method with Hirb::View.view_output. If using Wirble, you should call this after it. The view configuration can be specified in a hash via a config file, as options to this method, as this method’s block or any combination of these three. In addition to the config keys mentioned in Hirb, the options also take the following keys: Options:
-
config_file: Name of config file to read.
-
output_method: Specify an object’s class and instance method (separated by a period) to be realiased with hirb’s view system. The instance method should take a string to be output. Default is IRB::Irb.output_value if using irb.
Examples:
Hirb::View.enable
Hirb::View.enable :formatter=>false, :output_method=>"Mini.output"
Hirb::View.enable {|c| c.output = {'String'=>{:class=>'Hirb::Helpers::Table'}} }
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hirb/view.rb', line 24 def enable(={}, &block) return puts("Already enabled.") if @enabled @enabled = true Hirb.config_file = .delete(:config_file) if [:config_file] @output_method = "IRB::Irb.output_value" if Object.const_defined?(:IRB) @output_method = .delete(:output_method) if [:output_method] load_config(Util.recursive_hash_merge(, HashStruct.block_to_hash(block))) resize(config[:width], config[:height]) alias_output_method(@output_method) if @output_method true end |
.enabled? ⇒ Boolean
Indicates if Hirb::View is enabled.
37 38 39 |
# File 'lib/hirb/view.rb', line 37 def enabled? @enabled || false end |
.format_class(klass, helper_config) ⇒ Object
Sets the helper config for the given output class.
106 107 108 |
# File 'lib/hirb/view.rb', line 106 def format_class(klass, helper_config) formatter.format_class(klass, helper_config) end |
.formatter(reload = false) ⇒ Object
169 170 171 |
# File 'lib/hirb/view.rb', line 169 def formatter(reload=false) @formatter = reload || @formatter.nil? ? Formatter.new(config[:output]) : @formatter end |
.formatter=(value) ⇒ Object
173 |
# File 'lib/hirb/view.rb', line 173 def formatter=(value); @formatter = value; end |
.formatter_config ⇒ Object
Current formatter config
101 102 103 |
# File 'lib/hirb/view.rb', line 101 def formatter_config formatter.config end |
.height ⇒ Object
Current console height
96 97 98 |
# File 'lib/hirb/view.rb', line 96 def height config ? config[:height] : DEFAULT_HEIGHT end |
.load_config(additional_config = {}) ⇒ Object
175 176 177 178 179 |
# File 'lib/hirb/view.rb', line 175 def load_config(additional_config={}) @config = Util.recursive_hash_merge default_config, additional_config formatter(true) true end |
.page_output(output, inspect_mode = false) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/hirb/view.rb', line 154 def page_output(output, inspect_mode=false) if enabled? && config[:pager] && pager.activated_by?(output, inspect_mode) pager.page(output, inspect_mode) true else false end end |
.pager ⇒ Object
163 164 165 |
# File 'lib/hirb/view.rb', line 163 def pager @pager ||= Pager.new(config[:width], config[:height], :pager_command=>config[:pager_command]) end |
.pager=(value) ⇒ Object
167 |
# File 'lib/hirb/view.rb', line 167 def pager=(value); @pager = value; end |
.render_output(output, options = {}) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/hirb/view.rb', line 140 def render_output(output, ={}) if (formatted_output = formatter.format_output(output, )) render_method.call(formatted_output) true else false end end |
.reset_render_method ⇒ Object
Resets render_method back to its default.
86 87 88 |
# File 'lib/hirb/view.rb', line 86 def reset_render_method @render_method = default_render_method end |
.resize(width = nil, height = nil) ⇒ Object
Resizes the console width and height for use with the table and pager i.e. after having resized the console window. *nix users should only have to call this method. Non-*nix users should call this method with explicit width and height. If you don’t know your width and height, in irb play with “a”* width to find width and puts “an” * height to find height.
61 62 63 64 |
# File 'lib/hirb/view.rb', line 61 def resize(width=nil, height=nil) config[:width], config[:height] = determine_terminal_size(width, height) pager.resize(config[:width], config[:height]) end |
.toggle_formatter ⇒ Object
Toggles formatter on or off.
54 55 56 |
# File 'lib/hirb/view.rb', line 54 def toggle_formatter config[:formatter] = !config[:formatter] end |
.toggle_pager ⇒ Object
Toggles pager on or off. The pager only works while Hirb::View is enabled.
49 50 51 |
# File 'lib/hirb/view.rb', line 49 def toggle_pager config[:pager] = !config[:pager] end |
.unalias_output_method(output_method) ⇒ Object
:stopdoc:
111 112 113 114 115 116 117 118 |
# File 'lib/hirb/view.rb', line 111 def unalias_output_method(output_method) klass, klass_method = output_method.split(".") eval %[ ::#{klass}.class_eval do alias_method :#{klass_method}, :non_hirb_view_output end ] end |
.view_output(output, options = {}) ⇒ Object
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 successful renders it using render_method(). The options this method takes are helper config hashes as described in Hirb::Formatter.format_output(). Returns true if successful and false if no formatting is done or if not enabled.
69 70 71 |
# File 'lib/hirb/view.rb', line 69 def view_output(output, ={}) enabled? && config[:formatter] && render_output(output, ) end |
.width ⇒ Object
Current console width
91 92 93 |
# File 'lib/hirb/view.rb', line 91 def width config ? config[:width] : DEFAULT_WIDTH end |