mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
implement command_prefix feature
This commit is contained in:
parent
d74ddeea7a
commit
7ece261723
7 changed files with 65 additions and 4 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -7,3 +7,4 @@ doc/
|
||||||
pkg/
|
pkg/
|
||||||
coverage/
|
coverage/
|
||||||
.yardoc/
|
.yardoc/
|
||||||
|
/tags
|
||||||
|
|
|
@ -55,7 +55,10 @@ class Pry
|
||||||
def command_matched(val, target)
|
def command_matched(val, target)
|
||||||
_, cmd_data = commands.commands.find do |name, data|
|
_, 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 data.options[:interpolate]
|
||||||
# If interpolation fails then the command cannot be matched,
|
# If interpolation fails then the command cannot be matched,
|
||||||
|
|
|
@ -105,7 +105,8 @@ class Pry
|
||||||
:keep_retval => false,
|
:keep_retval => false,
|
||||||
:argument_required => false,
|
:argument_required => false,
|
||||||
:interpolate => true,
|
:interpolate => true,
|
||||||
:listing => name
|
:listing => name,
|
||||||
|
:require_prefix => true
|
||||||
}.merge!(options)
|
}.merge!(options)
|
||||||
|
|
||||||
unless command_dependencies_met? options
|
unless command_dependencies_met? options
|
||||||
|
|
|
@ -57,6 +57,11 @@ class Pry
|
||||||
# @return [String, #call]
|
# @return [String, #call]
|
||||||
attr_accessor :editor
|
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.
|
# @return [Boolean] Toggle Pry color on and off.
|
||||||
attr_accessor :color
|
attr_accessor :color
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ class Pry
|
||||||
|
|
||||||
Shell = Pry::CommandSet.new do
|
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
|
if cmd =~ /^cd\s+(.+)/i
|
||||||
dest = $1
|
dest = $1
|
||||||
begin
|
begin
|
||||||
|
|
|
@ -199,6 +199,7 @@ class Pry
|
||||||
config.editor = default_editor_for_platform
|
config.editor = default_editor_for_platform
|
||||||
config.should_load_rc = true
|
config.should_load_rc = true
|
||||||
config.disable_auto_reload = false
|
config.disable_auto_reload = false
|
||||||
|
config.command_prefix = ""
|
||||||
|
|
||||||
config.plugins ||= OpenStruct.new
|
config.plugins ||= OpenStruct.new
|
||||||
config.plugins.enabled = true
|
config.plugins.enabled = true
|
||||||
|
|
|
@ -93,6 +93,57 @@ describe "Pry::CommandProcessor" do
|
||||||
pos.should == command.name.length
|
pos.should == command.name.length
|
||||||
end
|
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
|
it 'should correctly match a regex command with spaces in its name' do
|
||||||
regex_command_name = /test\s+(.+)\s+command/
|
regex_command_name = /test\s+(.+)\s+command/
|
||||||
@pry.commands.command(regex_command_name) {}
|
@pry.commands.command(regex_command_name) {}
|
||||||
|
@ -201,5 +252,4 @@ describe "Pry::CommandProcessor" do
|
||||||
# you'll cause yourself incredible confusion
|
# you'll cause yourself incredible confusion
|
||||||
lambda { @command_processor.command_matched('boast #{c}', binding) }.should.not.raise NameError
|
lambda { @command_processor.command_matched('boast #{c}', binding) }.should.not.raise NameError
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue