module FancyIrb

Constants

DEFAULT_OPTIONS
SKIP_ROCKET_METHODS
TRACK_HEIGHT_INPUT_METHODS

TODO: each_byte, each_char, each_codepoint, each, etc

VERSION

Attributes

error_capturer[R]
skip_next_rocket[RW]

Public Class Methods

append_input_color(string) click to toggle source

Note: No reset, relies on next one

# File lib/fancy_irb/implementation.rb, line 74
def append_input_color(string)
  if input_color = @options[:colorize][:input]
    string + Paint.color(*Array(input_color))
  else
    string
  end
end
apply_user_options(user_options) click to toggle source
# File lib/fancy_irb/implementation.rb, line 33
def apply_user_options(user_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
  }
end
colorize(string, colorize_key) click to toggle source
# File lib/fancy_irb/implementation.rb, line 69
def colorize(string, colorize_key)
  Paint::NOTHING + Paint[string, *Array(@options[:colorize][colorize_key])]
end
extend!() click to toggle source

hook into IRB

# File lib/fancy_irb/implementation.rb, line 19
def extend!
  require_relative 'irb_ext'
  require_relative 'core_ext'
  require_relative 'clean_up'
end
get_offset_from_reline() click to toggle source
# File lib/fancy_irb/implementation.rb, line 105
def get_offset_from_reline
  last_buffer = (Reline::HISTORY || [])[-1] || ""
  last_line = last_buffer.split("\n").last
  1 + @current_indent + width_of(last_line)
end
handle_prompt(prompt, scanner_indent) click to toggle source
# File lib/fancy_irb/implementation.rb, line 58
def handle_prompt(prompt, scanner_indent)#, track_indent)
  # @tracked_indent = 2 if track_indent
  @current_indent = width_of(prompt) + scanner_indent# + @tracked_indent

  append_input_color colorize(prompt, :input_prompt)
end
output_value(context, _scanner) click to toggle source
# File lib/fancy_irb/implementation.rb, line 82
def output_value(context, _scanner)
  output = context.inspect_last_value
  if @options[:rocket_mode] && !@skip_next_rocket && !output.include?("\n")
    offset = get_offset_from_reline
    cols_to_show  = offset + width_of(@options[:rocket_prompt] + output)
    lines_to_show = 1 + @tracked_height

    if TerminalInfo.lines > lines_to_show && TerminalInfo.cols > cols_to_show
      print \
        Paint::NOTHING +
        TerminalInfo::TPUT[:sc] +                    # save current cursor position
        TerminalInfo::TPUT[:cuu1] * lines_to_show +  # move cursor upwards    to the original input line
        TerminalInfo::TPUT[:cuf1] * offset +         # move cursor rightwards to the original input offset
        colorize(@options[:rocket_prompt], :rocket_prompt) +   # draw rocket prompt
        output +                                               # draw output
        TerminalInfo::TPUT[:rc]                      # return to normal cursor position
      return
    end
  end
  @skip_next_rocket = false
  puts colorize(@options[:result_prompt], :result_prompt) + output
end
patch_stream(object, stream_name) click to toggle source
Calls superclass method
# File lib/fancy_irb/implementation.rb, line 133
def patch_stream(object, stream_name)
  object.define_singleton_method :write do |data|
    FancyIrb.track_height data
    super FancyIrb.colorize(data, stream_name)
  end
end
present_and_clear_captured_error!() click to toggle source
# File lib/fancy_irb/implementation.rb, line 144
def present_and_clear_captured_error!
  if @error_capturer
    @error_capturer.restore_original_stdout
    $stderr.puts colorize(
      @error_capturer.error_string.chomp,
      :irb_errors,
    )
    @error_capturer = nil
  end
end
register_error_capturer!() click to toggle source
# File lib/fancy_irb/implementation.rb, line 140
def register_error_capturer!
  @error_capturer = ErrorCapturer.new
end
register_height_trackers(object, methods_) click to toggle source

TODO testing and improving, e.g. getc does not contain ā€œnā€

Calls superclass method
# File lib/fancy_irb/implementation.rb, line 112
def register_height_trackers(object, methods_)
  methods_.each{ |method_|
    if object.respond_to?(method_)
      object.send :define_singleton_method, method_ do |*args|
        res = super(*args)
        FancyIrb.track_height(res)
        res
      end
    end
  }
end
register_skipped_rockets(object_class, methods_) click to toggle source
Calls superclass method
# File lib/fancy_irb/implementation.rb, line 124
def register_skipped_rockets(object_class, methods_)
  methods_.each{ |method_|
    object_class.send :define_method, method_ do |*args|
      FancyIrb.skip_next_rocket = true
      super(*args)
    end
  }
end
reset_line!() click to toggle source
# File lib/fancy_irb/implementation.rb, line 53
def reset_line!
  @tracked_height = 0
  @tracked_indent = 0
end
set_defaults() click to toggle source
# File lib/fancy_irb/implementation.rb, line 25
def set_defaults
  @skip_next_rocket = false
  @current_indent = Float::INFINITY

  @options = DEFAULT_OPTIONS.dup
  @options[:colorize] = @options[:colorize].dup if @options[:colorize]
end
start(user_options = {}) click to toggle source
# File lib/fancy_irb/implementation.rb, line 10
def start(user_options = {})
  set_defaults
  apply_user_options(user_options)
  reset_line!
  extend!
  true
end
track_height(data) click to toggle source
# File lib/fancy_irb/implementation.rb, line 65
def track_height(data)
  @tracked_height += height_of(data, TerminalInfo.cols)
end