class PryKeybind

Constants

VERSION

Attributes

layers[RW]
registry[RW]
key[R]
pry_instance[RW]

Public Class Methods

bind_all!(pry, source: nil) click to toggle source
# File lib/pry-keybind.rb, line 26
def self.bind_all!(pry, source: nil)
  # puts "binding / layer size?: #{layers.size} / source: #{source}"
  layers << [*registry.values].map do |key_binding|
    key_binding.pry_instance = pry
    key_binding.bind!
  end.compact
end
new(key, options = {}, &block) click to toggle source
# File lib/pry-keybind.rb, line 45
def initialize(key, options = {}, &block)
  raise ArgumentError, "block required" unless block_given?

  @options = options
  @key = KeySequence.new(key)
  @block = block
  @bound = false
end
register(constant, key, options = {}, &block) click to toggle source
# File lib/pry-keybind.rb, line 15
def self.register(constant, key, options = {}, &block)
  self.registry ||= {}
  self.layers ||= []
  self.registry[constant] = new(key, options, &block)
end
register_anonymous(key, options = {}, &block) click to toggle source
# File lib/pry-keybind.rb, line 21
def self.register_anonymous(key, options = {}, &block)
  constant = format("BINDING_%09d", Random.random_number(1_000_000_000)).to_sym
  register(constant, key, options, &block)
end
unbind_all!(pry_instance, source: nil) click to toggle source
# File lib/pry-keybind.rb, line 34
def self.unbind_all!(pry_instance, source: nil)
  # puts "unbinding / layer size?: #{layers.size} / source: #{source}"
  return unless layer = layers.pop

  layer.each do |key_binding|
    key_binding.pry_instance = pry_instance
    key_binding.unbind!
  end
end

Public Instance Methods

bind!() click to toggle source
# File lib/pry-keybind.rb, line 54
def bind!
  return nil if bound?

  Pryline.public_send(bind_method, key.for_readline, &block_caller)

  @bound = true

  self
end
unbind!() click to toggle source
# File lib/pry-keybind.rb, line 64
def unbind!
  return self unless bound?

  Pryline.public_send(unbind_method, key.for_readline)

  @bound = false

  self
end

Private Instance Methods

bind_method() click to toggle source
# File lib/pry-keybind.rb, line 83
def bind_method
  key.sequence? ? :bind_keyseq : :bind_key
end
block_caller() click to toggle source
# File lib/pry-keybind.rb, line 103
def block_caller
  @block_caller ||= Proc.new do
    # puts "\npry keybind 99 keybindings accepting line\n"
    if save_input?
      Pry.config.input_states << InputState.save!(pry_instance)
    end

    pry_instance.define_singleton_method(:whole_buffer) do
      "#{eval_string}#{Pryline.line_buffer}"
    end

    pry_instance.define_singleton_method(:whole_buffer=) do |input|
      self.eval_string = ""
      Pryline.delete_text
      Pryline.point = 0
      Pryline.insert_text input.chomp
    end

    @block.call(pry_instance)

    Pryline.refresh_line if refresh_line?
  end
end
bound?() click to toggle source
# File lib/pry-keybind.rb, line 79
def bound?
  @bound
end
no_refresh?() click to toggle source
# File lib/pry-keybind.rb, line 95
def no_refresh?
  @options[:no_refresh]
end
refresh_line?() click to toggle source
# File lib/pry-keybind.rb, line 99
def refresh_line?
  !no_refresh? || save_input?
end
save_input?() click to toggle source
# File lib/pry-keybind.rb, line 91
def save_input?
  @options[:save_input]
end
unbind_method() click to toggle source
# File lib/pry-keybind.rb, line 87
def unbind_method
  key.sequence? ? :unbind_keyseq : :unbind_key
end