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

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/hirb/view.rb', line 9

def config
  @config
end

.render_methodObject

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

Returns:

  • (Boolean)


181
# File 'lib/hirb/view.rb', line 181

def config_loaded?; !!@config; end

.default_configObject



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_methodObject



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

.disableObject

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(options={}, &block)
  return puts("Already enabled.") if @enabled
  @enabled = true
  Hirb.config_file = options.delete(:config_file) if options[:config_file]
  @output_method = "IRB::Irb.output_value" if Object.const_defined?(:IRB)
  @output_method = options.delete(:output_method) if options[:output_method]
  load_config(Util.recursive_hash_merge(options, 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.

Returns:

  • (Boolean)


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_configObject

Current formatter config



101
102
103
# File 'lib/hirb/view.rb', line 101

def formatter_config
  formatter.config
end

.heightObject

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

.pagerObject



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, options={})
  if (formatted_output = formatter.format_output(output, options))
    render_method.call(formatted_output)
    true
  else
    false
  end
end

.reset_render_methodObject

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_formatterObject

Toggles formatter on or off.



54
55
56
# File 'lib/hirb/view.rb', line 54

def toggle_formatter
  config[:formatter] = !config[:formatter]
end

.toggle_pagerObject

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, options={})
  enabled? && config[:formatter] && render_output(output, options)
end

.widthObject

Current console width



91
92
93
# File 'lib/hirb/view.rb', line 91

def width
  config ? config[:width] : DEFAULT_WIDTH
end