Class: Ruber::IRB::PromptMatcher

Inherits:
Object
  • Object
show all
Defined in:
plugins/irb/irb_controller.rb

Overview

Incapsulates the information about the special prompt used by the plugin

Constant Summary

MODES =

Mapping between mode names and characters used in the prompt

{'n' => :normal, 's' => :string, 'c' => :statement, 'i' => :indent, 'r' => :return }
TYPES =

Mapping between prompt types and characters used to represent them in the prompt

{'n' => :normal, 's' => :string, 'c' => :statement, 'i' => :indent, 'r' => :return }
PROMPTS =

Mapping between prompt names used by IRB and characters used to represent them in the prompt

{:PROMPT_I => 'n', :PROMPT_N => 'i', :PROMPT_S => 's', :PROMPT_C => 'c', :RETURN => 'r'}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (PromptMatcher) initialize(id, prompts)

A new instance of PromptMatcher

Parameters:

  • id (String)

    the identifier used to mark the beginning and end of a prompt

  • prompts ({Symbol=>String})

    the prompts to use. The recognized keys are: :PROMPT_I, :PROMPT_N, :PROMPT_S, :PROMPT_C, :RETURN. They have the same meaninig as the keys of any entry in IRB.conf[:PROMPT]



34
35
36
37
38
39
40
# File 'plugins/irb/irb_controller.rb', line 34

def initialize id, prompts
  @id = id
  @prompts = {}
  prompts.each_pair do |k, v|
    @prompts[k] = "quirb:#{@id}:#{v}:quirb:#{@id}:#{PROMPTS[k]}:"
  end
end

Instance Attribute Details

- ({Symbol=>String}) prompts (readonly)

The prompt to be used in IRB

Returns:



35
36
37
# File 'plugins/irb/irb_controller.rb', line 35

def prompts
  @prompts
end

Instance Method Details

- (Object) add_prompt(str, type)

TODO:

remove this



61
62
63
# File 'plugins/irb/irb_controller.rb', line 61

def add_prompt str, type
  "quirb:#{@id}:#{@prompts[type]}:quirb:#{@id}:#{MODES.invert[type]}:" << str
end

- (IrbLine?) match(str)

Checks whether a string begins with a prompt

Parameters:

  • str (String)

    the string to check

Returns:

  • (IrbLine, nil)

    an IrbLine with the information about the prompt and the string if the string starts with a prompt or nil if the string doesn’t start with a prompt



49
50
51
52
53
54
55
56
57
58
# File 'plugins/irb/irb_controller.rb', line 49

def match str
  res = str.split "quirb:#{@id}:", 3
  return if !res or !res[0].empty? or res.size < 3
  res[1].slice! -1 #removes an ending :
  letter, sep, line = res[2].partition ':'
  type = TYPES[letter]
  if type then IrbLine.new type, line, res[1]
  else nil
  end
end