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.



86
87
88
# File 'lib/hirb/view.rb', line 86

def render_method
  @render_method
end

Class Method Details

.capture_and_render(&block) ⇒ Object

Captures STDOUT and renders it using render_method(). The main use case is to conditionally page captured stdout.



79
80
81
# File 'lib/hirb/view.rb', line 79

def capture_and_render(&block)
  render_method.call Util.capture_stdout(&block)
end

.config_loaded?Boolean

Returns:

  • (Boolean)


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

def config_loaded?; !!@config; end

.default_configObject



167
168
169
# File 'lib/hirb/view.rb', line 167

def default_config
  Util.recursive_hash_merge({:pager=>true, :formatter=>true}, Hirb.config || {})
end

.default_render_methodObject



163
164
165
# File 'lib/hirb/view.rb', line 163

def default_render_method
  lambda {|output| page_output(output) || puts(output) }
end

.determine_terminal_size(width, height) ⇒ Object



125
126
127
128
# File 'lib/hirb/view.rb', line 125

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.



44
45
46
47
48
49
50
51
# File 'lib/hirb/view.rb', line 44

def disable
  @enabled = false
  if Object.const_defined?(:IRB)
    ::IRB::Irb.class_eval do
      alias :output_value :non_hirb_view_output
    end
  end
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.

Examples:

Hirb::View.enable
Hirb::View.enable :formatter=>false
Hirb::View.enable {|c| c.output = {'String'=>{:class=>'Hirb::Helpers::Table'}} }


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hirb/view.rb', line 21

def enable(options={}, &block)
  return puts("Already enabled.") if @enabled
  @enabled = true
  Hirb.config_file = options.delete(:config_file) if options[:config_file]
  load_config(Util.recursive_hash_merge(options, HashStruct.block_to_hash(block)))
  resize(config[:width], config[:height])
  if Object.const_defined?(:IRB)
    ::IRB::Irb.class_eval do
      alias :non_hirb_view_output  :output_value
      def output_value #:nodoc:
        Hirb::View.view_output(@context.last_value) || Hirb::View.page_output(@context.last_value.inspect, true) ||
          non_hirb_view_output
      end
    end
  end
end

.enabled?Boolean

Indicates if Hirb::View is enabled.

Returns:

  • (Boolean)


39
40
41
# File 'lib/hirb/view.rb', line 39

def enabled?
  @enabled || false
end

.format_class(klass, helper_config) ⇒ Object

Sets the helper config for the given output class.



111
112
113
# File 'lib/hirb/view.rb', line 111

def format_class(klass, helper_config)
  formatter.format_class(klass, helper_config)
end

.formatter(reload = false) ⇒ Object



145
146
147
# File 'lib/hirb/view.rb', line 145

def formatter(reload=false)
  @formatter = reload || @formatter.nil? ? Formatter.new(config[:output]) : @formatter
end

.formatter=(value) ⇒ Object



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

def formatter=(value); @formatter = value; end

.formatter_configObject

Current formatter config



106
107
108
# File 'lib/hirb/view.rb', line 106

def formatter_config
  formatter.config
end

.heightObject

Current console height



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

def height
  config ? config[:height] : DEFAULT_HEIGHT
end

.load_config(additional_config = {}) ⇒ Object



151
152
153
154
155
# File 'lib/hirb/view.rb', line 151

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



130
131
132
133
134
135
136
137
# File 'lib/hirb/view.rb', line 130

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



139
140
141
# File 'lib/hirb/view.rb', line 139

def pager
  @pager ||= Pager.new(config[:width], config[:height], :pager_command=>config[:pager_command])
end

.pager=(value) ⇒ Object



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

def pager=(value); @pager = value; end

.render_output(output, options = {}) ⇒ Object

:stopdoc:



116
117
118
119
120
121
122
123
# File 'lib/hirb/view.rb', line 116

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.



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

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.



66
67
68
69
# File 'lib/hirb/view.rb', line 66

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.



59
60
61
# File 'lib/hirb/view.rb', line 59

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

.toggle_pagerObject

Toggles pager on or off. The pager only works while Hirb::View is enabled.



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

def toggle_pager
  config[:pager] = !config[:pager]
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.



74
75
76
# File 'lib/hirb/view.rb', line 74

def view_output(output, options={})
  enabled? && config[:formatter] && render_output(output, options)
end

.widthObject

Current console width



96
97
98
# File 'lib/hirb/view.rb', line 96

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