Module: FancyIrb

Defined in:
lib/fancy_irb.rb

Constant Summary collapse

VERSION =
File.read( File.dirname(__FILE__) + '/../VERSION' ).chomp

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.capture_irb_errorsObject

Returns the value of attribute capture_irb_errors.



20
21
22
# File 'lib/fancy_irb.rb', line 20

def capture_irb_errors
  @capture_irb_errors
end

.continueObject

Returns the value of attribute continue.



22
23
24
# File 'lib/fancy_irb.rb', line 22

def continue
  @continue
end

.optionsObject

setup instance variable accessors



10
11
12
# File 'lib/fancy_irb.rb', line 10

def options
  @options
end

.original_stdoutObject

Returns the value of attribute original_stdout.



19
20
21
# File 'lib/fancy_irb.rb', line 19

def original_stdout
  @original_stdout
end

.real_lengthsObject

Returns the value of attribute real_lengths.



21
22
23
# File 'lib/fancy_irb.rb', line 21

def real_lengths
  @real_lengths
end

.skip_next_rocketObject

Returns the value of attribute skip_next_rocket.



24
25
26
# File 'lib/fancy_irb.rb', line 24

def skip_next_rocket
  @skip_next_rocket
end

.stdout_colorfulObject

Returns the value of attribute stdout_colorful.



23
24
25
# File 'lib/fancy_irb.rb', line 23

def stdout_colorful
  @stdout_colorful
end

Class Method Details

.[](key, key2 = nil) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/fancy_irb.rb', line 11

def [](key, key2 = nil)
  if key2
    @options[key][key2]
  else
    @options[key]
  end
end

.add_output_proc(prepend = false, &proc) ⇒ Object



95
96
97
98
# File 'lib/fancy_irb.rb', line 95

def add_output_proc(prepend = false, &proc)
  action = prepend ? :unshift : :push
  @options[:output_procs].send action, proc
end

.get_heightObject



117
118
119
# File 'lib/fancy_irb.rb', line 117

def get_height
  1 + ( @height_counter == [0] ? 0 : @height_counter.reduce(:+) || 0 )
end

.reset_heightObject



104
105
106
# File 'lib/fancy_irb.rb', line 104

def reset_height
  @height_counter = []
end

.set_result_proc(&proc) ⇒ Object



100
101
102
# File 'lib/fancy_irb.rb', line 100

def set_result_proc(&proc)
  @options[:result_proc] = proc
end

.start(user_options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fancy_irb.rb', line 26

def start(user_options = {})
  # track some irb stuff
  @height_counter   = []
  @real_lengths     = { :output => 1, :input_prompt => 9999 } # or whatever
  @stdout_colorful  = false
  @continue         = false
  @skip_next_rocket = false

  # set defaults and parse user options
  default_result_proc = proc{ |context|
    if context.inspect?
      context.last_value.inspect
    else
      context.last_value
    end
  }

  default_colorizer_proc = proc{ |value|
    FancyIrb.real_lengths[:output] =    value.size
    if defined?(Wirb) && FancyIrb[:colorize, :output]
      Wirb.colorize_result value
    else
      value 
    end
  }

  default_options = {
    :rocket_mode     => true,   # activate or deactivate #=> rocket output
    :rocket_prompt   => '#=> ', # prompt to use for the rocket
    :result_prompt   => '=> ',  # prompt to use for normal output
    :colorize => {              # colors hash. Set to nil to deactivate colorizing
      :rocket_prompt => [:blue],
      :result_prompt => [:blue],
      :input_prompt  => nil,
      :irb_errors    => [:red],
      :stderr        => [:red, :bright],
      :stdout        => nil,
      :input         => nil,
      :output        => true, # wirb's output colorization
     },
    :result_proc     => default_result_proc,       # how to get the output result
    :output_procs    => [default_colorizer_proc],  # you can modify/enhance/log your output
    :east_asian_width => false, # set to true if you have double-width characters (slower)
  }

  @options = default_options

  default_options.each{ |key, value|
    # (ugly) 1 level deep merge, maybe refactor
    if key == :colorize
      if user_options.has_key?(:colorize) && user_options[:colorize].nil?
        @options[:colorize] = {}
      else
        value.each{ |key2, _|
          if user_options[key] && user_options[key].has_key?(key2)
            @options[:colorize][key2] = user_options[key][key2]
          end
        }
      end
    else
      @options[key] = user_options.has_key?(key) ? user_options[key] : default_options[key]
    end
  }

  # hook code into IRB
  require 'fancy_irb/irb_ext'
  true
end

.track_height(data) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/fancy_irb.rb', line 108

def track_height(data)
  data       = Paint.unpaint(data.to_s)
  lines      = data.count("\n")
  long_lines = data.split("\n").inject(0){ |sum, line|
    sum + (line.display_size / current_length)
  }
  @height_counter << lines + long_lines
end

.win?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/fancy_irb.rb', line 125

def win?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end

.write_stream(stream, data, color = nil) ⇒ Object



121
122
123
# File 'lib/fancy_irb.rb', line 121

def write_stream(stream, data, color = nil)
  stream.write_non_fancy( FancyIrb.stdout_colorful ? Paint[data, *Array(color)] : data.to_s )
end

Instance Method Details

#current_lengthObject



131
132
133
# File 'lib/fancy_irb.rb', line 131

def current_length
   ENV['ANSICON'][/\((.*)x/, 1].to_i
end

#current_linesObject



135
136
137
# File 'lib/fancy_irb.rb', line 135

def current_lines
   ENV['ANSICON'][/\(.*x(.*)\)/, 1].to_i
end