Module: Hirb::View
- Defined in:
- lib/hirb/view.rb
Overview
This class is responsible for managing all view-related functionality.
Create a View
Let’s create a simple view for Hash objects:
$ irb -rubygems
>> require 'hirb'
=>true
>> Hirb.enable
=>nil
>> require 'yaml'
=>true
# A view method is the smallest view
>> def yaml(output); output.to_yaml; end
=> nil
# Add the view
>> Hirb.add_view Hash, :method=>:yaml
=> true
# Hashes now appear as yaml
>> {:a=>1, :b=>{:c=>3}}
---
:a : 1
:b :
:c : 3
=> true
Another way of creating a view is a Helper class:
# Create yaml view class
>> class Hirb::Helpers::Yaml; def self.render(output, options={}); output.to_yaml; end ;end
=>nil
# Add the view
>> Hirb.add_view Hash, :class=>Hirb::Helpers::Yaml
=>true
# Hashes appear as yaml like above ...
Configure a View
To configure the above Helper class as a view, either pass Hirb.enable a hash:
# In .irbrc
require 'hirb'
# View class needs to come before enable()
class Hirb::Helpers::Yaml; def self.render(output, ={}); output.to_yaml; end ;end
Hirb.enable :output=>{"Hash"=>{:class=>"Hirb::Helpers::Yaml"}}
Or create a config file at config/hirb.yml or ~/.hirb.yml:
# The config file for the yaml example would look like:
# ---
# :output :
# Hash :
# :class : Hirb::Helpers::Yaml
# In .irbrc
require 'hirb'
# View class needs to come before enable()
class Hirb::Helpers::Yaml; def self.render(output, ={}); output.to_yaml; end ;end
Hirb.enable
For more about configuring Hirb, see the Config Files section in Hirb.
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
Returns the value of attribute render_method.
Class Method Summary collapse
-
.add(klass, view_config) ⇒ Object
Adds a view when View is enabled.
-
.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.
- .disable_output_method ⇒ Object
-
.enable(options = {}) ⇒ Object
This activates view functionality i.e.
-
.enable_output_method ⇒ Object
:stopdoc:.
-
.enabled? ⇒ Boolean
Indicates if Hirb::View is enabled.
- .formatter(reload = false) ⇒ Object
- .formatter=(value) ⇒ Object
-
.formatter_config ⇒ Object
Current formatter config, storing a hash of all static views.
-
.height ⇒ Object
Current console height.
- .load_config(additional_config = {}) ⇒ Object
- .merge_or_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.
- .view_or_page_output(str) ⇒ Object
-
.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.
67 68 69 |
# File 'lib/hirb/view.rb', line 67 def config @config end |
.render_method ⇒ Object
Returns the value of attribute render_method.
66 67 68 |
# File 'lib/hirb/view.rb', line 66 def render_method @render_method end |
Class Method Details
.add(klass, view_config) ⇒ Object
Adds a view when View is enabled. See Formatter.add_view for more details.
177 178 179 180 181 182 183 |
# File 'lib/hirb/view.rb', line 177 def add(klass, view_config) if enabled? formatter.add_view(klass, view_config) else puts "View must be enabled to add a view" 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.
144 145 146 |
# File 'lib/hirb/view.rb', line 144 def capture_and_render(&block) render_method.call Util.capture_stdout(&block) end |
.config_loaded? ⇒ Boolean
292 |
# File 'lib/hirb/view.rb', line 292 def config_loaded?; !!@config; end |
.default_config ⇒ Object
303 304 305 |
# File 'lib/hirb/view.rb', line 303 def default_config Util.recursive_hash_merge({:pager=>true, :formatter=>true}, Hirb.config || {}) end |
.default_render_method ⇒ Object
299 300 301 |
# File 'lib/hirb/view.rb', line 299 def default_render_method lambda {|output| page_output(output) || puts(output) } end |
.determine_terminal_size(width, height) ⇒ Object
250 251 252 253 |
# File 'lib/hirb/view.rb', line 250 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.
95 96 97 98 99 |
# File 'lib/hirb/view.rb', line 95 def disable @enabled = false disable_output_method if @output_method false end |
.disable_output_method ⇒ Object
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/hirb/view.rb', line 226 def disable_output_method if defined?(IRB::Irb) && !defined?(Ripl) ::IRB::Irb.class_eval do undef output_value alias_method :output_value, :non_hirb_view_output end end @output_method = nil end |
.enable(options = {}) ⇒ 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. When called multiple times, new configs are merged into the existing config. If using Wirble, you should call this after it. The view configuration can be specified in a hash via a config file, or as options to this method. In addition to the config keys mentioned in Hirb, options also take the following keys:
Options:
-
config_file: Name of config file(s) that are merged into existing config
Examples:
Hirb.enable
Hirb.enable :formatter=>false
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hirb/view.rb', line 78 def enable(={}) Array(.delete(:config_file)).each {|e| @new_config_file = true Hirb.config_files << e } enable_output_method unless @output_method merge_or_load_config resize(config[:width], config[:height]) @enabled = true end |
.enable_output_method ⇒ Object
:stopdoc:
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/hirb/view.rb', line 186 def enable_output_method if defined?(Ripl) && Ripl.respond_to?(:started?) && Ripl.started? @output_method = true require 'ripl/hirb' unless defined? Ripl::Hirb elsif defined? Pry original_print = Pry.config.print Pry.config.print = proc do |output, result, pry_instance| Hirb::View.view_or_page_output(result) || original_print.call(output, result, pry_instance) end elsif defined?(IRB::Irb) @output_method = true ::IRB::Irb.class_eval do alias_method :non_hirb_view_output, :output_value def output_value(omit = false) #:nodoc: Hirb::View.view_or_page_output(@context.last_value) || original_output_value(omit) end # Do not pass the value if the default is given to keep backwards # compatiblity for Ruby =< 2.7.1 if !instance_methods(false).include?(:original_output_value) def original_output_value(omit) if omit non_hirb_view_output(omit) else non_hirb_view_output end end end end end end |
.enabled? ⇒ Boolean
Indicates if Hirb::View is enabled.
90 91 92 |
# File 'lib/hirb/view.rb', line 90 def enabled? @enabled || false end |
.formatter(reload = false) ⇒ Object
270 271 272 |
# File 'lib/hirb/view.rb', line 270 def formatter(reload=false) @formatter = reload || @formatter.nil? ? Formatter.new(config[:output]) : @formatter end |
.formatter=(value) ⇒ Object
274 |
# File 'lib/hirb/view.rb', line 274 def formatter=(value); @formatter = value; end |
.formatter_config ⇒ Object
Current formatter config, storing a hash of all static views
172 173 174 |
# File 'lib/hirb/view.rb', line 172 def formatter_config formatter.config end |
.height ⇒ Object
Current console height
167 168 169 |
# File 'lib/hirb/view.rb', line 167 def height config && config[:height] ? config[:height] : DEFAULT_HEIGHT end |
.load_config(additional_config = {}) ⇒ Object
286 287 288 289 290 |
# File 'lib/hirb/view.rb', line 286 def load_config(additional_config={}) @config = Util.recursive_hash_merge default_config, additional_config formatter(true) true end |
.merge_or_load_config(additional_config = {}) ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/hirb/view.rb', line 276 def merge_or_load_config(additional_config={}) if @config && (@new_config_file || !additional_config.empty?) Hirb.config = nil load_config Util.recursive_hash_merge(@config, additional_config) @new_config_file = false elsif !@enabled load_config(additional_config) end end |
.page_output(output, inspect_mode = false) ⇒ Object
255 256 257 258 259 260 261 262 |
# File 'lib/hirb/view.rb', line 255 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
264 265 266 |
# File 'lib/hirb/view.rb', line 264 def pager @pager ||= Pager.new(config[:width], config[:height], :pager_command=>config[:pager_command]) end |
.pager=(value) ⇒ Object
268 |
# File 'lib/hirb/view.rb', line 268 def pager=(value); @pager = value; end |
.render_output(output, options = {}) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/hirb/view.rb', line 241 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.
157 158 159 |
# File 'lib/hirb/view.rb', line 157 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.
118 119 120 121 |
# File 'lib/hirb/view.rb', line 118 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.
107 108 109 |
# File 'lib/hirb/view.rb', line 107 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.
102 103 104 |
# File 'lib/hirb/view.rb', line 102 def toggle_pager config[:pager] = !config[:pager] end |
.view_or_page_output(str) ⇒ Object
237 238 239 |
# File 'lib/hirb/view.rb', line 237 def view_or_page_output(str) view_output(str) || page_output(str.inspect, true) 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.
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/hirb/view.rb', line 130 def view_output(output, ={}) enabled? && config[:formatter] && render_output(output, ) rescue Exception=>e if config[:ignore_errors] $stderr.puts "Hirb Error: #{e.}" false else index = (obj = e.backtrace.find {|f| f =~ /^\(eval\)/}) ? e.backtrace.index(obj) : e.backtrace.length $stderr.puts "Hirb Error: #{e.}", e.backtrace.slice(0,index).map {|_e| " " + _e } true end end |
.width ⇒ Object
Current console width
162 163 164 |
# File 'lib/hirb/view.rb', line 162 def width config && config[:width] ? config[:width] : DEFAULT_WIDTH end |