summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--lib/pry/command_processor.rb5
-rw-r--r--lib/pry/command_set.rb3
-rw-r--r--lib/pry/config.rb5
-rw-r--r--lib/pry/default_commands/shell.rb2
-rw-r--r--lib/pry/pry_class.rb1
-rw-r--r--test/test_command_processor.rb52
7 files changed, 65 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 9c38e974..d0b8649e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,4 @@ doc/
pkg/
coverage/
.yardoc/
+/tags
diff --git a/lib/pry/command_processor.rb b/lib/pry/command_processor.rb
index bb273823..3679f3ff 100644
--- a/lib/pry/command_processor.rb
+++ b/lib/pry/command_processor.rb
@@ -55,7 +55,10 @@ class Pry
def command_matched(val, target)
_, cmd_data = commands.commands.find do |name, data|
- command_regex = /^#{convert_to_regex(name)}(?!\S)/
+ prefix = Regexp.escape(Pry.config.command_prefix)
+ prefix = "(?:#{prefix})?" unless data.options[:require_prefix]
+
+ command_regex = /^#{prefix}#{convert_to_regex(name)}(?!\S)/
if data.options[:interpolate]
# If interpolation fails then the command cannot be matched,
diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb
index 3b185382..1c30299e 100644
--- a/lib/pry/command_set.rb
+++ b/lib/pry/command_set.rb
@@ -105,7 +105,8 @@ class Pry
:keep_retval => false,
:argument_required => false,
:interpolate => true,
- :listing => name
+ :listing => name,
+ :require_prefix => true
}.merge!(options)
unless command_dependencies_met? options
diff --git a/lib/pry/config.rb b/lib/pry/config.rb
index f9b88fa2..9f0e93d6 100644
--- a/lib/pry/config.rb
+++ b/lib/pry/config.rb
@@ -57,6 +57,11 @@ class Pry
# @return [String, #call]
attr_accessor :editor
+ # A string that must precede all Pry commands (e.g., if command_prefix is
+ # set to "%", the "cd" command must be invoked as "%cd").
+ # @return [String]
+ attr_accessor :command_prefix
+
# @return [Boolean] Toggle Pry color on and off.
attr_accessor :color
diff --git a/lib/pry/default_commands/shell.rb b/lib/pry/default_commands/shell.rb
index 371fd970..7b31b49c 100644
--- a/lib/pry/default_commands/shell.rb
+++ b/lib/pry/default_commands/shell.rb
@@ -3,7 +3,7 @@ class Pry
Shell = Pry::CommandSet.new do
- command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>") do |cmd|
+ command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>", :require_prefix => false) do |cmd|
if cmd =~ /^cd\s+(.+)/i
dest = $1
begin
diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb
index d3754dae..a7d669e3 100644
--- a/lib/pry/pry_class.rb
+++ b/lib/pry/pry_class.rb
@@ -199,6 +199,7 @@ class Pry
config.editor = default_editor_for_platform
config.should_load_rc = true
config.disable_auto_reload = false
+ config.command_prefix = ""
config.plugins ||= OpenStruct.new
config.plugins.enabled = true
diff --git a/test/test_command_processor.rb b/test/test_command_processor.rb
index d277f49d..02f40821 100644
--- a/test/test_command_processor.rb
+++ b/test/test_command_processor.rb
@@ -93,6 +93,57 @@ describe "Pry::CommandProcessor" do
pos.should == command.name.length
end
+ it 'should correctly match a command preceded by the command_prefix if one is defined' do
+ Pry.config.command_prefix = "%"
+
+ @pry.commands.command("test-command") {}
+ command, captures, pos = @command_processor.command_matched "%test-command hello", binding
+
+ command.name.should == "test-command"
+ captures.should == []
+ pos.should == "test-command".length + "%".length
+
+ Pry.config.command_prefix = ''
+ end
+
+ it 'should not match a command not preceded by the command_prefix if one is defined' do
+ Pry.config.command_prefix = "%"
+
+ @pry.commands.command("test-command") {}
+ command, captures, pos = @command_processor.command_matched "test-command hello", binding
+
+ command.should == nil
+ captures.should == nil
+
+ Pry.config.command_prefix = ''
+ end
+
+ it 'should match a command preceded by the command_prefix when :require_prefix => false' do
+ Pry.config.command_prefix = "%"
+
+ @pry.commands.command("test-command", "", :require_prefix => false) {}
+ command, captures, pos = @command_processor.command_matched "%test-command hello", binding
+
+ command.name.should == "test-command"
+ captures.should == []
+ pos.should == "test-command".length + "%".length
+
+ Pry.config.command_prefix = ''
+ end
+
+ it 'should match a command not preceded by the command_prefix when :require_prefix => false' do
+ Pry.config.command_prefix = "%"
+
+ @pry.commands.command("test-command", "", :require_prefix => false) {}
+ command, captures, pos = @command_processor.command_matched "test-command hello", binding
+
+ command.name.should == "test-command"
+ captures.should == []
+ pos.should == "test-command".length
+
+ Pry.config.command_prefix = ''
+ end
+
it 'should correctly match a regex command with spaces in its name' do
regex_command_name = /test\s+(.+)\s+command/
@pry.commands.command(regex_command_name) {}
@@ -201,5 +252,4 @@ describe "Pry::CommandProcessor" do
# you'll cause yourself incredible confusion
lambda { @command_processor.command_matched('boast #{c}', binding) }.should.not.raise NameError
end
-
end