Class: Byebug::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/byebug/command.rb

Defined Under Namespace

Classes: Subcmd

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Command

Returns a new instance of Command.



159
160
161
# File 'lib/byebug/command.rb', line 159

def initialize(state)
  @match, @state = nil, state
end

Class Attribute Details

.allow_in_controlObject

Returns the value of attribute allow_in_control.



26
27
28
# File 'lib/byebug/command.rb', line 26

def allow_in_control
  @allow_in_control
end

.allow_in_post_mortemObject



29
30
31
# File 'lib/byebug/command.rb', line 29

def allow_in_post_mortem
  @allow_in_post_mortem ||= !defined?(@allow_in_post_mortem) ? true : false
end

.always_runObject



33
34
35
# File 'lib/byebug/command.rb', line 33

def always_run
  @always_run ||= 0
end

.need_contextObject

Returns the value of attribute need_context.



26
27
28
# File 'lib/byebug/command.rb', line 26

def need_context
  @need_context
end

.unknownObject

Returns the value of attribute unknown.



26
27
28
# File 'lib/byebug/command.rb', line 26

def unknown
  @unknown
end

Class Method Details

.command_exists?(command) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/byebug/command.rb', line 129

def command_exists?(command)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? {
    |d| File.exist? File.join(d, command) }
end

.commandsObject



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

def commands
  @commands ||= []
end

.find(subcmds, param) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/byebug/command.rb', line 47

def find(subcmds, param)
  param.downcase!
  for try_subcmd in subcmds do
    if (param.size >= try_subcmd.min) and
        (try_subcmd.name[0..param.size-1] == param)
      return try_subcmd
    end
  end
  return nil
end

.format_subcmd(subcmd_name) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/byebug/command.rb', line 58

def format_subcmd(subcmd_name)
  subcmd = find(self::Subcommands, subcmd_name)
  return "Invalid \"#{names.join("|")}\" " \
         "subcommand \"#{args[1]}\"." unless subcmd

  return "#{subcmd.help}.\n"
end

.format_subcmdsObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/byebug/command.rb', line 66

def format_subcmds
  cmd_name = names.join("|")
  s = "\n"                                     \
      "--\n"                                   \
      "List of \"#{cmd_name}\" subcommands:\n" \
      "--\n"
  w = self::Subcommands.map(&:name).max_by(&:size).size
  for subcmd in self::Subcommands do
    s += sprintf "%s %-#{w}s -- %s\n", cmd_name, subcmd.name, subcmd.help
  end
  return s
end

.help(args) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/byebug/command.rb', line 37

def help(args)
  if args && args[1]
    output = format_subcmd(args[1])
  else
    output = description.gsub(/^ +/, '') + "\n"
    output += format_subcmds if defined? self::Subcommands
  end
  output
end

.inherited(klass) ⇒ Object



79
80
81
# File 'lib/byebug/command.rb', line 79

def inherited(klass)
  commands << klass
end

.load_commandsObject



83
84
85
86
87
88
# File 'lib/byebug/command.rb', line 83

def load_commands
  Dir[File.join(File.dirname(__FILE__), 'commands', '*')].each {
    |file| require file }
  Byebug.constants.grep(/Functions$/).map {
    |name| Byebug.const_get(name) }.each { |mod| include mod }
end

.register_setting_get(name, &block) ⇒ Object



119
120
121
122
# File 'lib/byebug/command.rb', line 119

def register_setting_get(name, &block)
  settings_map[name] ||= {}
  settings_map[name][:getter] = block
end

.register_setting_set(name, &block) ⇒ Object



124
125
126
127
# File 'lib/byebug/command.rb', line 124

def register_setting_set(name, &block)
  settings_map[name] ||= {}
  settings_map[name][:setter] = block
end

.register_setting_var(name, default) ⇒ Object



112
113
114
115
116
117
# File 'lib/byebug/command.rb', line 112

def register_setting_var(name, default)
  var_name = "@@#{name}"
  class_variable_set(var_name, default)
  register_setting_get(name) { class_variable_get(var_name) }
  register_setting_set(name) { |value| class_variable_set(var_name, value) }
end

.settingsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/byebug/command.rb', line 95

def settings
  unless defined? @settings and @settings
    @settings = Object.new
    map = settings_map
    c = class << @settings; self end
    c.send(:define_method, :[]) do |name|
      raise "No such setting #{name}" unless map.has_key?(name)
      map[name][:getter].call
    end
    c.send(:define_method, :[]=) do |name, value|
      raise "No such setting #{name}" unless map.has_key?(name)
      map[name][:setter].call(value)
    end
  end
  @settings
end

.settings_mapObject



90
91
92
# File 'lib/byebug/command.rb', line 90

def settings_map
  @@settings_map ||= {}
end

.terminal_widthObject



134
135
136
137
138
139
140
141
142
# File 'lib/byebug/command.rb', line 134

def terminal_width
  if ENV['COLUMNS'] =~ /^\d+$/
    ENV['COLUMNS'].to_i
  elsif STDIN.tty? && command_exists?('stty')
    `stty size`.scan(/\d+/)[1].to_i
  else
    nil
  end
end

Instance Method Details

#match(input) ⇒ Object



163
164
165
# File 'lib/byebug/command.rb', line 163

def match(input)
  @match = regexp.match(input)
end