Class: JazzFingers::Prompt

Inherits:
Object
  • Object
show all
Includes:
PryVersion012AndPrior, PryVersion013AndLater
Defined in:
lib/jazz_fingers/prompt.rb,
lib/jazz_fingers/prompt/pry_version_012_and_prior.rb,
lib/jazz_fingers/prompt/pry_version_013_and_later.rb

Defined Under Namespace

Modules: PryVersion012AndPrior, PryVersion013AndLater

Constant Summary collapse

OBJECT_INSTANCE =
/#<(.+)>/

Instance Method Summary collapse

Methods included from PryVersion012AndPrior

#config, #main_prompt, #wait_prompt

Methods included from PryVersion013AndLater

#config

Constructor Details

#initialize(options = {}) ⇒ Prompt

Returns a new instance of Prompt.



16
17
18
19
20
# File 'lib/jazz_fingers/prompt.rb', line 16

def initialize(options = {})
  @colored = options.fetch(:colored)
  @separator = options.fetch(:separator)
  @application_name = options.fetch(:application_name)
end

Instance Method Details

#abbreviated_context(object_label) ⇒ Object

Abbreviate the object path in the given ‘object_label` string so the prompt doesn’t overflow. Display only the root and leaf namespaces.

Examples:

In:  #<Class1::Class2::Class3::Class4::Class5>
Out: #<Class1::...::Class5>

In:  #<Class1::Class2>
Out: #<Class1::Class2>

In:  #<NoPathJustASingleLongClassName>
Out: #<NoPathJustASingleLongClassName>


96
97
98
99
100
101
102
103
# File 'lib/jazz_fingers/prompt.rb', line 96

def abbreviated_context(object_label)
  object_path = object_label[OBJECT_INSTANCE, 1]
  object_path_components = object_path.split("::")
  return object_label if object_path_components.length <= 2

  root, *_, leaf = object_path_components
  "#<#{root}::...::#{leaf}>"
end

#blue_text(text) ⇒ Object



32
33
34
35
36
# File 'lib/jazz_fingers/prompt.rb', line 32

def blue_text(text)
  return text.to_s unless colored?

  "\001\e[0;34m\002#{text}\001\e[0m\002"
end

#bold_text(text) ⇒ Object



38
39
40
41
42
# File 'lib/jazz_fingers/prompt.rb', line 38

def bold_text(text)
  return text.to_s unless colored?

  "\001\e[1m\002#{text}\001\e[0m\002"
end

#colored?Boolean

Returns:

  • (Boolean)


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

def colored?
  @colored
end

#context(module_name = "main") ⇒ Object

Return the current Pry context

When the Pry context is ‘“main”` or `“nil”`, use the application name from the JazzFingers config. Examples: “(my_rails_app_name)”, “(jazz_fingers)”.

When in the context of an object instance, use the abbreviated object path. Example: “(#<Pry::Prompt>)”, “(#<RSpec::…::ClassName>)”

Fall back to the raw context provided by Pry.view_clip. Example: “(Pry::Prompt)”



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jazz_fingers/prompt.rb', line 62

def context(module_name = "main")
  name =
    case module_name
    when "main", "nil"
      @application_name
    when OBJECT_INSTANCE
      abbreviated_context(module_name)
    else
      module_name
    end

  blue_text("(#{name})")
end

#line_number(pry) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/jazz_fingers/prompt.rb', line 76

def line_number(pry)
  if pry.respond_to? :input_ring
    bold_text(pry.input_ring.size)
  else
    bold_text(pry.input_array.size)
  end
end

#main_separatorObject



44
45
46
# File 'lib/jazz_fingers/prompt.rb', line 44

def main_separator
  red_text(@separator)
end

#red_text(text) ⇒ Object



26
27
28
29
30
# File 'lib/jazz_fingers/prompt.rb', line 26

def red_text(text)
  return text.to_s unless colored?

  "\001\e[0;31m\002#{text}\001\e[0m\002"
end

#template(module_name, pry, separator) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/jazz_fingers/prompt.rb', line 105

def template(module_name, pry, separator)
  format(
    "%<ruby>s %<context>s[%<line>s] %<separator>s ",
    ruby: RUBY_VERSION,
    context: context(module_name),
    line: line_number(pry),
    separator: separator
  )
end

#wait_separatorObject



48
49
50
# File 'lib/jazz_fingers/prompt.rb', line 48

def wait_separator
  "*"
end